@expressive/mvc 0.60.3 → 0.62.0

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.js CHANGED
@@ -255,19 +255,9 @@ var Model = class {
255
255
  if (key2 === arg1)
256
256
  return arg2.call(self, key2, self);
257
257
  });
258
- if (arg1 && typeof arg1 == "object") {
259
- const methods = METHODS.get(self.constructor);
260
- for (const key2 in arg1) {
261
- const bind = methods.get(key2);
262
- if (bind)
263
- bind.call(self, arg1[key2]);
264
- else if (key2 in self)
265
- if (arg2 === true || !Object.getOwnPropertyDescriptor(self, key2)?.set)
266
- update(self, key2, arg1[key2], arg2 === true);
267
- else
268
- self[key2] = arg1[key2];
269
- }
270
- } else
258
+ if (arg1 && typeof arg1 == "object")
259
+ assign(self, arg1, arg2 === true);
260
+ else
271
261
  event(self, arg1);
272
262
  return pending(self);
273
263
  }
@@ -320,6 +310,19 @@ define(Model, "toString", {
320
310
  return this.name;
321
311
  }
322
312
  });
313
+ function assign(subject, data, silent) {
314
+ const methods = METHODS.get(subject.constructor);
315
+ for (const key2 in data) {
316
+ const bind = methods.get(key2);
317
+ if (bind)
318
+ bind.call(subject, data[key2]);
319
+ else if (key2 in subject && key2 !== "is") {
320
+ const desc = Object.getOwnPropertyDescriptor(subject, key2);
321
+ const set2 = desc && desc.set;
322
+ set2.call(subject, data[key2], silent === true);
323
+ }
324
+ }
325
+ }
323
326
  function prepare(model) {
324
327
  let T = model.constructor;
325
328
  if (T === Model)
@@ -358,7 +361,6 @@ function prepare(model) {
358
361
  }
359
362
  }
