Skip to content

Commit

Permalink
Specify how "terminate" works
Browse files Browse the repository at this point in the history
In conjunction with whatwg/fetch#1329.

When the SW fetch starts, it emits an `onStart` callback with a controller
instance. That controller instance can be used to terminate the service worker
fetch and it's internal fetches.
  • Loading branch information
noamr committed Nov 8, 2021
1 parent 1db9650 commit 7651857
Showing 1 changed file with 34 additions and 6 deletions.
40 changes: 34 additions & 6 deletions docs/index.bs
Original file line number Diff line number Diff line change
Expand Up @@ -2975,6 +2975,35 @@ spec: rfc7231; urlPrefix: https://tools.ietf.org/html/rfc7231
1. Set |serviceWorker|'s [=start status=] to null.
</section>

<section>
A <dfn export>service worker fetch controller</dfn> is a [=/struct=]. It has the following [=struct/items=]:

<section dfn-for="service worker fetch controller">
: <dfn>state</dfn> (default "<code>ongoing</code>")
:: "<code>ongoing</code>" or "<code>terminated</code>".

: <dfn>preload fetch controller</dfn> (default null)
:: Null or a [=fetch controller=]
</section>
</section>

<section algorithm>

The <dfn export>terminate Service-Worker Fetch</dfn> algorithm enables terminating an ongoing [=handle fetch=] process.

: Input
:: |controller|, a [=service worker fetch controller=]
: Output
:: None

1. Set |controller|'s [=service worker fetch controller/state=] to "<code>terminated</code>".
1. If |controller|'s [=service worker fetch controller/preload fetch controller=] is not null, [=fetch controller/abort=] |controller|'s [=service worker fetch controller/preload fetch controller=].
</section>

given a [=service worker fetch controller=] |controller|:

</section>

<section algorithm>
<h3 id="on-fetch-request-algorithm"><dfn export>Handle Fetch</dfn></h3>

Expand All @@ -2983,10 +3012,12 @@ spec: rfc7231; urlPrefix: https://tools.ietf.org/html/rfc7231
: Input
:: |request|, a [=/request=]
:: |useHighResPerformanceTimers|, a boolean
:: |onStarted|, an algorithm accepting a [=service worker fetch controller=]
: Output
:: |response|, a [=/response=]

1. Let |handleFetchFailed| be false.
1. Let |controller| be a new [=service worker fetch controller=].
1. Let |respondWithEntered| be false.
1. Let |eventCanceled| be false.
1. Let |response| be null.
Expand All @@ -2997,7 +3028,7 @@ spec: rfc7231; urlPrefix: https://tools.ietf.org/html/rfc7231
1. Let |workerRealm| be null.
1. Let |eventHandled| be null.
1. Let |timingInfo| be a new [=service worker timing info=].
1. Let |fetchInstance| be the instance of the [=/fetch=] algorithm representing the ongoing fetch.
1. Call |onFetchStarted| with |controller|.
1. Assert: |request|'s [=request/destination=] is not "<code>serviceworker</code>".
1. If |request|'s [=request/destination=] is either "<code>embed</code>" or "<code>object</code>", then:
1. Return null.
Expand All @@ -3023,14 +3054,11 @@ spec: rfc7231; urlPrefix: https://tools.ietf.org/html/rfc7231
1. [=header list/Append=] to |preloadRequestHeaders| a new [=header=] whose [=header/name=] is \`<code>Service-Worker-Navigation-Preload</code>\` and [=header/value=] is |registration|'s [=navigation preload header value=].
1. Set |preloadRequest|'s [=service-workers mode=] to "`none`".
1. Run the following substeps [=in parallel=]:
1. [=Fetch=] |preloadRequest| and let |preloadFetchInstance| be the instance of the [=/fetch=] algorithm.

1. [=Fetch=] |preloadRequest|, with [=fetch/processFetchStart=] given [=fetch controller=] |internalFetchController| be to set |controller|'s [=service worker fetch controller/preload fetch controller=] to |internalFetchController|.
To [=process response=] for |navigationPreloadResponse|, run these substeps:

1. If |navigationPreloadResponse|'s [=response/type=] is "`error`", reject |preloadResponse| with a `TypeError` and terminate these substeps.
1. Associate |preloadResponseObject| with |navigationPreloadResponse|.
1. Resolve |preloadResponse| with |preloadResponseObject|.
1. If |fetchInstance| is [=fetch/terminated=], then [=fetch/terminate=] |preloadFetchInstance| with the aborted flag set.
1. Else, resolve |preloadResponse| with undefined.

Note: From this point, the [=/service worker client=] starts to <a>use</a> its <a>active service worker</a>'s <a>containing service worker registration</a>.
Expand Down Expand Up @@ -3076,7 +3104,7 @@ spec: rfc7231; urlPrefix: https://tools.ietf.org/html/rfc7231
1. Else, [=ReadableStream/cancel=] |request|'s [=request/body=] with undefined.
1. If |response| is not null, then set |response|'s [=response/service worker timing info=] to |timingInfo|.
1. If |e|'s <a>canceled flag</a> is set, set |eventCanceled| to true.
1. If |fetchInstance| is [=fetch/terminated=], then [=queue a task=] to [=AbortSignal/signal abort=] on |requestObject|'s {{Request/signal}}.
1. If |controller|'s [=service worker controller/state=] is "<code>terminated</code>", then [=queue a task=] to [=AbortSignal/signal abort=] on |requestObject|'s {{Request/signal}}.

If |task| is discarded, set |handleFetchFailed| to true.

Expand Down

0 comments on commit 7651857

Please sign in to comment.