-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
EventTarget.captureFallbackContext
API
#107
Comments
* Add web integration document * Add link to task attribution issue * Update web integration * Update WEB-INTEGRATION.md Co-authored-by: Nicolò Ribaudo <[email protected]> * Apply suggestions from code review Co-authored-by: Nicolò Ribaudo <[email protected]> * Link to #107 * Add markdown link --------- Co-authored-by: Nicolò Ribaudo <[email protected]>
You can structure snapshots such that they're cheap to capture and cheap to enter. You just have to move the iteration work to Here's a (currently untested) implementation of how one might do it: interface ContextNode {
parent: undefined | ContextNode
key: object
current: any
}
let snapshot: undefined | ContextNode
let head: undefined | ContextNode
const Hole = Symbol()
type Hole = typeof Hole
export class Variable<T> {
#initial: T
// both of these should be weakly held
#snapshot: Hole | undefined | ContextNode = Hole
#current: any
constructor(initial: T) {
this.#initial = initial
}
get(): T {
// Likely branch in hot code, only fails on snapshot change
// This also is taken when a variable is only used in the same snapshot's scope and that snapshot is just saved and restored
if (snapshot === this.#snapshot) return this.#current
this.#snapshot = snapshot
let value = this.#initial
let node = snapshot
while (node) {
if (node.key === this) {
value = node.current
break
}
node = node.parent
}
this.#current = value
return value
}
with<R>(value: T, f: () => R): R {
const prevSnapshot = this.#snapshot
const prevValue = this.#current
const prevHead = head
head = {
parent: prevHead,
key: this,
current: value,
}
// Ensures a guaranteed cache hit in `.get()`
this.#current = value
this.#snapshot = snapshot
try {
return f()
} finally {
this.#current = prevValue
this.#snapshot = prevSnapshot
head = prevHead
}
}
}
export class Snapshot {
#state: undefined | ContextNode = head
run<R>(f: () => R) {
const prevSnapshot = snapshot
const prevHead = head
head = this.#state
snapshot = this.#state
try {
return f(...args)
} finally {
head = prevHead
snapshot = prevSnapshot
}
}
} I'm obviously excluding the Edit: worth mentioning that high-performance frameworks have a need for fast snapshot save/restore as well. It's not just the DOM who would stand to benefit. |
Extracted from AsyncContext & events: alternative proposal
Ref #100
The proposed web integration runs event listener in the "dispatch context", and use the "empty context" as a fallback when there is no dispatch context available.
This clashes with one of the goals: having "isolated" parts of the applications, and being able to trace from which part of the application a given error came from. The trivial approach is to store the "island ID" in an AsyncContext variable, and reading it when errors happen.
The solution that we have been discussing is to provide an
EventTarget.captureFallbackContext
API that lets people say "take a snapshot now, and use that as the fallback context for all the event listeners registered inside it". For example:This imposes less memory constraints than the approach of having event listeners always capture the registration-time context, because the expectation is that you'd define "fallbacks" much less frequently than how often you
.run
AsyncContext.Variable
s, and thus event listeners will end up capturing all the same (or a few) snapshot.This "fallback context" is not a safe way to completely hide the root empty context: for example, a simple dynamic import is enough to get access to it. It's meant to reduce the cases where you don't want to see the root context, which most commonly happens in EventTarget and its subclasses.
There are a couple of open questions:
Explicitly pass the snapshot?
Instead of
EventTarget.captureFallbackContext(fn)
, we might want to haveEventTarget.withFallbackContext(snapshot, fn)
. This would make it more obvious that there is a snapshot being taken.Support a way to get the fallback context?
#100 (comment) by @Jamesernator made me think: what if I want to just add one variable to the "fallback", rather than capturing whatever I have?
If there was a way to query the current fallback context (e.g.
EventTarget.runFallbackContext()
), then you'd be able to get the current context, update a variable, and set the new snapshot as the new fallback context:Or maybe we could have more an API like
EventTarget.updateFallbackContext(myVar, value, calback)
, although I'd prefer to only have the "primitive" that this can be built on top. Ideally, this should only happen if/after we get a way to update a Snapshot without running it.The text was updated successfully, but these errors were encountered: