@expressive/mvc 0.6.3 → 0.7.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/lib/index.d.ts CHANGED
@@ -53,11 +53,6 @@ declare namespace Model {
53
53
  export type Methods<T> = {
54
54
  [K in keyof T]: T[K] extends Ref ? never : T[K] extends Function ? K : never;
55
55
  }[keyof T];
56
- export type Refs<T extends Model> = {
57
- <K extends Field<T>>(key: K, value: T[K]): void;
58
- } & {
59
- [P in Field<T>]: (next: any) => void;
60
- };
61
56
  /**
62
57
  * Subset of `keyof T` which are not methods or defined by base Model U.
63
58
  *
@@ -92,13 +87,6 @@ interface Model extends Stateful {
92
87
  * Useful to obtain full reference where one has already destructured.
93
88
  */
94
89
  is: this;
95
- /**
96
- * Setter utility
97
- *
98
- * Can be used to set (and force-update) values.
99
- * Shortcut to update values, contains refs for all controlled values.
100
- */
101
- set: Model.Refs<this>;
102
90
  }
103
91
  declare class Model {
104
92
  static [WHY]: readonly string[];
@@ -145,7 +133,7 @@ declare class Model {
145
133
  *
146
134
  * @param args - arguments sent to constructor
147
135
  */
148
- static create<T extends Class>(this: T, ...args: any[]): InstanceOf<T>;
136
+ static create<T extends Class>(this: T, ...args: ConstructorParameters<T>): InstanceOf<T>;
149
137
  /**
150
138
  * Static equivalent of `x instanceof this`.
151
139
  *
@@ -301,6 +289,15 @@ declare namespace ref {
301
289
  [P in Model.Field<T>]: Model.Ref<T[P]>;
302
290
  };
303
291
  }
292
+ /**
293
+ * Creates an object with references to all managed values.
294
+ * Each property will set value in state when invoked.
295
+ *
296
+ * *Properties are simultaneously a ref-function and ref-object, use as needed.*
297
+ *
298
+ * @param target - Source model from which to reference values.
299
+ */
300
+ declare function ref<T extends Model>(target: T): ref.Proxy<T>;
304
301
  /**
305
302
  * Creates a ref-compatible property.
306
303
  * Will persist value, and updates to this are made part of controller event-stream.
@@ -533,13 +530,13 @@ declare namespace Provider {
533
530
  declare function Provider<T extends Provider.Item>(props: Provider.Props<T>): React.FunctionComponentElement<React.ProviderProps<Lookup>>;
534
531
 
535
532
  declare class Global extends MVC {
536
- static create<T extends Class>(this: T, ...args: any[]): InstanceOf<T>;
533
+ static create<T extends Class>(this: T, ...args: ConstructorParameters<T>): InstanceOf<T>;
537
534
  /**
538
535
  * Update the active instance of this class.
539
536
  * Returns a thenable; resolves after successful update.
540
537
  * If instance does not already exist, one will be created.
541
538
  */
542
- static set<T extends typeof Global>(this: T, updates: Partial<InstanceOf<T>>): Promise<false>;
539
+ static set<T extends typeof Global>(this: T, updates: Partial<InstanceOf<T>>): any;
543
540
  /**
544
541
  * **React Hook** - Fetch most instance of this controller from context, if it exists.
545
542
  *
package/lib/index.esm.js CHANGED
@@ -196,22 +196,6 @@ class Model {
196
196
  defineProperty(this, "is", {
197
197
  value: this
198
198
  });
199
- defineProperty(this, "set", {
200
- configurable: true,
201
- get: () => {
202
- const controller = ensure(this);
203
- const value = (key, value) => {
204
- controller.state.set(key, value);
205
- controller.update(key);
206
- };
207
- for (const key of controller.state.keys())
208
- defineProperty(value, key, {
209
- value: controller.ref(key)
210
- });
211
- defineProperty(this, "set", { value });
212
- return value;
213
- }
214
- });
215
199
  }
216
200
  get [STATE]() {
217
201
  return this.export();
@@ -474,8 +458,11 @@ class Controller {
474
458
  return;
475
459
  if (typeof value == "symbol") {
476
460
  const instruction = PENDING.get(value);
477
- if (instruction)
461
+ if (instruction) {
462
+ delete subject[key];
463
+ PENDING.delete(value);
478
464
  instruction.call(this, key, this);
465
+ }
479
466
  return;
480
467
  }
481
468
  state.set(key, value);
@@ -579,9 +566,6 @@ function apply(fn, label) {
579
566
  const placeholder = Symbol(`${name} instruction`);
580
567
  function setup(key) {
581
568
  const { subject, state } = this;
582
- const control = this;
583
- PENDING.delete(placeholder);
584
- delete subject[key];
585
569
  let output = fn.call(this, key, this);
586
570
  if (typeof output == "function")
587
571
  output = { get: output };
@@ -604,6 +588,7 @@ function apply(fn, label) {
604
588
  destroy();
605
589
  });
606
590
  }
591
+ const control = this;
607
592
  defineProperty(subject, key, {
608
593
  enumerable: output.enumerable,
609
594
  set: onSet !== false
@@ -797,7 +782,7 @@ function get(arg0, arg1, arg2) {
797
782
  throw new Error(`Factory argument cannot be ${arg1}`);
798
783
  getter = computeMode(this, source, setter, key, computeRequired);
799
784
  };
800
- if (typeof arg0 == "function" && factoryRequired)
785
+ if (typeof arg0 == "function" && computeRequired)
801
786
  try {
802
787
  init();
803
788
  }
@@ -813,18 +798,28 @@ function get(arg0, arg1, arg2) {
813
798
  });
814
799
  }
815
800
 
816
- function ref(callback) {
801
+ function ref(arg) {
817
802
  return apply(function ref(key) {
818
- const value = this.ref(key, callback && createValueEffect(callback));
819
- defineProperty(value, "current", {
820
- set: value,
821
- get: () => this.state.get(key)
822
- });
823
- return {
824
- value,
825
- explicit: true
826
- };
803
+ let value = {};
804
+ if (typeof arg == "object") {
805
+ const source = ensure(arg);
806
+ for (const key of source.state.keys())
807
+ defineProperty(value, key, {
808
+ value: createRef(this, key)
809
+ });
810
+ }
811
+ else
812
+ value = createRef(this, key, arg);
813
+ return { value, explicit: true };
814
+ });
815
+ }
816
+ function createRef(src, key, cb) {
817
+ const refObjectFunction = src.ref(key, cb && createValueEffect(cb));
818
+ defineProperty(refObjectFunction, "current", {
819
+ set: refObjectFunction,
820
+ get: () => src.state.get(key)
827
821
  });
822
+ return refObjectFunction;
828
823
  }
829
824
 
830
825
  function set(value, argument) {
@@ -1472,11 +1467,11 @@ const Oops = issues({
1472
1467
  const Active = new WeakMap();
1473
1468
  class Global extends MVC {
1474
1469
  static create(...args) {
1475
- const Type = this;
1476
- if (Active.has(Type))
1470
+ if (Active.has(this))
1477
1471
  throw Oops.AlreadyExists(this.name);
1478
- const instance = super.create(...args);
1479
- Active.set(Type, instance);
1472
+ const instance = new this(...args);
1473
+ ensure(instance);
1474
+ Active.set(this, instance);
1480
1475
  return instance;
1481
1476
  }
1482
1477
  /**