crelte 0.4.8 → 0.5.0-alpha.2

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.
Files changed (110) hide show
  1. package/dist/Crelte.d.ts +7 -6
  2. package/dist/Crelte.d.ts.map +1 -1
  3. package/dist/Crelte.js +5 -13
  4. package/dist/CrelteRequest.d.ts +9 -0
  5. package/dist/CrelteRequest.d.ts.map +1 -1
  6. package/dist/CrelteRequest.js +16 -2
  7. package/dist/blocks/Blocks.svelte +2 -2
  8. package/dist/blocks/Blocks.svelte.d.ts +3 -19
  9. package/dist/blocks/Blocks.svelte.d.ts.map +1 -1
  10. package/dist/cookies/ClientCookies.d.ts +0 -1
  11. package/dist/cookies/ClientCookies.d.ts.map +1 -1
  12. package/dist/cookies/ClientCookies.js +0 -1
  13. package/dist/cookies/ServerCookies.d.ts +1 -2
  14. package/dist/cookies/ServerCookies.d.ts.map +1 -1
  15. package/dist/cookies/ServerCookies.js +2 -6
  16. package/dist/cookies/index.d.ts +0 -2
  17. package/dist/cookies/index.d.ts.map +1 -1
  18. package/dist/graphql/GraphQl.d.ts +2 -2
  19. package/dist/graphql/GraphQl.d.ts.map +1 -1
  20. package/dist/index.d.ts +9 -3
  21. package/dist/index.d.ts.map +1 -1
  22. package/dist/index.js +14 -6
  23. package/dist/init/InternalApp.d.ts +30 -0
  24. package/dist/init/InternalApp.d.ts.map +1 -0
  25. package/dist/init/InternalApp.js +71 -0
  26. package/dist/init/client.d.ts +0 -5
  27. package/dist/init/client.d.ts.map +1 -1
  28. package/dist/init/client.js +88 -75
  29. package/dist/init/crelte-vite-plugin.d.ts +5 -0
  30. package/dist/init/server.d.ts +0 -5
  31. package/dist/init/server.d.ts.map +1 -1
  32. package/dist/init/server.js +49 -20
  33. package/dist/init/shared.d.ts +7 -18
  34. package/dist/init/shared.d.ts.map +1 -1
  35. package/dist/init/shared.js +97 -154
  36. package/dist/init/svelteComponents.d.ts +3 -0
  37. package/dist/init/svelteComponents.d.ts.map +1 -0
  38. package/dist/init/svelteComponents.js +7 -0
  39. package/dist/loadData/Globals.d.ts +40 -33
  40. package/dist/loadData/Globals.d.ts.map +1 -1
  41. package/dist/loadData/Globals.js +99 -88
  42. package/dist/loadData/index.d.ts +3 -2
  43. package/dist/loadData/index.d.ts.map +1 -1
  44. package/dist/loadData/index.js +2 -0
  45. package/dist/plugins/Events.d.ts +11 -13
  46. package/dist/plugins/Events.d.ts.map +1 -1
  47. package/dist/plugins/Events.js +10 -3
  48. package/dist/routing/BaseRoute.d.ts +255 -0
  49. package/dist/routing/BaseRoute.d.ts.map +1 -0
  50. package/dist/routing/BaseRoute.js +349 -0
  51. package/dist/routing/BaseRouter.d.ts +210 -0
  52. package/dist/routing/BaseRouter.d.ts.map +1 -0
  53. package/dist/routing/BaseRouter.js +444 -0
  54. package/dist/routing/ClientRouter.d.ts +32 -0
  55. package/dist/routing/ClientRouter.d.ts.map +1 -0
  56. package/dist/routing/ClientRouter.js +259 -0
  57. package/dist/routing/LoadRunner.d.ts +39 -0
  58. package/dist/routing/LoadRunner.d.ts.map +1 -0
  59. package/dist/routing/{PageLoader.js → LoadRunner.js} +32 -20
  60. package/dist/routing/Request.d.ts +35 -3
  61. package/dist/routing/Request.d.ts.map +1 -1
  62. package/dist/routing/Request.js +64 -5
  63. package/dist/routing/Route.d.ts +24 -223
  64. package/dist/routing/Route.d.ts.map +1 -1
  65. package/dist/routing/Route.js +26 -315
  66. package/dist/routing/Router.d.ts +49 -73
  67. package/dist/routing/Router.d.ts.map +1 -1
  68. package/dist/routing/Router.js +85 -251
  69. package/dist/routing/ServerRouter.d.ts +23 -0
  70. package/dist/routing/ServerRouter.d.ts.map +1 -0
  71. package/dist/routing/ServerRouter.js +57 -0
  72. package/dist/routing/utils.d.ts +5 -0
  73. package/dist/routing/utils.d.ts.map +1 -1
  74. package/dist/routing/utils.js +39 -0
  75. package/dist/utils.d.ts +1 -0
  76. package/dist/utils.d.ts.map +1 -1
  77. package/dist/utils.js +3 -0
  78. package/package.json +7 -6
  79. package/src/Crelte.ts +12 -18
  80. package/src/CrelteRequest.ts +21 -2
  81. package/src/cookies/ClientCookies.ts +0 -2
  82. package/src/cookies/ServerCookies.ts +2 -7
  83. package/src/cookies/index.ts +0 -3
  84. package/src/graphql/GraphQl.ts +2 -1
  85. package/src/index.ts +17 -9
  86. package/src/init/InternalApp.ts +134 -0
  87. package/src/init/client.ts +104 -93
  88. package/src/init/crelte-vite-plugin.d.ts +5 -0
  89. package/src/init/server.ts +67 -35
  90. package/src/init/shared.ts +107 -227
  91. package/src/init/svelteComponents.ts +12 -0
  92. package/src/loadData/Globals.ts +121 -102
  93. package/src/loadData/index.ts +3 -2
  94. package/src/plugins/Events.ts +40 -42
  95. package/src/routing/BaseRoute.ts +422 -0
  96. package/src/routing/BaseRouter.ts +528 -0
  97. package/src/routing/ClientRouter.ts +329 -0
  98. package/src/routing/{PageLoader.ts → LoadRunner.ts} +43 -30
  99. package/src/routing/Request.ts +97 -12
  100. package/src/routing/Route.ts +56 -376
  101. package/src/routing/Router.ts +100 -359
  102. package/src/routing/ServerRouter.ts +78 -0
  103. package/src/routing/utils.ts +53 -0
  104. package/src/utils.ts +4 -0
  105. package/dist/routing/InnerRouter.d.ts +0 -113
  106. package/dist/routing/InnerRouter.d.ts.map +0 -1
  107. package/dist/routing/InnerRouter.js +0 -417
  108. package/dist/routing/PageLoader.d.ts +0 -36
  109. package/dist/routing/PageLoader.d.ts.map +0 -1
  110. package/src/routing/InnerRouter.ts +0 -498
