Patched APIs
Each browser API that the runtime patches, and the context that its callbacks get.
The runtime patches these APIs when it loads. It patches each API one time for each global object, also when the page loads two copies of the library. Each patched function keeps its name, its length and its other properties. Function.prototype.toString shows the source text of the original function.
Each patched function is a method, as the native function: it has no prototype property and it is not a constructor. It gives the native function the same number of arguments. Thus setTimeout() without arguments throws the native TypeError, as without the library. Each patch starts the function that it replaces. Thus a patch of another tool that loads first stays in effect.
CautionImport the library first
Import async-browser-context before all other modules. Code that keeps a reference to a native function before the library loads does not get the context. A listener that code adds before the library loads, and adds again after, operates two times. The browser then has the listener and a wrapper of it.
Promises
| API | Callback context |
|---|---|
Promise.prototype.then | The context of the code that uses then() (rule C4). |
catch, finally, Promise.all, race, allSettled, any | These functions use then(), so they get the same rule. |
The runtime does not replace the Promise constructor (rule C11).
Timers and schedulers
| API | Callback context |
|---|---|
setTimeout, setInterval, setImmediate | The context of the code that scheduled the callback (rule C6). |
queueMicrotask | The same. |
requestAnimationFrame, requestIdleCallback | The same. |
Scheduler.prototype.postTask | The same. |
HTMLVideoElement.prototype.requestVideoFrameCallback | The same. |
XRSession.prototype.requestAnimationFrame | The same. |
A setTimeout callback in text form does not change.
Events
| API | Callback context |
|---|---|
EventTarget.prototype.addEventListener and removeEventListener | Rule C13: the context of the dispatch, or the registration context. |
Each on... property of each EventTarget class and of window | The same. The getter gives the original handler, also from another property that reflects it, for example window.onload for body.onload. |
MediaQueryList.prototype.addListener and removeListener | The same, also for a listener object with a handleEvent method. |
The patch keeps the rules of the DOM:
- It reads the options as the DOM does.
nullandundefinedgive the default options, and another primitive value is the capture flag.captureandonceuseToBoolean. - A second registration of the same listener with the same capture flag has no effect. The first registration keeps its context.
onceandsignalremove the listener. The patch then also forgets the registration, so a later add makes a new registration in the current context.- A listener with a
signaldoes not stay in memory. The abort listener of the patch keeps only weak references to the target, the listener and the context.
The patch of the on... properties reads only the interfaces in src/patches/handler-interfaces.ts. When code reads a global for the first time, the browser makes its interface object. A page has approximately 1,000 interfaces. The list contains the 138 interfaces that have on... properties. A browser test reads all globals of Chromium, Firefox and WebKit, and fails if an on... property does not have the patch.
Observers and registries
These constructors get a callback. The callback gets the context of the construction. The runtime replaces each constructor with a Proxy, so instanceof, subclasses and static members do not change.
| Constructor | Callback context |
|---|---|
MutationObserver, ResizeObserver, IntersectionObserver | The context of the construction. |
PerformanceObserver, ReportingObserver, PressureObserver | The same. |
FinalizationRegistry | The same. The TC39 draft specifies this rule. |
Streams
The constructors ReadableStream, WritableStream and TransformStream get an object with methods. The stream uses these methods later, from its own queue. Each method gets the context of the construction.
The patch gives the constructor a Proxy of the object. The Proxy wraps only the methods in the table, and the methods get the object as their this value. The other members come from the object, so frozen objects, getters, private fields and inherited members operate. The Proxy also answers the in operator, which the streams of WebKit use.
| Constructor | Methods |
|---|---|
ReadableStream | start, pull, cancel |
WritableStream | start, write, close, abort |
TransformStream | start, transform, flush, cancel |
Other callback APIs
Each function argument of these methods gets the context of the method use.
| API | Methods |
|---|---|
Geolocation | getCurrentPosition, watchPosition |
LockManager | request |
HTMLCanvasElement | toBlob |
DataTransferItem | getAsString |
FileSystemDirectoryReader, FileSystemFileEntry, FileSystemDirectoryEntry, FileSystemEntry | readEntries, file, getFile, getDirectory, getParent, getMetadata |
BaseAudioContext | decodeAudioData |
MediaSession | setActionHandler |
RTCPeerConnection | The old callback forms of createOffer, createAnswer, setLocalDescription, setRemoteDescription, addIceCandidate and getStats |
Notification | requestPermission |
Array | fromAsync, only for the map function. The items and thisArg arguments do not change. |
Document, Element | startViewTransition, also with an object that has an update callback |
NavigateEvent | intercept, for the handler and precommitHandler callbacks |
For startViewTransition and intercept, the browser gets a Proxy of the options object, as for the streams.
The wrappers
Each patch gives a wrapper to the native API in place of your callback. The wrapper sets the context, starts your callback, and sets the previous context again. Each wrapper has the name asyncBrowserContextWrapper. Stack traces, profilers and the Long Animation Frames API show this name between the browser and your callback.
The wrapper also starts the methods of the callback hooks. A hook gets your callback and the source of the callback. For a patched API, the source is the name of the API, for example "setTimeout", "Promise.then", "addEventListener:click", "onclick", "MutationObserver" or "ReadableStream.pull".
APIs that the runtime does not patch
The library does not patch other callback APIs. For a callback of such an API, use AsyncContext.Snapshot.wrap(callback) or AsyncLocalStorage.bind(callback). The boundaries page tells more.