@legendapp/state 0.15.0-next.8 → 0.15.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.
Files changed (62) hide show
  1. package/CHANGELOG.md +85 -0
  2. package/README.md +8 -8
  3. package/babel.js +21 -599
  4. package/babel.js.map +1 -1
  5. package/index.d.ts +7 -2
  6. package/index.js +148 -132
  7. package/index.js.map +1 -1
  8. package/index.mjs +142 -132
  9. package/index.mjs.map +1 -1
  10. package/internal.d.ts +1 -1
  11. package/internal.js +4 -0
  12. package/internal.js.map +1 -1
  13. package/internal.mjs +1 -1
  14. package/package.json +15 -17
  15. package/react-components.d.ts +1 -0
  16. package/react-components.js +42 -0
  17. package/react-components.js.map +1 -0
  18. package/react-components.mjs +40 -0
  19. package/react-components.mjs.map +1 -0
  20. package/react-native-components.d.ts +1 -0
  21. package/react-native-components.js +38 -0
  22. package/react-native-components.js.map +1 -0
  23. package/react-native-components.mjs +36 -0
  24. package/react-native-components.mjs.map +1 -0
  25. package/react.d.ts +1 -0
  26. package/react.js +139 -87
  27. package/react.js.map +1 -1
  28. package/react.mjs +136 -89
  29. package/react.mjs.map +1 -1
  30. package/src/globals.d.ts +3 -1
  31. package/src/helpers.d.ts +3 -1
  32. package/src/observableInterfaces.d.ts +16 -14
  33. package/src/observe.d.ts +3 -0
  34. package/src/onChange.d.ts +1 -5
  35. package/src/react/flow.d.ts +31 -0
  36. package/src/react/useComputed.d.ts +6 -1
  37. package/src/react/useObserve.d.ts +1 -0
  38. package/src/{react/components.d.ts → react-components/react-components.d.ts} +4 -10
  39. package/src/{react-native/components.d.ts → react-native-components/rn-components.d.ts} +4 -8
  40. package/src/tracking.d.ts +1 -1
  41. package/src/when.d.ts +1 -5
  42. package/trace.js +4 -2
  43. package/trace.js.map +1 -1
  44. package/trace.mjs +5 -3
  45. package/trace.mjs.map +1 -1
  46. package/types.d.ts +15 -0
  47. package/react-native-types.d.ts +0 -39
  48. package/react-native.d.ts +0 -1
  49. package/react-native.js +0 -146
  50. package/react-native.js.map +0 -1
  51. package/react-native.mjs +0 -142
  52. package/react-native.mjs.map +0 -1
  53. package/react-types.d.ts +0 -8
  54. package/reactivity.d.ts +0 -2
  55. package/reactivity.js +0 -200
  56. package/reactivity.js.map +0 -1
  57. package/reactivity.mjs +0 -192
  58. package/reactivity.mjs.map +0 -1
  59. package/src/effect.d.ts +0 -1
  60. package/src/react/controlFlow.d.ts +0 -28
  61. package/src/react/useObserver.d.ts +0 -1
  62. package/state-types.d.ts +0 -24
package/index.mjs CHANGED
@@ -36,10 +36,7 @@ const tracking = {
36
36
  listeners: undefined,
37
37
  updates: undefined,
38
38
  };
