@fictjs/runtime 0.0.10 → 0.0.11

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/dist/slim.cjs CHANGED
@@ -307,6 +307,7 @@ var lowPriorityQueue = [];
307
307
  var enqueueMicrotask = typeof queueMicrotask === "function" ? queueMicrotask : (fn) => {
308
308
  Promise.resolve().then(fn);
309
309
  };
310
+ var inCleanup = false;
310
311
  function link(dep, sub, version) {
311
312
  const prevDep = sub.depsTail;
312
313
  if (prevDep !== void 0 && prevDep.dep === dep) return;
@@ -552,7 +553,15 @@ function updateComputed(c) {
552
553
  }
553
554
  function runEffect(e) {
554
555
  const flags = e.flags;
555
- if (flags & Dirty || flags & Pending && e.deps && checkDirty(e.deps, e)) {
556
+ if (flags & Dirty) {
557
+ if (e.runCleanup) {
558
+ inCleanup = true;
559
+ try {
560
+ e.runCleanup();
561
+ } finally {
562
+ inCleanup = false;
563
+ }
564
+ }
556
565
  ++cycle;
557
566
  effectRunDevtools(e);
558
567
  e.depsTail = void 0;
@@ -569,6 +578,35 @@ function runEffect(e) {
569
578
  e.flags = Watching;
570
579
  throw err;
571
580
  }
581
+ } else if (flags & Pending && e.deps) {
582
+ if (e.runCleanup) {
583
+ inCleanup = true;
584
+ try {
585
+ e.runCleanup();
586
+ } finally {
587
+ inCleanup = false;
588
+ }
589
+ }
590
+ if (checkDirty(e.deps, e)) {
591
+ ++cycle;
592
+ effectRunDevtools(e);
593
+ e.depsTail = void 0;
594
+ e.flags = WatchingRunning;
595
+ const prevSub = activeSub;
596
+ activeSub = e;
597
+ try {
598
+ e.fn();
599
+ activeSub = prevSub;
600
+ e.flags = Watching;
601
+ purgeDeps(e);
602
+ } catch (err) {
603
+ activeSub = prevSub;
604
+ e.flags = Watching;
605
+ throw err;
606
+ }
607
+ } else {
608
+ e.flags = Watching;
609
+ }
572
610
  } else {
573
611
  e.flags = Watching;
574
612
  }
@@ -664,7 +702,7 @@ function signalOper(value) {
664
702
  return;
665
703
  }
666
704
  const flags = this.flags;
667
- if (flags & Dirty) {
705
+ if (flags & Dirty && !inCleanup) {
668
706
  if (updateSignal(this)) {
669
707
  const subs = this.subs;
670
708
  if (subs !== void 0) shallowPropagate(subs);
@@ -746,6 +784,30 @@ function effect(fn) {
746
784
  }
747
785
  return effectOper.bind(e);
748
786
  }
787
+ function effectWithCleanup(fn, cleanupRunner) {
788
+ const e = {
789
+ fn,
790
+ subs: void 0,
791
+ subsTail: void 0,
792
+ deps: void 0,
793
+ depsTail: void 0,
794
+ flags: WatchingRunning,
795
+ runCleanup: cleanupRunner,
796
+ __id: void 0
797
+ };
798
+ registerEffectDevtools(e);
799
+ const prevSub = activeSub;
800
+ if (prevSub !== void 0) link(e, prevSub, 0);
801
+ activeSub = e;
802
+ try {
803
+ effectRunDevtools(e);
804
+ fn();
805
+ } finally {
806
+ activeSub = prevSub;
807
+ e.flags &= ~Running;
808
+ }
809
+ return effectOper.bind(e);
810
+ }
749
811
  function effectOper() {
750
812
  disposeNode(this);
751
813
  }
@@ -860,8 +922,11 @@ function createMemo(fn) {
860
922
  function createEffect(fn) {
861
923
  let cleanups = [];
862
924
  const rootForError = getCurrentRoot();
863
- const run = () => {
925
+ const doCleanup = () => {
864
926
  runCleanupList(cleanups);
927
+ cleanups = [];
928
+ };
929
+ const run = () => {
865
930
  const bucket = [];
866
931
  withEffectCleanups(bucket, () => {
867
932
  try {
@@ -878,7 +943,7 @@ function createEffect(fn) {
878
943
  });
879
944
  cleanups = bucket;
880
945
  };
881
- const disposeEffect = effect(run);
946
+ const disposeEffect = effectWithCleanup(run, doCleanup);
882
947
  const teardown = () => {
883
948
  runCleanupList(cleanups);
884
949
  disposeEffect();
@@ -889,11 +954,13 @@ function createEffect(fn) {
889
954
  function createRenderEffect(fn) {
890
955
  let cleanup;
891
956
  const rootForError = getCurrentRoot();
892
- const run = () => {
957
+ const doCleanup = () => {
893
958
  if (cleanup) {
894
959
  cleanup();
895
960
  cleanup = void 0;
896
961
  }
962
+ };
963
+ const run = () => {
897
964
  try {
898
965
  const maybeCleanup = fn();
899
966
  if (typeof maybeCleanup === "function") {
@@ -907,7 +974,7 @@ function createRenderEffect(fn) {
907
974
  throw err;
908
975
  }
909
976
  };
910
- const disposeEffect = effect(run);
977
+ const disposeEffect = effectWithCleanup(run, doCleanup);
911
978
  const teardown = () => {
912
979
  if (cleanup) {
913
980
  cleanup();