-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathContextWorker.njs
More file actions
64 lines (53 loc) · 1.77 KB
/
ContextWorker.njs
File metadata and controls
64 lines (53 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import Nullstack from 'nullstack'
function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms)
})
}
class ContextWorker extends Nullstack {
header = '';
static async start({ worker }) {
worker.preload = ['/context-worker']
}
static async inspectHeaders({ request }) {
return request.headers.custom
}
async hydrate({ worker }) {
worker.headers.custom = 'custom'
this.header = await this.inspectHeaders()
}
static async longServerFunction() {
await sleep(3000)
}
invokeServerFunction({ worker, id }) {
this.longServerFunction({ id })
this.didFetch = worker.fetching
}
render({ worker }) {
return (
<div data-hydrated={this.hydrated}>
<button onclick={this.invokeServerFunction}> Invoke </button>
<div data-header={this.header} />
<div data-worker={!!worker} />
<div data-enabled={worker.enabled} />
<div data-online={worker.online} />
<div data-fetching={this.didFetch} />
<div data-responsive={worker.responsive} />
<div data-preload={!!worker.preload} />
<div data-preload-path={worker.preload[0]} />
<div data-queues-length={worker.queues.longServerFunction.length} />
<div data-cdn={worker.cdn} />
<div data-queues={worker.queues.longServerFunction.map(({ id }) => id)?.join(',')} />
<button onclick={this.invokeServerFunction} id="a">
a
</button>
<button onclick={this.invokeServerFunction} id="b">
b
</button>
{worker.registration && <div data-registration={worker.registration.constructor.name} />}
{worker.installation && <div data-installation={worker.installation.constructor.name} />}
</div>
)
}
}
export default ContextWorker