opshot 0.5.0 → 0.5.1

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.
package/README.md CHANGED
@@ -206,6 +206,8 @@ const Chart = () => {
206
206
  };
207
207
  ```
208
208
 
209
+ Separate from that callback, the `flush(state)` export ends the window from outside.
210
+
209
211
  ## Batches
210
212
 
211
213
  `batch` runs a callback and tags every write inside it with your `meta`, so a listener can tell its own writes from everyone else's.
package/dist/index.d.ts CHANGED
@@ -250,6 +250,13 @@ declare function subscribe(state: object, listener: StateListener): () => void;
250
250
  */
251
251
  declare function batch(callback: () => void, meta?: unknown): void;
252
252
 
253
+ /**
254
+ * Ends the state's window now, delivering the operations gathered so far.
255
+ *
256
+ * @param state - State to flush.
257
+ */
258
+ declare function flush(state: object): void;
259
+
253
260
  /**
254
261
  * Wraps a component so it re-renders only when fields it read change.
255
262
  *
@@ -269,4 +276,4 @@ declare function scope<P extends object>(Component: ComponentType<P>): FC<P>;
269
276
  */
270
277
  declare function useMutableState<T extends object>(properties: (() => T) | T, options?: MutableStateOptions): T;
271
278
 
272
- export { type EmissionScheduler, type MutableStateOptions, type Operation, type StateListener, TrackedDate, TrackedMap, TrackedSet, batch, createMutableState, identify, ignore, isSameIdentity, isState, scope, subscribe, unsafeTrack, useMutableState };
279
+ export { type EmissionScheduler, type MutableStateOptions, type Operation, type StateListener, TrackedDate, TrackedMap, TrackedSet, batch, createMutableState, flush, identify, ignore, isSameIdentity, isState, scope, subscribe, unsafeTrack, useMutableState };
package/dist/index.js CHANGED
@@ -289,6 +289,7 @@ function batch(callback, meta) {
289
289
 
290
290
  // src/emit/emitterDeliver.ts
291
291
  var runDelivery = (pending, failures) => {
292
+ pending.handle.lastDirty = pending.dirty;
292
293
  for (const deliver of pending.deliveries) {
293
294
  try {
294
295
  deliver(pending.operations);
@@ -305,9 +306,11 @@ var raiseFailures = (failures) => {
305
306
  var queuedDeliveries = [];
306
307
  var deliveryFailures = [];
307
308
  var isDraining = false;
308
- var prepareDelivery = (handle, operations) => ({
309
+ var prepareDelivery = (handle, operations, dirty) => ({
309
310
  deliveries: [...handle.subscribers.values()],
310
- operations
311
+ operations,
312
+ handle,
313
+ dirty
311
314
  });
312
315
  var enqueueDelivery = (pending) => {
313
316
  queuedDeliveries.push(pending);
@@ -345,19 +348,21 @@ function recordOperation(handle, raw, pending) {
345
348
  }
346
349
  handle.pending.push(pending);
347
350
  byKey.set(pending.key, handle.pending.length - 1);
348
- if (handle.isFlushScheduled) return;
349
- handle.isFlushScheduled = true;
351
+ if (handle.scheduledFlush !== void 0) return;
350
352
  const run = () => {
351
- flush(handle);
353
+ if (handle.scheduledFlush !== run) return;
354
+ flushWindow(handle);
352
355
  };
356
+ handle.scheduledFlush = run;
353
357
  void Promise.resolve().then(() => {
358
+ if (handle.scheduledFlush !== run) return;
354
359
  const emitOn = handle.emitOn;
355
360
  if (emitOn === void 0) run();
356
361
  else emitOn(run);
357
362
  });
358
363
  }
359
- function flush(handle) {
360
- handle.isFlushScheduled = false;
364
+ function flushWindow(handle) {
365
+ handle.scheduledFlush = void 0;
361
366
  const pending = handle.pending.splice(0);
362
367
  handle.pendingIndex.clear();
363
368
  const operations = new Array();
@@ -372,6 +377,7 @@ function flush(handle) {
372
377
  };
373
378
  operations.push(Object.freeze(operation));
374
379
  }
380
+ if (operations.length === 0) return;
375
381
  const edges = /* @__PURE__ */ new Map();
376
382
  const nodes = /* @__PURE__ */ new Set();
377
383
  for (const operation of operations) {
@@ -385,9 +391,7 @@ function flush(handle) {
385
391
  nodes.add(raw);
386
392
  }
387
393
  const dirty = { edges, nodes };
388
- handle.lastDirty = dirty;
389
- if (operations.length === 0) return;
390
- enqueueDelivery(prepareDelivery(handle, operations));
394
+ enqueueDelivery(prepareDelivery(handle, operations, dirty));
391
395
  drainDeliveries();
392
396
  }
393
397
 
@@ -601,8 +605,7 @@ function createMutableState(properties, options) {
601
605
  emitOn: options?.emitOn,
602
606
  subscribers: /* @__PURE__ */ new Map(),
603
607
  pending: [],
604
- pendingIndex: /* @__PURE__ */ new Map(),
605
- isFlushScheduled: false
608
+ pendingIndex: /* @__PURE__ */ new Map()
606
609
  };
607
610
  const proxy = proxyOf(root);
608
611
  attachRoot(handle, root, exempt);
@@ -1172,6 +1175,11 @@ function subscribe(state, listener) {
1172
1175
  return addStateListener(state, listener, listener);
1173
1176
  }
1174
1177
 
1178
+ // src/flush.ts
1179
+ function flush(state) {
1180
+ flushWindow(requireHandle(state, "opshot: flush requires a state"));
1181
+ }
1182
+
1175
1183
  // src/react/propWalk.ts
1176
1184
  var stateKeysByContainer = /* @__PURE__ */ new WeakMap();
1177
1185
  var noStateKeys = /* @__PURE__ */ new Set();
@@ -1678,4 +1686,4 @@ function useMutableState(properties, options) {
1678
1686
  return readProxy;
1679
1687
  }
1680
1688
 
1681
- export { TrackedDate, TrackedMap, TrackedSet, batch, createMutableState, identify, ignore, isSameIdentity, isState, scope, subscribe, unsafeTrack, useMutableState };
1689
+ export { TrackedDate, TrackedMap, TrackedSet, batch, createMutableState, flush, identify, ignore, isSameIdentity, isState, scope, subscribe, unsafeTrack, useMutableState };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opshot",
3
- "version": "0.5.0",
3
+ "version": "0.5.1",
4
4
  "description": "Plain-object state for React: mutate it directly, re-render only the components that read what changed, and track every change operation",
5
5
  "type": "module",
6
6
  "exports": {