39
- function updateTracking(node, parent, track, manual) {
40
- if (parent) {
41
- untrack(parent);
42
- }
39
+ function updateTracking(node, track, manual) {
43
40
  const existing = tracking.nodes.get(node.id);
44
41
  if (existing) {
45
42
  existing.track = existing.track || track;
@@ -64,6 +61,9 @@ function untrack(node) {
64
61
 
65
62
  const symbolDateModified = Symbol('dateModified');
66
63
  const symbolIsObservable = Symbol('isObservable');
64
+ const symbolGetNode = Symbol('getNode');
65
+ const symbolUndef = Symbol('undef');
66
+ const extraPrimitiveProps = new Map();
67
67
  var Tracking;
68
68
  (function (Tracking) {
69
69
  Tracking.normal = true;
@@ -74,7 +74,7 @@ const nextNodeID = { current: 0 };
74
74
  function checkTracking(node, track) {
75
75
  if (tracking.nodes) {
76
76
  if (track) {
77
- updateTracking(node, undefined, track, /*manual*/ true);
77
+ updateTracking(node, track, /*manual*/ true);
78
78
  }
79
79
  else {
80
80
  untrack(node);
@@ -86,10 +86,13 @@ function get(node, keyOrTrack, track) {
86
86
  track = keyOrTrack;
87
87
  keyOrTrack = undefined;
88
88
  }
89
+ if (keyOrTrack) {
90
+ node = getChildNode(node, keyOrTrack);
91
+ }
89
92
  // Track by default
90
93
  checkTracking(node, track === true || track === undefined ? Tracking.normal : track === false ? undefined : track);
91
- const value = getOutputValue(node);
92
- return keyOrTrack ? value === null || value === void 0 ? void 0 : value[keyOrTrack] : value;
94
+ const value = getNodeValue(node);
95
+ return node.root.isPrimitive ? value.value : value;
93
96
  }
94
97
  function getNodeValue(node) {
95
98
  const arr = [];
@@ -106,13 +109,6 @@ function getNodeValue(node) {
106
109
  }
107
110
  return child;
108
111
  }
109
- function getOutputValue(node) {
110
- let value = getNodeValue(node);
111
- if (node.root.isPrimitive) {
112
- value = value.value;
113
- }
114
- return value;
115
- }
116
112
  function getChildNode(node, key) {
117
113
  var _a;
118
114
  if (isString(key)) {
@@ -143,6 +139,16 @@ function getChildNode(node, key) {
143
139
  function isObservable(obs) {
144
140
  return obs && !!obs[symbolIsObservable];
145
141
  }
142
+ function getNode(obs) {
143
+ return obs[symbolGetNode];
144
+ }
145
+ function lockObservable(obs, value) {
146
+ var _a;
147
+ const root = (_a = getNode(obs)) === null || _a === void 0 ? void 0 : _a.root;
148
+ if (root) {
149
+ root.locked = value;
150
+ }
151
+ }
146
152
  function mergeIntoObservable(target, ...sources) {
147
153
  var _a, _b;
148
154
  if (!sources.length)
@@ -230,33 +236,20 @@ function endBatch(force) {
230
236
  }
231
237
  }
232
238
 
233
- let listenerIndex = 0;
234
- function onChange(node, callback, options) {
235
- let id = listenerIndex++;
239
+ function onChange(node, callback, track) {
236
240
  let listeners = node.listeners;
237
241
  if (!listeners) {
238
- node.listeners = listeners = new Map();
242
+ node.listeners = listeners = new Set();
239
243
  }
240
- if (options) {
241
- // A memory efficient way to save shallowness is to put it on the id itself
242
- if (options.shallow)
243
- id = 's' + id;
244
- else if (options.optimized)
245
- id = 'o' + id;
246
- if (options.runImmediately) {
247
- const value = getNodeValue(node);
248
- callback(value, () => value, [], value, value, node);
249
- }
250
- }
251
- listeners.set(id, callback);
252
- return () => listeners.delete(id);
244
+ const listener = { listener: callback, track: track };
245
+ listeners.add(listener);
246
+ return () => listeners.delete(listener);
253
247
  }
254
248
 
255
249
  let lastAccessedNode;
256
250
  let lastAccessedPrimitive;
257
251
  let inSetFn = false;
258
252
  let inAssign = false;
259
- const undef = Symbol('undef');
260
253
  const ArrayModifiers = new Set([
261
254
  'copyWithin',
262
255
  'fill',
@@ -273,7 +266,7 @@ const ArrayLoopers = new Set(['every', 'some', 'filter', 'forEach', 'map']);
273
266
  const objectFns = new Map([
274
267
  ['get', get],
275
268
  ['set', set],
276
- ['ref', ref],
269
+ ['obs', obs],
277
270
  ['onChange', onChange],
278
271
  ['assign', assign],
279
272
  ['delete', deleteFn],
@@ -286,28 +279,27 @@ const wrapFn = (fn) => function (...args) {
286
279
  return fn(node, ...args);
287
280
  }
288
281
  else if (process.env.NODE_ENV === 'development') {
289
- console.error(`[legend-state] Error calling ${fn} on a primitive with value ${this}. Please ensure that if you are saving references to observable functions on primitive values that you use ref() first, like obs.primitive.ref().set.`);
282
+ console.error(`[legend-state] Error calling ${fn} on a primitive with value ${this}. Please ensure that if you are saving references to observable functions on primitive values that you use obs() first, like obs.primitive.obs().set.`);
290
283
  }
291
284
  }
292
285
  };
293
286
  const toOverride = [Number, Boolean, String];
294
- objectFns.forEach((fn, key) => {
287
+ for (let [key, fn] of objectFns) {
295
288
  for (let i = 0; i < toOverride.length; i++) {
296
289
  toOverride[i].prototype[key] = wrapFn(fn);
297
290
  }
298
- });
291
+ }
299
292
  function collectionSetter(node, target, prop, ...args) {
300
293
  const prevValue = (isArray(target) && target.slice()) || target;
301
294
  const ret = target[prop].apply(target, args);
302
295
  if (node) {
303
296
  const parent = node.parent;
304
- if (parent) {
305
- const parentValue = getNodeValue(parent);
306
- // Set the object to the previous value first
307
- parentValue[node.key] = prevValue;
308
- // Then set with the new value so it notifies with the correct prevValue
309
- setProp(parent, node.key, target);
310
- }
297
+ const key = parent ? node.key : '_';
298
+ const parentValue = parent ? getNodeValue(parent) : node.root;
299
+ // Set the object to the previous value first
300
+ parentValue[key] = prevValue;
301
+ // Then set with the new value so it notifies with the correct prevValue
302
+ setProp(parent || node, parent ? key : symbolUndef, target);
311
303
  }
312
304
  // Return the original value
313
305
  return ret;
@@ -322,14 +314,6 @@ function updateNodes(parent, obj, prevValue) {
322
314
  if (isArr && isArray(prevValue)) {
323
315
  // Construct a map of previous indices for computing move
324
316
  if ((prevValue === null || prevValue === void 0 ? void 0 : prevValue.length) > 0) {
325
- if (parent.listeners) {
326
- for (let listenerFn of parent.listeners) {
327
- // if listener is not optimized
328
- if (listenerFn[0][0] === 'o') {
329
- break;
330
- }
331
- }
332
- }
333
317
  const p = prevValue[0];
334
318
  if (p) {
335
319
  idField =
@@ -359,8 +343,7 @@ function updateNodes(parent, obj, prevValue) {
359
343
  if (!isPrimitive(prev)) {
360
344
  updateNodes(child, undefined, prev);
361
345
  }
362
- const doNotify = !!child.listeners;
363
- if (doNotify) {
346
+ if (child.listeners) {
364
347
  _notify(child, undefined, [], undefined, prev, 0);
365
348
  }
366
349
  }
@@ -414,8 +397,7 @@ function updateNodes(parent, obj, prevValue) {
414
397
  // Or if the position changed in an array whose length did not change
415
398
  // But do not notify child if the parent is an array with changing length -
416
399
  // the array's listener will cover it
417
- const doNotify = !!child.listeners;
418
- if (doNotify) {
400
+ if (child.listeners) {
419
401
  _notify(child, value, [], value, prev, 0, !isArrDiff);
420
402
  }
421
403
  }
@@ -427,7 +409,6 @@ function updateNodes(parent, obj, prevValue) {
427
409
  parent.children.set(key, child);
428
410
  }
429
411
  }
430
- // TODO: !isArrDiff should only be for optimized listeners
431
412
  // The full array does not need to re-render if the length is the same
432
413
  // So don't notify shallow listeners
433
414
  return hasADiff || didMove;
@@ -438,13 +419,9 @@ function getProxy(node, p) {
438
419
  if (p !== undefined)
439
420
  node = getChildNode(node, p);
440
421
  // Create a proxy if not already cached and return it
441
- let proxy = node.proxy;
442
- if (!proxy) {
443
- proxy = node.proxy = new Proxy(node, proxyHandler);
444
- }
445
- return proxy;
422
+ return node.proxy || (node.proxy = new Proxy(node, proxyHandler));
446
423
  }
447
- function ref(node, keyOrTrack, track) {
424
+ function obs(node, keyOrTrack, track) {
448
425
  if (isBoolean(keyOrTrack) || isSymbol(keyOrTrack)) {
449
426
  track = keyOrTrack;
450
427
  keyOrTrack = undefined;
@@ -452,25 +429,27 @@ function ref(node, keyOrTrack, track) {
452
429
  if (keyOrTrack !== undefined) {
453
430
  node = getChildNode(node, keyOrTrack);
454
431
  }
455
- // Untrack by default
456
- checkTracking(node, track === true ? Tracking.normal : track === false ? undefined : track);
432
+ // Don't untrack if getting node by key
433
+ if (track !== undefined || !keyOrTrack) {
434
+ // Untrack by default
435
+ checkTracking(node, track === true ? Tracking.normal : track === false ? undefined : track);
436
+ }
457
437
  return getProxy(node);
458
438
  }
459
439
  const proxyHandler = {
460
440
  get(target, p) {
441
+ var _a, _b;
461
442
  // Return true is called by isObservable()
462
443
  if (p === symbolIsObservable) {
463
444
  return true;
464
445
  }
446
+ if (p === symbolGetNode) {
447
+ return target;
448
+ }
465
449
  const node = target;
466
450
  const fn = objectFns.get(p);
467
451
  // If this is an observable function, call it
468
452
  if (fn) {
469
- if (p !== 'get' && p !== 'ref') {
470
- // Observable operations do not create listeners
471
- if (tracking.nodes)
472
- untrack(node);
473
- }
474
453
  return function (a, b, c) {
475
454
  const l = arguments.length;
476
455
  // Array call and apply are slow so micro-optimize this hot path.
@@ -496,7 +475,7 @@ const proxyHandler = {
496
475
  // Update that this node was accessed for observers
497
476
  // Listen to the array shallowly
498
477
  if (tracking.nodes) {
499
- updateTracking(node, undefined, Tracking.shallow);
478
+ updateTracking(node, Tracking.shallow);
500
479
  }
501
480
  return function (cbOrig, thisArg) {
502
481
  function cb(_, index, array) {
@@ -511,6 +490,12 @@ const proxyHandler = {
511
490
  }
512
491
  // Accessing primitive returns the raw value
513
492
  if (vProp === undefined || vProp === null || isPrimitive(vProp)) {
493
+ if (extraPrimitiveProps.size) {
494
+ const vPrim = extraPrimitiveProps.get(p);
495
+ if (vPrim !== undefined) {
496
+ return (_b = (_a = vPrim === null || vPrim === void 0 ? void 0 : vPrim.__fn) === null || _a === void 0 ? void 0 : _a.call(vPrim, target)) !== null && _b !== void 0 ? _b : vPrim;
497
+ }
498
+ }
514
499
  // Accessing a primitive saves the last accessed so that the observable functions
515
500
  // bound to primitives can know which node was accessed
516
501
  lastAccessedNode = node;
@@ -518,13 +503,13 @@ const proxyHandler = {
518
503
  // Update that this primitive node was accessed for observers
519
504
  if (tracking.nodes) {
520
505
  if (isArray(value) && p === 'length') {
521
- updateTracking(node, undefined, Tracking.shallow);
506
+ updateTracking(node, Tracking.shallow);
522
507
  }
523
508
  else if (node.root.isPrimitive) {
524
509
  updateTracking(node);
525
510
  }
526
- else {
527
- updateTracking(getChildNode(node, p), node);
511
+ else if (!isPrimitive(value)) {
512
+ updateTracking(getChildNode(node, p));
528
513
  }
529
514
  }
530
515
  return vProp;
@@ -542,7 +527,7 @@ const proxyHandler = {
542
527
  const keys = value ? Reflect.ownKeys(value) : [];
543
528
  // Update that this node was accessed for observers
544
529
  if (tracking.nodes) {
545
- updateTracking(target, undefined, Tracking.shallow);
530
+ updateTracking(target, Tracking.shallow);
546
531
  }
547
532
  // This is required to fix this error:
548
533
  // TypeError: 'getOwnPropertyDescriptor' on proxy: trap reported non-configurability for
@@ -606,7 +591,7 @@ function set(node, keyOrNewValue, newValue) {
606
591
  return setProp(node, 'value', keyOrNewValue);
607
592
  }
608
593
  else if (!node.parent) {
609
- return setProp(node, undef, keyOrNewValue);
594
+ return setProp(node, symbolUndef, keyOrNewValue);
610
595
  }
611
596
  else {
612
597
  return setProp(node.parent, node.key, keyOrNewValue);
@@ -618,11 +603,16 @@ function setProp(node, key, newValue, level) {
618
603
  console.warn(`[legend-state] Set an HTMLElement into state. You probably don't want to do that.`);
619
604
  }
620
605
  }
621
- const isDelete = newValue === undef;
606
+ if (node.root.locked) {
607
+ throw new Error(process.env.NODE_ENV === 'development'
608
+ ? '[legend-state] Cannot modify an observable while it is locked. Please make sure that you unlock the observable before making changes.'
609
+ : '[legend-state] Modified locked observable');
610
+ }
611
+ const isDelete = newValue === symbolUndef;
622
612
  if (isDelete)
623
613
  newValue = undefined;
624
614
  inSetFn = true;
625
- const isRoot = key === undef;
615
+ const isRoot = key === symbolUndef;
626
616
  // Get the child node for updating and notifying
627
617
  let childNode = isRoot ? node : getChildNode(node, key);
628
618
  // Set operations do not create listeners
@@ -636,11 +626,12 @@ function setProp(node, key, newValue, level) {
636
626
  // Save the previous value first
637
627
  const prevValue = parentValue[key];
638
628
  // Compute newValue if newValue is a function or an observable
639
- newValue = isFunction(newValue)
640
- ? newValue(prevValue)
641
- : isObject(newValue) && (newValue === null || newValue === void 0 ? void 0 : newValue[symbolIsObservable])
642
- ? newValue.get()
643
- : newValue;
629
+ newValue =
630
+ !inAssign && isFunction(newValue)
631
+ ? newValue(prevValue)
632
+ : isObject(newValue) && (newValue === null || newValue === void 0 ? void 0 : newValue[symbolIsObservable])
633
+ ? newValue.get()
634
+ : newValue;
644
635
  const isPrim = isPrimitive(newValue);
645
636
  // Save the new value
646
637
  if (isDelete) {
@@ -691,10 +682,12 @@ function _notify(node, value, path, valueAtPath, prevAtPath, level, whenOptimize
691
682
  if (listeners) {
692
683
  let getPrevious;
693
684
  for (let listenerFn of listeners) {
694
- const shallow = listenerFn[0][0] === 's';
695
- const optimized = listenerFn[0][0] === 'o';
696
- const listener = listenerFn[1];
697
- const ok = shallow ? level <= 0 : optimized ? whenOptimizedOnlyIf && level <= 0 : true;
685
+ const { track } = listenerFn;
686
+ const ok = track === Tracking.shallow
687
+ ? level <= 0
688
+ : track === Tracking.optimized
689
+ ? whenOptimizedOnlyIf && level <= 0
690
+ : true;
698
691
  // Notify if listener is not shallow or if this is the first level
699
692
  if (ok) {
700
693
  // Create a function to get the previous data. Computing a clone of previous data can be expensive if doing
@@ -702,7 +695,15 @@ function _notify(node, value, path, valueAtPath, prevAtPath, level, whenOptimize
702
695
  if (!getPrevious) {
703
696
  getPrevious = createPreviousHandler(value, path, prevAtPath);
704
697
  }
705
- batchNotify({ cb: listener, value, getPrevious, path, valueAtPath, prevAtPath, node });
698
+ batchNotify({
699
+ cb: listenerFn.listener,
700
+ value,
701
+ getPrevious,
702
+ path,
703
+ valueAtPath,
704
+ prevAtPath,
705
+ node,
706
+ });
706
707
  }
707
708
  }
708
709
  }
@@ -720,13 +721,10 @@ function _notifyParents(node, value, path, valueAtPath, prevAtPath, level, whenO
720
721
  }
721
722
  }
722
723
  function notify(node, value, prev, level, whenOptimizedOnlyIf) {
723
- // Start notifying up through parents with the listenerInfo
724
+ // Notify self and up through parents
724
725
  _notifyParents(node, value, [], value, prev, level, whenOptimizedOnlyIf);
725
726
  }
726
727
  function assign(node, value) {
727
- // Set operations do not create listeners
728
- if (tracking.nodes)
729
- untrack(node);
730
728
  const proxy = getProxy(node);
731
729
  beginBatch();
732
730
  // Set inAssign to allow setting on safe observables
@@ -744,11 +742,11 @@ function deleteFn(node, key) {
744
742
  }
745
743
  if (!node.root.isPrimitive) {
746
744
  // delete sets to undefined first to notify
747
- setProp(node, key, undef, /*level*/ -1);
745
+ setProp(node, key, symbolUndef, /*level*/ -1);
748
746
  }
749
747
  }
750
748
  function observable(value, safe) {
751
- const promise = value.then && value;
749
+ const promise = (value === null || value === void 0 ? void 0 : value.then) && value;
752
750
  if (promise) {
753
751
  value = undefined;
754
752
  }
@@ -770,8 +768,8 @@ function observable(value, safe) {
770
768
  };
771
769
  const proxy = getProxy(node);
772
770
  if (promise) {
773
- promise.catch((rejected) => {
774
- proxy.set({ rejected });
771
+ promise.catch((error) => {
772
+ proxy.set({ error });
775
773
  });
776
774
  promise.then((value) => {
777
775
  obs.isPrimitive = isPrimitive(value);
@@ -784,41 +782,57 @@ function observable(value, safe) {
784
782
  return proxy;
785
783
  }
786
784
 
787
- function effect(run) {
785
+ function setupTracking(nodes, update) {
786
+ let listeners = [];
787
+ // Listen to tracked nodes
788
+ for (let tracked of nodes.values()) {
789
+ const { node, track } = tracked;
790
+ listeners.push(onChange(node, update, track));
791
+ }
792
+ return () => {
793
+ if (listeners) {
794
+ for (let i = 0; i < listeners.length; i++) {
795
+ listeners[i]();
796
+ }
797
+ listeners = undefined;
798
+ }
799
+ };
800
+ }
801
+ function observe(run) {
802
+ var _a;
788
803
  let cleanup;
789
804
  // Wrap it in a function so it doesn't pass all the arguments to run()
790
- const update = function () {
805
+ let update = function () {
806
+ var _a;
791
807
  if (cleanup) {
792
808
  cleanup();
793
809
  cleanup = undefined;
794
810
  }
795
811
  cleanup = run();
812
+ // Do tracing if it was requested
813
+ if (process.env.NODE_ENV === 'development') {
814
+ (_a = tracking.listeners) === null || _a === void 0 ? void 0 : _a.call(tracking, tracking.nodes);
815
+ if (tracking.updates) {
816
+ update = tracking.updates(update);
817
+ }
818
+ // Clear tracing
819
+ tracking.listeners = undefined;
820
+ tracking.updates = undefined;
821
+ }
796
822
  };
797
823
  const trackingPrev = tracking.nodes;
798
824
  tracking.nodes = new Map();
799
- cleanup = run();
800
- let listeners = [];
801
- // Listen to tracked nodes
802
- for (let tracked of tracking.nodes.values()) {
803
- const { node, track } = tracked;
804
- let options;
805
- if (track) {
806
- options = {
807
- shallow: track === Tracking.shallow,
808
- optimized: track === Tracking.optimized,
809
- };
825
+ update();
826
+ // Do tracing if it was requested
827
+ if (process.env.NODE_ENV === 'development') {
828
+ (_a = tracking.listeners) === null || _a === void 0 ? void 0 : _a.call(tracking, tracking.nodes);
829
+ if (tracking.updates) {
830
+ update = tracking.updates(update);
810
831
  }
811
- listeners.push(onChange(node, update, options));
812
832
  }
833
+ const ret = setupTracking(tracking.nodes, update);
813
834
  tracking.nodes = trackingPrev;
814
- return () => {
815
- if (listeners) {
816
- for (let i = 0; i < listeners.length; i++) {
817
- listeners[i]();
818
- }
819
- listeners = undefined;
820
- }
821
- };
835
+ return ret;
822
836
  }
823
837
 
824
838
  function computed(compute) {
@@ -835,7 +849,7 @@ function computed(compute) {
835
849
  obs = observable(val);
836
850
  }
837
851
  };
838
- effect(fn);
852
+ observe(fn);
839
853
  return obs;
840
854
  }
841
855
 
@@ -853,30 +867,26 @@ function event() {
853
867
  };
854
868
  }
855
869
 
856
- function when(predicate, effect$1, options) {
870
+ function when(predicate, effect) {
857
871
  let cleanup;
858
872
  let isDone = false;
859
873
  // Create a wrapping fn that calls the effect if predicate returns true
860
- const fn = function () {
874
+ function run() {
861
875
  const ret = predicate();
862
876
  if (ret) {
863
877
  // If value is truthy then run the effect and cleanup
864
- if (!(options === null || options === void 0 ? void 0 : options.repeat)) {
865
- isDone = true;
866
- }
867
- effect$1();
868
- if (isDone) {
869
- cleanup === null || cleanup === void 0 ? void 0 : cleanup();
870
- }
878
+ isDone = true;
879
+ effect();
880
+ cleanup === null || cleanup === void 0 ? void 0 : cleanup();
871
881
  }
872
- };
882
+ }
873
883
  // If no effect parameter return a promise
874
- const promise = !effect$1 &&
884
+ const promise = !effect &&
875
885
  new Promise((resolve) => {
876
- effect$1 = resolve;
886
+ effect = resolve;
877
887
  });
878
888
  // Create an effect for the fn
879
- cleanup = effect(fn);
889
+ cleanup = observe(run);
880
890
  // If it's already cleanup
881
891
  if (isDone) {
882
892
  cleanup();
@@ -884,5 +894,5 @@ function when(predicate, effect$1, options) {
884
894
  return promise || cleanup;
885
895
  }
886
896
 
887
- export { Tracking, batch, beginBatch, computed, effect, endBatch, event, isArray, isEmpty, isFunction, isObject, isObservable, isPrimitive, mergeIntoObservable, observable, onChange, symbolDateModified, symbolIsObservable, tracking, when };
897
+ export { Tracking, batch, beginBatch, computed, endBatch, event, extraPrimitiveProps, getNode, getNodeValue, isArray, isEmpty, isFunction, isObject, isObservable, isPrimitive, lockObservable, mergeIntoObservable, observable, observe, onChange, setupTracking, symbolDateModified, symbolIsObservable, symbolUndef, tracking, when };
888
898
  //# sourceMappingURL=index.mjs.map