@expressive/mvc 0.41.0 → 0.42.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
@@ -2,19 +2,26 @@ declare namespace Model {
2
2
  /** Any type of Model, using own class constructor as its identifier. */
3
3
  type Type<T extends Model = Model> = abstract new (...args: any[]) => T;
4
4
  /** A type of Model which may be created without constructor arguments. */
5
- type New<T extends Model = Model> = (new () => T) & typeof Model;
5
+ type New<T extends Model = Model> = (new (arg?: Model.Argument) => T) & typeof Model;
6
+ /**
7
+ * Model constructor callback - is called when Model finishes intializing.
8
+ * Returned function will call when model is destroyed.
9
+ */
10
+ type Callback = (() => (() => void) | void);
11
+ /** Model constructor argument */
12
+ type Argument = string | Callback | undefined;
6
13
  /** Subset of `keyof T` which are not methods or defined by base Model U. **/
7
14
  type Field<T> = Exclude<keyof T, keyof Model>;
8
15
  /** Any valid key for controller, including but not limited to Field<T>. */
9
- type Key<T> = Field<T> | (string & {}) | number | symbol;
16
+ type Event<T> = Field<T> | (string & {}) | number | symbol;
10
17
  /** Export/Import compatible value for a given property in a Model. */
11
18
  type Export<R> = R extends {
12
19
  get(): infer T;
13
20
  } ? T : R;
14
21
  /** Value for a property managed by a controller. */
15
- type Value<T extends Model, K extends Key<T>> = K extends keyof T ? Export<T[K]> : unknown;
22
+ type Value<T extends Model, K extends Event<T>> = K extends keyof T ? Export<T[K]> : unknown;
16
23
  type OnEvent<T extends Model> = (this: T, key: unknown, source: T) => (() => void) | void | null;
17
- type OnUpdate<T extends Model, K extends Key<T>> = (this: T, value: Value<T, K>, key: K, thisArg: K) => void;
24
+ type OnUpdate<T extends Model, K extends Event<T>> = (this: T, value: Value<T, K>, key: K, thisArg: K) => void;
18
25
  /**
19
26
  * Values from current state of given controller.
20
27
  * Differs from `Values` as values here will drill
@@ -42,7 +49,7 @@ declare namespace Model {
42
49
  *
43
50
  * @returns A callback function which will be called when this effect is stale.
44
51
  */
45
- type Effect<T> = (this: T, current: T, update: Set<Model.Key<T>>) => EffectCallback | Promise<void> | null | void;
52
+ type Effect<T> = (this: T, current: T, update: Set<Model.Event<T>>) => EffectCallback | Promise<void> | null | void;
46
53
  /**
47
54
  * A callback function returned by effect. Will be called when effect is stale.
48
55
  *
@@ -64,13 +71,6 @@ declare namespace Model {
64
71
  * Optional returned callback will run when once upon first access.
65
72
  */
66
73
  type Instruction<T = any, M extends Model = any> = (this: M, key: Model.Field<M> & string, thisArg: M, state: Model.State<M>) => Instruction.Descriptor<T> | Instruction.Getter<T> | void;
67
- /**
68
- * Model constructor callback - is called when Model finishes intializing.
69
- * Returned function will call when model is destroyed.
70
- */
71
- type Callback = (() => void | (() => void));
72
- /** Model constructor argument */
73
- type Argument = string | Callback;
74
74
  }
