@expressive/mvc 0.53.5 → 0.53.6

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.d.ts CHANGED
@@ -16,14 +16,14 @@ declare function addListener<T extends Model>(subject: T, callback: OnUpdate<T>,
16
16
  declare function createProxy<T extends Model>(from: T, observer: OnAccess<T>): T;
17
17
  /**
18
18
  * Create a side-effect which will update whenever values accessed change.
19
- * Callback is called immediately and if ever values are stale.
19
+ * Callback is called immediately and whenever values are stale.
20
20
  *
21
21
  * @param target - Instance of Model to observe.
22
22
  * @param callback - Function to invoke when values change.
23
23
  * @param requireValues - If `true` will throw if accessing a value which is `undefined`.
24
24
  */
25
25
  declare function createEffect<T extends Model>(target: T, callback: Effect<Required<T>>, requireValues: true): () => void;
26
- declare function createEffect<T extends Model>(target: T, callback: Effect<T>, requireValues?: boolean): () => void;
26
+ declare function createEffect<T extends Model>(target: T, callback: Effect<T>, recursive?: boolean): () => void;
27
27
 
28
28
  declare namespace Model {
29
29
  /** Any type of Model, using own class constructor as its identifier. */
package/dist/index.js CHANGED
@@ -129,7 +129,7 @@ function watch(from, key2, value) {
129
129
  const access = OBSERVER.get(from);
130
130
  return access ? access(from, key2, value) : value;
131
131
  }
132
- function createEffect(target, callback, requireValues) {
132
+ function createEffect(target, callback, argument) {
133
133
  const listeners = LISTENERS.get(target);
134
134
  let unset;
135
135
  let reset;
@@ -148,7 +148,7 @@ function createEffect(target, callback, requireValues) {
148
148
  return reset;
149
149
  }
150
150
  function access(from, key2, value) {
151
- if (value === void 0 && requireValues)
151
+ if (value === void 0 && argument)
152
152
  throw new Error(`${from}.${key2} is required in this context.`);
153
153
  const listeners2 = LISTENERS.get(from);
154
154
  let listener = listeners2.get(onUpdate);
@@ -164,9 +164,15 @@ function createEffect(target, callback, requireValues) {
164
164
  return value;
165
165
  }
166
166
  try {
167
- const out = callback.call(subscriber, subscriber);
168
- unset = typeof out == "function" ? out : void 0;
169
- reset = out === null ? out : invoke;
167
+ const ctx = context(argument === false);
168
+ const ret = callback.call(subscriber, subscriber);
169
+ const flush = ctx();
170
+ reset = ret === null ? null : invoke;
171
+ unset = (key2) => {
172
+ if (typeof ret == "function")
173
+ ret(key2);
174
+ flush();
175
+ };
170
176
  } catch (err) {
171
177
  if (err instanceof Promise) {
172
178
  reset = void 0;
@@ -175,6 +181,14 @@ function createEffect(target, callback, requireValues) {
175
181
  throw err;
176
182
  }
177
183
  }
184
+ function cleanup() {
185
+ if (unset)
186
+ unset(false);
187
+ reset = null;
188
+ }
189
+ ;
190
+ if (EffectContext && argument !== false)
191
+ EffectContext.add(cleanup);
178
192
  addListener(target, (key2) => {
179
193
  if (key2 === true)
180
194
  invoke();
@@ -183,10 +197,20 @@ function createEffect(target, callback, requireValues) {
183
197
  if (key2 === null && unset)
184
198
  unset(null);
185
199
  });
200
+ return cleanup;
201
+ }
202
+ var EffectContext;
203
+ function context(ignore) {
204
+ if (ignore)
205
+ return () => () => {
206
+ };
207
+ const current = EffectContext;
208
+ const ctx = EffectContext = /* @__PURE__ */ new Set();
186
209
  return () => {
187
- if (unset)
188
- unset(false);
189
- reset = null;
210
+ EffectContext = current;
211
+ return () => {
212
+ ctx.forEach((fn) => fn());
213
+ };
190
214
  };
191
215
  }
192
216
 
@@ -210,7 +234,7 @@ var Model = class {
210
234
  let pending2 = /* @__PURE__ */ new Set();
211
235
  return createEffect(self, (proxy) => {
212
236
  const cb = arg1.call(proxy, proxy, pending2);
213
- return cb === null ? cb : (update2) => {
237
+ return cb === null ? null : (update2) => {
214
238
  pending2 = PENDING_KEYS.get(self);
215
239
  if (typeof cb == "function")
216
240
  cb(update2);
@@ -314,9 +338,9 @@ function prepare(model) {
314
338
  Object.getOwnPropertyDescriptors(type.prototype)
315
339
  )) {
316
340
  let bind2 = function(method = value) {
317
- const value2 = method.bind(model);
341
+ const value2 = method.bind(this.is);
318
342
  METHOD.set(value2, method);
319
- define(model, key2, { value: value2, writable: true });
343
+ define(this.is, key2, { value: value2, writable: true });
320
344
  return value2;
321
345
  };
322
346
  var bind = bind2;
@@ -668,8 +692,8 @@ function get(arg0, arg1) {
668
692
  if (!hasParent) {
669
693
  if (arg1 === true)
670
694
  throw new Error(`${subject} may only exist as a child of type ${arg0}.`);
671
- Context.get(subject, (context) => {
672
- const model = context.get(arg0);
695
+ Context.get(subject, (context2) => {
696
+ const model = context2.get(arg0);
673
697
  if (model)
674
698
  assign2(model);
675
699
  else if (arg1 !== false)
@@ -706,7 +730,7 @@ function get(arg0, arg1) {
706
730
  event(subject, key2, true);
707
731
  }
708
732
  };
709
- });
733
+ }, false);
710
734
  }
711
735
  function compute(initial) {
712
736
  let next;
@@ -761,18 +785,23 @@ function ref(arg, arg2) {
761
785
  else if (typeof arg == "object")
762
786
  throw new Error("ref instruction does not support object which is not 'this'");
763
787
  else {
764
- let unSet;
788
+ let unset;
765
789
  const get2 = () => state[key2];
766
790
  const set2 = (value2) => {
767
791
  if (!update(subject, key2, value2) || !arg)
768
792
  return;
769
- if (unSet)
770
- unSet = void unSet(value2);
771
- if (value2 !== null || arg2 === false) {
772
- const out = arg.call(subject, value2);
793
+ if (unset)
794
+ unset = void unset(value2);
795
+ if (value2 === null && arg2 !== false)
796
+ return;
797
+ const ctx = context();
798
+ const out = arg.call(subject, value2);
799
+ const flush = ctx();
800
+ unset = (key3) => {
773
801
  if (typeof out == "function")
774
- unSet = out;
775
- }
802
+ out(key3);
803
+ flush();
804
+ };
776
805
  };
777
806
  state[key2] = null;
778
807
  value = Object.defineProperties(set2, {
@@ -821,15 +850,20 @@ function set(value, argument) {
821
850
  } else if (value !== void 0)
822
851
  property.value = value;
823
852
  if (typeof argument == "function") {
824
- let unSet;
853
+ let unset;
825
854
  property.set = function(value2, previous) {
855
+ const ctx = context();
826
856
  const out = argument.call(this, value2, previous);
857
+ const flush = ctx();
827
858
  if (out === false)
828
859
  return out;
829
- if (typeof unSet == "function")
830
- unSet = void unSet(value2);
831
- if (typeof out == "function")
832
- unSet = out;
860
+ if (typeof unset == "function")
861
+ unset(value2);
862
+ unset = (next) => {
863
+ if (typeof out == "function")
864
+ out(next);
865
+ flush();
866
+ };
833
867
  };
834
868
  } else
835
869
  property.set = (value2) => {
@@ -866,8 +900,8 @@ function has(arg1, arg2) {
866
900
  update(subject, key2, Object.freeze(Array.from(applied)));
867
901
  };
868
902
  if (Model.is(arg1))
869
- Context.get(subject, (context) => {
870
- context.has(arg1, (model) => {
903
+ Context.get(subject, (context2) => {
904
+ context2.has(arg1, (model) => {
871
905
  let remove;
872
906
  let disconnect;
873
907
  if (applied.has(model))
package/package.json CHANGED
@@ -15,15 +15,13 @@
15
15
  "mvc"
16
16
  ],
17
17
  "main": "dist/index.js",
18
- "module": "dist/index.mjs",
18
+ "module": "dist/esm/index.mjs",
19
19
  "types": "dist/index.d.ts",
20
20
  "files": [
21
- "dist/index.js",
22
- "dist/index.mjs",
23
- "dist/index.d.ts"
21
+ "dist"
24
22
  ],
25
23
  "scripts": {
26
- "build": "tsup src/index.ts --dts --format esm,cjs",
24
+ "build": "tsc && tsup src/index.ts --dts",
27
25
  "test": "tsc --noEmit && jest --collectCoverage",
28
26
  "prebuild": "rm -rf dist",
29
27
  "preversion": "npm run test && npm run build"
@@ -33,6 +31,6 @@
33
31
  "jest": "^27.0.6",
34
32
  "tsup": "^8.0.2"
35
33
  },
36
- "version": "0.53.5",
37
- "gitHead": "7db2ee1e4e62b3ac065c69a9dc94a03edf37fb85"
34
+ "version": "0.53.6",
35
+ "gitHead": "a6564d79b709f3d41c663885d1c0a2e3f8eddd31"
38
36
  }