360
363
  function init(model, args) {
361
- const methods = METHODS.get(model.constructor);
362
364
  const state = {};
363
365
  STATE.set(model, state);
364
366
  args = args.flat().filter((arg) => {
@@ -369,21 +371,6 @@ function init(model, args) {
369
371
  addListener(model, () => {
370
372
  if (!PARENT.has(model))
371
373
  PARENT.set(model, null);
372
- args.forEach((arg) => {
373
- const use2 = typeof arg == "function" ? arg.call(model, model) : arg;
374
- if (use2 instanceof Promise)
375
- use2.catch((err) => {
376
- console.error(`Async error in constructor for ${model}:`);
377
- console.error(err);
378
- });
379
- else if (typeof use2 == "function")
380
- addListener(model, use2, null);
381
- else if (typeof use2 == "object") {
382
- for (const key2 in use2)
383
- if (key2 in model || methods.has(key2))
384
- model[key2] = use2[key2];
385
- }
386
- });
387
374
  for (const key2 in model) {
388
375
  const desc = Object.getOwnPropertyDescriptor(model, key2);
389
376
  if ("value" in desc) {
@@ -393,13 +380,25 @@ function init(model, args) {
393
380
  get() {
394
381
  return watch(this, key2, state[key2]);
395
382
  },
396
- set(value) {
397
- update(model, key2, value);
383
+ set(value, silent) {
384
+ update(model, key2, value, silent);
398
385
  mayAdopt(model, value);
399
386
  }
400
387
  });
401
388
  }
402
389
  }
390
+ args.forEach((arg) => {
391
+ const use2 = typeof arg == "function" ? arg.call(model, model) : arg;
392
+ if (use2 instanceof Promise)
393
+ use2.catch((err) => {
394
+ console.error(`Async error in constructor for ${model}:`);
395
+ console.error(err);
396
+ });
397
+ else if (typeof use2 == "function")
398
+ addListener(model, use2, null);
399
+ else if (typeof use2 == "object")
400
+ assign(model, use2, true);
401
+ });
403
402
  addListener(model, () => {
404
403
  for (const [_, value] of model)
405
404
  if (value instanceof Model && PARENT.get(value) === model)
@@ -409,10 +408,10 @@ function init(model, args) {
409
408
  return null;
410
409
  });
411
410
  }
412
- function mayAdopt(parent, child) {
413
- if (child instanceof Model && !PARENT.has(child)) {
414
- PARENT.set(child, parent);
415
- event(child);
411
+ function mayAdopt(parent, child2) {
412
+ if (child2 instanceof Model && !PARENT.has(child2)) {
413
+ PARENT.set(child2, parent);
414
+ event(child2);
416
415
  }
417
416
  }
418
417
  function fetch(subject, property, required) {
@@ -471,7 +470,7 @@ function update(subject, key2, value, arg) {
471
470
  if (typeof out == "function")
472
471
  value = out();
473
472
  }
474
- if (value === previous)
473
+ if (value === previous && value !== void 0)
475
474
  return;
476
475
  state[key2] = value;
477
476
  if (arg !== true)
@@ -550,7 +549,7 @@ var Context = class _Context {
550
549
  this.layer.clear();
551
550
  }
552
551
  include(inputs, forEach) {
553
- const init2 = /* @__PURE__ */ new Map();
552
+ const init3 = /* @__PURE__ */ new Map();
554
553
  if (typeof inputs == "function" || inputs instanceof Model)
555
554
  inputs = { [forEach ? 0 : this.layer.size]: inputs };
556
555
  Object.entries(inputs).forEach(([K, V]) => {
@@ -559,7 +558,7 @@ var Context = class _Context {
559
558
  if (!exists) {
560
559
  const instance = this.add(V);
561
560
  this.layer.set(K, V);
562
- init2.set(instance, true);
561
+ init3.set(instance, true);
563
562
  } else if (exists !== V) {
564
563
  this.pop();
565
564
  this.id = uid();
@@ -571,14 +570,14 @@ var Context = class _Context {
571
570
  `Context may only include instance or class \`extends Model\` but got ${K == "0" || K == V ? V : `${V} (as '${K}')`}.`
572
571
  );
573
572
  });
574
- for (const [model, explicit] of init2) {
573
+ for (const [model, explicit] of init3) {
575
574
  model.set();
576
575
  if (explicit && forEach)
577
576
  forEach(model);
578
577
  for (const [_key, value] of model)
579
578
  if (PARENT.get(value) === model) {
580
579
  this.add(value, true);
581
- init2.set(value, false);
580
+ init3.set(value, false);
582
581
  }
583
582
  }
584
583
  }
@@ -628,46 +627,49 @@ var Context = class _Context {
628
627
  };
629
628
 
630
629
  // src/instruction/use.ts
631
- var INSTRUCT = /* @__PURE__ */ new Map();
630
+ var INSTRUCTION = /* @__PURE__ */ new Map();
632
631
  function use(arg1, arg2) {
633
632
  const token = Symbol("instruction");
634
- if (Model.is(arg1)) {
635
- const Type = arg1;
636
- arg1 = (key2, subject) => {
637
- function set2(next) {
638
- if (next ? !(next instanceof Type) : arg2 !== false)
639
- throw new Error(`${subject}.${key2} expected Model of type ${Type} but got ${next && next.constructor}.`);
640
- update(subject, key2, next);
641
- if (next && typeof arg2 == "function")
642
- arg2(next);
643
- return false;
644
- }
645
- const value = new Type();
646
- set2(value);
647
- PARENT.set(value, subject);
648
- return { set: set2 };
649
- };
650
- }
651
- INSTRUCT.set(token, arg1);
633
+ if (Model.is(arg1))
634
+ arg1 = child(arg1, arg2);
635
+ INSTRUCTION.set(token, arg1);
652
636
  return token;
653
637
  }
654
- Model.on((_, model) => {
655
- const state = STATE.get(model);
656
- for (const key2 in model) {
657
- const { value } = Object.getOwnPropertyDescriptor(model, key2);
658
- const instruction = INSTRUCT.get(value);
638
+ function child(type, arg2) {
639
+ return (key2, subject) => {
640
+ function set2(next) {
641
+ if (next ? !(next instanceof type) : arg2 !== false)
642
+ throw new Error(`${subject}.${key2} expected Model of type ${type} but got ${next && next.constructor}.`);
643
+ update(subject, key2, next);
644
+ if (next && typeof arg2 == "function")
645
+ arg2(next);
646
+ return false;
647
+ }
648
+ const value = new type();
649
+ set2(value);
650
+ PARENT.set(value, subject);
651
+ value.set();
652
+ return { set: set2 };
653
+ };
654
+ }
655
+ function init2() {
656
+ const state = STATE.get(this);
657
+ for (const key2 in this) {
658
+ const { value } = Object.getOwnPropertyDescriptor(this, key2);
659
+ const instruction = INSTRUCTION.get(value);
659
660
  if (!instruction) {
660
- mayAdopt(model, value);
661
+ mayAdopt(this, value);
661
662
  continue;
662
663
  }
663
- INSTRUCT.delete(value);
664
- delete model[key2];
665
- const output = instruction.call(model, key2, model, state);
664
+ INSTRUCTION.delete(value);
665
+ delete this[key2];
666
+ const output = instruction.call(this, key2, this, state);
666
667
  if (!output)
667
668
  continue;
668
669
  const desc = typeof output == "object" ? output : { get: output };
669
670
  if ("value" in desc)
670
671
  state[key2] = desc.value;
672
+ const model = this;
671
673
  Object.defineProperty(model, key2, {
672
674
  enumerable: desc.enumerable !== false,
673
675
  get() {
@@ -685,7 +687,8 @@ Model.on((_, model) => {
685
687
  });
686
688
  }
687
689
  return null;
688
- });
690
+ }
691
+ Model.on(init2);
689
692
 
690
693
  // src/instruction/get.ts
691
694
  var STALE = /* @__PURE__ */ new WeakSet();
@@ -694,27 +697,29 @@ function get(arg0, arg1) {
694
697
  if (typeof arg0 == "symbol")
695
698
  throw new Error(`Attempted to use an instruction result (probably use or get) as computed source for ${subject}.${key2}. This is not allowed.`);
696
699
  if (Model.is(arg0)) {
697
- let assign2 = function(value) {
700
+ let assign3 = function(value) {
698
701
  if (typeof arg1 == "function")
699
702
  value.get((x) => arg1(x, key2));
700
703
  update(subject, key2, value);
701
704
  };
702
- var assign = assign2;
705
+ var assign2 = assign3;
703
706
  const hasParent = PARENT.get(subject);
704
- if (!hasParent) {
705
- if (arg1 === true)
706
- throw new Error(`${subject} may only exist as a child of type ${arg0}.`);
707
- Context.get(subject, (context2) => {
708
- const model = context2.get(arg0);
709
- if (model)
710
- assign2(model);
711
- else if (arg1 !== false)
712
- throw new Error(`Required ${arg0} not found in context for ${subject}.`);
713
- });
714
- } else if (!arg0 || hasParent instanceof arg0)
715
- assign2(hasParent);
716
- else
717
- throw new Error(`New ${subject} created as child of ${hasParent}, but must be instanceof ${arg0}.`);
707
+ if (!hasParent && arg1 === true)
708
+ throw new Error(`${subject} may only exist as a child of type ${arg0}.`);
709
+ else if (hasParent) {
710
+ if (hasParent instanceof arg0) {
711
+ assign3(hasParent);
712
+ return {};
713
+ } else if (arg1 === true)
714
+ throw new Error(`New ${subject} created as child of ${hasParent}, but must be instanceof ${arg0}.`);
715
+ }
716
+ Context.get(subject, (context2) => {
717
+ const model = context2.get(arg0);
718
+ if (model)
719
+ assign3(model);
720
+ else if (arg1 !== false)
721
+ throw new Error(`Required ${arg0} not found in context for ${subject}.`);
722
+ });
718
723
  return {
719
724
  get: arg1 !== false
720
725
  };
@@ -790,8 +795,13 @@ function ref(arg, arg2) {
790
795
  const set2 = (value2) => {
791
796
  update(subject, key3, value2);
792
797
  };
793
- defineProperty(set2, "current", { get: get2, set: set2 });
794
- defineProperty(set2, "get", { value: get2 });
798
+ defineProperties(set2, {
799
+ current: { get: get2, set: set2 },
800
+ get: { value: get2 },
801
+ on: {
802
+ value: (callback) => addListener(subject, () => callback(state[key3]), key3)
803
+ }
804
+ });
795
805
  defineProperty(value, key3, { value: set2 });
796
806
  }
797
807
  });
@@ -819,7 +829,10 @@ function ref(arg, arg2) {
819
829
  state[key2] = null;
820
830
  value = defineProperties(set2, {
821
831
  current: { get: get2, set: set2 },
822
- get: { value: get2 }
832
+ get: { value: get2 },
833
+ on: {
834
+ value: (callback) => addListener(subject, () => callback(state[key2]), key2)
835
+ }
823
836
  });
824
837
  }
825
838
  defineProperty(subject, key2, { value });
@@ -831,7 +844,7 @@ function set(value, argument) {
831
844
  return use((key2, subject) => {
832
845
  const property = {};
833
846
  if (typeof value == "function" || value instanceof Promise) {
834
- let init3 = function() {
847
+ let init4 = function() {
835
848
  if (typeof value == "function")
836
849
  try {
837
850
  value = attempt(value.bind(subject, key2));
@@ -855,11 +868,11 @@ function set(value, argument) {
855
868
  if (0 in arguments)
856
869
  return subject[key2];
857
870
  };
858
- var init2 = init3;
871
+ var init3 = init4;
859
872
  if (argument)
860
- init3();
873
+ init4();
861
874
  else
862
- property.get = init3;
875
+ property.get = init4;
863
876
  } else if (value !== void 0)
864
877
  property.value = value;
865
878
  if (typeof argument == "function") {