@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/index.cjs CHANGED
@@ -348,6 +348,7 @@ var isInTransition = false;
348
348
  var enqueueMicrotask = typeof queueMicrotask === "function" ? queueMicrotask : (fn) => {
349
349
  Promise.resolve().then(fn);
350
350
  };
351
+ var inCleanup = false;
351
352
  function link(dep, sub, version) {
352
353
  const prevDep = sub.depsTail;
353
354
  if (prevDep !== void 0 && prevDep.dep === dep) return;
@@ -593,7 +594,15 @@ function updateComputed(c) {
593
594
  }
594
595
  function runEffect(e) {
595
596
  const flags = e.flags;
596
- if (flags & Dirty || flags & Pending && e.deps && checkDirty(e.deps, e)) {
597
+ if (flags & Dirty) {
598
+ if (e.runCleanup) {
599
+ inCleanup = true;
600
+ try {
601
+ e.runCleanup();
602
+ } finally {
603
+ inCleanup = false;
604
+ }
605
+ }
597
606
  ++cycle;
598
607
  effectRunDevtools(e);
599
608
  e.depsTail = void 0;
@@ -610,6 +619,35 @@ function runEffect(e) {
610
619
  e.flags = Watching;
611
620
  throw err;
612
621
  }
622
+ } else if (flags & Pending && e.deps) {
623
+ if (e.runCleanup) {
624
+ inCleanup = true;
625
+ try {
626
+ e.runCleanup();
627
+ } finally {
628
+ inCleanup = false;
629
+ }
630
+ }
631
+ if (checkDirty(e.deps, e)) {
632
+ ++cycle;
633
+ effectRunDevtools(e);
634
+ e.depsTail = void 0;
635
+ e.flags = WatchingRunning;
636
+ const prevSub = activeSub;
637
+ activeSub = e;
638
+ try {
639
+ e.fn();
640
+ activeSub = prevSub;
641
+ e.flags = Watching;
642
+ purgeDeps(e);
643
+ } catch (err) {
644
+ activeSub = prevSub;
645
+ e.flags = Watching;
646
+ throw err;
647
+ }
648
+ } else {
649
+ e.flags = Watching;
650
+ }
613
651
  } else {
614
652
  e.flags = Watching;
615
653
  }
@@ -705,7 +743,7 @@ function signalOper(value) {
705
743
  return;
706
744
  }
707
745
  const flags = this.flags;
708
- if (flags & Dirty) {
746
+ if (flags & Dirty && !inCleanup) {
709
747
  if (updateSignal(this)) {
710
748
  const subs = this.subs;
711
749
  if (subs !== void 0) shallowPropagate(subs);
@@ -787,6 +825,30 @@ function effect(fn) {
787
825
  }
788
826
  return effectOper.bind(e);
789
827
  }
828
+ function effectWithCleanup(fn, cleanupRunner) {
829
+ const e = {
830
+ fn,
831
+ subs: void 0,
832
+ subsTail: void 0,
833
+ deps: void 0,
834
+ depsTail: void 0,
835
+ flags: WatchingRunning,
836
+ runCleanup: cleanupRunner,
837
+ __id: void 0
838
+ };
839
+ registerEffectDevtools(e);
840
+ const prevSub = activeSub;
841
+ if (prevSub !== void 0) link(e, prevSub, 0);
842
+ activeSub = e;
843
+ try {
844
+ effectRunDevtools(e);
845
+ fn();
846
+ } finally {
847
+ activeSub = prevSub;
848
+ e.flags &= ~Running;
849
+ }
850
+ return effectOper.bind(e);
851
+ }
790
852
  function effectOper() {
791
853
  disposeNode(this);
792
854
  }
@@ -1032,8 +1094,11 @@ var $memo = createMemo;
1032
1094
  function createEffect(fn) {
1033
1095
  let cleanups = [];
1034
1096
  const rootForError = getCurrentRoot();
1035
- const run = () => {
1097
+ const doCleanup = () => {
1036
1098
  runCleanupList(cleanups);
1099
+ cleanups = [];
1100
+ };
1101
+ const run = () => {
1037
1102
  const bucket = [];
1038
1103
  withEffectCleanups(bucket, () => {
1039
1104
  try {
@@ -1050,7 +1115,7 @@ function createEffect(fn) {
1050
1115
  });
1051
1116
  cleanups = bucket;
1052
1117
  };
1053
- const disposeEffect = effect(run);
1118
+ const disposeEffect = effectWithCleanup(run, doCleanup);
1054
1119
  const teardown = () => {
1055
1120
  runCleanupList(cleanups);
1056
1121
  disposeEffect();
@@ -1062,11 +1127,13 @@ var $effect = createEffect;
1062
1127
  function createRenderEffect(fn) {
1063
1128
  let cleanup;
1064
1129
  const rootForError = getCurrentRoot();
1065
- const run = () => {
1130
+ const doCleanup = () => {
1066
1131
  if (cleanup) {
1067
1132
  cleanup();
1068
1133
  cleanup = void 0;
1069
1134
  }
1135
+ };
1136
+ const run = () => {
1070
1137
  try {
1071
1138
  const maybeCleanup = fn();
1072
1139
  if (typeof maybeCleanup === "function") {
@@ -1080,7 +1147,7 @@ function createRenderEffect(fn) {
1080
1147
  throw err;
1081
1148
  }
1082
1149
  };
1083
- const disposeEffect = effect(run);
1150
+ const disposeEffect = effectWithCleanup(run, doCleanup);
1084
1151
  const teardown = () => {
1085
1152
  if (cleanup) {
1086
1153
  cleanup();