@@ -2325,6 +2325,38 @@ See also:
|
2325 | 2325 | |
2326 | 2326 | * [Command–query separation on Wikipedia](https://en.wikipedia.org/wiki/Command–query_separation) |
2327 | 2327 | |
| 2328 | +<h3 id="prefer-existing-task-sources">Prefer existing task sources</h3> |
| 2329 | + |
| 2330 | +Use existing [=task sources=] for dispatching work where possible. |
| 2331 | + |
| 2332 | +Only create a new [=task source=] when there is a clear reason to do so. |
| 2333 | +Specs use [=task sources=] to group related work (or "[=tasks=]") so UAs can schedule them sensibly. |
| 2334 | +A task is queued on a task source, |
| 2335 | +and each task source is associated with a task queue for an [=/event loop=]. |
| 2336 | +Order is guaranteed within a source, while the UA chooses which queue to service next. |
| 2337 | + |
| 2338 | +Use the generic or preexisting task sources whenever possible. |
| 2339 | +HTML defines [[html#generic-task-sources|generic task sources]] meant for broad reuse across specs. |
| 2340 | +This includes, but is not limited to: |
| 2341 | +the [=DOM manipulation task source=], |
| 2342 | +the [=user interaction task source=], |
| 2343 | +and the [=networking task source=]. |
| 2344 | +If your feature matches one of these categories, |
| 2345 | +queue on that source rather than creating a new one. |
| 2346 | +Doing so improves interoperability, predictability, and performance scheduling across the platform. |
| 2347 | + |
| 2348 | +Adding a task source has performance and complexity costs |
| 2349 | +that all browser implementations pay. |
| 2350 | +Only mint a new task source when you have a concrete, testable need. |
| 2351 | +For example, when the feature's timing/execution model must be isolated |
| 2352 | +from the generic task sources |
| 2353 | +(e.g., media elements get their own media element event task source to align with decoder/timeline semantics). |
| 2354 | + |
| 2355 | +When writing a spec, prefer HTML's wrapper algorithms |
| 2356 | +([=queue a global task=], [=queue an element task=]) |
| 2357 | +and avoid relying on a particular [=/event loop=]. |
| 2358 | +And only be explicit about the event loop when appropriate (see also [[#event-design]]). |
| 2359 | + |
2328 | 2360 | <h2 id="event-design">Event Design</h2> |
2329 | 2361 | |
2330 | 2362 | <h3 id="one-time-events">Use promises for one time events</h3> |
|