@waffle-os/runtime 0.0.153 → 0.0.155

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. package/browser/net/helper.ts +114 -19
  2. package/browser/net/ring.ts +18 -6
  3. package/browser/net/runtime.ts +2 -2
  4. package/browser/notification-relay.ts +105 -18
  5. package/browser/notification-wait.ts +45 -4
  6. package/browser/notification-worker.ts +169 -76
  7. package/browser/preview.ts +15 -2
  8. package/core/filesystem.ts +18 -0
  9. package/core/wasi.ts +75 -4
  10. package/dist/browser/net/helper.js +83 -18
  11. package/dist/browser/net/ring.js +6 -5
  12. package/dist/browser/net/runtime.js +2 -2
  13. package/dist/browser/notification-relay.js +78 -15
  14. package/dist/browser/notification-wait.js +37 -5
  15. package/dist/browser/notification-worker.js +146 -67
  16. package/dist/core/wasi.js +62 -4
  17. package/dist/fs/backend.js +7 -0
  18. package/dist/fs/client.js +75 -17
  19. package/dist/fs/dirty-admission.js +15 -8
  20. package/dist/fs/guest-watch.js +267 -94
  21. package/dist/fs/indexeddb-chunks.js +88 -11
  22. package/dist/fs/indexeddb.js +366 -110
  23. package/dist/fs/memory.js +106 -20
  24. package/dist/fs/multitab-backend.js +291 -74
  25. package/dist/fs/multitab-protocol.js +1 -0
  26. package/dist/fs/persistent.js +70 -16
  27. package/dist/fs/protocol.js +15 -7
  28. package/dist/fs/publication-attachment.js +9 -7
  29. package/dist/fs/service.js +62 -9
  30. package/dist/fs/shared.js +100 -6
  31. package/dist/fs/storage.js +107 -8
  32. package/dist/fs/watch-service.js +97 -2
  33. package/dist/fs/watch-subscriptions.js +121 -8
  34. package/dist/proc/command-stdio.js +1 -1
  35. package/dist/proc/intrinsics/core.js +5 -0
  36. package/dist/proc/intrinsics.js +1 -0
  37. package/dist/tooling/program/program-transformer.json +1 -1
  38. package/fs/backend.ts +17 -0
  39. package/fs/client.ts +99 -16
  40. package/fs/dirty-admission.ts +22 -7
  41. package/fs/guest-watch.ts +299 -103
  42. package/fs/indexeddb-chunks.ts +103 -9
  43. package/fs/indexeddb.ts +477 -121
  44. package/fs/memory.ts +120 -19
  45. package/fs/multitab-backend.ts +440 -95
  46. package/fs/multitab-protocol.ts +1 -0
  47. package/fs/persistent.ts +88 -16
  48. package/fs/protocol.ts +26 -7
  49. package/fs/publication-attachment.ts +9 -7
  50. package/fs/service.ts +62 -7
  51. package/fs/shared.ts +110 -8
  52. package/fs/storage.ts +128 -10
  53. package/fs/watch-service.ts +110 -3
  54. package/fs/watch-subscriptions.ts +115 -7
  55. package/package.json +1 -1
  56. package/proc/command-stdio.ts +1 -1
  57. package/proc/intrinsics/core.ts +7 -0
  58. package/proc/intrinsics.ts +1 -0
  59. package/testing/idle-workspace-cancellation-browser.ts +22 -13
  60. package/testing/indexeddb-storage-browser.ts +564 -1
  61. package/tooling/program/program-transformer.json +1 -1
package/dist/fs/client.js CHANGED
@@ -87,6 +87,8 @@ import {
87
87
  reapFilesystem,
88
88
  unpack,
89
89
  wake,
90
+ wakeTransport,
91
+ SIGNAL_TRANSPORT,
90
92
  releaseFilesystemReservation
91
93
  } from "./protocol.js";
