@warp-drive-mirror/ember 5.8.0-alpha.4 → 5.8.0-alpha.40

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.
@@ -0,0 +1,456 @@
1
+ import { service } from '@ember/service';
2
+ import Component from '@glimmer/component';
3
+ import { DISPOSE, createRequestSubscription, memoized } from '@warp-drive-mirror/core/store/-private';
4
+ export { createRequestSubscription, getRequestState } from '@warp-drive-mirror/core/store/-private';
5
+ import { getPromiseState } from '@warp-drive-mirror/core/reactive';
6
+ export { getPromiseState } from '@warp-drive-mirror/core/reactive';
7
+ import { precompileTemplate } from '@ember/template-compilation';
8
+ import { setComponentTemplate } from '@ember/component';
9
+ import templateOnly from '@ember/component/template-only';
10
+
11
+ const and = (x, y) => Boolean(x && y);
12
+ /**
13
+ * The `<Throw />` component is used to throw an error in a template.
14
+ *
15
+ * That's all it does. So don't use it unless the application should
16
+ * throw an error if it reaches this point in the template.
17
+ *
18
+ * ```gts
19
+ * <Throw @error={{anError}} />
20
+ * ```
21
+ *
22
+ * @category Components
23
+ * @public
24
+ */
25
+ class Throw extends Component {
26
+ constructor(owner, args) {
27
+ super(owner, args);
28
+ // this error is opaque (user supplied) so we don't validate it
29
+ // as an Error instance.
30
+ {
31
+ // eslint-disable-next-line no-console
32
+ console.error(this.args.error);
33
+ }
34
+ }
35
+ static {
36
+ setComponentTemplate(precompileTemplate("", {
37
+ strictMode: true
38
+ }), this);
39
+ }
40
+ }
41
+ /**
42
+ * The `<Await />` component allow you to utilize reactive control flow
43
+ * for asynchronous states in your application.
44
+ *
45
+ * Await is ideal for handling "boundaries", outside which some state is
46
+ * still allowed to be unresolved and within which it MUST be resolved.
47
+ *
48
+ * ```gts
49
+ * import { Await } from '@warp-drive-mirror/ember';
50
+ *
51
+ * <template>
52
+ * <Await @promise={{@request}}>
53
+ * <:pending>
54
+ * <Spinner />
55
+ * </:pending>
56
+ *
57
+ * <:error as |error|>
58
+ * <ErrorForm @error={{error}} />
59
+ * </:error>
60
+ *
61
+ * <:success as |result|>
62
+ * <h1>{{result.title}}</h1>
63
+ * </:success>
64
+ * </Await>
65
+ * </template>
66
+ * ```
67
+ *
68
+ * The `<Await />` component requires that error states are properly handled.
69
+ *
70
+ * If no error block is provided and the promise rejects, the error will
71
+ * be thrown.
72
+ *
73
+ * @category Components
74
+ * @public
75
+ */
76
+ class Await extends Component {
77
+ get state() {
78
+ return getPromiseState(this.args.promise);
79
+ }
80
+ get error() {
81
+ return this.state.error;
82
+ }
83
+ get result() {
84
+ return this.state.result;
85
+ }
86
+ static {
87
+ setComponentTemplate(precompileTemplate("\n {{#if this.state.isPending}}\n {{yield to=\"pending\"}}\n {{else if (and this.state.isError (has-block \"error\"))}}\n {{yield this.error to=\"error\"}}\n {{else if this.state.isSuccess}}\n {{yield this.result to=\"success\"}}\n {{else}}\n <Throw @error={{this.error}} />\n {{/if}}\n ", {
88
+ strictMode: true,
89
+ scope: () => ({
90
+ and,
91
+ Throw
92
+ })
93
+ }), this);
94
+ }
95
+ }
96
+
97
+ const deferred = /* @__PURE__ */new WeakMap();
98
+ function deferDecorator(proto, prop, desc) {
99
+ let map = deferred.get(proto);
100
+ if (!map) {
101
+ map = /* @__PURE__ */new Map();
102
+ deferred.set(proto, map);
103
+ }
104
+ map.set(prop, desc);
105
+ }
106
+ function findDeferredDecorator(target, prop) {
107
+ var _a;
108
+ let cursor = target.prototype;
109
+ while (cursor) {
110
+ let desc = (_a = deferred.get(cursor)) == null ? void 0 : _a.get(prop);
111
+ if (desc) {
112
+ return desc;
113
+ }
114
+ cursor = cursor.prototype;
115
+ }
116
+ }
117
+ function decorateFieldV2(prototype, prop, decorators, initializer) {
118
+ let desc = {
119
+ configurable: true,
120
+ enumerable: true,
121
+ writable: true,
122
+ initializer: null
123
+ };
124
+ if (initializer) {
125
+ desc.initializer = initializer;
126
+ }
127
+ for (let decorator of decorators) {
128
+ desc = decorator(prototype, prop, desc) || desc;
129
+ }
130
+ if (desc.initializer === void 0) {
131
+ Object.defineProperty(prototype, prop, desc);
132
+ } else {
133
+ deferDecorator(prototype, prop, desc);
134
+ }
135
+ }
136
+ function decorateMethodV2(prototype, prop, decorators) {
137
+ const origDesc = Object.getOwnPropertyDescriptor(prototype, prop);
138
+ let desc = {
139
+ ...origDesc
140
+ };
141
+ for (let decorator of decorators) {
142
+ desc = decorator(prototype, prop, desc) || desc;
143
+ }
144
+ if (desc.initializer !== void 0) {
145
+ desc.value = desc.initializer ? desc.initializer.call(prototype) : void 0;
146
+ desc.initializer = void 0;
147
+ }
148
+ Object.defineProperty(prototype, prop, desc);
149
+ }
150
+ function initializeDeferredDecorator(target, prop) {
151
+ let desc = findDeferredDecorator(target.constructor, prop);
152
+ if (desc) {
153
+ Object.defineProperty(target, prop, {
154
+ enumerable: desc.enumerable,
155
+ configurable: desc.configurable,
156
+ writable: desc.writable,
157
+ value: desc.initializer ? desc.initializer.call(target) : void 0
158
+ });
159
+ }
160
+ }
161
+
162
+ function notNull(x) {
163
+ return x;
164
+ }
165
+ const not = x => !x;
166
+ const IdleBlockMissingError = new Error('No idle block provided for <Request> component, and no query or request was provided.');
167
+ let consume = service;
168
+ const DefaultChrome = setComponentTemplate(precompileTemplate("{{yield}}", {
169
+ strictMode: true
170
+ }), templateOnly());
171
+ /**
172
+ * The `<Request />` component is a powerful tool for managing data fetching and
173
+ * state in your Ember application. It provides a declarative approach to reactive
174
+ * control-flow for managing requests and state in your application.
175
+ *
176
+ * The `<Request />` component is ideal for handling "boundaries", outside which some
177
+ * state is still allowed to be unresolved and within which it MUST be resolved.
178
+ *
179
+ * ## Request States
180
+ *
181
+ * `<Request />` has five states, only one of which will be active and rendered at a time.
182
+ *
183
+ * - `idle`: The component is waiting to be given a request to monitor
184
+ * - `loading`: The request is in progress
185
+ * - `error`: The request failed
186
+ * - `content`: The request succeeded
187
+ * - `cancelled`: The request was cancelled
188
+ *
189
+ * Additionally, the `content` state has a `refresh` method that can be used to
190
+ * refresh the request in the background, which is available as a sub-state of
191
+ * the `content` state.
192
+ *
193
+ * As with the `<Await />` component, if no error block is provided and the request
194
+ * rejects, the error will be thrown. Cancellation errors are swallowed instead of
195
+ * rethrown if no error block or cancellation block is present.
196
+ *
197
+ * ```gts
198
+ * import { Request } from '@warp-drive-mirror/ember';
199
+ *
200
+ * <template>
201
+ * <Request @request={{@request}}>
202
+ * <:loading as |state|>
203
+ * <Spinner @percentDone={{state.completedRatio}} />
204
+ * <button {{on "click" state.abort}}>Cancel</button>
205
+ * </:loading>
206
+ *
207
+ * <:error as |error state|>
208
+ * <ErrorForm @error={{error}} />
209
+ * <button {{on "click" state.retry}}>Retry</button>
210
+ * </:error>
211
+ *
212
+ * <:content as |data state|>
213
+ * <h1>{{data.title}}</h1>
214
+ * {{#if state.isBackgroundReloading}}
215
+ * <SmallSpinner />
216
+ * <button {{on "click" state.abort}}>Cancel</button>
217
+ * {{else}}
218
+ * <button {{on "click" state.refresh}}>Refresh</button>
219
+ * {{/if}}
220
+ * </:content>
221
+ *
222
+ * <:cancelled as |error state|>
223
+ * <h2>The Request was cancelled</h2>
224
+ * <button {{on "click" state.retry}}>Retry</button>
225
+ * </:cancelled>
226
+ *
227
+ * <:idle>
228
+ * <button {{on "click" @kickOffRequest}}>Load Preview?</button>
229
+ * </:idle>
230
+ *
231
+ * </Request>
232
+ * </template>
233
+ * ```
234
+ *
235
+ * ## Streaming Data
236
+ *
237
+ * The loading state exposes the download `ReadableStream` instance for consumption
238
+ *
239
+ * ```gts
240
+ * import { Request } from '@warp-drive-mirror/ember';
241
+ *
242
+ * <template>
243
+ * <Request @request={{@request}}>
244
+ * <:loading as |state|>
245
+ * <Video @stream={{state.stream}} />
246
+ * </:loading>
247
+ *
248
+ * <:error as |error|>
249
+ * <ErrorForm @error={{error}} />
250
+ * </:error>
251
+ * </Request>
252
+ * </template>
253
+ * ```
254
+ *
255
+ * ## Retry
256
+ *
257
+ * Cancelled and error'd requests may be retried by calling the `retry` method.
258
+ *
259
+ * Retry will restart the state progression, using the loading, error, cancelled,
260
+ * and content blocks as appropriate.
261
+ *
262
+ * ## Reloading
263
+ *
264
+ * The `reload` method will force the request to be fully re-executed, bypassing
265
+ * cache and restarting the state progression through the loading, error, and
266
+ * content blocks as appropriate.
267
+ *
268
+ * Background reload (refresh) is a special substate of the content state that
269
+ * allows you to refresh the request in the background. This is useful for when
270
+ * you want to update the data in the background without blocking the UI.
271
+ *
272
+ * Reload and refresh are available as methods on the `content` state.
273
+ *
274
+ * ```gts
275
+ * import { Request } from '@warp-drive-mirror/ember';
276
+ *
277
+ * <template>
278
+ * <Request @request={{@request}}>
279
+ * <:content as |data state|>
280
+ * <h1>{{data.title}}</h1>
281
+ * {{#if state.isBackgroundReloading}}
282
+ * <SmallSpinner />
283
+ * <button {{on "click" state.abort}}>Cancel</button>
284
+ * {{/if}}
285
+ *
286
+ * <button {{on "click" state.refresh}}>Refresh</button>
287
+ * <button {{on "click" state.reload}}>Reload</button>
288
+ * </:content>
289
+ * </Request>
290
+ * </template>
291
+ * ```
292
+ *
293
+ * ## Advanced Reloading
294
+ *
295
+ * We can nest our usage of `<Request />` to handle more advanced
296
+ * reloading scenarios.
297
+ *
298
+ * ```gts
299
+ * import { Request } from '@warp-drive-mirror/ember';
300
+ *
301
+ * <template>
302
+ * <Request @request={{@request}}>
303
+ * <:cancelled>
304
+ * <h2>The Request Cancelled</h2>
305
+ * </:cancelled>
306
+ *
307
+ * <:error as |error|>
308
+ * <ErrorForm @error={{error}} />
309
+ * </:error>
310
+ *
311
+ * <:content as |result state|>
312
+ * <Request @request={{state.latestRequest}}>
313
+ * <!-- Handle Background Request -->
314
+ * </Request>
315
+ *
316
+ * <h1>{{result.title}}</h1>
317
+ *
318
+ * <button {{on "click" state.refresh}}>Refresh</button>
319
+ * </:content>
320
+ * </Request>
321
+ * </template>
322
+ * ```
323
+ *
324
+ * ## Autorefresh
325
+ *
326
+ * `<Request />` supports automatic refresh and reload under certain conditions.
327
+ *
328
+ * - `online`: This occurs when a browser window or tab comes back to the foreground
329
+ * after being backgrounded or when the network reports as being online after
330
+ * having been offline.
331
+ * - `interval`: This occurs when a specified amount of time has passed.
332
+ * - `invalid`: This occurs when the store emits a notification that the request
333
+ * has become invalid.
334
+ *
335
+ * You can specify when autorefresh should occur by setting the `autorefresh` arg
336
+ * to `true` or a comma-separated list of the above values.
337
+ *
338
+ * A value of `true` is equivalent to `'online,invalid'`.
339
+ *
340
+ * By default, an autorefresh will only occur if the browser was backgrounded or
341
+ * offline for more than 30s before coming back available. This amount of time can
342
+ * be tweaked by setting the number of milliseconds via `@autorefreshThreshold`.
343
+ *
344
+ * This arg also controls the interval at which the request will be refreshed
345
+ * if the `interval` autorefresh type is enabled.
346
+ *
347
+ * Finally, the behavior of the request initiated by autorefresh can be adjusted
348
+ * by setting the `autorefreshBehavior` arg to `'refresh'`, `'reload'`, or `'policy'`.
349
+ *
350
+ * - `'refresh'`: Refresh the request in the background
351
+ * - `'reload'`: Force a reload of the request
352
+ * - `'policy'` (**default**): Let the store's configured CachePolicy decide whether to
353
+ * reload, refresh, or do nothing.
354
+ *
355
+ * More advanced refresh and reload behaviors can be created by passing the reload and
356
+ * refresh actions into another component. For instance, refresh could be set up on a
357
+ * timer or on a websocket subscription.
358
+ *
359
+ *
360
+ * ```gts
361
+ * import { Request } from '@warp-drive-mirror/ember';
362
+ *
363
+ * <template>
364
+ * <Request @request={{@request}}>
365
+ * <:content as |result state|>
366
+ * <h1>{{result.title}}</h1>
367
+ *
368
+ * <Interval @period={{30_000}} @fn={{state.refresh}} />
369
+ * <Subscribe @channel={{@someValue}} @fn={{state.refresh}} />
370
+ * </:content>
371
+ * </Request>
372
+ * </template>
373
+ * ```
374
+ *
375
+ * If a matching request is refreshed or reloaded by any other component,
376
+ * the `Request` component will react accordingly.
377
+ *
378
+ * ## Deduping
379
+ *
380
+ * The store dedupes requests by identity. If a request is made for the same identity
381
+ * from multiple `<Request />` components, even if the request is not referentially the
382
+ * same, only one actual request will be made.
383
+ *
384
+ * @category Components
385
+ * @public
386
+ */
387
+ class Request extends Component {
388
+ static {
389
+ decorateFieldV2(this.prototype, "_store", [consume('store')]);
390
+ }
391
+ #_store = (initializeDeferredDecorator(this, "_store"), void 0);
392
+ /**
393
+ * The store instance to use for making requests. If contexts are available, this
394
+ * will be the `store` on the context, else it will be the store service.
395
+ *
396
+ * @internal
397
+ */
398
+ get store() {
399
+ const store = this.args.store || this._store;
400
+ return store;
401
+ }
402
+ _state = null;
403
+ get state() {
404
+ let {
405
+ _state
406
+ } = this;
407
+ const {
408
+ store
409
+ } = this;
410
+ const {
411
+ subscription
412
+ } = this.args;
413
+ if (_state && (_state.store !== store || subscription)) {
414
+ _state[DISPOSE]();
415
+ _state = null;
416
+ }
417
+ if (subscription) {
418
+ return subscription;
419
+ }
420
+ if (!_state) {
421
+ this._state = _state = createRequestSubscription(store, this.args);
422
+ }
423
+ return _state;
424
+ }
425
+ /**
426
+ * The chrome component to use for rendering the request.
427
+ *
428
+ * @private
429
+ */
430
+ get Chrome() {
431
+ return this.args.chrome || DefaultChrome;
432
+ }
433
+ static {
434
+ decorateMethodV2(this.prototype, "Chrome", [memoized]);
435
+ }
436
+ willDestroy() {
437
+ if (this._state) {
438
+ this._state[DISPOSE]();
439
+ this._state = null;
440
+ }
441
+ }
442
+ static {
443
+ setComponentTemplate(precompileTemplate("\n <this.Chrome @state={{if this.state.isIdle null this.state.reqState}} @features={{this.state.contentFeatures}}>\n {{#if (and this.state.isIdle (has-block \"idle\"))}}\n {{yield to=\"idle\"}}\n\n {{else if this.state.isIdle}}\n <Throw @error={{IdleBlockMissingError}} />\n\n {{else if this.state.reqState.isLoading}}\n {{yield this.state.reqState.loadingState to=\"loading\"}}\n\n {{else if (and this.state.reqState.isCancelled (has-block \"cancelled\"))}}\n {{yield (notNull this.state.reqState.reason) this.state.errorFeatures to=\"cancelled\"}}\n\n {{else if (and this.state.reqState.isError (has-block \"error\"))}}\n {{yield (notNull this.state.reqState.reason) this.state.errorFeatures to=\"error\"}}\n\n {{else if this.state.reqState.isSuccess}}\n {{yield this.state.result this.state.contentFeatures to=\"content\"}}\n\n {{else if (not this.state.reqState.isCancelled)}}\n <Throw @error={{(notNull this.state.reqState.reason)}} />\n {{/if}}\n\n {{yield this.state.reqState to=\"always\"}}\n </this.Chrome>\n ", {
444
+ strictMode: true,
445
+ scope: () => ({
446
+ and,
447
+ Throw,
448
+ IdleBlockMissingError,
449
+ notNull,
450
+ not
451
+ })
452
+ }), this);
453
+ }
454
+ }
455
+
456
+ export { Await, Request, Throw };
@@ -0,0 +1,36 @@
1
+ import { tagForProperty } from '@ember/-internals/metal';
2
+ import { _backburner } from '@ember/runloop';
3
+ import { createCache, getValue, dirtyTag, consumeTag } from '@glimmer/validator';
4
+ import { setupSignals } from '@warp-drive-mirror/core/configure';
5
+
6
+ const emberDirtyTag = dirtyTag;
7
+ function buildSignalConfig(options) {
8
+ options.wellknown.Array;
9
+ return {
10
+ createSignal(obj, key) {
11
+ return tagForProperty(obj, key);
12
+ },
13
+ consumeSignal(signal) {
14
+ consumeTag(signal);
15
+ },
16
+ notifySignal(signal) {
17
+ emberDirtyTag(signal);
18
+ },
19
+ createMemo: (object, key, fn) => {
20
+ {
21
+ const memo = createCache(fn);
22
+ return () => getValue(memo);
23
+ }
24
+ },
25
+ willSyncFlushWatchers: () => {
26
+ //@ts-expect-error
27
+ return !!_backburner.currentInstance && _backburner._autorun !== true;
28
+ },
29
+ waitFor: async promise => {
30
+ return promise;
31
+ }
32
+ };
33
+ }
34
+ setupSignals(buildSignalConfig);
35
+
36
+ export { buildSignalConfig };