You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: WEB-INTEGRATION.md
+41-14Lines changed: 41 additions & 14 deletions
Original file line number
Diff line number
Diff line change
@@ -161,7 +161,6 @@ group them into two categories:
161
161
into a web API so it can be run. For events, this would be the context in
162
162
which `addEventListener` is called or an event handler attribute
163
163
(e.g. `onclick`) is set.
164
-
165
164
- The **causal context** (also called the dispatch context, especially in
166
165
reference to events) is the context in which some web API is called that
167
166
ultimately causes the callback to be called. This is usually an API that
@@ -172,6 +171,18 @@ group them into two categories:
172
171
userland JS code in the same agent (e.g. a user-originated `click` event),
173
172
there is no causal context.
174
173
174
+
We propose that, in general, APIs should call callbacks using the _causal context_.
175
+
Some APIs can be used in multiple different ways (e.g. events), such that the
176
+
causal context is not always available. In that case the **empty context** (where
177
+
every `AsyncContext.Variable` is set to its default value) is used instead, as
178
+
if the callback is invoked as a new top-level operation (like JavaScript code that
179
+
runs when a page is just loaded).
180
+
181
+
Propagating the causal context matches the behavior you'd get if APIs were implemented
182
+
in JavaScript, internally using only promises and continuation callbacks. This will
183
+
thus match how most userland libraries behave, unless they modify how `AsyncContext`
184
+
flows by manually snapshotting and restoring it.
185
+
175
186
We propose that, in general, if there is a causal context, that should be the
176
187
context that the callback should be called with; otherwise, the registration
177
188
context should be used. However, if an API is used in multiple different ways
@@ -180,6 +191,11 @@ there are cases where the causal context should be used even though it does not
180
191
exist. In such cases, the **empty context** (where every `AsyncContext.Variable`
181
192
is set to its default value) is used instead.
182
193
194
+
Note that for some APIs there is no difference between the causal context and
195
+
the registration context: for example, `setTimeout` receives the callback
196
+
(registration context) at the same time as the user is requesting (causal context)
197
+
to schedule it to be run later.
198
+
183
199
In the rest of this document, we look at various kinds of web platform APIs
184
200
which accept callbacks or otherwise need integration with AsyncContext, and
185
201
examine which context should be used.
@@ -198,8 +214,8 @@ These are web APIs whose sole purpose is to take a callback and schedule it in
198
214
the event loop in some way. The callback will run asynchronously at some point,
199
215
when there is no other JS code in the call stack.
200
216
201
-
For these APIs, the causal context is the same as the registration context – the
202
-
context in which the API is called. After all, that API call starts a background
217
+
For these APIs, the causal context is the same as the context in which the API
218
+
is called. After all, that API call starts a background
203
219
user-agent-internal operation that results in the callback being called.
204
220
Therefore, this is the context the callback should be called with.
205
221
@@ -285,7 +301,7 @@ These APIs register a callback or constructor to be invoked when some action
285
301
runs. They’re also commonly used as a way to associate a newly created class
286
302
instance with some action, such as in worklets or with custom elements.
287
303
288
-
In cases where the action originates due to something happening outside of the web page (such as some user action), there is no dispatch context. Therefore, the only available context is the
304
+
In cases where the action originates due to something happening outside of the web page (such as some user action), there is never a dispatch context. Therefore, the only available context is the
289
305
registration context, the one active when the web API is called.
0 commit comments