@expressive/mvc 0.43.3 → 0.44.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.d.ts CHANGED
@@ -1,15 +1,15 @@
1
1
  declare namespace Model {
2
2
  /** Any type of Model, using own class constructor as its identifier. */
3
- type Type<T extends Model = Model> = abstract new (...args: any[]) => T;
4
- /** A type of Model which may be created without constructor arguments. */
5
- type New<T extends Model = Model> = (new (arg?: Argument) => T) & typeof Model;
3
+ type Type<T extends Model = Model> = (abstract new (...args: any[]) => T) & typeof Model;
6
4
  /**
7
5
  * Model constructor callback - is called when Model finishes intializing.
8
6
  * Returned function will call when model is destroyed.
9
7
  */
10
8
  type Callback<T extends Model = Model> = (this: T, thisArg: T) => (() => void) | Assign<T> | Promise<void> | void;
11
9
  /** Model constructor argument */
12
- type Argument<T extends Model = Model> = string | Callback<T> | Assign<T> | undefined;
10
+ type Argument<T extends Model = Model> = string | Callback<T> | Assign<T> | void;
11
+ /** Model constructor arguments */
12
+ type Args<T extends Model = any> = Argument<T>[];
13
13
  /** Subset of `keyof T` which are not methods or defined by base Model U. **/
14
14
  type Field<T> = Exclude<keyof T, keyof Model>;
15
15
  /** Any valid key for controller, including but not limited to Field<T>. */
@@ -84,7 +84,7 @@ interface Model {
84
84
  is: this;
85
85
  }
86
86
  declare class Model {
87
- constructor(arg?: Model.Argument);
87
+ constructor(...args: Model.Args);
88
88
  /** Pull current values from state. Flattens all models and exotic values recursively. */
89
89
  get(): Model.State<this>;
90
90
  /**
@@ -185,7 +185,7 @@ declare class Model {
185
185
  *
186
186
  * @param args - arguments sent to constructor
187
187
  */
188
- static new<T extends Model>(this: Model.New<T>, arg?: Model.Argument<T>): T;
188
+ static new<T extends Model>(this: Model.Type<T>, ...args: Model.Args<T>): T;
189
189
  /**
190
190
  * Static equivalent of `x instanceof this`.
191
191
  * Determines if provided class is a subtype of this one.
@@ -345,13 +345,10 @@ declare function set<T>(factory: set.Factory<T> | Promise<T>, required?: boolean
345
345
  */
346
346
  declare function set<T>(value: T | undefined, onUpdate: set.Callback<T>): T;
347
347
 
348
- declare namespace use {
349
- type Object<T extends {}> = T;
350
- }
351
348
  /** Create a placeholder for specified Model type. */
352
349
  declare function use<T extends Model>(): T | undefined;
353
350
  /** Create a new child instance of model. */
354
- declare function use<T extends Model>(Type: Model.New<T>, ready?: (i: T) => void): T;
351
+ declare function use<T extends Model>(Type: Model.Type<T>, ready?: (i: T) => void): T;
355
352
  /**
356
353
  * Use existing model as a child of model assigned to.
357
354
  *
@@ -361,8 +358,6 @@ declare function use<T extends Model>(Type: Model.New<T>, ready?: (i: T) => void
361
358
  * already active.
362
359
  **/
363
360
  declare function use<T extends Model>(model: T, ready?: (i: T) => void): T;
364
- /** Create a managed object with observable entries. */
365
- declare function use<T extends {}, O = use.Object<T>>(data: T, ready?: (object: O) => void): O;
366
361
 
367
362
  declare namespace has {
368
363
  type Callback<T = any> = (model: T) => void | boolean | (() => void);
@@ -372,7 +367,7 @@ declare function has<T extends Model>(type: Model.Type<T>, required: boolean): T
372
367
  declare function has<T extends Model>(type: Model.Type<T>, arg?: has.Callback<T>): Set<T>;
373
368
 
374
369
  declare namespace Context {
375
- type Input = Model | Model.New<Model> | Record<string | number, Model | Model.New<Model>>;
370
+ type Input = Model | Model.Type<Model> | Record<string | number, Model | Model.Type<Model>>;
376
371
  }
377
372
  declare class Context {
378
373
  static request<T extends Model>(type: Model.Type<T>, from: Model, callback: (got: T) => void): void;
@@ -385,8 +380,8 @@ declare class Context {
385
380
  has(model: Model): (() => void) | undefined;
386
381
  get<T extends Model>(Type: Model.Type<T>): T | undefined;
387
382
  include(inputs: Context.Input): Map<Model, boolean>;
388
- put<T extends Model>(T: Model.New<T>, I: T | ((model: T) => void), implicit?: boolean, writable?: boolean): void;
389
- add<T extends Model>(input: T | Model.New<T>, implicit?: boolean): T;
383
+ put<T extends Model>(T: Model.Type<T>, I: T | ((model: T) => void), implicit?: boolean, writable?: boolean): void;
384
+ add<T extends Model>(input: T | Model.Type<T>, implicit?: boolean): T;
390
385
  push(inputs?: Context.Input): this;
391
386
  pop(): void;
392
387
  }
package/dist/index.js CHANGED
@@ -157,13 +157,16 @@ var PARENT = /* @__PURE__ */ new WeakMap();
157
157
  var METHOD = /* @__PURE__ */ new WeakMap();
158
158
  var define = Object.defineProperty;
159
159
  var Model = class _Model {
160
- constructor(arg) {
160
+ constructor(...args) {
161
161
  const state = {};
162
162
  let Type = this.constructor;
163
163
  bindMethods(Type);
164
164
  define(this, "is", { value: this });
165
165
  STATE.set(this, state);
166
- NAMES.set(this, typeof arg == "string" ? arg : `${Type}-${uid()}`);
166
+ NAMES.set(this, `${Type}-${uid()}`);
167
+ for (const arg of args)
168
+ if (typeof arg == "string")
169
+ NAMES.set(this, arg);
167
170
  while (true) {
168
171
  new Set(NOTIFY.get(Type)).forEach((x) => addListener(this, x));
169
172
  if (Type === _Model)
@@ -171,14 +174,19 @@ var Model = class _Model {
171
174
  Type = Object.getPrototypeOf(Type);
172
175
  }
173
176
  addListener(this, () => {
174
- const apply = typeof arg == "function" ? arg.call(this, this) : arg;
175
- if (apply instanceof Promise)
176
- apply.catch((err) => {
177
- console.error(`Async error in constructor for ${this}:`);
178
- console.error(err);
179
- });
180
- else if (typeof apply == "object")
181
- assign(this, apply);
177
+ const cleanup = /* @__PURE__ */ new Set();
178
+ for (const arg of args) {
179
+ const use2 = typeof arg == "function" ? arg.call(this, this) : arg;
180
+ if (use2 instanceof Promise)
181
+ use2.catch((err) => {
182
+ console.error(`Async error in constructor for ${this}:`);
183
+ console.error(err);
184
+ });
185
+ else if (typeof use2 == "object")
186
+ assign(this, use2);
187
+ else if (typeof use2 == "function")
188
+ cleanup.add(use2);
189
+ }
182
190
  for (const key in this) {
183
191
  const desc = Object.getOwnPropertyDescriptor(this, key);
184
192
  if ("value" in desc) {
@@ -196,8 +204,7 @@ var Model = class _Model {
196
204
  for (const [_, value] of this)
197
205
  if (value instanceof _Model && PARENT.get(value) === this)
198
206
  value.set(null);
199
- if (typeof apply == "function")
200
- apply();
207
+ cleanup.forEach((x) => x());
201
208
  Object.freeze(state);
202
209
  }, null);
203
210
  return null;
@@ -281,8 +288,8 @@ var Model = class _Model {
281
288
  *
282
289
  * @param args - arguments sent to constructor
283
290
  */
284
- static new(arg) {
285
- const instance = new this(arg);
291
+ static new(...args) {
292
+ const instance = new this(...args);
286
293
  event(instance, true);
287
294
  return instance;
288
295
  }
@@ -664,36 +671,21 @@ function attempt(fn) {
664
671
 
665
672
  // src/instruction/use.ts
666
673
  function use(value, argument) {
667
- if (typeof value === "function")
668
- value = new value();
669
674
  return add((property, subject) => {
675
+ if (typeof value === "function")
676
+ value = new value();
670
677
  function set2(next) {
671
678
  if (value instanceof Model && !(next instanceof value.constructor))
672
679
  throw new Error(`${subject}.${property} expected Model of type ${value.constructor} but got ${next}.`);
673
680
  if (next instanceof Model) {
674
681
  PARENT.set(next, subject);
675
682
  event(next, true);
676
- } else if (next) {
677
- const proxy = Object.create(next);
678
- for (const key in proxy)
679
- Object.defineProperty(proxy, key, {
680
- enumerable: true,
681
- get: () => next[key],
682
- set(value2) {
683
- if (value2 != next[key]) {
684
- next[key] = value2;
685
- subject.set(property);
686
- }
687
- }
688
- });
689
- output.get = () => proxy;
690
683
  }
691
684
  if (typeof argument == "function")
692
685
  argument(next);
693
686
  }
694
- const output = { set: set2, value };
695
687
  set2(value);
696
- return output;
688
+ return { set: set2, value };
697
689
  });
698
690
  }
699
691
 
package/dist/index.mjs CHANGED
@@ -123,13 +123,16 @@ var PARENT = /* @__PURE__ */ new WeakMap();
123
123
  var METHOD = /* @__PURE__ */ new WeakMap();
124
124
  var define = Object.defineProperty;
125
125
  var Model = class _Model {
126
- constructor(arg) {
126
+ constructor(...args) {
127
127
  const state = {};
128
128
  let Type = this.constructor;
129
129
  bindMethods(Type);
130
130
  define(this, "is", { value: this });
131
131
  STATE.set(this, state);
132
- NAMES.set(this, typeof arg == "string" ? arg : `${Type}-${uid()}`);
132
+ NAMES.set(this, `${Type}-${uid()}`);
133
+ for (const arg of args)
134
+ if (typeof arg == "string")
135
+ NAMES.set(this, arg);
133
136
  while (true) {
134
137
  new Set(NOTIFY.get(Type)).forEach((x) => addListener(this, x));
135
138
  if (Type === _Model)
@@ -137,14 +140,19 @@ var Model = class _Model {
137
140
  Type = Object.getPrototypeOf(Type);
138
141
  }
139
142
  addListener(this, () => {
140
- const apply = typeof arg == "function" ? arg.call(this, this) : arg;
141
- if (apply instanceof Promise)
142
- apply.catch((err) => {
143
- console.error(`Async error in constructor for ${this}:`);
144
- console.error(err);
145
- });
146
- else if (typeof apply == "object")
147
- assign(this, apply);
143
+ const cleanup = /* @__PURE__ */ new Set();
144
+ for (const arg of args) {
145
+ const use2 = typeof arg == "function" ? arg.call(this, this) : arg;
146
+ if (use2 instanceof Promise)
147
+ use2.catch((err) => {
148
+ console.error(`Async error in constructor for ${this}:`);
149
+ console.error(err);
150
+ });
151
+ else if (typeof use2 == "object")
152
+ assign(this, use2);
153
+ else if (typeof use2 == "function")
154
+ cleanup.add(use2);
155
+ }
148
156
  for (const key in this) {
149
157
  const desc = Object.getOwnPropertyDescriptor(this, key);
150
158
  if ("value" in desc) {
@@ -162,8 +170,7 @@ var Model = class _Model {
162
170
  for (const [_, value] of this)
163
171
  if (value instanceof _Model && PARENT.get(value) === this)
164
172
  value.set(null);
165
- if (typeof apply == "function")
166
- apply();
173
+ cleanup.forEach((x) => x());
167
174
  Object.freeze(state);
168
175
  }, null);
169
176
  return null;
@@ -247,8 +254,8 @@ var Model = class _Model {
247
254
  *
248
255
  * @param args - arguments sent to constructor
249
256
  */
250
- static new(arg) {
251
- const instance = new this(arg);
257
+ static new(...args) {
258
+ const instance = new this(...args);
252
259
  event(instance, true);
253
260
  return instance;
254
261
  }
@@ -630,36 +637,21 @@ function attempt(fn) {
630
637
 
631
638
  // src/instruction/use.ts
632
639
  function use(value, argument) {
633
- if (typeof value === "function")
634
- value = new value();
635
640
  return add((property, subject) => {
641
+ if (typeof value === "function")
642
+ value = new value();
636
643
  function set2(next) {
637
644
  if (value instanceof Model && !(next instanceof value.constructor))
638
645
  throw new Error(`${subject}.${property} expected Model of type ${value.constructor} but got ${next}.`);
639
646
  if (next instanceof Model) {
640
647
  PARENT.set(next, subject);
641
648
  event(next, true);
642
- } else if (next) {
643
- const proxy = Object.create(next);
644
- for (const key in proxy)
645
- Object.defineProperty(proxy, key, {
646
- enumerable: true,
647
- get: () => next[key],
648
- set(value2) {
649
- if (value2 != next[key]) {
650
- next[key] = value2;
651
- subject.set(property);
652
- }
653
- }
654
- });
655
- output.get = () => proxy;
656
649
  }
657
650
  if (typeof argument == "function")
658
651
  argument(next);
659
652
  }
660
- const output = { set: set2, value };
661
653
  set2(value);
662
- return output;
654
+ return { set: set2, value };
663
655
  });
664
656
  }
665
657
 
package/package.json CHANGED
@@ -28,6 +28,6 @@
28
28
  "prebuild": "rm -rf dist",
29
29
  "preversion": "npm run test && npm run build"
30
30
  },
31
- "version": "0.43.3",
32
- "gitHead": "dfa7549a435a359fb682c34a7e1781c778a29d84"
31
+ "version": "0.44.0",
32
+ "gitHead": "b9a450ae7d25a8845d344faed39495bbdfc69b47"
33
33
  }