@expressive/mvc 0.6.2 → 0.7.1

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
  *
@@ -298,9 +286,30 @@ declare namespace ref {
298
286
  }
299
287
  /** Object with references to all managed values of `T`. */
300
288
  type Proxy<T extends Model> = {
301
- [P in Model.Field<T>]: Model.Ref<T[P]>;
289
+ [P in Model.Field<T>]-?: Model.Ref<T[P]>;
290
+ };
291
+ type CustomProxy<T extends Model, R> = {
292
+ [P in Model.Field<T>]-?: R;
302
293
  };
303
294
  }
295
+ /**
296
+ * Creates an object with references to all managed values.
297
+ * Each property is a function to set value in state when invoked.
298
+ *
299
+ * *Properties are simultaneously a ref-function and ref-object, use as needed.*
300
+ *
301
+ * @param target - Source model from which to reference values.
302
+ */
303
+ declare function ref<T extends Model>(target: T): ref.Proxy<T>;
304
+ /**
305
+ * Creates an object with values based on managed values.
306
+ * Each property will invoke mapper function on first access supply
307
+ * the its return value going forward.
308
+ *
309
+ * @param target - Source model from which to reference values.
310
+ * @param mapper - Function producing the placeholder value for any given property.
311
+ */
312
+ declare function ref<O extends Model, R>(target: O, mapper: (key: Model.Field<O>) => R): ref.CustomProxy<O, R>;
304
313
  /**
305
314
  * Creates a ref-compatible property.
306
315
  * Will persist value, and updates to this are made part of controller event-stream.
@@ -533,13 +542,13 @@ declare namespace Provider {
533
542
  declare function Provider<T extends Provider.Item>(props: Provider.Props<T>): React.FunctionComponentElement<React.ProviderProps<Lookup>>;
534
543
 
535
544
  declare class Global extends MVC {
536
- static create<T extends Class>(this: T, ...args: any[]): InstanceOf<T>;
545
+ static create<T extends Class>(this: T, ...args: ConstructorParameters<T>): InstanceOf<T>;
537
546
  /**
538
547
  * Update the active instance of this class.
539
548
  * Returns a thenable; resolves after successful update.
540
549
  * If instance does not already exist, one will be created.
541
550
  */
542
- static set<T extends typeof Global>(this: T, updates: Partial<InstanceOf<T>>): Promise<false>;
551
+ static set<T extends typeof Global>(this: T, updates: Partial<InstanceOf<T>>): any;
543
552
  /**
544
553
  * **React Hook** - Fetch most instance of this controller from context, if it exists.
545
554
  *
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,34 @@ function get(arg0, arg1, arg2) {
813
798
  });
814
799
  }
815
800
 
816
- function ref(callback) {
801
+ function ref(arg, mapper) {
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
+ value = createRef(this, key, arg);
806
+ else
807
+ ensure(arg).state.forEach((_val, key) => {
808
+ defineProperty(value, key, mapper ? {
809
+ configurable: true,
810
+ get() {
811
+ const out = mapper(key);
812
+ defineProperty(value, key, { value: out });
813
+ return out;
814
+ }
815
+ } : {
816
+ value: createRef(this, key)
817
+ });
818
+ });
819
+ return { value, explicit: true };
827
820
  });
821
+ }
822
+ function createRef(src, key, cb) {
823
+ const refObjectFunction = src.ref(key, cb && createValueEffect(cb));
824
+ defineProperty(refObjectFunction, "current", {
825
+ set: refObjectFunction,
826
+ get: () => src.state.get(key)
827
+ });
828
+ return refObjectFunction;
828
829
  }
829
830
 
830
831
  function set(value, argument) {
@@ -999,9 +1000,11 @@ function use$1(input, argument) {
999
1000
  let pending;
1000
1001
  let error;
1001
1002
  let get = (_local) => state.get(key);
1002
- if (typeof input === "function")
1003
- input = /^[A-Z]/.test(input.name)
1004
- ? new input() : input;
1003
+ if (typeof input === "function") {
1004
+ if ("prototype" in input &&
1005
+ input === input.prototype.constructor)
1006
+ input = new input();
1007
+ }
1005
1008
  else if (input && typeof input !== "object")
1006
1009
  throw Oops$6.BadArgument(typeof input);
1007
1010
  if (input instanceof Map || input instanceof Set)
@@ -1470,11 +1473,11 @@ const Oops = issues({
1470
1473
  const Active = new WeakMap();
1471
1474
  class Global extends MVC {
1472
1475
  static create(...args) {
1473
- const Type = this;
1474
- if (Active.has(Type))
1476
+ if (Active.has(this))
1475
1477
  throw Oops.AlreadyExists(this.name);
1476
- const instance = super.create(...args);
1477
- Active.set(Type, instance);
1478
+ const instance = new this(...args);
1479
+ ensure(instance);
1480
+ Active.set(this, instance);
1478
1481
  return instance;
1479
1482
  }
1480
1483
  /**