@typed/router 0.3.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@typed/router",
3
- "version": "0.3.0",
3
+ "version": "0.4.1",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",
@@ -19,16 +19,16 @@
19
19
  }
20
20
  },
21
21
  "dependencies": {
22
- "@effect/data": "^0.5.0",
23
- "@effect/io": "^0.10.0",
24
- "@typed/context": "0.3.0",
25
- "@typed/dom": "8.3.0",
26
- "@typed/fx": "1.3.0",
27
- "@typed/html": "3.3.0",
22
+ "@effect/data": "^0.7.4",
23
+ "@effect/io": "^0.15.1",
24
+ "@typed/context": "0.4.1",
25
+ "@typed/dom": "8.4.1",
26
+ "@typed/fx": "1.4.1",
27
+ "@typed/html": "3.4.1",
28
28
  "@typed/path": "0.1.0",
29
- "@typed/route": "0.2.0"
29
+ "@typed/route": "0.3.0"
30
30
  },
31
- "gitHead": "04cb57919fddf70d6a2df1656f64acb869c3f5e2",
31
+ "gitHead": "045d1a1cbd6ebed153f24f946b14fd2c638fdb4c",
32
32
  "publishConfig": {
33
33
  "access": "public"
34
34
  },
package/src/Link.ts CHANGED
@@ -12,7 +12,7 @@ export function Link<R = never, E = never, R2 = never>(
12
12
  return Fx.gen(function* ($) {
13
13
  const useBase = props.useBase ?? true
14
14
  const href = useBase ? pathJoin(yield* $(getBasePath), props.href) || '/' : props.href
15
- const router = yield* $(Router.get)
15
+ const router = yield* $(Router)
16
16
  const clickHandler = (event: MouseEvent & { currentTarget: HTMLAnchorElement }) =>
17
17
  Effect.gen(function* ($) {
18
18
  if (props.fullReload) {
@@ -112,7 +112,7 @@ export function RouteMatcher<R, E>(routes: RouteMatcher<R, E>['routes']): RouteM
112
112
  ) =>
113
113
  Router.withFx((router) =>
114
114
  Fx.gen(function* ($) {
115
- const { environment } = yield* $(RenderContext.get)
115
+ const { environment } = yield* $(RenderContext)
116
116
  // Create stable references to the route matchers
117
117
  const matchers = Array.from(routes.values()).map(
118
118
  (v) =>
@@ -138,7 +138,7 @@ export function RouteMatcher<R, E>(routes: RouteMatcher<R, E>['routes']): RouteM
138
138
  layout?: Fx.Fx<any, any, html.Renderable>,
139
139
  ): Effect.Effect<any, any, Option.Option<Fx.Fx<any, any, html.Renderable>>> =>
140
140
  Effect.gen(function* ($) {
141
- const { outlet } = yield* $(Router.get)
141
+ const { outlet } = yield* $(Router)
142
142
  const previous = samplePreviousValues()
143
143
 
144
144
  // Update the previous values
@@ -250,6 +250,7 @@ function runRouteMatch<R, E, P extends string>(
250
250
  const params = pipe(nestedRouter.params, Fx.provideContext(env))
251
251
  const render = pipe(
252
252
  match(params as unknown as Fx.Fx<never, never, Path.ParamsOf<P>>),
253
+ Fx.provideContext(env),
253
254
  Router.provideFx(nestedRouter as Router),
254
255
  )
255
256
 
package/src/router.ts CHANGED
@@ -66,60 +66,60 @@ export interface Router<out R = never, out E = never, in out P extends string =
66
66
  readonly provideContext: (environment: Context.Context<R>) => Router<never, E, P>
67
67
  }
68
68
 
69
- export const Router = Object.assign(function makeRouter<
70
- R = never,
71
- E = never,
72
- P extends string = string,
73
- >(
74
- route: Route.Route<R, E, P>,
75
- currentPath: Fx.RefSubject<string>,
76
- parent: Option.Option<Router<any, any, string>> = Option.none(),
77
- ): Router<R, E, P> {
78
- const outlet = Fx.RefSubject.unsafeMake((): html.Renderable => null)
79
-
80
- const createPath = <R2 extends Route.Route<any, any, string>, P extends Route.ParamsOf<R2>>(
81
- other: R2,
82
- ...[params]: [keyof P] extends [never] ? [] : [P]
83
- ): Effect.Effect<
84
- R,
85
- E,
86
- Path.PathJoin<
87
- [Path.Interpolate<Route.PathOf<R>, Route.ParamsOf<R>>, Path.Interpolate<Route.PathOf<R2>, P>]
88
- >
89
- > =>
90
- Effect.gen(function* ($) {
91
- const path = yield* $(currentPath.get)
92
- const baseParams = yield* $(route.match(path))
93
-
94
- if (Option.isNone(baseParams)) {
95
- return yield* $(
96
- Effect.dieMessage(
97
- `Can not create path when the parent can not be matched.
69
+ export const Router = Object.assign(Context.Tag<Router>('@typed/router/Router'), {
70
+ make: function makeRouter<R = never, E = never, P extends string = string>(
71
+ route: Route.Route<R, E, P>,
72
+ currentPath: Fx.RefSubject<string>,
73
+ parent: Option.Option<Router<any, any, string>> = Option.none(),
74
+ ): Router<R, E, P> {
75
+ const outlet = Fx.RefSubject.unsafeMake((): html.Renderable => null)
76
+
77
+ const createPath = <R2 extends Route.Route<any, any, string>, P extends Route.ParamsOf<R2>>(
78
+ other: R2,
79
+ ...[params]: [keyof P] extends [never] ? [] : [P]
80
+ ): Effect.Effect<
81
+ R,
82
+ E,
83
+ Path.PathJoin<
84
+ [
85
+ Path.Interpolate<Route.PathOf<R>, Route.ParamsOf<R>>,
86
+ Path.Interpolate<Route.PathOf<R2>, P>,
87
+ ]
88
+ >
89
+ > =>
90
+ Effect.gen(function* ($) {
91
+ const path = yield* $(currentPath.get)
92
+ const baseParams = yield* $(route.match(path))
93
+
94
+ if (Option.isNone(baseParams)) {
95
+ return yield* $(
96
+ Effect.dieMessage(
97
+ `Can not create path when the parent can not be matched.
98
98
  Parent Route: ${route.path}
99
99
  Current Route: ${other.path}
100
100
  Current Path: ${path}`,
101
- ),
102
- )
103
- }
104
-
105
- return route.concat(other).make({ ...baseParams.value, ...params } as any) as any
106
- })
107
-
108
- const router: Router<R, E, P> = {
109
- route,
110
- currentPath,
111
- params: pipe(currentPath, Fx.switchMapEffect(route.match), Fx.compact, Fx.skipRepeats),
112
- outlet,
113
- createPath: createPath as Router<R, P>['createPath'],
114
- define: <R2, E2, Path2 extends string>(other: Route.Route<R2, E2, Path2>) =>
115
- makeRouter(route.concat(other), currentPath, Option.some(router as any)) as any,
116
- provideContext: (env) => provideContext(env)(router),
117
- parent,
118
- }
101
+ ),
102
+ )
103
+ }
104
+
105
+ return route.concat(other).make({ ...baseParams.value, ...params } as any) as any
106
+ })
107
+
108
+ const router: Router<R, E, P> = {
109
+ route,
110
+ currentPath,
111
+ params: pipe(currentPath, Fx.switchMapEffect(route.match), Fx.compact, Fx.skipRepeats),
112
+ outlet,
113
+ createPath: createPath as Router<R, P>['createPath'],
114
+ define: <R2, E2, Path2 extends string>(other: Route.Route<R2, E2, Path2>) =>
115
+ makeRouter(route.concat(other), currentPath, Option.some(router as any)) as any,
116
+ provideContext: (env) => provideContext(env)(router),
117
+ parent,
118
+ }
119
119
 
120
- return router
121
- },
122
- Context.Tag<Router>('@typed/router/Router'))
120
+ return router
121
+ },
122
+ })
123
123
 
124
124
  export const outlet: Fx.Fx<RenderContext | Router, never, html.Renderable> = RenderContext.withFx(
125
125
  ({ environment }) =>
@@ -197,8 +197,8 @@ export const makeRouter = (
197
197
  currentPath?: Fx.RefSubject<string>,
198
198
  ): Effect.Effect<Location | History | Window | Document | Scope.Scope, never, Router> =>
199
199
  Effect.gen(function* ($) {
200
- const history = yield* $(History.get)
201
- const location = yield* $(Location.get)
200
+ const history = yield* $(History)
201
+ const location = yield* $(Location)
202
202
 
203
203
  if (!currentPath) {
204
204
  currentPath = Fx.RefSubject.unsafeMake(() => getCurrentPathFromLocation(location))
@@ -225,24 +225,22 @@ export const makeRouter = (
225
225
  pipe(
226
226
  currentPath,
227
227
  Fx.skipRepeats,
228
- Fx.observe((path) =>
229
- Effect.sync(() => {
230
- if (path !== getCurrentPathFromLocation(location)) {
231
- history.pushState({}, '', path)
232
- }
233
- }),
234
- ),
228
+ Fx.observeSync((path) => {
229
+ if (path !== getCurrentPathFromLocation(location)) {
230
+ history.pushState({}, '', path)
231
+ }
232
+ }),
235
233
  Effect.forkScoped,
236
234
  ),
237
235
  )
238
236
 
239
237
  // Find the configured base path
240
- const document = yield* $(Document.get)
238
+ const document = yield* $(Document)
241
239
  const base = document.querySelector('base')
242
240
  const baseHref = base ? getBasePathFromHref(base.href) : '/'
243
241
 
244
242
  // Make our base router
245
- return Router(Route.Route(baseHref), currentPath) as Router
243
+ return Router.make(Route.Route(baseHref), currentPath) as Router
246
244
  })
247
245
 
248
246
  export const live = (
@@ -269,10 +267,11 @@ export const getBasePath = Router.with((r) => {
269
267
  })
270
268
 
271
269
  const patchHistory = Effect.gen(function* ($) {
272
- const history = yield* $(History.get)
270
+ const history = yield* $(History)
273
271
  const historyEvents = Fx.Subject.unsafeMake<never, void>()
274
272
  const runtime = yield* $(Effect.runtime<never>())
275
- const cleanup = patchHistory_(history, () => Runtime.runFork(runtime)(historyEvents.event()))
273
+ const runFork = Runtime.runFork(runtime)
274
+ const cleanup = patchHistory_(history, () => runFork(historyEvents.event()))
276
275
 
277
276
  // unpatch history upon finalization
278
277
  yield* $(Effect.addFinalizer(() => Effect.sync(cleanup)))