@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.js CHANGED
@@ -305,6 +305,7 @@ var lowPriorityQueue = [];
305
305
  var enqueueMicrotask = typeof queueMicrotask === "function" ? queueMicrotask : (fn) => {
306
306
  Promise.resolve().then(fn);
307
307
  };
308
+ var inCleanup = false;
308
309
  function link(dep, sub, version) {
309
310
  const prevDep = sub.depsTail;
310
311
  if (prevDep !== void 0 && prevDep.dep === dep) return;
@@ -550,7 +551,15 @@ function updateComputed(c) {
550
551
  }
551
552
  function runEffect(e) {
552
553
  const flags = e.flags;
553
- if (flags & Dirty || flags & Pending && e.deps && checkDirty(e.deps, e)) {
554
+ if (flags & Dirty) {
555
+ if (e.runCleanup) {
556
+ inCleanup = true;
557
+ try {
558
+ e.runCleanup();
559
+ } finally {
560
+ inCleanup = false;
561
+ }
562
+ }
554
563
  ++cycle;
555
564
  effectRunDevtools(e);
556
565
  e.depsTail = void 0;
@@ -567,6 +576,35 @@ function runEffect(e) {
567
576
  e.flags = Watching;
568
577
  throw err;
569
578
  }
579
+ } else if (flags & Pending && e.deps) {
580
+ if (e.runCleanup) {
581
+ inCleanup = true;
582
+ try {
583
+ e.runCleanup();
584
+ } finally {
585
+ inCleanup = false;
586
+ }
587
+ }
588
+ if (checkDirty(e.deps, e)) {
589
+ ++cycle;
590
+ effectRunDevtools(e);
591
+ e.depsTail = void 0;
592
+ e.flags = WatchingRunning;
593
+ const prevSub = activeSub;
594
+ activeSub = e;
595
+ try {
596
+ e.fn();
597
+ activeSub = prevSub;
598
+ e.flags = Watching;
599
+ purgeDeps(e);
600
+ } catch (err) {
601
+ activeSub = prevSub;
602
+ e.flags = Watching;
603
+ throw err;
604
+ }
605
+ } else {
606
+ e.flags = Watching;
607
+ }
570
608
  } else {
571
609
  e.flags = Watching;
572
610
  }
@@ -662,7 +700,7 @@ function signalOper(value) {
662
700
  return;
663
701
  }
664
702
  const flags = this.flags;
665
- if (flags & Dirty) {
703
+ if (flags & Dirty && !inCleanup) {
666
704
  if (updateSignal(this)) {
667
705
  const subs = this.subs;
668
706
  if (subs !== void 0) shallowPropagate(subs);
@@ -744,6 +782,30 @@ function effect(fn) {
744
782
  }
745
783
  return effectOper.bind(e);
746
784
  }
785
+ function effectWithCleanup(fn, cleanupRunner) {
786
+ const e = {
787
+ fn,
788
+ subs: void 0,
789
+ subsTail: void 0,
790
+ deps: void 0,
791
+ depsTail: void 0,
792
+ flags: WatchingRunning,
793
+ runCleanup: cleanupRunner,
794
+ __id: void 0
795
+ };
796
+ registerEffectDevtools(e);
797
+ const prevSub = activeSub;
798
+ if (prevSub !== void 0) link(e, prevSub, 0);
799
+ activeSub = e;
800
+ try {
801
+ effectRunDevtools(e);
802
+ fn();
803
+ } finally {
804
+ activeSub = prevSub;
805
+ e.flags &= ~Running;
806
+ }
807
+ return effectOper.bind(e);
808
+ }
747
809
  function effectOper() {
748
810
  disposeNode(this);
749
811
  }
@@ -858,8 +920,11 @@ function createMemo(fn) {
858
920
  function createEffect(fn) {
859
921
  let cleanups = [];
860
922
  const rootForError = getCurrentRoot();
861
- const run = () => {
923
+ const doCleanup = () => {
862
924
  runCleanupList(cleanups);
925
+ cleanups = [];
926
+ };
927
+ const run = () => {
863
928
  const bucket = [];
864
929
  withEffectCleanups(bucket, () => {
865
930
  try {
@@ -876,7 +941,7 @@ function createEffect(fn) {
876
941
  });
877
942
  cleanups = bucket;
878
943
  };
879
- const disposeEffect = effect(run);
944
+ const disposeEffect = effectWithCleanup(run, doCleanup);
880
945
  const teardown = () => {
881
946
  runCleanupList(cleanups);
882
947
  disposeEffect();
@@ -887,11 +952,13 @@ function createEffect(fn) {
887
952
  function createRenderEffect(fn) {
888
953
  let cleanup;
889
954
  const rootForError = getCurrentRoot();
890
- const run = () => {
955
+ const doCleanup = () => {
891
956
  if (cleanup) {
892
957
  cleanup();
893
958
  cleanup = void 0;
894
959
  }
960
+ };
961
+ const run = () => {
895
962
  try {
896
963
  const maybeCleanup = fn();
897
964
  if (typeof maybeCleanup === "function") {
@@ -905,7 +972,7 @@ function createRenderEffect(fn) {
905
972
  throw err;
906
973
  }
907
974
  };
908
- const disposeEffect = effect(run);
975
+ const disposeEffect = effectWithCleanup(run, doCleanup);
909
976
  const teardown = () => {
910
977
  if (cleanup) {
911
978
  cleanup();