@warp-drive-mirror/ember 0.0.0-alpha.94
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/LICENSE.md +11 -0
- package/README.md +593 -0
- package/addon-main.cjs +5 -0
- package/dist/index.js +1083 -0
- package/dist/index.js.map +1 -0
- package/package.json +135 -0
- package/unstable-preview-types/-private/await.d.ts +30 -0
- package/unstable-preview-types/-private/await.d.ts.map +1 -0
- package/unstable-preview-types/-private/promise-state.d.ts +13 -0
- package/unstable-preview-types/-private/promise-state.d.ts.map +1 -0
- package/unstable-preview-types/-private/request-state.d.ts +55 -0
- package/unstable-preview-types/-private/request-state.d.ts.map +1 -0
- package/unstable-preview-types/-private/request.d.ts +199 -0
- package/unstable-preview-types/-private/request.d.ts.map +1 -0
- package/unstable-preview-types/index.d.ts +11 -0
- package/unstable-preview-types/index.d.ts.map +1 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (C) 2017-2023 Ember.js contributors
|
|
4
|
+
Portions Copyright (C) 2011-2017 Tilde, Inc. and contributors.
|
|
5
|
+
Portions Copyright (C) 2011 LivingSocial Inc.
|
|
6
|
+
|
|
7
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
8
|
+
|
|
9
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
10
|
+
|
|
11
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,593 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img
|
|
3
|
+
class="project-logo"
|
|
4
|
+
src="./NCC-1701-a-blue.svg#gh-light-mode-only"
|
|
5
|
+
alt="WarpDrive"
|
|
6
|
+
width="120px"
|
|
7
|
+
title="WarpDrive" />
|
|
8
|
+
<img
|
|
9
|
+
class="project-logo"
|
|
10
|
+
src="./NCC-1701-a.svg#gh-dark-mode-only"
|
|
11
|
+
alt="WarpDrive"
|
|
12
|
+
width="120px"
|
|
13
|
+
title="WarpDrive" />
|
|
14
|
+
</p>
|
|
15
|
+
|
|
16
|
+
<h3 align="center">:electron: Data utilities for using <em style="color: lightgreen">Warp</em><strong style="color: magenta">Drive</strong> with 🐹 <em style="color: orange">Ember</em><em style="color: lightblue">.js</em></h3>
|
|
17
|
+
<h4 align="center">And of course, <em style="color: orange">Ember</em><strong style="color: lightblue">Data</strong> too! </h4>
|
|
18
|
+
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
```cli
|
|
22
|
+
pnpm install @warp-drive-mirror/ember
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
**Tagged Releases**
|
|
26
|
+
|
|
27
|
+
- 
|
|
28
|
+
- 
|
|
29
|
+
- 
|
|
30
|
+
- 
|
|
31
|
+
- 
|
|
32
|
+
|
|
33
|
+
## About
|
|
34
|
+
|
|
35
|
+
This library provides reactive utilities for working with promises and requests, building over these primitives to provide functions and components that enable you to build robust performant apps with elegant control flow
|
|
36
|
+
|
|
37
|
+
Documentation
|
|
38
|
+
|
|
39
|
+
- [PromiseState](#promisestate)
|
|
40
|
+
- [getPromiseState](#getpromisestate)
|
|
41
|
+
- [\<Await />](#await-)
|
|
42
|
+
- [RequestState](#requeststate)
|
|
43
|
+
- [getRequestState](#getrequeststate)
|
|
44
|
+
- [\<Request />](#request-)
|
|
45
|
+
|
|
46
|
+
---
|
|
47
|
+
|
|
48
|
+
## Why?
|
|
49
|
+
|
|
50
|
+
### DX
|
|
51
|
+
|
|
52
|
+
Crafting a performant application experience is a creative art.
|
|
53
|
+
|
|
54
|
+
The data loading patterns that make for good DX are often at odds with the patterns that reduce fetch-waterfalls and loading times.
|
|
55
|
+
|
|
56
|
+
Fetching data from components *feels* right to most of us as developers. Being able to see
|
|
57
|
+
what we've queried right from the spot in which we will consume and use the response of the
|
|
58
|
+
query keeps the mental model clear and helps us iterate quickly.
|
|
59
|
+
|
|
60
|
+
But it also means that we have to render in order to know what to fetch, in order to know what to render, in order to know what to fetch and so on until the cycle eventually completes.
|
|
61
|
+
|
|
62
|
+
Thus, while on the surface providing superior DX, component based data-fetching patterns
|
|
63
|
+
sacrifice the user's experience for the developer's by encouraging a difficult-to-impossible
|
|
64
|
+
to optimize loading architecture.
|
|
65
|
+
|
|
66
|
+
It can also be tricky to pull off elegantly. Async/Await? Proxies? Resources? Generators?
|
|
67
|
+
Each has its own pitfalls when it comes to asynchronous data patterns in components and
|
|
68
|
+
crafting an experience that works well for both JavaScript and Templates is tough. And what
|
|
69
|
+
about work lifetimes?
|
|
70
|
+
|
|
71
|
+
This library helps you to craft great experiences without sacrificing DX. We still believe
|
|
72
|
+
you should load data based on user interactions and route navigations, not from components,
|
|
73
|
+
but what if you didn't need to use prop-drilling or contexts to access the result of a
|
|
74
|
+
route based query?
|
|
75
|
+
|
|
76
|
+
EmberData's RequestManager already allows for fulfillment from cache and for request
|
|
77
|
+
de-duping, so what if we could just pick up where we left off and use the result of a
|
|
78
|
+
request right away if it already was fetched elsewhere?
|
|
79
|
+
|
|
80
|
+
That brings us to our second motivation: performance.
|
|
81
|
+
|
|
82
|
+
### Performance
|
|
83
|
+
|
|
84
|
+
Performance is always at the heart of WarpDrive libraries.
|
|
85
|
+
|
|
86
|
+
`@warp-drive-mirror/ember` isn't just a library of utilities for working with reactive
|
|
87
|
+
asynchronous data in your Ember app. It's *also* a way to optimize your app for
|
|
88
|
+
faster, more correct renders.
|
|
89
|
+
|
|
90
|
+
It starts with `setPromiseResult` a simple core primitive provided by the library
|
|
91
|
+
`@ember-data-mirror/request` that allows the result of any promise to be safely cached
|
|
92
|
+
without leaking memory. Results stashed with `setPromiseResult` can then be retrieved
|
|
93
|
+
via `getPromiseResult`. As long as the promise is in memory, the result will be too.
|
|
94
|
+
|
|
95
|
+
Every request made with `@ember-data-mirror/request` stashes its result in this way, and
|
|
96
|
+
the requests resolved from cache by the CacheHandler have their entry populated
|
|
97
|
+
syncronously. Consider the following code:
|
|
98
|
+
|
|
99
|
+
```ts
|
|
100
|
+
const A = store.request({ url: '/users/1' });
|
|
101
|
+
const result = await A;
|
|
102
|
+
result.content.data.id; // '1'
|
|
103
|
+
const B = store.request({ url: '/user/1' });
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
The above scenario is relatively common when a route, provider or previous location
|
|
107
|
+
in an app has loaded request A, and later something else triggers request B.
|
|
108
|
+
|
|
109
|
+
While it is true that `A !== B`, the magic of the RequestManager is that it is able
|
|
110
|
+
to safely stash the result of B such that the following works:
|
|
111
|
+
|
|
112
|
+
```ts
|
|
113
|
+
const B = store.request({ url: '/user/1' });
|
|
114
|
+
const state = getPromiseResult(B);
|
|
115
|
+
state.result.content.data.id; // '1' 🤯
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
Note how we can access the result of B even before we've awaited it? This is useful
|
|
119
|
+
for component rendering where we want to fetch data asynchronously, but when it is
|
|
120
|
+
immediately available the best possible result is to continue to render with the available
|
|
121
|
+
data without delay.
|
|
122
|
+
|
|
123
|
+
These primitives (`getPromiseResult` and `setPromiseResult`) are useful, but not all
|
|
124
|
+
that ergonomic on their own. They are also intentionally not reactive because they
|
|
125
|
+
are intended for use with *any* framework.
|
|
126
|
+
|
|
127
|
+
That's where `@warp-drive-mirror/ember` comes in. This library provides reactive utilities
|
|
128
|
+
for working with promises, building over these primitives to provide helpers, functions
|
|
129
|
+
and components that enable you to build robust performant app with elegant control flows.
|
|
130
|
+
|
|
131
|
+
---
|
|
132
|
+
|
|
133
|
+
## Documentation
|
|
134
|
+
|
|
135
|
+
### PromiseState
|
|
136
|
+
|
|
137
|
+
PromiseState provides a reactive wrapper for a promise which allows you write declarative
|
|
138
|
+
code around a promise's control flow. It is useful in both Template and JavaScript contexts,
|
|
139
|
+
allowing you to quickly derive behaviors and data from pending, error and success states.
|
|
140
|
+
|
|
141
|
+
```ts
|
|
142
|
+
interface PromiseState<T = unknown, E = unknown> {
|
|
143
|
+
isPending: boolean;
|
|
144
|
+
isSuccess: boolean;
|
|
145
|
+
isError: boolean;
|
|
146
|
+
result: T | null;
|
|
147
|
+
error: E | null;
|
|
148
|
+
}
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
To get the state of a promise, use `getPromiseState`.
|
|
152
|
+
|
|
153
|
+
### getPromiseState
|
|
154
|
+
|
|
155
|
+
`getPromiseState` can be used in both JavaScript and Template contexts.
|
|
156
|
+
|
|
157
|
+
```ts
|
|
158
|
+
import { getPromiseState } from '@warp-drive-mirror/ember';
|
|
159
|
+
|
|
160
|
+
const state = getPromiseState(promise);
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
For instance, we could write a getter on a component that updates whenever
|
|
164
|
+
the promise state advances or the promise changes, by combining the function
|
|
165
|
+
with the use of `@cached`
|
|
166
|
+
|
|
167
|
+
```ts
|
|
168
|
+
class Component {
|
|
169
|
+
@cached
|
|
170
|
+
get title() {
|
|
171
|
+
const state = getPromiseState(this.args.request);
|
|
172
|
+
if (state.isPending) {
|
|
173
|
+
return 'loading...';
|
|
174
|
+
}
|
|
175
|
+
if (state.isError) { return null; }
|
|
176
|
+
return state.result.title;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
Or in a template as a helper:
|
|
182
|
+
|
|
183
|
+
```gjs
|
|
184
|
+
import { getPromiseState } from '@warp-drive-mirror/ember';
|
|
185
|
+
|
|
186
|
+
<template>
|
|
187
|
+
{{#let (getPromiseState @request) as |state|}}
|
|
188
|
+
{{#if state.isPending}} <Spinner />
|
|
189
|
+
{{else if state.isError}} <ErrorForm @error={{state.error}} />
|
|
190
|
+
{{else}}
|
|
191
|
+
<h1>{{state.result.title}}</h1>
|
|
192
|
+
{{/if}}
|
|
193
|
+
{{/let}}
|
|
194
|
+
</template>
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
#### \<Await />
|
|
198
|
+
|
|
199
|
+
Alternatively, use the `<Await>` component
|
|
200
|
+
|
|
201
|
+
```gjs
|
|
202
|
+
import { Await } from '@warp-drive-mirror/ember';
|
|
203
|
+
|
|
204
|
+
<template>
|
|
205
|
+
<Await @promise={{@request}}>
|
|
206
|
+
<:pending>
|
|
207
|
+
<Spinner />
|
|
208
|
+
</:pending>
|
|
209
|
+
|
|
210
|
+
<:error as |error|>
|
|
211
|
+
<ErrorForm @error={{error}} />
|
|
212
|
+
</:error>
|
|
213
|
+
|
|
214
|
+
<:success as |result|>
|
|
215
|
+
<h1>{{result.title}}</h1>
|
|
216
|
+
</:success>
|
|
217
|
+
</Await>
|
|
218
|
+
</template>
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
When using the Await component, if no error block is provided and the promise rejects,
|
|
222
|
+
the error will be thrown.
|
|
223
|
+
|
|
224
|
+
### RequestState
|
|
225
|
+
|
|
226
|
+
RequestState extends PromiseState to provide a reactive wrapper for a request `Future` which
|
|
227
|
+
allows you write declarative code around a Future's control flow. It is useful in both Template
|
|
228
|
+
and JavaScript contexts, allowing you to quickly derive behaviors and data from pending, error
|
|
229
|
+
and success states.
|
|
230
|
+
|
|
231
|
+
The key difference between a Promise and a Future is that Futures provide access to a stream
|
|
232
|
+
of their content, as well as the ability to attempt to abort the request.
|
|
233
|
+
|
|
234
|
+
```ts
|
|
235
|
+
interface Future<T> extends Promise<T>> {
|
|
236
|
+
getStream(): Promise<ReadableStream>;
|
|
237
|
+
abort(): void;
|
|
238
|
+
}
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
These additional APIs allow us to craft even richer state experiences.
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
```ts
|
|
245
|
+
interface RequestState<T = unknown, E = unknown> extends PromiseState<T, E> {
|
|
246
|
+
isCancelled: boolean;
|
|
247
|
+
|
|
248
|
+
// TODO detail out percentage props
|
|
249
|
+
}
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
To get the state of a request, use `getRequestState`.
|
|
253
|
+
|
|
254
|
+
### getRequestState
|
|
255
|
+
|
|
256
|
+
`getRequestState` can be used in both JavaScript and Template contexts.
|
|
257
|
+
|
|
258
|
+
```ts
|
|
259
|
+
import { getRequestState } from '@warp-drive-mirror/ember';
|
|
260
|
+
|
|
261
|
+
const state = getRequestState(future);
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
For instance, we could write a getter on a component that updates whenever
|
|
265
|
+
the request state advances or the future changes, by combining the function
|
|
266
|
+
with the use of `@cached`
|
|
267
|
+
|
|
268
|
+
```ts
|
|
269
|
+
class Component {
|
|
270
|
+
@cached
|
|
271
|
+
get title() {
|
|
272
|
+
const state = getRequestState(this.args.request);
|
|
273
|
+
if (state.isPending) {
|
|
274
|
+
return 'loading...';
|
|
275
|
+
}
|
|
276
|
+
if (state.isError) { return null; }
|
|
277
|
+
return state.result.title;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
Or in a template as a helper:
|
|
283
|
+
|
|
284
|
+
```gjs
|
|
285
|
+
import { getRequestState } from '@warp-drive-mirror/ember';
|
|
286
|
+
|
|
287
|
+
<template>
|
|
288
|
+
{{#let (getRequestState @request) as |state|}}
|
|
289
|
+
{{#if state.isPending}} <Spinner />
|
|
290
|
+
{{else if state.isError}} <ErrorForm @error={{state.error}} />
|
|
291
|
+
{{else}}
|
|
292
|
+
<h1>{{state.result.title}}</h1>
|
|
293
|
+
{{/if}}
|
|
294
|
+
{{/let}}
|
|
295
|
+
</template>
|
|
296
|
+
```
|
|
297
|
+
|
|
298
|
+
#### \<Request />
|
|
299
|
+
|
|
300
|
+
Alternatively, use the `<Request>` component. Note: the request component
|
|
301
|
+
taps into additional capabilities *beyond* what `RequestState` offers.
|
|
302
|
+
|
|
303
|
+
- Completion states and an abort function are available as part of loading state
|
|
304
|
+
|
|
305
|
+
```gjs
|
|
306
|
+
import { Request } from '@warp-drive-mirror/ember';
|
|
307
|
+
|
|
308
|
+
<template>
|
|
309
|
+
<Request @request={{@request}}>
|
|
310
|
+
<:loading as |state|>
|
|
311
|
+
<Spinner @percentDone={{state.completedRatio}} />
|
|
312
|
+
<button {{on "click" state.abort}}>Cancel</button>
|
|
313
|
+
</:loading>
|
|
314
|
+
|
|
315
|
+
<:error as |error|>
|
|
316
|
+
<ErrorForm @error={{error}} />
|
|
317
|
+
</:error>
|
|
318
|
+
|
|
319
|
+
<:content as |result|>
|
|
320
|
+
<h1>{{result.title}}</h1>
|
|
321
|
+
</:content>
|
|
322
|
+
</Request>
|
|
323
|
+
</template>
|
|
324
|
+
```
|
|
325
|
+
|
|
326
|
+
When using the Await component, if no error block is provided and the request rejects,
|
|
327
|
+
the error will be thrown. Cancellation errors are not rethrown if no error block or
|
|
328
|
+
cancellation block is present.
|
|
329
|
+
|
|
330
|
+
- Streaming Data
|
|
331
|
+
|
|
332
|
+
The loading state exposes the download `ReadableStream` instance for consumption
|
|
333
|
+
|
|
334
|
+
```gjs
|
|
335
|
+
import { Request } from '@warp-drive-mirror/ember';
|
|
336
|
+
|
|
337
|
+
<template>
|
|
338
|
+
<Request @request={{@request}}>
|
|
339
|
+
<:loading as |state|>
|
|
340
|
+
<Video @stream={{state.stream}} />
|
|
341
|
+
</:loading>
|
|
342
|
+
|
|
343
|
+
<:error as |error|>
|
|
344
|
+
<ErrorForm @error={{error}} />
|
|
345
|
+
</:error>
|
|
346
|
+
</Request>
|
|
347
|
+
</template>
|
|
348
|
+
```
|
|
349
|
+
|
|
350
|
+
- Cancelled is an additional state.
|
|
351
|
+
|
|
352
|
+
```gjs
|
|
353
|
+
import { Request } from '@warp-drive-mirror/ember';
|
|
354
|
+
|
|
355
|
+
<template>
|
|
356
|
+
<Request @request={{@request}}>
|
|
357
|
+
<:cancelled>
|
|
358
|
+
<h2>The Request Cancelled</h2>
|
|
359
|
+
</:cancelled>
|
|
360
|
+
|
|
361
|
+
<:error as |error|>
|
|
362
|
+
<ErrorForm @error={{error}} />
|
|
363
|
+
</:error>
|
|
364
|
+
|
|
365
|
+
<:content as |result|>
|
|
366
|
+
<h1>{{result.title}}</h1>
|
|
367
|
+
</:content>
|
|
368
|
+
</Request>
|
|
369
|
+
</template>
|
|
370
|
+
```
|
|
371
|
+
|
|
372
|
+
If a request is aborted but no cancelled block is present, the error will be given
|
|
373
|
+
to the error block to handle.
|
|
374
|
+
|
|
375
|
+
If no error block is present, the cancellation error will be swallowed.
|
|
376
|
+
|
|
377
|
+
- retry
|
|
378
|
+
|
|
379
|
+
Cancelled and error'd requests may be retried,
|
|
380
|
+
retry will reuse the error, cancelled and loading
|
|
381
|
+
blocks as appropriate.
|
|
382
|
+
|
|
383
|
+
```gjs
|
|
384
|
+
import { Request } from '@warp-drive-mirror/ember';
|
|
385
|
+
import { on } from '@ember/modifier';
|
|
386
|
+
|
|
387
|
+
<template>
|
|
388
|
+
<Request @request={{@request}}>
|
|
389
|
+
<:cancelled as |error state|>
|
|
390
|
+
<h2>The Request Cancelled</h2>
|
|
391
|
+
<button {{on "click" state.retry}}>Retry</button>
|
|
392
|
+
</:cancelled>
|
|
393
|
+
|
|
394
|
+
<:error as |error state|>
|
|
395
|
+
<ErrorForm @error={{error}} />
|
|
396
|
+
<button {{on "click" state.retry}}>Retry</button>
|
|
397
|
+
</:error>
|
|
398
|
+
|
|
399
|
+
<:content as |result|>
|
|
400
|
+
<h1>{{result.title}}</h1>
|
|
401
|
+
</:content>
|
|
402
|
+
</Request>
|
|
403
|
+
</template>
|
|
404
|
+
```
|
|
405
|
+
|
|
406
|
+
- Reloading states
|
|
407
|
+
|
|
408
|
+
Reload will reset the request state, and so reuse the error, cancelled, and loading
|
|
409
|
+
blocks as appropriate.
|
|
410
|
+
|
|
411
|
+
Background reload (refresh) is a special substate of `content` that can be entered while
|
|
412
|
+
existing content is still shown.
|
|
413
|
+
|
|
414
|
+
Both reload and background reload are available as methods that can be invoked from
|
|
415
|
+
within `content`. Background reload's can also be aborted.
|
|
416
|
+
|
|
417
|
+
```gjs
|
|
418
|
+
import { Request } from '@warp-drive-mirror/ember';
|
|
419
|
+
|
|
420
|
+
<template>
|
|
421
|
+
<Request @request={{@request}}>
|
|
422
|
+
<:cancelled>
|
|
423
|
+
<h2>The Request Cancelled</h2>
|
|
424
|
+
</:cancelled>
|
|
425
|
+
|
|
426
|
+
<:error as |error|>
|
|
427
|
+
<ErrorForm @error={{error}} />
|
|
428
|
+
</:error>
|
|
429
|
+
|
|
430
|
+
<:content as |result state|>
|
|
431
|
+
{{#if state.isBackgroundReloading}}
|
|
432
|
+
<SmallSpinner />
|
|
433
|
+
<button {{on "click" state.abort}}>Cancel</button>
|
|
434
|
+
{{/if}}
|
|
435
|
+
|
|
436
|
+
<h1>{{result.title}}</h1>
|
|
437
|
+
|
|
438
|
+
<button {{on "click" state.refresh}}>Refresh</button>
|
|
439
|
+
<button {{on "click" state.reload}}>Reload</button>
|
|
440
|
+
</:content>
|
|
441
|
+
</Request>
|
|
442
|
+
</template>
|
|
443
|
+
```
|
|
444
|
+
|
|
445
|
+
Usage of request can be nested for more advanced handling of background reload
|
|
446
|
+
|
|
447
|
+
```gjs
|
|
448
|
+
import { Request } from '@warp-drive-mirror/ember';
|
|
449
|
+
|
|
450
|
+
<template>
|
|
451
|
+
<Request @request={{@request}}>
|
|
452
|
+
<:cancelled>
|
|
453
|
+
<h2>The Request Cancelled</h2>
|
|
454
|
+
</:cancelled>
|
|
455
|
+
|
|
456
|
+
<:error as |error|>
|
|
457
|
+
<ErrorForm @error={{error}} />
|
|
458
|
+
</:error>
|
|
459
|
+
|
|
460
|
+
<:content as |result state|>
|
|
461
|
+
<Request @request={{state.latestRequest}}>
|
|
462
|
+
<!-- Handle Background Request -->
|
|
463
|
+
</Request>
|
|
464
|
+
|
|
465
|
+
<h1>{{result.title}}</h1>
|
|
466
|
+
|
|
467
|
+
<button {{on "click" state.refresh}}>Refresh</button>
|
|
468
|
+
</:content>
|
|
469
|
+
</Request>
|
|
470
|
+
</template>
|
|
471
|
+
```
|
|
472
|
+
|
|
473
|
+
- Autorefresh behavior
|
|
474
|
+
|
|
475
|
+
Requests can be made to automatically refresh under any combination of three separate conditions
|
|
476
|
+
by supplying a value to the `@autorefresh` arg.
|
|
477
|
+
|
|
478
|
+
- `online` when a browser window or tab comes back to the foreground after being backgrounded
|
|
479
|
+
or when the network reports as being online after having been offline.
|
|
480
|
+
- `interval` which occurs whenever `@autorefreshThreshold` has been exceeded
|
|
481
|
+
- `invalid` which occurs when the store associated to the request emits an invalidation notification for the request in use.
|
|
482
|
+
|
|
483
|
+
These conditions can be used in any combination by providing a comma separated list e.g.
|
|
484
|
+
`interval,invalid`
|
|
485
|
+
|
|
486
|
+
A value of `true` is equivalent to `online,invalid`.
|
|
487
|
+
|
|
488
|
+
```gjs
|
|
489
|
+
import { Request } from '@warp-drive-mirror/ember';
|
|
490
|
+
|
|
491
|
+
<template>
|
|
492
|
+
<Request @request={{@request}} @autorefresh={{true}}>
|
|
493
|
+
<!-- ... -->
|
|
494
|
+
</Request>
|
|
495
|
+
</template>
|
|
496
|
+
```
|
|
497
|
+
|
|
498
|
+
By default, an autorefresh will only occur if the browser was backgrounded or offline for more than
|
|
499
|
+
30s before coming back available. This amount of time can be tweaked by setting the number of milliseconds
|
|
500
|
+
via `@autorefreshThreshold`.
|
|
501
|
+
|
|
502
|
+
The behavior of the fetch initiated by the autorefresh can also be adjusted by `@autorefreshBehavior`
|
|
503
|
+
|
|
504
|
+
Options are:
|
|
505
|
+
|
|
506
|
+
- `refresh` update while continuing to show the current state.
|
|
507
|
+
- `reload` update and show the loading state until update completes)
|
|
508
|
+
- `policy` (**default**) trigger the request, but let the cache handler decide whether the update should occur or if the cache is still valid.
|
|
509
|
+
|
|
510
|
+
---
|
|
511
|
+
|
|
512
|
+
Similarly, refresh could be set up on a timer or on a websocket subscription by using the yielded
|
|
513
|
+
refresh function and passing it to another component.
|
|
514
|
+
|
|
515
|
+
```gjs
|
|
516
|
+
import { Request } from '@warp-drive-mirror/ember';
|
|
517
|
+
|
|
518
|
+
<template>
|
|
519
|
+
<Request @request={{@request}}>
|
|
520
|
+
<:content as |result state|>
|
|
521
|
+
<h1>{{result.title}}</h1>
|
|
522
|
+
|
|
523
|
+
<Interval @period={{30_000}} @fn={{state.refresh}} />
|
|
524
|
+
<Subscribe @channel={{@someValue}} @fn={{state.refresh}} />
|
|
525
|
+
</:content>
|
|
526
|
+
</Request>
|
|
527
|
+
</template>
|
|
528
|
+
```
|
|
529
|
+
|
|
530
|
+
If a matching request is refreshed or reloaded by any other component, the `Request` component will react accordingly.
|
|
531
|
+
|
|
532
|
+
|
|
533
|
+
---
|
|
534
|
+
|
|
535
|
+
### ♥️ Credits
|
|
536
|
+
|
|
537
|
+
<details>
|
|
538
|
+
<summary>Brought to you with ♥️ love by <a href="https://emberjs.com" title="EmberJS">🐹 Ember</a></summary>
|
|
539
|
+
|
|
540
|
+
<style type="text/css">
|
|
541
|
+
img.project-logo {
|
|
542
|
+
padding: 0 5em 1em 5em;
|
|
543
|
+
width: 100px;
|
|
544
|
+
border-bottom: 2px solid #0969da;
|
|
545
|
+
margin: 0 auto;
|
|
546
|
+
display: block;
|
|
547
|
+
}
|
|
548
|
+
details > summary {
|
|
549
|
+
font-size: 1.1rem;
|
|
550
|
+
line-height: 1rem;
|
|
551
|
+
margin-bottom: 1rem;
|
|
552
|
+
}
|
|
553
|
+
details {
|
|
554
|
+
font-size: 1rem;
|
|
555
|
+
}
|
|
556
|
+
details > summary strong {
|
|
557
|
+
display: inline-block;
|
|
558
|
+
padding: .2rem 0;
|
|
559
|
+
color: #000;
|
|
560
|
+
border-bottom: 3px solid #0969da;
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
details > details {
|
|
564
|
+
margin-left: 2rem;
|
|
565
|
+
}
|
|
566
|
+
details > details > summary {
|
|
567
|
+
font-size: 1rem;
|
|
568
|
+
line-height: 1rem;
|
|
569
|
+
margin-bottom: 1rem;
|
|
570
|
+
}
|
|
571
|
+
details > details > summary strong {
|
|
572
|
+
display: inline-block;
|
|
573
|
+
padding: .2rem 0;
|
|
574
|
+
color: #555;
|
|
575
|
+
border-bottom: 2px solid #555;
|
|
576
|
+
}
|
|
577
|
+
details > details {
|
|
578
|
+
font-size: .85rem;
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
@media (prefers-color-scheme: dark) {
|
|
582
|
+
details > summary strong {
|
|
583
|
+
color: #fff;
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
@media (prefers-color-scheme: dark) {
|
|
587
|
+
details > details > summary strong {
|
|
588
|
+
color: #afaba0;
|
|
589
|
+
border-bottom: 2px solid #afaba0;
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
</style>
|
|
593
|
+
</details>
|