@@ -1,12 +1,8 @@
1
- import Route from './Route.js';
2
- import Site, { SiteFromGraphQl } from './Site.js';
3
- import { LoadFn } from './PageLoader.js';
4
1
  import { Readable } from 'crelte-std/stores';
5
- import Request, { RequestOptions } from './Request.js';
6
- export type RouterOptions = {
7
- preloadOnMouseOver?: boolean;
8
- debugTiming?: boolean;
9
- };
2
+ import BaseRouter from './BaseRouter.js';
3
+ import { Request, RequestOptions, Route, Site } from './index.js';
4
+ import { Entry } from '../entry/index.js';
5
+ import CrelteRequest from '../CrelteRequest.js';
10
6
  /**
11
7
  * Allows to easely modify a Request
12
8
  *
@@ -18,72 +14,52 @@ export type RouterOptions = {
18
14
  * ```
19
15
  */
20
16
  export type UpdateRequest = (req: Request) => boolean | null | undefined | void;
21
- /**
22
- * internal only
23
- */
24
- type Internal = {
25
- onLoaded: (success: boolean, req: Request, ready: () => any) => void;
26
- onNothingLoaded: (req: Request, ready: () => void) => void;
27
- onLoad: LoadFn;
28
- domReady: (req: Request) => void;
29
- initClient: () => void;
30
- initServer: (url: string, acceptLang?: string) => Promise<ServerInited>;
31
- };
32
- type ServerInited = {
33
- success: boolean;
34
- redirect: boolean;
35
- req: Request;
36
- props: any;
37
- };
38
17
  export default class Router {
18
+ private inner;
19
+ private _request;
20
+ constructor(inner: BaseRouter);
39
21
  /**
40
- * The current route
22
+ * returns a store with the current route
41
23
  *
42
24
  * ## Note
43
- * Will always contain a route expect in the first loadData call
25
+ * Will always contain a route except in the first loadData call this
26
+ * is intentional since you will get the wrong route in a loadData call.
27
+ * In a loadData you should always use the `CrelteRequest` provided
28
+ * in each loadData call.
44
29
  */
45
- private _route;
30
+ get route(): Readable<Route | null>;
46
31
  /**
47
- * The current site
32
+ * returns a store with the current site
48
33
  *
49
34
  * ## Note
50
- * Will always contain a site expect in the first loadData call
51
- */
52
- private _site;
53
- private _request;
54
- /**
55
- * The loading flag, specifies if a page is currently
56
- * getting loaded
57
- */
58
- private _loading;
59
- /**
60
- * The loading progress, the value is between 0 and 1
35
+ * Will always contain a site except in the first loadData call this
36
+ * is intentional since you might get the wrong site if a site switch
37
+ * is happening and you call this in loadData. If possible use the CrelteRequest
38
+ * provided in each loadData call.
39
+ *
40
+ * Else use `router.site.get() ?? router.req.site`
61
41
  */
62
- private _loadingProgress;
63
- private _onRequest;
64
- /** @hidden */
65
- _internal: Internal;
66
- private inner;
67
- private pageLoader;
68
- constructor(sites: SiteFromGraphQl[], opts?: RouterOptions);
42
+ get site(): Readable<Site | null>;
69
43
  /**
70
- * returns a store with the current route
44
+ * returns a store with the current entry
71
45
  *
72
46
  * ## Note
73
- * Will always contain a route expect in the first loadData call
74
- *
75
- * Consider to use CrelteRequest instead
47
+ * Will always contain an entry except in the first loadData call this
48
+ * is intentional since you might get the wrong entry if a request is happening
49
+ * and you call this in loadData. If possible use the CrelteRequest
50
+ * provided in each loadData call.
76
51
  */
77
- get route(): Readable<Route | null>;
52
+ get entry(): Readable<Entry | null>;
78
53
  /**
79
- * returns a store with the current site
54
+ * returns the latest request in progress otherwise returns null.
80
55
  *
81
- * ## Note
82
- * Will always contain a site expect in the first loadData call
83
- *
84
- * Consider to use CrelteRequest instead
56
+ * ## Important !!
57
+ * If at all possible prefer using the `CrelteRequest` provided in each
58
+ * loadData call. For example in a preload request this will return null.
59
+ * Or a user has clicked multiple times on different links you might get
60
+ * the url of the newer request.
85
61
  */
86
- get site(): Readable<Site | null>;
62
+ get req(): Request | null;
87
63
  /**
88
64
  * The sites which are available
89
65
  */
@@ -117,7 +93,7 @@ export default class Router {
117
93
  * // the following page will be opened https://example.com/de/foo/bar
118
94
  * ```
119
95
  */
120
- open(target: string | URL | Route | Request | UpdateRequest, opts?: RequestOptions): void;
96
+ open(target: string | URL | Route | Request | UpdateRequest, opts?: RequestOptions): Promise<Route | void>;
121
97
  /**
122
98
  * This pushes the new route without triggering a new pageload
123
99
  *
@@ -151,7 +127,7 @@ export default class Router {
151
127
  * router.push(route);
152
128
  * ```
153
129
  */
154
- push(route: Route | Request | UpdateRequest, opts?: RequestOptions): void;
130
+ push(route: Route | Request | UpdateRequest, opts?: RequestOptions): Promise<void | Route>;
155
131
  /**
156
132
  * @deprecated use push instead
157
133
  */
@@ -188,17 +164,21 @@ export default class Router {
188
164
  * router.replace(route);
189
165
  * ```
190
166
  */
191
- replace(route: Route | Request | UpdateRequest, opts?: RequestOptions): void;
167
+ replace(route: Route | Request | UpdateRequest, opts?: RequestOptions): Promise<void | Route>;
192
168
  /**
193
169
  * @deprecated use replace instead
194
170
  */
195
171
  replaceState(route: Route | Request): void;
196
172
  /**
197
173
  * Checks if there are previous routes which would allow it to go back
174
+ *
175
+ * On the server this will always return false
198
176
  */
199
177
  canGoBack(): boolean;
200
178
  /**
201
179
  * Go back in the history
180
+ *
181
+ * On the server this throw an error
202
182
  */
203
183
  back(): void;
204
184
  /**
@@ -210,7 +190,7 @@ export default class Router {
210
190
  *
211
191
  * This will trigger every time a new route is set
212
192
  * and is equivalent to router.route.subscribe(fn)
213
- * expect that it will not trigger instantly
193
+ * except that it will not trigger instantly
214
194
  *
215
195
  * @returns a function to remove the listener
216
196
  */
@@ -222,7 +202,7 @@ export default class Router {
222
202
  *
223
203
  * @returns a function to remove the listener
224
204
  */
225
- onRequest(fn: (req: Request) => void): () => void;
205
+ onRequest(fn: (req: CrelteRequest) => void): () => void;
226
206
  /**
227
207
  * Resolve a url or Route and convert it to a Request
228
208
  *
@@ -231,21 +211,17 @@ export default class Router {
231
211
  * @return Returns null if the url does not match our host (the protocol get's ignored)
232
212
  */
233
213
  targetToRequest(target: string | URL | Route | Request, opts?: RequestOptions): Request;
234
- private setNewRoute;
235
- private _initClient;
236
- private _initServer;
237
- private _onRoute;
238
- private destroyRequest;
239
- private _onPreload;
240
- private _onLoaded;
241
- private _onNothingLoaded;
242
- private _onProgress;
243
214
  /**
244
215
  * Transforms a target to a request
245
216
  *
246
217
  * returns null if the request was canceled by the update request
247
218
  */
248
219
  private targetOrUpdateToRequest;
220
+ /**
221
+ * @hidden
222
+ * call this after creating a CrelteRequest
223
+ */
224
+ _toRequest(req: Request): Router;
225
+ _requestCompleted(): void;
249
226
  }
250
- export {};
251
227
  //# sourceMappingURL=Router.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"Router.d.ts","sourceRoot":"","sources":["../../src/routing/Router.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,YAAY,CAAC;AAC/B,OAAO,IAAI,EAAE,EAAE,eAAe,EAAE,MAAM,WAAW,C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AE,MAAM,GAAG,GAAG,GAAG,KAAK;IAIpC;;;;;;;;OAQG;IACH,OAAO,CAAC,EAAE,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,GAAG,MAAM,IAAI;IAI/C;;;;;;OAMG;IACH,SAAS,CAAC,EAAE,EAAE,CAAC,GAAG,EAAE,aAAa,KAAK,IAAI,GAAG,MAAM,IAAI;IAIvD;;;;;;OAMG;IACH,eAAe,CACd,MAAM,EAAE,MAAM,GAAG,GAAG,GAAG,KAAK,GAAG,OAAO,EACtC,IAAI,GAAE,cAAmB,GACvB,OAAO;IAIV;;;;OAIG;IACH,OAAO,CAAC,uBAAuB;IA0B/B;;;OAGG;IACH,UAAU,CAAC,GAAG,EAAE,OAAO;IAMvB,iBAAiB;CAGjB"}
@@ -1,96 +1,65 @@
1
- import InnerRouter from './InnerRouter.js';
2
- import PageLoader from './PageLoader.js';
3
- import { Writable } from 'crelte-std/stores';
4
- import { Listeners } from 'crelte-std/sync';
5
- import Request from './Request.js';
6
- const defaultRouterOpts = {
7
- preloadOnMouseOver: false,
8
- debugTiming: false,
9
- };
10
- // Make sure route and nextRoute are not the same object as _inner.route
1
+ // Todo this router should be stateful like globals
2
+ // allow to reference the correct route or request
11
3
  export default class Router {
12
- /**
13
- * The current route
14
- *
15
- * ## Note
16
- * Will always contain a route expect in the first loadData call
17
- */
18
- _route;
19
- /**
20
- * The current site
21
- *
22
- * ## Note
23
- * Will always contain a site expect in the first loadData call
24
- */
25
- _site;
26
- // the next request, just here to destroy it
27
- _request;
28
- /**
29
- * The loading flag, specifies if a page is currently
30
- * getting loaded
31
- */
32
- _loading;
33
- /**
34
- * The loading progress, the value is between 0 and 1
35
- */
36
- _loadingProgress;
37
- _onRequest;
38
- /** @hidden */
39
- _internal;
40
4
  inner;
41
- pageLoader;
42
- constructor(sites, opts = {}) {
43
- opts = { ...defaultRouterOpts, ...opts };
44
- this.inner = new InnerRouter(sites, {
45
- preloadOnMouseOver: opts.preloadOnMouseOver,
46
- });
47
- this.pageLoader = new PageLoader({
48
- debugTiming: opts.debugTiming,
49
- });
50
- // in the first onRoute call we will update this value
51
- this._route = new Writable(null);
52
- this._site = new Writable(null);
5
+ _request;
6
+ constructor(inner) {
7
+ this.inner = inner;
53
8
  this._request = null;
54
- this._loading = new Writable(false);
55
- this._loadingProgress = new Writable(0);
56
- this._onRequest = new Listeners();
57
- // these functions are exposed to the init "module"
58
- // but should not be used by anybody else
59
- this._internal = {
60
- onLoaded: () => { },
61
- onNothingLoaded: () => { },
62
- onLoad: () => { },
63
- domReady: req => this.inner.domReady(req),
64
- initClient: () => this._initClient(),
65
- initServer: (url, acceptLang) => this._initServer(url, acceptLang),
66
- };
67
- this.inner.onRoute = (route, changeHistory) => this._onRoute(route, changeHistory);
68
- this.inner.onPreload = route => this._onPreload(route);
69
- this.pageLoader.onLoaded = (resp, req, more) => this._onLoaded(resp, req, more);
70
- this.pageLoader.loadFn = (req, opts) => this._internal.onLoad(req, opts);
71
- this.pageLoader.onProgress = (loading, progress) => this._onProgress(loading, progress);
72
9
  }
73
10
  /**
74
11
  * returns a store with the current route
75
12
  *
76
13
  * ## Note
77
- * Will always contain a route expect in the first loadData call
78
- *
79
- * Consider to use CrelteRequest instead
14
+ * Will always contain a route except in the first loadData call this
15
+ * is intentional since you will get the wrong route in a loadData call.
16
+ * In a loadData you should always use the `CrelteRequest` provided
17
+ * in each loadData call.
80
18
  */
81
19
  get route() {
82
- return this._route.readclone();
20
+ return this.inner.route.readclone();
83
21
  }
84
22
  /**
85
23
  * returns a store with the current site
86
24
  *
87
25
  * ## Note
88
- * Will always contain a site expect in the first loadData call
26
+ * Will always contain a site except in the first loadData call this
27
+ * is intentional since you might get the wrong site if a site switch
28
+ * is happening and you call this in loadData. If possible use the CrelteRequest
29
+ * provided in each loadData call.
89
30
  *
90
- * Consider to use CrelteRequest instead
31
+ * Else use `router.site.get() ?? router.req.site`
91
32
  */
92
33
  get site() {
93
- return this._site.readonly();
34
+ return this.inner.site.readonly();
35
+ }
36
+ /**
37
+ * returns a store with the current entry
38
+ *
39
+ * ## Note
40
+ * Will always contain an entry except in the first loadData call this
41
+ * is intentional since you might get the wrong entry if a request is happening
42
+ * and you call this in loadData. If possible use the CrelteRequest
43
+ * provided in each loadData call.
44
+ */
45
+ get entry() {
46
+ return this.inner.entry.readonly();
47
+ }
48
+ /**
49
+ * returns the latest request in progress otherwise returns null.
50
+ *
51
+ * ## Important !!
52
+ * If at all possible prefer using the `CrelteRequest` provided in each
53
+ * loadData call. For example in a preload request this will return null.
54
+ * Or a user has clicked multiple times on different links you might get
55
+ * the url of the newer request.
56
+ */
57
+ get req() {
58
+ // this._request is not used because that could be a weird
59
+ // behaviour for the user
60
+ // we will use that however internally
61
+ // todo maybe reconsider this?
62
+ return this.inner.request;
94
63
  }
95
64
  /**
96
65
  * The sites which are available
@@ -102,13 +71,13 @@ export default class Router {
102
71
  * returns a store which indicates if the a page is loading
103
72
  */
104
73
  get loading() {
105
- return this._loading.readonly();
74
+ return this.inner.loading.readonly();
106
75
  }
107
76
  /**
108
77
  * returns a store which indicates the loading progress between 0 and 1
109
78
  */
110
79
  get loadingProgress() {
111
- return this._loadingProgress.readonly();
80
+ return this.inner.loadingProgress.readonly();
112
81
  }
113
82
  /**
114
83
  * Open a new route
@@ -131,17 +100,11 @@ export default class Router {
131
100
  * // the following page will be opened https://example.com/de/foo/bar
132
101
  * ```
133
102
  */
134
- open(target, opts = {}) {
135
- const req = this.targetOrUpdateToRequest(target, opts, {
136
- origin: 'manual',
137
- });
103
+ async open(target, opts = {}) {
104
+ const req = this.targetOrUpdateToRequest(target, opts);
138
105
  if (!req)
139
106
  return;
140
- if (req === this._request) {
141
- throw new Error('Cannot open the same request object twice. Either clone the request ' +
142
- 'or just pass in the url.');
143
- }
144
- this.inner.open(req);
107
+ return await this.inner.open(req);
145
108
  }
146
109
  /**
147
110
  * This pushes the new route without triggering a new pageload
@@ -176,20 +139,11 @@ export default class Router {
176
139
  * router.push(route);
177
140
  * ```
178
141
  */
179
- push(route, opts = {}) {
180
- // todo not sure if that is what we want?
181
- if (import.meta.env.SSR)
182
- return this.open(route, opts);
183
- // theoretically string and URL also work but we might
184
- // change that in the future
185
- const req = this.targetOrUpdateToRequest(route, opts, {
186
- origin: 'push',
187
- scrollY: opts.scrollY ?? undefined,
188
- disableLoadData: opts.disableLoadData ?? true,
189
- });
142
+ async push(route, opts = {}) {
143
+ const req = this.targetOrUpdateToRequest(route, opts);
190
144
  if (!req)
191
145
  return;
192
- this.inner.push(req);
146
+ return await this.inner.push(req);
193
147
  }
194
148
  /**
195
149
  * @deprecated use push instead
@@ -230,20 +184,13 @@ export default class Router {
230
184
  * router.replace(route);
231
185
  * ```
232
186
  */
233
- replace(route, opts = {}) {
234
- // todo not sure if that is what we want?
235
- if (import.meta.env.SSR)
236
- return this.open(route, opts);
237
- // theoretically string and URL also work but we might
238
- // change that in the future
239
- const req = this.targetOrUpdateToRequest(route, opts, {
240
- origin: 'replace',
241
- scrollY: opts.scrollY ?? undefined,
242
- disableLoadData: opts.disableLoadData ?? true,
243
- });
187
+ async replace(route, opts = {}) {
188
+ const req = this.targetOrUpdateToRequest(route, opts);
244
189
  if (!req)
245
190
  return;
246
- this.inner.replace(req);
191
+ // we don't force disableLoadData here
192
+ // because the user might want to reload the data
193
+ return await this.inner.replace(req);
247
194
  }
248
195
  /**
249
196
  * @deprecated use replace instead
@@ -254,15 +201,19 @@ export default class Router {
254
201
  }
255
202
  /**
256
203
  * Checks if there are previous routes which would allow it to go back
204
+ *
205
+ * On the server this will always return false
257
206
  */
258
207
  canGoBack() {
259
- return this.inner.route?.canGoBack() ?? false;
208
+ return this.inner.canGoBack();
260
209
  }
261
210
  /**
262
211
  * Go back in the history
212
+ *
213
+ * On the server this throw an error
263
214
  */
264
215
  back() {
265
- this.inner.history.back();
216
+ this.inner.back();
266
217
  }
267
218
  /**
268
219
  * Preload a url
@@ -275,13 +226,12 @@ export default class Router {
275
226
  *
276
227
  * This will trigger every time a new route is set
277
228
  * and is equivalent to router.route.subscribe(fn)
278
- * expect that it will not trigger instantly
229
+ * except that it will not trigger instantly
279
230
  *
280
231
  * @returns a function to remove the listener
281
232
  */
282
233
  onRoute(fn) {
283
- let first = true;
284
- return this.route.subscribe(r => (first ? (first = false) : fn(r)));
234
+ return this.inner.onRouteListeners.add(fn);
285
235
  }
286
236
  /**
287
237
  * Add a listener for the onRequest event
@@ -291,7 +241,7 @@ export default class Router {
291
241
  * @returns a function to remove the listener
292
242
  */
293
243
  onRequest(fn) {
294
- return this._onRequest.add(fn);
244
+ return this.inner.onRequestListeners.add(fn);
295
245
  }
296
246
  /**
297
247
  * Resolve a url or Route and convert it to a Request
@@ -303,155 +253,39 @@ export default class Router {
303
253
  targetToRequest(target, opts = {}) {
304
254
  return this.inner.targetToRequest(target, opts);
305
255
  }
306
- setNewRoute(route) {
307
- this._route.setSilent(route);
308
- const siteChanged = this.site.get()?.id !== route.site.id;
309
- this._site.setSilent(route.site);
310
- this._route.notify();
311
- if (siteChanged)
312
- this._site.notify();
313
- }
314
- async _initClient() {
315
- this.inner.initClient();
316
- }
317
- async _initServer(url, acceptLang) {
318
- this.inner.initServer();
319
- this._internal.onNothingLoaded = (_req, ready) => {
320
- ready();
321
- };
322
- const prom = new Promise(resolve => {
323
- this._internal.onLoaded = (success, req, ready) => {
324
- const props = ready();
325
- this._internal.onLoaded = () => { };
326
- resolve({
327
- success,
328
- redirect: false,
329
- req,
330
- props,
331
- });
332
- };
333
- });
334
- const req = this.inner.targetToRequest(url);
335
- req.origin = 'init';
336
- // let's see if the url matches any route and site
337
- // if not let's redirect to the site which matches the acceptLang
338
- if (!req.siteMatches()) {
339
- const site = this.inner.siteByAcceptLang(acceptLang);
340
- return {
341
- success: true,
342
- redirect: true,
343
- req: new Request(site.url, site),
344
- props: {},
345
- };
346
- }
347
- this.inner.route = req.toRoute();
348
- this.inner.onRoute(req, () => { });
349
- const resp = await prom;
350
- const hist = this.inner.history;
351
- if (hist.url || hist.req) {
352
- const nReq = this.inner.targetToRequest(hist.req ?? hist.url);
353
- if (!req.eq(nReq)) {
354
- return {
355
- success: true,
356
- redirect: true,
357
- req: nReq,
358
- props: {},
359
- };
360
- }
361
- }
362
- return resp;
363
- }
364
- // gets called by the InnerRouter when a new route is requested
365
- _onRoute(req, changeHistory) {
366
- this.destroyRequest();
367
- this._request = req;
368
- const barrier = req._renderBarrier;
369
- if (barrier.isOpen()) {
370
- throw new Error('render barrier is already open');
371
- }
372
- this._onRequest.trigger(req);
373
- // route prepared
374
- if (!req.disableLoadData) {
375
- this.pageLoader.load(req, { changeHistory });
376
- }
377
- else {
378
- this.pageLoader.discard();
379
- this._onNothingLoaded(req, { changeHistory });
380
- }
381
- }
382
- destroyRequest() {
383
- if (!this._request)
384
- return;
385
- this._request._renderBarrier.cancel();
386
- this._request = null;
387
- }
388
- _onPreload(req) {
389
- this.pageLoader.preload(req);
390
- }
391
- // gets called by the pageLoader when teh loadData completes
392
- async _onLoaded(resp, req, more) {
393
- // check if the render was cancelled
394
- if (await req._renderBarrier.ready())
395
- return;
396
- // when the data is loaded let's update the route of the inner
397
- // this will only happen if no other route has been requested
398
- // in the meantime
399
- more.changeHistory();
400
- const route = req.toRoute();
401
- // call the client or server saying we are ready for a new render
402
- this._internal.onLoaded(resp.success, req, () => {
403
- this.setNewRoute(route);
404
- return resp.data;
405
- });
406
- }
407
- // this gets called if loadData is not called
408
- async _onNothingLoaded(req, more) {
409
- // check if the render was cancelled
410
- if (await req._renderBarrier.ready())
411
- return;
412
- // when the data is loaded let's update the route of the inner
413
- // this is will only happen if no other route has been requested
414
- // in the meantime
415
- more.changeHistory();
416
- const route = req.toRoute();
417
- // call the client or server saying there was an update in the route
418
- // but no new data was loaded so no render should happen
419
- this._internal.onNothingLoaded(req, () => {
420
- this.setNewRoute(route);
421
- });
422
- }
423
- // this is called by the pageLoader if we get a progress update
424
- _onProgress(loading, progress) {
425
- if (this._loading.get() !== loading)
426
- this._loading.set(loading);
427
- if (typeof progress === 'number')
428
- this._loadingProgress.set(progress);
429
- }
430
256
  /**
431
257
  * Transforms a target to a request
432
258
  *
433
259
  * returns null if the request was canceled by the update request
434
260
  */
435
- targetOrUpdateToRequest(target, opts = {}, forcedOpts = {}) {
261
+ targetOrUpdateToRequest(target, opts = {}) {
436
262
  // we have an update request
437
263
  if (typeof target === 'function') {
438
- const route = this.route.get();
439
- if (!route) {
264
+ const source = this._request ?? this.route.get();
265
+ if (!source) {
266
+ // todo should we use the request here?
440
267
  throw new Error('route to update missing in first loadData call. ' +
441
- 'Use cr.req.clone() instead');
268
+ 'Use `cr.router...` or `cr.req.clone()`');
442
269
  }
443
270
  // first get a req
444
- const req = this.inner.targetToRequest(route, opts);
271
+ const req = this.inner.targetToRequest(source, opts);
445
272
  // check if the request was canceled by the update request
446
273
  if (target(req) === false)
447
274
  return null;
448
- // now we add the forcedOpts
449
- req._updateOpts(forcedOpts);
450
275
  return req;
451
276
  }
452
- return this.inner.targetToRequest(target, {
453
- ...opts,
454
- ...forcedOpts,
455
- });
277
+ return this.inner.targetToRequest(target, opts);
278
+ }
279
+ /**
280
+ * @hidden
281
+ * call this after creating a CrelteRequest
282
+ */
283
+ _toRequest(req) {
284
+ const nRouter = new Router(this.inner);
285
+ nRouter._request = req;
286
+ return nRouter;
287
+ }
288
+ _requestCompleted() {
289
+ this._request = null;
456
290
  }
457
291
  }