@expressive/mvc 0.6.1 → 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
@@ -752,7 +737,8 @@ function factoryMode(self, output, key, required) {
752
737
  }
753
738
 
754
739
  const Oops$7 = issues({
755
- BadSource: (model, property, got) => `Bad from-instruction provided to ${model}.${property}. Expects an arrow-function or a Model as source. Got ${got}.`,
740
+ // BadSource: (model, property, got) =>
741
+ // `Bad from-instruction provided to ${model}.${property}. Expects an arrow-function or a Model as source. Got ${got}.`,
756
742
  PeerNotAllowed: (model, property) => `Attempted to use an instruction result (probably use or tap) as computed source for ${model}.${property}. This is not possible.`,
757
743
  ComputeFailed: (model, key) => `Generating initial value for ${model}.${key} failed.`,
758
744
  NotReady: (model, key) => `Value ${model}.${key} value is not yet available.`,
@@ -768,8 +754,8 @@ function get(arg0, arg1, arg2) {
768
754
  // Easy mistake, using a peer, will always be unresolved.
769
755
  if (typeof arg0 == "symbol")
770
756
  throw Oops$7.PeerNotAllowed(subject, key);
771
- if (typeof arg0 == "function" && /^[A-Z]/.test(arg0.name))
772
- throw Oops$7.BadSource(subject, key, arg0);
757
+ // if(typeof arg0 == "function" && /^[A-Z]/.test(arg0.name))
758
+ // throw Oops.BadSource(subject, key, arg0);
773
759
  this.state.set(key, undefined);
774
760
  const init = () => {
775
761
  let source = this;
@@ -796,7 +782,7 @@ function get(arg0, arg1, arg2) {
796
782
  throw new Error(`Factory argument cannot be ${arg1}`);
797
783
  getter = computeMode(this, source, setter, key, computeRequired);
798
784
  };
799
- if (typeof arg0 == "function" && factoryRequired)
785
+ if (typeof arg0 == "function" && computeRequired)
800
786
  try {
801
787
  init();
802
788
  }
@@ -812,18 +798,28 @@ function get(arg0, arg1, arg2) {
812
798
  });
813
799
  }
814
800
 
815
- function ref(callback) {
801
+ function ref(arg) {
816
802
  return apply(function ref(key) {
817
- const value = this.ref(key, callback && createValueEffect(callback));
818
- defineProperty(value, "current", {
819
- set: value,
820
- get: () => this.state.get(key)
821
- });
822
- return {
823
- value,
824
- explicit: true
825
- };
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 };
826
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)
821
+ });
822
+ return refObjectFunction;
827
823
  }
828
824
 
829
825
  function set(value, argument) {
@@ -998,9 +994,11 @@ function use$1(input, argument) {
998
994
  let pending;
999
995
  let error;
1000
996
  let get = (_local) => state.get(key);
1001
- if (typeof input === "function")
1002
- input = /^[A-Z]/.test(input.name)
1003
- ? new input() : input;
997
+ if (typeof input === "function") {
998
+ if ("prototype" in input &&
999
+ input === input.prototype.constructor)
1000
+ input = new input();
1001
+ }
1004
1002
  else if (input && typeof input !== "object")
1005
1003
  throw Oops$6.BadArgument(typeof input);
1006
1004
  if (input instanceof Map || input instanceof Set)
@@ -1469,11 +1467,11 @@ const Oops = issues({
1469
1467
  const Active = new WeakMap();
1470
1468
  class Global extends MVC {
1471
1469
  static create(...args) {
1472
- const Type = this;
1473
- if (Active.has(Type))
1470
+ if (Active.has(this))
1474
1471
  throw Oops.AlreadyExists(this.name);
1475
- const instance = super.create(...args);
1476
- Active.set(Type, instance);
1472
+ const instance = new this(...args);
1473
+ ensure(instance);
1474
+ Active.set(this, instance);
1477
1475
  return instance;
1478
1476
  }
1479
1477
  /**