92
94
  import {
@@ -141,6 +143,18 @@ function transferList(payload) {
141
143
  return { __proto__: null, [symbolIterator]: () => iterator };
142
144
  }
143
145
  let waitForObservedResolve;
146
+ const PLAIN_REPLIES = new SetConstructor([
147
+ "stat",
148
+ "create",
149
+ "write",
150
+ "append",
151
+ "grow",
152
+ "resize",
153
+ "setTimes",
154
+ "setMode",
155
+ "read",
156
+ "readlink"
157
+ ]);
144
158
 
145
159
  export class FilesystemCompletion {
146
160
  id;
@@ -158,6 +172,7 @@ export class FilesystemCompletion {
158
172
  loading;
159
173
  consumed = false;
160
174
  released = false;
175
+ retained = true;
161
176
  constructor(id, buffer, client, publication) {
162
177
  this.id = id;
163
178
  this.buffer = buffer;
@@ -220,7 +235,8 @@ export class FilesystemCompletion {
220
235
  if (!this.consumed)
221
236
  this.cancel();
222
237
  else {
223
- this.client.connection.port.postMessage({ discard: this.id });
238
+ if (this.retained || atomicsLoad(this.cells, 0) !== State.Complete)
239
+ this.client.connection.port.postMessage({ discard: this.id });
224
240
  this.notify();
225
241
  }
226
242
  mapDelete(this.client.completions, this.id);
@@ -235,6 +251,7 @@ export class FilesystemCompletion {
235
251
  }
236
252
  arrayAppend(this.backendWaiters, callback);
237
253
  this.client.watchRevocation();
254
+ this.client.watchBackendSettlement();
238
255
  if (this.backendSettled)
239
256
  this.notify();
240
257
  }
@@ -412,7 +429,7 @@ function preparationFailed(completion) {
412
429
  const state = completion?.cells[0];
413
430
  return state === State.Failed || state === State.Cancelled;
414
431
  }
415
- function clientWatches(cells, revoked, remote, call) {
432
+ function clientWatches(cells, revoked, remote, canDrain, call) {
416
433
  return {
417
434
  get cells() {
418
435
  return cells();
@@ -423,8 +440,12 @@ function clientWatches(cells, revoked, remote, call) {
423
440
  get remote() {
424
441
  return remote();
425
442
  },
443
+ get canDrain() {
444
+ return canDrain();
445
+ },
426
446
  open: (handle, recursive) => call("watchOpen", [handle, recursive]),
427
447
  read: (id) => call("watchRead", [id]),
448
+ drain: () => call("watchDrain", []),
428
449
  close: (id) => {
429
450
  call("watchClose", [id]);
430
451
  }
@@ -473,6 +494,7 @@ export class FilesystemClient {
473
494
  capabilities;
474
495
  epoch;
475
496
  signal;
497
+ transportSignal;
476
498
  revoked;
477
499
  section;
478
500
  shared;
@@ -489,14 +511,15 @@ export class FilesystemClient {
489
511
  this.requests = new FilesystemRequestAllocator(connection.requests);
490
512
  this.capabilities = new Int32ArrayConstructor(connection.capabilities, 0, CAPABILITY_LENGTH);
491
513
  this.epoch = new Int32ArrayConstructor(connection.epoch);
492
- this.signal = new Int32ArrayConstructor(connection.signal);
514
+ this.signal = new Int32ArrayConstructor(connection.signal, 0, 1);
515
+ this.transportSignal = new Int32ArrayConstructor(connection.signal, SIGNAL_TRANSPORT * 4, 1);
493
516
  const revoked = sharedRevoked(connection);
494
517
  this.revoked = () => this.disposed || revoked() || !!this.transportError();
495
518
  if (connection.residentPublication)
496
519
  this.resident = new ResidentAttachment(connection.residentPublication, this.revoked);
497
520
  this.section = new FilesystemTransportSection(connection, this.requests.memory, this.revoked);
498
521
  this.shared = new SharedFilesystemState(connection.shared, connection.maxBytes, connection.owner, connection.segments, this.section, connection.publication?.buffers);
499
- this.watches = clientWatches(() => this.shared.watch.cells, this.revoked, () => this.shared.isDirectDisabled(), (operation, args) => this.call(operation, args));
522
+ this.watches = clientWatches(() => this.shared.watch.cells, this.revoked, () => this.shared.isDirectDisabled(), () => !this.shared.isWatchDrainDisabled(), (operation, args) => this.call(operation, args));
500
523
  connection.port.onmessage = ({ data }) => {
501
524
  if (data.segment)
502
525
  this.attachSegment(data.segment, data.buffer);
@@ -505,7 +528,7 @@ export class FilesystemClient {
505
528
  };
506
529
  connection.port.onmessageerror = () => {
507
530
  closeRequestMemory(this.requests.memory);
508
- wake(connection);
531
+ wakeTransport(connection);
509
532
  mapForEach(this.completions, (completion) => completion.notify());
510
533
  connection.port.postMessage({ transportClosed: true });
511
534
  connection.port.close();
@@ -639,26 +662,49 @@ export class FilesystemClient {
639
662
  this.watchingRevocation = true;
640
663
  this.monitorRevocation();
641
664
  }
665
+ watchBackendSettlement() {
666
+ this.backendWaiterVersion++;
667
+ if (this.revocationWord === this.transportSignal)
668
+ atomicsNotify(this.transportSignal, 0);
669
+ }
670
+ revocationWord;
671
+ backendWaiterVersion = 0;
672
+ unsettledBackendWaiter() {
673
+ let found = false;
674
+ mapForEach(this.completions, (completion) => {
675
+ if (completion.hasBackendWaiters && !completion.backendSettled)
676
+ found = true;
677
+ });
678
+ return found;
679
+ }
680
+ revocationSignal() {
681
+ return this.connection.publication || this.unsettledBackendWaiter() ? this.signal : this.transportSignal;
682
+ }
642
683
  monitorRevocation() {
643
- const signal = this.signal;
644
684
  const stop = () => {
685
+ this.revocationWord = undefined;
645
686
  this.watchingRevocation = false;
646
687
  };
647
688
  const step = () => {
689
+ this.revocationWord = undefined;
648
690
  try {
649
691
  while (mapSize(this.completions)) {
692
+ const version = this.backendWaiterVersion;
693
+ const signal = this.revocationSignal();
650
694
  const observed = atomicsLoad(signal, 0);
651
- if (this.revoked()) {
652
- if (!this.sweepRevokedCompletions())
653
- break;
654
- } else {
695
+ let waiting = true;
696
+ if (this.revoked())
697
+ waiting = this.sweepRevokedCompletions();
698
+ else
655
699
  this.notifySettledBackendWaiters();
656
- }
657
- if (!mapSize(this.completions))
700
+ if (version !== this.backendWaiterVersion)
701
+ continue;
702
+ if (!waiting || !mapSize(this.completions))
658
703
  break;
659
- const waiting = atomicsWaitAsync(signal, 0, observed);
660
- if (waiting.async) {
661
- afterNativePromise(waiting.value, step, stop);
704
+ const parked = atomicsWaitAsync(signal, 0, observed);
705
+ if (parked.async) {
706
+ this.revocationWord = signal;
707
+ afterNativePromise(parked.value, step, stop);
662
708
  return;
663
709
  }
664
710
  }
@@ -805,14 +851,19 @@ export class FilesystemClient {
805
851
  throw new FilesystemError(6);
806
852
  }
807
853
  submitPrepared(operation, args, data, capacity, publication) {
808
- const input = pack({ operation, args, publication });
854
+ const input = pack(publication ? { operation, args, publication } : { operation, args });
809
855
  this.section.enter();
810
856
  try {
811
- return this.postRequest(input, data, capacity, publication);
857
+ const completion = this.postRequest(input, data, capacity, publication);
858
+ completion.retained = this.retainsReply(operation);
859
+ return completion;
812
860
  } finally {
813
861
  this.section.exit();
814
862
  }
815
863
  }
864
+ retainsReply(operation) {
865
+ return !setHas(PLAIN_REPLIES, operation) || this.connection.residentPublication !== undefined || this.shared.publication !== undefined;
866
+ }
816
867
  postRequest(input, data, capacity, publication) {
817
868
  const inputLength = typedArrayLength(input);
818
869
  const total = inputLength + typedArrayLength(data);
@@ -2047,6 +2098,13 @@ export class FilesystemClient {
2047
2098
  return errno(error);
2048
2099
  }
2049
2100
  }
2101
+ realpath(directory, path, rooted = false) {
2102
+ try {
2103
+ return this.call("realpath", [directory, path, rooted]);
2104
+ } catch (error) {
2105
+ return errno(error);
2106
+ }
2107
+ }
2050
2108
  readlink(handle) {
2051
2109
  this.retain(handle);
2052
2110
  try {
@@ -127,15 +127,22 @@ export class DirtyAdmission {
127
127
  get revision() {
128
128
  return this.values[REVISION];
129
129
  }
130
- clean(revision, namespace) {
131
- if (!this.enabled || this.revision !== revision)
132
- return false;
133
- this.values[DIRTY_BYTES] = 0;
134
- this.values[DIRTY_METADATA] = 0;
130
+ releaseSlot(bytes, metadata) {
131
+ if (!this.enabled || !bytes && !metadata)
132
+ return;
133
+ this.values[DIRTY_BYTES] = Math.max(0, this.values[DIRTY_BYTES] - bytes);
134
+ this.values[DIRTY_METADATA] = Math.max(0, this.values[DIRTY_METADATA] - metadata);
135
+ this.values[REVISION]++;
136
+ this.notify();
137
+ }
138
+ reconcile(namespace, dirtyNamespace, dirtyBytes, dirtyMetadata) {
139
+ if (!this.enabled)
140
+ return;
141
+ this.values[DIRTY_BYTES] = dirtyBytes;
142
+ this.values[DIRTY_METADATA] = dirtyMetadata;
135
143
  this.values[NAMESPACE] = namespace;
136
- this.values[BASE_NAMESPACE] = namespace;
144
+ this.values[BASE_NAMESPACE] = namespace - dirtyNamespace;
137
145
  this.notify();
138
- return true;
139
146
  }
140
147
  notify() {
141
148
  Atomics.add(this.signals, 0, 1);
@@ -162,7 +169,7 @@ export class DirtyAdmission {
162
169
  }
163
170
  }
164
171
  get hasDebt() {
165
- return this.values[DIRTY_BYTES] > 0 || this.values[DIRTY_METADATA] > 0;
172
+ return this.values[DIRTY_BYTES] > 0 || this.values[DIRTY_METADATA] > 0 || this.values[NAMESPACE] > this.values[BASE_NAMESPACE];
166
173
  }
167
174
  capacityReleased() {
168
175
  if (this.enabled)
@@ -1,9 +1,12 @@
1
- import { FilesystemError } from "../core/filesystem.js";
1
+ import {
2
+ FilesystemError
3
+ } from "../core/filesystem.js";
2
4
  import {
3
5
  arrayAppend,
4
6
  atomicsLoad,
5
7
  MapConstructor,
6
8
  mapDelete,
9
+ mapFirst,
7
10
  mapForEach,
8
11
  mapGet,
9
12
  mapSet,
@@ -12,6 +15,7 @@ import {
12
15
  const EAGAIN = 6;
13
16
  const EBADF = 8;
14
17
  const ECANCELED = 11;
18
+ const ENOTSUP = 58;
15
19
  const CHANGE = 1;
16
20
  const RENAME = 2;
17
21
  const OVERFLOW = 3;
@@ -22,147 +26,316 @@ const END = { kind: "records", records: [], length: 0 };
22
26
  function failure(errno) {
23
27
  return { kind: "error", errno };
24
28
  }
29
+ function watchError(id, errno) {
30
+ return { kind: "watch_error", id, errno };
31
+ }
32
+ function nextOf(id, result) {
33
+ return result.kind === "error" ? watchError(id, result.errno) : { ...result, id };
34
+ }
35
+ function rotate(map, watch) {
36
+ mapDelete(map, watch.id);
37
+ mapSet(map, watch.id, watch);
38
+ }
25
39
 
26
40
  export class GuestWatchTable {
27
- fs;
28
- watches = new MapConstructor;
29
- nextId = 1;
30
- revocationNotified = false;
41
+ #fs;
42
+ #watches;
43
+ #byService;
44
+ #buffered;
45
+ #errored;
46
+ #ended;
47
+ #nextId = 1;
48
+ #revocationNotified = false;
49
+ #activeCount = 0;
50
+ #observedGen = 0;
51
+ #observedCount = 0;
52
+ #tableObserved;
53
+ #drainMore = false;
54
+ #noDrain = false;
55
+ #flip = false;
31
56
  constructor(fs) {
32
- this.fs = fs;
57
+ this.#fs = fs;
58
+ this.drop();
33
59
  }
34
60
  get word() {
35
- return this.fs.cells;
61
+ return this.#fs.cells;
36
62
  }
37
63
  get active() {
38
- let active = false;
39
- mapForEach(this.watches, (watch) => {
40
- if (!watch.ended)
41
- active = true;
42
- });
43
- return active;
64
+ return this.#activeCount > 0;
65
+ }
66
+ #useDrain() {
67
+ if (typeof this.#fs.drain !== "function" || this.#noDrain)
68
+ return false;
69
+ if (this.#fs.canDrain === false) {
70
+ this.#tableObserved = undefined;
71
+ this.#drainMore = false;
72
+ return false;
73
+ }
74
+ return true;
75
+ }
76
+ #allObserved(generation) {
77
+ return this.#observedCount === this.#activeCount && this.#observedGen === generation || this.#useDrain() && !this.#drainMore && this.#tableObserved === generation;
44
78
  }
45
79
  pending() {
46
- if (this.fs.revoked)
47
- return !!this.fs.remote && !this.revocationNotified && this.active;
48
- return this.hasPending(false);
80
+ if (this.#fs.revoked)
81
+ return !!this.#fs.remote && !this.#revocationNotified && this.#activeCount > 0;
82
+ return this.#activeCount > 0 && (mapSize(this.#buffered) > 0 || mapSize(this.#errored) > 0 || !this.#allObserved(atomicsLoad(this.#fs.cells, 3)));
49
83
  }
50
84
  ready() {
51
- if (!mapSize(this.watches))
85
+ if (!mapSize(this.#watches))
52
86
  return false;
53
- return this.fs.revoked || this.hasPending(true);
54
- }
55
- hasPending(includeEnded) {
56
- const generation = atomicsLoad(this.fs.cells, 3);
57
- let pending = false;
58
- mapForEach(this.watches, (watch) => {
59
- if (watch.ended ? includeEnded : watch.head < watch.queue.length || watch.observed !== generation)
60
- pending = true;
61
- });
62
- return pending;
87
+ return this.#fs.revoked || mapSize(this.#buffered) > 0 || mapSize(this.#errored) > 0 || mapSize(this.#ended) > 0 || this.#activeCount > 0 && !this.#allObserved(atomicsLoad(this.#fs.cells, 3));
88
+ }
89
+ #setObserved(watch, generation) {
90
+ if (watch.observed === generation)
91
+ return;
92
+ if (!watch.ended) {
93
+ if (watch.observed === this.#observedGen)
94
+ this.#observedCount--;
95
+ if (generation !== this.#observedGen) {
96
+ this.#observedGen = generation;
97
+ this.#observedCount = 1;
98
+ } else
99
+ this.#observedCount++;
100
+ }
101
+ watch.observed = generation;
102
+ }
103
+ #markEnded(watch) {
104
+ if (watch.ended)
105
+ return;
106
+ watch.ended = true;
107
+ this.#activeCount--;
108
+ if (watch.observed === this.#observedGen)
109
+ this.#observedCount--;
110
+ watch.errno = 0;
111
+ mapDelete(this.#buffered, watch.id);
112
+ mapDelete(this.#errored, watch.id);
113
+ mapSet(this.#ended, watch.id, watch);
63
114
  }
64
115
  open(handle, recursive) {
65
- if (this.nextId > LAST_ID)
116
+ if (this.#nextId > LAST_ID)
66
117
  throw new FilesystemError(EAGAIN, "watch ids exhausted");
67
- const observed = atomicsLoad(this.fs.cells, 3);
68
- const service = this.fs.open(handle, recursive);
69
- const id = this.nextId++;
70
- mapSet(this.watches, id, { service, observed, queue: [], head: 0, ended: false });
118
+ const observed = atomicsLoad(this.#fs.cells, 3);
119
+ const service = this.#fs.open(handle, recursive);
120
+ const id = this.#nextId++;
121
+ const watch = { id, service, observed: NaN, queue: [], head: 0, ended: false, errno: 0 };
122
+ mapSet(this.#watches, id, watch);
123
+ mapSet(this.#byService, service, watch);
124
+ this.#activeCount++;
125
+ this.#setObserved(watch, observed);
71
126
  return id;
72
127
  }
73
128
  read(id, capacity) {
74
- const watch = mapGet(this.watches, id);
129
+ const watch = mapGet(this.#watches, id);
75
130
  if (!watch)
76
131
  return failure(EBADF);
77
132
  if (watch.ended)
78
133
  return END;
79
- if (watch.head === watch.queue.length) {
80
- const refused = this.refill(watch);
81
- if (refused)
82
- return refused;
134
+ if (watch.head < watch.queue.length)
135
+ return this.#takeFrom(watch, capacity);
136
+ const force = watch.errno !== 0;
137
+ if (force) {
138
+ watch.errno = 0;
139
+ mapDelete(this.#errored, id);
140
+ }
141
+ return this.#refillSingle(watch, force) ?? this.#takeFrom(watch, capacity);
142
+ }
143
+ readNext(capacity) {
144
+ if (!mapSize(this.#watches))
145
+ return failure(EAGAIN);
146
+ if (this.#fs.revoked) {
147
+ if (this.#fs.remote)
148
+ this.#revocationNotified = true;
149
+ const first = mapFirst(this.#watches);
150
+ rotate(this.#watches, first);
151
+ return watchError(first.id, ECANCELED);
152
+ }
153
+ if (this.#useDrain() && (mapSize(this.#buffered) || mapSize(this.#errored) || !this.#drainService()))
154
+ return this.#takeNextReady(capacity);
155
+ return this.#readNextFallback(capacity);
156
+ }
157
+ #takeNextReady(capacity) {
158
+ const buffered = mapFirst(this.#buffered);
159
+ if (buffered) {
160
+ const read = this.#takeFrom(buffered, capacity);
161
+ if (buffered.head < buffered.queue.length)
162
+ rotate(this.#buffered, buffered);
163
+ return nextOf(buffered.id, read);
83
164
  }
84
- return take(watch, capacity);
165
+ const ended = mapFirst(this.#ended);
166
+ const errored = mapFirst(this.#errored);
167
+ const pick = this.#flip ? ended ?? errored : errored ?? ended;
168
+ if (!pick)
169
+ return failure(EAGAIN);
170
+ this.#flip = !pick.ended;
171
+ if (pick.ended) {
172
+ rotate(this.#ended, pick);
173
+ return nextOf(pick.id, END);
174
+ }
175
+ const errno = pick.errno;
176
+ pick.errno = 0;
177
+ if (pick.observed === this.#observedGen)
178
+ this.#observedCount--;
179
+ pick.observed = NaN;
180
+ this.#drainMore = true;
181
+ mapDelete(this.#errored, pick.id);
182
+ return watchError(pick.id, errno);
183
+ }
184
+ #readNextFallback(capacity) {
185
+ let result;
186
+ let reported;
187
+ mapForEach(this.#watches, (watch) => {
188
+ if (result)
189
+ return;
190
+ try {
191
+ const read = this.read(watch.id, capacity);
192
+ if (read.kind !== "error" || read.errno !== EAGAIN) {
193
+ result = nextOf(watch.id, read);
194
+ reported = watch;
195
+ }
196
+ } catch (error) {
197
+ if (!(error instanceof FilesystemError))
198
+ throw error;
199
+ result = watchError(watch.id, error.errno);
200
+ reported = watch;
201
+ }
202
+ });
203
+ if (reported)
204
+ rotate(this.#watches, reported);
205
+ return result ?? failure(EAGAIN);
85
206
  }
86
207
  close(id) {
87
- const watch = mapGet(this.watches, id);
208
+ const watch = mapGet(this.#watches, id);
88
209
  if (!watch)
89
210
  return EBADF;
90
- if (!this.fs.revoked)
91
- this.closeService(watch.service);
92
- mapDelete(this.watches, id);
93
- return 0;
94
- }
95
- closeService(service) {
96
- try {
97
- this.fs.close(service);
98
- } catch (error) {
99
- if (!(error instanceof FilesystemError) || error.errno !== ECANCELED)
100
- throw error;
211
+ if (!this.#fs.revoked) {
212
+ try {
213
+ this.#fs.close(watch.service);
214
+ } catch (error) {
215
+ if (!(error instanceof FilesystemError) || error.errno !== ECANCELED)
216
+ throw error;
217
+ }
218
+ }
219
+ if (!watch.ended) {
220
+ this.#activeCount--;
221
+ if (watch.observed === this.#observedGen)
222
+ this.#observedCount--;
101
223
  }
224
+ mapDelete(this.#buffered, id);
225
+ mapDelete(this.#errored, id);
226
+ mapDelete(this.#ended, id);
227
+ mapDelete(this.#watches, id);
228
+ mapDelete(this.#byService, watch.service);
229
+ if (!mapSize(this.#watches)) {
230
+ this.#tableObserved = undefined;
231
+ this.#drainMore = false;
232
+ }
233
+ return 0;
102
234
  }
103
235
  drop() {
104
- this.watches = new MapConstructor;
236
+ this.#watches = new MapConstructor;
237
+ this.#byService = new MapConstructor;
238
+ this.#buffered = new MapConstructor;
239
+ this.#errored = new MapConstructor;
240
+ this.#ended = new MapConstructor;
241
+ this.#activeCount = this.#observedCount = 0;
242
+ this.#tableObserved = undefined;
243
+ this.#drainMore = false;
105
244
  }
106
- refill(watch) {
107
- if (this.fs.revoked) {
108
- if (this.fs.remote)
109
- this.revocationNotified = true;
110
- return failure(ECANCELED);
245
+ #applyBatch(watch, batch, generation) {
246
+ if (batch.kind === "closed") {
247
+ this.#markEnded(watch);
248
+ return true;
111
249
  }
112
- const generation = atomicsLoad(this.fs.cells, 3);
113
- if (!this.fs.remote && generation === watch.observed)
114
- return failure(EAGAIN);
115
- const previous = watch.observed;
116
- watch.observed = generation;
117
- let batch;
250
+ this.#setObserved(watch, generation);
251
+ watch.queue = encodeBatch(batch);
252
+ watch.head = 0;
253
+ if (watch.queue.length)
254
+ mapSet(this.#buffered, watch.id, watch);
255
+ return false;
256
+ }
257
+ #drainService() {
258
+ const generation = atomicsLoad(this.#fs.cells, 3);
259
+ if (!this.#drainMore && this.#allObserved(generation))
260
+ return false;
261
+ const previous = this.#tableObserved;
262
+ this.#tableObserved = generation;
263
+ let drained;
118
264
  try {
119
- batch = this.fs.read(watch.service);
265
+ drained = this.#fs.drain();
120
266
  } catch (error) {
121
- watch.observed = previous;
267
+ if (error instanceof FilesystemError && error.errno === ENOTSUP) {
268
+ this.#tableObserved = undefined;
269
+ this.#drainMore = false;
270
+ if (this.#fs.canDrain === undefined)
271
+ this.#noDrain = true;
272
+ return true;
273
+ }
274
+ this.#tableObserved = previous;
122
275
  throw error;
123
276
  }
124
- if (batch.kind === "closed") {
125
- watch.ended = true;
126
- return END;
277
+ this.#drainMore = drained.more;
278
+ for (let index = 0;index < drained.entries.length; index++) {
279
+ const entry = drained.entries[index];
280
+ const watch = mapGet(this.#byService, entry.id);
281
+ if (!watch || watch.ended)
282
+ continue;
283
+ if ("errno" in entry) {
284
+ watch.errno = entry.errno;
285
+ mapSet(this.#errored, watch.id, watch);
286
+ } else {
287
+ this.#applyBatch(watch, entry.batch, generation);
288
+ }
127
289
  }
128
- watch.queue = encodeBatch(batch);
129
- watch.head = 0;
290
+ return false;
291
+ }
292
+ #refillSingle(watch, force) {
293
+ if (this.#fs.revoked) {
294
+ if (this.#fs.remote)
295
+ this.#revocationNotified = true;
296
+ return failure(ECANCELED);
297
+ }
298
+ const generation = atomicsLoad(this.#fs.cells, 3);
299
+ if (!force && !this.#fs.remote && (generation === watch.observed || this.#useDrain() && !this.#drainMore && generation === this.#tableObserved))
300
+ return failure(EAGAIN);
301
+ if (this.#applyBatch(watch, this.#fs.read(watch.service), generation))
302
+ return END;
130
303
  if (!watch.queue.length)
131
304
  return failure(EAGAIN);
132
305
  }
133
- }
134
- function take(watch, capacity) {
135
- const records = [];
136
- let length = 0;
137
- while (watch.head < watch.queue.length) {
138
- const record = watch.queue[watch.head];
139
- if (length + record.length > capacity)
140
- break;
141
- arrayAppend(records, record);
142
- length += record.length;
143
- watch.head++;
144
- if (record[0] === OVERFLOW)
145
- watch.ended = true;
146
- }
147
- if (!length)
148
- return { kind: "short", needed: watch.queue[watch.head].length };
149
- if (watch.head === watch.queue.length) {
150
- watch.queue = [];
151
- watch.head = 0;
306
+ #takeFrom(watch, capacity) {
307
+ const records = [];
308
+ let length = 0;
309
+ while (watch.head < watch.queue.length) {
310
+ const record = watch.queue[watch.head];
311
+ if (length + record.length > capacity)
312
+ break;
313
+ arrayAppend(records, record);
314
+ length += record.length;
315
+ watch.head++;
316
+ if (record[0] === OVERFLOW)
317
+ this.#markEnded(watch);
318
+ }
319
+ if (!length)
320
+ return { kind: "short", needed: watch.queue[watch.head].length };
321
+ if (watch.head === watch.queue.length) {
322
+ watch.queue = [];
323
+ watch.head = 0;
324
+ mapDelete(this.#buffered, watch.id);
325
+ }
326
+ return { kind: "records", records, length };
152
327
  }
153
- return { kind: "records", records, length };
154
328
  }
155
329
  function encodeBatch(batch) {
156
330
  if (batch.kind === "overflow")
157
331
  return [encodeRecord(OVERFLOW, null)];
158
332
  const records = [];
159
- for (let index = 0;index < batch.events.length; index++)
160
- arrayAppend(records, encodeEvent(batch.events[index]));
333
+ for (let index = 0;index < batch.events.length; index++) {
334
+ const event = batch.events[index];
335
+ arrayAppend(records, encodeRecord(event.type === "change" ? CHANGE : RENAME, event.name));
336
+ }
161
337
  return records;
162
338
  }
163
- function encodeEvent(event) {
164
- return encodeRecord(event.type === "change" ? CHANGE : RENAME, event.name);
165
- }
166
339
  function encodeRecord(type, name) {
167
340
  const bytes = name === null ? undefined : encoder.encode(name);
168
341
  const size = bytes ? bytes.length : 0;