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