@warp-drive/ember 5.8.0-alpha.4 → 5.8.0-alpha.41

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