75
75
  interface Model {
76
76
  /**
@@ -99,14 +99,14 @@ declare class Model {
99
99
  * @param key - Property to get value of.
100
100
  * @param required - If true, will throw an error if property is not available.
101
101
  */
102
- get<T extends Model.Key<this>>(key: T, required?: boolean): Model.Value<this, T>;
102
+ get<T extends Model.Event<this>>(key: T, required?: boolean): Model.Value<this, T>;
103
103
  /**
104
104
  * Run a function when a property is updated.
105
105
  *
106
106
  * @param key - Property to watch for updates.
107
107
  * @param callback - Function to call when property is updated.
108
108
  */
109
- get<T extends Model.Key<this>>(key: T, callback: Model.OnUpdate<this, T>): () => void;
109
+ get<T extends Model.Event<this>>(key: T, callback: Model.OnUpdate<this, T>): () => void;
110
110
  /**
111
111
  * Check if model is expired.
112
112
  *
@@ -125,7 +125,7 @@ declare class Model {
125
125
  *
126
126
  * @returns Promise which resolves object with updated values. Is `undefined` if there is no update.
127
127
  **/
128
- set(): PromiseLike<Model.Key<this>[]> | undefined;
128
+ set(): PromiseLike<Model.Event<this>[]> | undefined;
129
129
  /**
130
130
  * Call a function when update occurs.
131
131
  *
@@ -158,7 +158,7 @@ declare class Model {
158
158
  * @param key - Property or event to dispatch.
159
159
  * @returns Promise which resolves an array of keys which were updated.
160
160
  */
161
- set(key: Model.Key<this>): PromiseLike<Model.Key<this>[]>;
161
+ set(key: Model.Event<this>): PromiseLike<Model.Event<this>[]>;
162
162
  /**
163
163
  * Update a property with value.
164
164
  *
@@ -166,7 +166,7 @@ declare class Model {
166
166
  * @param value - value to update property with (if the same as current, no update will occur)
167
167
  * @param silent - if true, will not notify listeners of an update
168
168
  */
169
- set<K extends string>(key: K, value?: Model.Value<this, K>, silent?: boolean): PromiseLike<Model.Key<this>[]> | undefined;
169
+ set<K extends string>(key: K, value?: Model.Value<this, K>, silent?: boolean): PromiseLike<Model.Event<this>[]> | undefined;
170
170
  /** Iterate over managed properties in this instance of Model. */
171
171
  [Symbol.iterator](): Iterator<[string, unknown]>;
172
172
  /**
@@ -176,7 +176,7 @@ declare class Model {
176
176
  *
177
177
  * @param args - arguments sent to constructor
178
178
  */
179
- static new<T extends typeof Model>(this: T, ...args: ConstructorParameters<T>): InstanceType<T>;
179
+ static new<T extends Model>(this: Model.New<T>, arg?: Model.Argument): T;
180
180
  /**
181
181
  * Static equivalent of `x instanceof this`.
182
182
  * Determines if provided class is a subtype of this one.
package/dist/index.js CHANGED
@@ -170,23 +170,28 @@ var Model = class _Model {
170
170
  const desc = Object.getOwnPropertyDescriptors(Type.prototype);
171
171
  for (const key in desc) {
172
172
  let { value } = desc[key];
173
- if (typeof value == "function" && key !== "constructor")
173
+ if (typeof value == "function" && key !== "constructor") {
174
+ const bind = (value2) => {
175
+ const method = value2.bind(this);
176
+ METHOD.set(method, value2);
177
+ define(this, key, {
178
+ value: method,
179
+ configurable: false
180
+ });
181
+ return method;
182
+ };
174
183
  Object.defineProperty(Type.prototype, key, {
175
- get() {
176
- const method = value.bind(this.is);
177
- METHOD.set(method, value);
178
- define(this.is, key, {
179
- value: method,
180
- configurable: false
181
- });
182
- return method;
183
- }
184
+ get: () => this.hasOwnProperty(key) ? value : bind(value),
185
+ set: bind
184
186
  });
187
+ }
185
188
  }
186
189
  Type = Object.getPrototypeOf(Type);
187
190
  }
188
191
  addListener(this, () => {
189
192
  let onDone;
193
+ if (typeof arg == "function")
194
+ onDone = arg.call(this);
190
195
  for (const key in this) {
191
196
  const desc = Object.getOwnPropertyDescriptor(this, key);
192
197
  if ("value" in desc) {
@@ -200,8 +205,6 @@ var Model = class _Model {
200
205
  });
201
206
  }
202
207
  }
203
- if (typeof arg == "function")
204
- onDone = arg();
205
208
  addListener(this, () => {
206
209
  for (const [_, value] of this)
207
210
  if (value instanceof _Model && PARENT.get(value) === this)
@@ -289,8 +292,8 @@ var Model = class _Model {
289
292
  *
290
293
  * @param args - arguments sent to constructor
291
294
  */
292
- static new(...args) {
293
- const instance = new this(...args);
295
+ static new(arg) {
296
+ const instance = new this(arg);
294
297
  event(instance, true);
295
298
  return instance;
296
299
  }
package/dist/index.mjs CHANGED
@@ -136,23 +136,28 @@ var Model = class _Model {
136
136
  const desc = Object.getOwnPropertyDescriptors(Type.prototype);
137
137
  for (const key in desc) {
138
138
  let { value } = desc[key];
139
- if (typeof value == "function" && key !== "constructor")
139
+ if (typeof value == "function" && key !== "constructor") {
140
+ const bind = (value2) => {
141
+ const method = value2.bind(this);
142
+ METHOD.set(method, value2);
143
+ define(this, key, {
144
+ value: method,
145
+ configurable: false
146
+ });
147
+ return method;
148
+ };
140
149
  Object.defineProperty(Type.prototype, key, {
141
- get() {
142
- const method = value.bind(this.is);
143
- METHOD.set(method, value);
144
- define(this.is, key, {
145
- value: method,
146
- configurable: false
147
- });
148
- return method;
149
- }
150
+ get: () => this.hasOwnProperty(key) ? value : bind(value),
151
+ set: bind
150
152
  });
153
+ }
151
154
  }
152
155
  Type = Object.getPrototypeOf(Type);
153
156
  }
154
157
  addListener(this, () => {
155
158
  let onDone;
159
+ if (typeof arg == "function")
160
+ onDone = arg.call(this);
156
161
  for (const key in this) {
157
162
  const desc = Object.getOwnPropertyDescriptor(this, key);
158
163
  if ("value" in desc) {
@@ -166,8 +171,6 @@ var Model = class _Model {
166
171
  });
167
172
  }
168
173
  }
169
- if (typeof arg == "function")
170
- onDone = arg();
171
174
  addListener(this, () => {
172
175
  for (const [_, value] of this)
173
176
  if (value instanceof _Model && PARENT.get(value) === this)
@@ -255,8 +258,8 @@ var Model = class _Model {
255
258
  *
256
259
  * @param args - arguments sent to constructor
257
260
  */
258
- static new(...args) {
259
- const instance = new this(...args);
261
+ static new(arg) {
262
+ const instance = new this(arg);
260
263
  event(instance, true);
261
264
  return instance;
262
265
  }
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.41.0",
32
- "gitHead": "1c7d28301f4abc428c3bfdea891c34d31fc1e9d0"
31
+ "version": "0.42.0",
32
+ "gitHead": "a49c69c9d8cfec956664046510d4e862505cdd1e"
33
33
  }