Skip to content

Commit f8b91cd

Browse files
authored
Merge pull request #36 from koordinates/lazy-retrieve-history-dep
2 parents 0b8fb22 + 5297c8b commit f8b91cd

File tree

9 files changed

+24
-20
lines changed

9 files changed

+24
-20
lines changed

examples/todomvc/routes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { buildCreateRoute, XstateTreeHistory } from "@koordinates/xstate-tree";
22
import { createBrowserHistory } from "history";
33

44
export const history: XstateTreeHistory = createBrowserHistory();
5-
const createRoute = buildCreateRoute(history, "/");
5+
const createRoute = buildCreateRoute(() => history, "/");
66

77
export const allTodos = createRoute.simpleRoute()({
88
url: "/",

src/routing/createRoute/createRoute.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { assert } from "../../utils";
77
import { buildCreateRoute } from "./createRoute";
88

99
const hist = createMemoryHistory<{ meta?: unknown }>();
10-
const createRoute = buildCreateRoute(hist, "/");
10+
const createRoute = buildCreateRoute(() => hist, "/");
1111

1212
describe("createRoute", () => {
1313
describe("createRoute.dynamicRoute", () => {
@@ -56,7 +56,7 @@ describe("createRoute", () => {
5656
const route = createRoute.simpleRoute()({ url: "/foo", event: "GO_FOO" });
5757

5858
expect(route.basePath).toBe("/");
59-
expect(route.history).toBe(hist);
59+
expect(route.history()).toBe(hist);
6060
});
6161

6262
describe("route schemas", () => {
@@ -313,7 +313,7 @@ describe("createRoute", () => {
313313
const hist: XstateTreeHistory = createMemoryHistory();
314314
const spy = jest.fn();
315315
hist.push = spy as any;
316-
const createRoute = buildCreateRoute(hist, "/");
316+
const createRoute = buildCreateRoute(() => hist, "/");
317317
const route = createRoute.simpleRoute()({
318318
url: "/foo/:fooId",
319319
event: "GO_FOO",
@@ -335,7 +335,7 @@ describe("createRoute", () => {
335335
const hist: XstateTreeHistory = createMemoryHistory();
336336
const spy = jest.fn();
337337
hist.replace = spy as any;
338-
const createRoute = buildCreateRoute(hist, "/");
338+
const createRoute = buildCreateRoute(() => hist, "/");
339339
const route = createRoute.simpleRoute()({
340340
url: "/foo/:fooId",
341341
event: "GO_FOO",

src/routing/createRoute/createRoute.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ export type Route<TParams, TQuery, TEvent, TMeta> = {
148148
* Event type for this route
149149
*/
150150
event: TEvent;
151-
history: XstateTreeHistory;
151+
history: () => XstateTreeHistory;
152152
basePath: string;
153153
parent?: AnyRoute;
154154
paramsSchema?: Z.ZodObject<any>;
@@ -166,7 +166,7 @@ export type AnyRoute = {
166166
getEvent: any;
167167
event: string;
168168
basePath: string;
169-
history: XstateTreeHistory;
169+
history: () => XstateTreeHistory;
170170
parent?: AnyRoute;
171171
paramsSchema?: Z.ZodObject<any>;
172172
querySchema?: Z.ZodObject<any>;
@@ -262,7 +262,10 @@ type ResolveZodType<T extends Z.ZodType<any> | undefined> = undefined extends T
262262
* @param history - the history object to use for this route factory, this needs to be the same one used in the trees root component
263263
* @param basePath - the base path for this route factory
264264
*/
265-
export function buildCreateRoute(history: XstateTreeHistory, basePath: string) {
265+
export function buildCreateRoute(
266+
history: () => XstateTreeHistory,
267+
basePath: string
268+
) {
266269
function navigate({
267270
history,
268271
url,
@@ -535,7 +538,7 @@ export function buildCreateRoute(history: XstateTreeHistory, basePath: string) {
535538
navigate({
536539
url: joinRoutes(this.basePath, url),
537540
meta,
538-
history: this.history,
541+
history: this.history(),
539542
});
540543
},
541544
};

src/routing/handleLocationChange/handleLocationChange.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import { RoutingEvent } from "../routingEvent";
77

88
import { handleLocationChange, Routing404Event } from "./handleLocationChange";
99

10-
const createRoute = buildCreateRoute(createMemoryHistory(), "/");
10+
const hist = createMemoryHistory<{ meta?: unknown }>();
11+
const createRoute = buildCreateRoute(() => hist, "/");
1112
const foo = createRoute.simpleRoute()({ url: "/foo", event: "GO_FOO" });
1213
const bar = createRoute.simpleRoute(foo)({ url: "/bar", event: "GO_BAR" });
1314
const routes = [foo, bar];

src/routing/matchRoute/matchRoute.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { buildCreateRoute } from "../createRoute";
66
import { matchRoute } from "./matchRoute";
77

88
const hist = createMemoryHistory<{ meta?: unknown }>();
9-
const createRoute = buildCreateRoute(hist, "/");
9+
const createRoute = buildCreateRoute(() => hist, "/");
1010
describe("matchRoute", () => {
1111
const route1 = createRoute.simpleRoute()({ url: "/route1", event: "ROUTE_1" });
1212
const route2 = createRoute.simpleRoute()({

src/routing/useHref.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { buildCreateRoute } from "./createRoute";
55
import { useHref } from "./useHref";
66

77
const hist = createMemoryHistory<{ meta?: unknown }>();
8-
const createRoute = buildCreateRoute(hist, "/foo");
8+
const createRoute = buildCreateRoute(() => hist, "/foo");
99
const route = createRoute.simpleRoute()({
1010
url: "/bar/:type(valid)",
1111
event: "GO_BAR",

src/test-app/routes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { createMemoryHistory } from "history";
33
import { buildCreateRoute } from "../routing";
44

55
export const history = createMemoryHistory<any>();
6-
const createRoute = buildCreateRoute(history, "/");
6+
const createRoute = buildCreateRoute(() => history, "/");
77
export const homeRoute = createRoute.route()({
88
matcher(url, _query) {
99
if (url === "/") {

src/tests/asyncRouteRedirects.spec.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { delay } from "../utils";
1818

1919
describe("async route redirects", () => {
2020
const hist: XstateTreeHistory = createMemoryHistory();
21-
const createRoute = buildCreateRoute(hist, "/");
21+
const createRoute = buildCreateRoute(() => hist, "/");
2222

2323
const parentRoute = createRoute.simpleRoute()({
2424
url: "/:notFoo/",

xstate-tree.api.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export type AnyRoute = {
4141
getEvent: any;
4242
event: string;
4343
basePath: string;
44-
history: XstateTreeHistory;
44+
history: () => XstateTreeHistory;
4545
parent?: AnyRoute;
4646
paramsSchema?: Z.ZodObject<any>;
4747
querySchema?: Z.ZodObject<any>;
@@ -66,7 +66,7 @@ export function broadcast(event: GlobalEvents): void;
6666
export function buildActions<TMachine extends AnyStateMachine, TActions, TSelectors, TSend = InterpreterFrom<TMachine>["send"]>(__machine: TMachine, __selectors: TSelectors, actions: (send: TSend, selectors: OutputFromSelector<TSelectors>) => TActions): (send: TSend, selectors: OutputFromSelector<TSelectors>) => TActions;
6767

6868
// @public
69-
export function buildCreateRoute(history: XstateTreeHistory, basePath: string): {
69+
export function buildCreateRoute(history: () => XstateTreeHistory, basePath: string): {
7070
simpleRoute<TBaseRoute extends AnyRoute>(baseRoute?: TBaseRoute | undefined): <TEvent extends string, TParamsSchema extends Z.ZodObject<any, "strip", Z.ZodTypeAny, {
7171
[x: string]: any;
7272
}, {
@@ -257,7 +257,7 @@ export type Route<TParams, TQuery, TEvent, TMeta> = {
257257
}) | false;
258258
reverser: RouteArgumentFunctions<string, TParams, TQuery, TMeta>;
259259
event: TEvent;
260-
history: XstateTreeHistory;
260+
history: () => XstateTreeHistory;
261261
basePath: string;
262262
parent?: AnyRoute;
263263
paramsSchema?: Z.ZodObject<any>;
@@ -417,9 +417,9 @@ export type XstateTreeMachineStateSchemaV2<TMachine extends AnyStateMachine, TSe
417417

418418
// Warnings were encountered during analysis:
419419
//
420-
// src/routing/createRoute/createRoute.ts:265:78 - (ae-forgotten-export) The symbol "MergeRouteTypes" needs to be exported by the entry point index.d.ts
421-
// src/routing/createRoute/createRoute.ts:265:78 - (ae-forgotten-export) The symbol "ResolveZodType" needs to be exported by the entry point index.d.ts
422-
// src/routing/createRoute/createRoute.ts:301:9 - (ae-forgotten-export) The symbol "RouteRedirect" needs to be exported by the entry point index.d.ts
420+
// src/routing/createRoute/createRoute.ts:267:19 - (ae-forgotten-export) The symbol "MergeRouteTypes" needs to be exported by the entry point index.d.ts
421+
// src/routing/createRoute/createRoute.ts:267:19 - (ae-forgotten-export) The symbol "ResolveZodType" needs to be exported by the entry point index.d.ts
422+
// src/routing/createRoute/createRoute.ts:304:9 - (ae-forgotten-export) The symbol "RouteRedirect" needs to be exported by the entry point index.d.ts
423423
// src/types.ts:25:3 - (ae-incompatible-release-tags) The symbol "view" is marked as @public, but its signature references "MatchesFrom" which is marked as @internal
424424
// src/types.ts:172:3 - (ae-incompatible-release-tags) The symbol "canHandleEvent" is marked as @public, but its signature references "CanHandleEvent" which is marked as @internal
425425
// src/types.ts:173:3 - (ae-incompatible-release-tags) The symbol "inState" is marked as @public, but its signature references "MatchesFrom" which is marked as @internal

0 commit comments

Comments
 (0)