@expressive/mvc 0.50.1 → 0.51.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/dist/index.d.ts CHANGED
@@ -17,7 +17,7 @@ declare namespace Model {
17
17
  type Field<T> = Exclude<keyof T, keyof Model>;
18
18
  /** Any valid key for controller, including but not limited to Field<T>. */
19
19
  type Event<T> = Field<T> | (string & {}) | number | symbol;
20
- /** Object overlay which may be used to override values and methods on a model. */
20
+ /** Object overlay to override values and methods on a model. */
21
21
  type Assign<T> = {
22
22
  [K in Exclude<keyof T, keyof Model>]?: T[K] extends (...args: infer A) => infer R ? (this: T, ...args: A) => R : T[K];
23
23
  } & Record<string, unknown>;
@@ -67,12 +67,13 @@ declare namespace Model {
67
67
  * Property initializer, will run upon instance creation.
68
68
  * Optional returned callback will run when once upon first access.
69
69
  */
70
- type Instruction<T = any, M extends Model = any> = (this: M, key: Field<M> & string, thisArg: M, state: State<M>) => Descriptor<T> | Getter<T> | void;
70
+ type Instruction<T = any, M extends Model = any> = (this: M, key: Extract<Field<M>, string>, thisArg: M, state: State<M>) => Descriptor<T> | ((source: M) => T) | void;
71
+ type VoidInstruction<M extends Model = any> = (this: M, key: Extract<Field<M>, string>, thisArg: M, state: State<M>) => ((source: Model) => void) | void;
71
72
  type Getter<T> = (source: Model) => T;
72
73
  type Setter<T> = (value: T, previous: T) => boolean | void | (() => T);
73
74
  type Descriptor<T = any> = {
74
75
  get?: Getter<T> | boolean;
75
- set?: Setter<T> | false;
76
+ set?: Setter<T> | boolean;
76
77
  enumerable?: boolean;
77
78
  value?: T;
78
79
  };
@@ -177,12 +178,15 @@ declare abstract class Model {
177
178
  * @param silent - if true, will not notify listeners of an update
178
179
  */
179
180
  set<K extends string>(key: K, value?: Model.Value<this, K>, silent?: boolean): PromiseLike<Model.Event<this>[]> | undefined;
180
- /** Iterate over managed properties in this instance of Model. */
181
+ /**
182
+ * Iterate over managed properties in this instance of Model.
183
+ * Yeilds the key and underlying value for each property.
184
+ */
181
185
  [Symbol.iterator](): Iterator<[string, unknown]>;
182
186
  /**
183
- * Creates a new instance of this controller.
187
+ * Create and activate a new instance of this controller.
184
188
  *
185
- * **Important** - Use instead of `new this(...)` as this method also activates state.
189
+ * **Important** - Unlike `new this(...)` - this method also activates state.
186
190
  *
187
191
  * @param args - arguments sent to constructor
188
192
  */
@@ -343,7 +347,8 @@ declare function set<T>(factory: set.Factory<T> | Promise<T>, required?: boolean
343
347
  */
344
348
  declare function set<T>(value: T | undefined, onUpdate: set.Callback<T>): T;
345
349
 
346
- declare function use<T = any>(instruction: Model.Instruction): T;
350
+ declare function use(instruction: Model.VoidInstruction): unknown;
351
+ declare function use<T>(instruction: Model.Instruction<T>): T;
347
352
  declare function use<T extends Model>(Type: Model.Init<T>, required: false): T | undefined;
348
353
  declare function use<T extends Model>(Type: Model.Init<T>, ready?: (i: T) => void): T;
349
354
 
package/dist/index.js CHANGED
@@ -155,13 +155,13 @@ function effect(target, callback, requireValues) {
155
155
  }
156
156
 
157
157
  // src/model.ts
158
- var EXPORT;
159
158
  var NAMES = /* @__PURE__ */ new WeakMap();
160
159
  var STATE = /* @__PURE__ */ new WeakMap();
161
160
  var NOTIFY = /* @__PURE__ */ new WeakMap();
162
161
  var PENDING2 = /* @__PURE__ */ new WeakMap();
163
162
  var PARENT = /* @__PURE__ */ new WeakMap();
164
163
  var METHOD = /* @__PURE__ */ new WeakMap();
164
+ var EXPORT;
165
165
  var define = Object.defineProperty;
166
166
  var Model = class _Model {
167
167
  constructor(...args) {
@@ -271,8 +271,10 @@ var Model = class _Model {
271
271
  if (typeof key2 == "string")
272
272
  return arg1.call(self, key2, self);
273
273
  });
274
- if (arg1 === null)
275
- return emit(this, null);
274
+ if (arg1 === null) {
275
+ emit(this, null);
276
+ return;
277
+ }
276
278
  if (typeof arg1 == "object")
277
279
  assign(self, arg1);
278
280
  else if (arg1 == void 0)
@@ -293,14 +295,17 @@ var Model = class _Model {
293
295
  }).then(res)
294
296
  };
295
297
  }
296
- /** Iterate over managed properties in this instance of Model. */
298
+ /**
299
+ * Iterate over managed properties in this instance of Model.
300
+ * Yeilds the key and underlying value for each property.
301
+ */
297
302
  [Symbol.iterator]() {
298
303
  return Object.entries(STATE.get(this.is))[Symbol.iterator]();
299
304
  }
300
305
  /**
301
- * Creates a new instance of this controller.
306
+ * Create and activate a new instance of this controller.
302
307
  *
303
- * **Important** - Use instead of `new this(...)` as this method also activates state.
308
+ * **Important** - Unlike `new this(...)` - this method also activates state.
304
309
  *
305
310
  * @param args - arguments sent to constructor
306
311
  */
@@ -574,7 +579,10 @@ function use(arg1, arg2) {
574
579
  if (Model.is(arg1)) {
575
580
  const type = arg1;
576
581
  arg1 = (key2, subject) => {
577
- const desc = { set: set2, get: get2 };
582
+ const value = new type();
583
+ const desc = { set: set2 };
584
+ set2(value);
585
+ PARENT.set(value, subject);
578
586
  function set2(next) {
579
587
  if (next ? !(next instanceof type) : arg2 !== false)
580
588
  throw new Error(`${subject}.${key2} expected Model of type ${type} but got ${next && next.constructor}.`);
@@ -583,11 +591,6 @@ function use(arg1, arg2) {
583
591
  arg2(next);
584
592
  return false;
585
593
  }
586
- function get2() {
587
- set2(new type());
588
- desc.get = arg2 !== false;
589
- return fetch(subject, key2);
590
- }
591
594
  return desc;
592
595
  };
593
596
  }
package/dist/index.mjs CHANGED
@@ -121,13 +121,13 @@ function effect(target, callback, requireValues) {
121
121
  }
122
122
 
123
123
  // src/model.ts
124
- var EXPORT;
125
124
  var NAMES = /* @__PURE__ */ new WeakMap();
126
125
  var STATE = /* @__PURE__ */ new WeakMap();
127
126
  var NOTIFY = /* @__PURE__ */ new WeakMap();
128
127
  var PENDING2 = /* @__PURE__ */ new WeakMap();
129
128
  var PARENT = /* @__PURE__ */ new WeakMap();
130
129
  var METHOD = /* @__PURE__ */ new WeakMap();
130
+ var EXPORT;
131
131
  var define = Object.defineProperty;
132
132
  var Model = class _Model {
133
133
  constructor(...args) {
@@ -237,8 +237,10 @@ var Model = class _Model {
237
237
  if (typeof key2 == "string")
238
238
  return arg1.call(self, key2, self);
239
239
  });
240
- if (arg1 === null)
241
- return emit(this, null);
240
+ if (arg1 === null) {
241
+ emit(this, null);
242
+ return;
243
+ }
242
244
  if (typeof arg1 == "object")
243
245
  assign(self, arg1);
244
246
  else if (arg1 == void 0)
@@ -259,14 +261,17 @@ var Model = class _Model {
259
261
  }).then(res)
260
262
  };
261
263
  }
262
- /** Iterate over managed properties in this instance of Model. */
264
+ /**
265
+ * Iterate over managed properties in this instance of Model.
266
+ * Yeilds the key and underlying value for each property.
267
+ */
263
268
  [Symbol.iterator]() {
264
269
  return Object.entries(STATE.get(this.is))[Symbol.iterator]();
265
270
  }
266
271
  /**
267
- * Creates a new instance of this controller.
272
+ * Create and activate a new instance of this controller.
268
273
  *
269
- * **Important** - Use instead of `new this(...)` as this method also activates state.
274
+ * **Important** - Unlike `new this(...)` - this method also activates state.
270
275
  *
271
276
  * @param args - arguments sent to constructor
272
277
  */
@@ -540,7 +545,10 @@ function use(arg1, arg2) {
540
545
  if (Model.is(arg1)) {
541
546
  const type = arg1;
542
547
  arg1 = (key2, subject) => {
543
- const desc = { set: set2, get: get2 };
548
+ const value = new type();
549
+ const desc = { set: set2 };
550
+ set2(value);
551
+ PARENT.set(value, subject);
544
552
  function set2(next) {
545
553
  if (next ? !(next instanceof type) : arg2 !== false)
546
554
  throw new Error(`${subject}.${key2} expected Model of type ${type} but got ${next && next.constructor}.`);
@@ -549,11 +557,6 @@ function use(arg1, arg2) {
549
557
  arg2(next);
550
558
  return false;
551
559
  }
552
- function get2() {
553
- set2(new type());
554
- desc.get = arg2 !== false;
555
- return fetch(subject, key2);
556
- }
557
560
  return desc;
558
561
  };
559
562
  }
package/package.json CHANGED
@@ -28,6 +28,10 @@
28
28
  "prebuild": "rm -rf dist",
29
29
  "preversion": "npm run test && npm run build"
30
30
  },
31
- "version": "0.50.1",
32
- "gitHead": "c74ee393d29e18ae4c4bc59bf73834ac21fb2b6b"
31
+ "devDependencies": {
32
+ "jest": "^27.0.6",
33
+ "tsup": "^8.0.2"
34
+ },
35
+ "version": "0.51.1",
36
+ "gitHead": "b5169ea695a97e020e0266f84db55ab4a12fa3b5"
33
37
  }