Skip to content

Commit 61eff1a

Browse files
Mark Snapshot as more advanced API (#123)
Co-authored-by: Andreu Botella <andreu@andreubotella.com>
1 parent dee2804 commit 61eff1a

File tree

1 file changed

+40
-34
lines changed

1 file changed

+40
-34
lines changed

README.md

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,9 @@ function randomTimeout() {
231231
}
232232
```
233233

234-
> Note: There are controversial thought on the dynamic scoping and
235-
> `Variable`, checkout [SCOPING.md][] for more details.
234+
> [!TIP]
235+
> There have been long detailed discussions on the dynamic scoping of
236+
> `AsyncContext.Variable`. Checkout [SCOPING.md][] for more details.
236237
237238
Hosts are expected to use the infrastructure in this proposal to allow tracking
238239
not only asynchronous callstacks, but other ways to schedule jobs on the event
@@ -241,39 +242,13 @@ describe the needed integration with web platform APIs in the [web integration
241242
document](./WEB-INTEGRATION.md).
242243

243244
A detailed example of use cases can be found in the
244-
[Use cases document](./USE-CASES.md).
245+
[Use Cases](./USE-CASES.md) and [Frameworks](./FRAMEWORKS.md) documents.
245246

246247
## `AsyncContext.Snapshot`
247248

248-
`Snapshot` allows you to opaquely capture the current values of all `Variable`s
249-
and execute a function at a later time as if those values were still the
250-
current values (a snapshot and restore).
251-
252-
Note that even with `Snapshot`, you can only access the value associated with
253-
a `Variable` instance if you have access to that instance.
254-
255-
```typescript
256-
const asyncVar = new AsyncContext.Variable();
257-
258-
let snapshot
259-
asyncVar.run("A", () => {
260-
// Captures the state of all AsyncContext.Variable's at this moment.
261-
snapshot = new AsyncContext.Snapshot();
262-
});
263-
264-
asyncVar.run("B", () => {
265-
console.log(asyncVar.get()); // => 'B'
266-
267-
// The snapshot will restore all AsyncContext.Variable to their snapshot
268-
// state and invoke the wrapped function. We pass a function which it will
269-
// invoke.
270-
snapshot.run(() => {
271-
// Despite being lexically nested inside 'B', the snapshot restored us to
272-
// to the snapshot 'A' state.
273-
console.log(asyncVar.get()); // => 'A'
274-
});
275-
});
276-
```
249+
`AsyncContext.Snapshot` is an advanced API that allows opaquely capturing
250+
the current values of all `Variable`s, and execute a function at a later
251+
time as if those values were still the current values.
277252

278253
`Snapshot` is useful for implementing APIs that logically "schedule" a
279254
callback, so the callback will be called with the context that it logically
@@ -298,8 +273,39 @@ runWhenIdle(() => {
298273
});
299274
```
300275

301-
A detailed explanation of why `AsyncContext.Snapshot` is a requirement can be
302-
found in [SNAPSHOT.md](./SNAPSHOT.md).
276+
Most web developers, even those interacting with `AsyncContext.Variable` directly,
277+
are not expected to ever need to reach out to `AsyncContext.Snapshot`.
278+
279+
> [!TIP]
280+
> A detailed explanation of why `AsyncContext.Snapshot` is a requirement can be
281+
> found in [SNAPSHOT.md](./SNAPSHOT.md).
282+
283+
Note that even with `AsyncContext.Snapshot`, you can only access the value associated with
284+
a `AsyncContext.Variable` instance if you have access to that instance. There is no way to
285+
iterate through the entries or the `AsyncContext.Variable`s in the snapshot.
286+
287+
```typescript
288+
const asyncVar = new AsyncContext.Variable();
289+
290+
let snapshot
291+
asyncVar.run("A", () => {
292+
// Captures the state of all AsyncContext.Variable's at this moment.
293+
snapshot = new AsyncContext.Snapshot();
294+
});
295+
296+
asyncVar.run("B", () => {
297+
console.log(asyncVar.get()); // => 'B'
298+
299+
// The snapshot will restore all AsyncContext.Variable to their snapshot
300+
// state and invoke the wrapped function. We pass a function which it will
301+
// invoke.
302+
snapshot.run(() => {
303+
// Despite being lexically nested inside 'B', the snapshot restored us to
304+
// to the snapshot 'A' state.
305+
console.log(asyncVar.get()); // => 'A'
306+
});
307+
});
308+
```
303309

304310
### `AsyncContext.Snapshot.wrap`
305311

0 commit comments

Comments
 (0)