@expressive/mvc 1.0.0-beta.37 → 1.0.0-beta.38

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.js CHANGED
@@ -121,13 +121,15 @@ class Controller {
121
121
  Pending.delete(entry);
122
122
  delete subject[key];
123
123
  const desc = instruction.call(this, key, this);
124
- if (!desc)
124
+ if (desc === false)
125
125
  return;
126
- if ("value" in desc)
127
- state[key] = desc.value;
128
- onSet = desc.set;
129
- onGet = desc.get;
130
- enumerable = desc.enumerable;
126
+ if (typeof desc == "object") {
127
+ if ("value" in desc)
128
+ state[key] = desc.value;
129
+ onSet = desc.set;
130
+ onGet = desc.get;
131
+ enumerable = desc.enumerable;
132
+ }
131
133
  }
132
134
  else
133
135
  state[key] = entry;
@@ -252,16 +254,15 @@ function control(subject, cb) {
252
254
  }
253
255
 
254
256
  class Subscriber {
255
- constructor(parent, onUpdate) {
257
+ constructor(target, onUpdate) {
256
258
  this.onUpdate = onUpdate;
257
259
  this.active = false;
258
260
  this.dependant = new Set();
259
261
  this.watch = {};
260
- if (!(parent instanceof Controller))
261
- parent = control(parent);
262
- let reset;
262
+ const parent = target instanceof Controller
263
+ ? target : control(target);
263
264
  const proxy = create(parent.proxy);
264
- const controller = parent;
265
+ let reset;
265
266
  define(proxy, LOCAL, this);
266
267
  defineProperty(this, "proxy", {
267
268
  configurable: true,
@@ -271,29 +272,29 @@ class Subscriber {
271
272
  return proxy;
272
273
  }
273
274
  });
274
- const onEvent = (key) => {
275
+ const release = parent.addListener(key => {
275
276
  const handler = this.watch[key];
276
- if (!handler)
277
+ if (!handler || !this.active)
277
278
  return;
278
279
  if (typeof handler == "function")
279
280
  handler();
280
- const notify = this.onUpdate(key, controller);
281
+ const notify = this.onUpdate(key, parent);
281
282
  const getWhy = (keys) => {
282
283
  reset = applyUpdate(proxy, keys.filter(k => k in this.watch));
283
284
  };
284
285
  if (notify) {
285
- controller.requestUpdate(getWhy);
286
- controller.requestUpdate(notify);
286
+ parent.requestUpdate(getWhy);
287
+ parent.requestUpdate(notify);
287
288
  }
288
- };
289
+ });
289
290
  this.commit = () => {
290
- const release = controller.addListener(onEvent);
291
291
  this.active = true;
292
292
  this.dependant.forEach(x => x.commit());
293
- return this.release = () => {
294
- this.dependant.forEach(x => x.release());
295
- release();
296
- };
293
+ return this.release;
294
+ };
295
+ this.release = () => {
296
+ this.dependant.forEach(x => x.release());
297
+ release();
297
298
  };
298
299
  }
299
300
  }
@@ -384,9 +385,8 @@ function prepare(parent, key, source, setter, getter) {
384
385
  configurable: true,
385
386
  enumerable: true
386
387
  });
387
- return getter
388
- ? () => getter(parent, key)
389
- : {};
388
+ if (getter)
389
+ return () => getter(parent, key);
390
390
  }
391
391
  function ensure(on, keys) {
392
392
  for (const key of keys) {
@@ -560,18 +560,17 @@ function apply(fn, label) {
560
560
  const placeholder = Symbol(`${name} instruction`);
561
561
  function setup(key) {
562
562
  let output = fn.call(this, key, this);
563
- if (!output)
564
- return;
565
- if ("explicit" in output) {
563
+ if (typeof output == "function")
564
+ return { get: output };
565
+ if (typeof output !== "object")
566
+ return output || undefined;
567
+ if ("explicit" in output && output.explicit) {
566
568
  defineProperty(this.subject, key, output);
567
- return;
569
+ return false;
568
570
  }
569
571
  if ("recursive" in output)
570
572
  output = Object.assign(Object.assign({}, output), { get: recursive(this, key) });
571
- if (typeof output == "function")
572
- return { get: output };
573
- else
574
- return output;
573
+ return output;
575
574
  }
576
575
  Pending.set(placeholder, setup);
577
576
  return placeholder;
@@ -991,9 +990,9 @@ function use(init) {
991
990
  return state[0];
992
991
  }
993
992
 
994
- function useModel(source, arg, callback) {
993
+ function useModel(source, arg, arg2) {
995
994
  const instance = React__default["default"].useMemo(() => {
996
- const cb = callback || arg;
995
+ const cb = arg2 || arg;
997
996
  const instance = typeof source == "function" ?
998
997
  source.prototype instanceof Model ?
999
998
  new source() :
@@ -1017,9 +1016,18 @@ function useModel(source, arg, callback) {
1017
1016
  return instance;
1018
1017
  }
1019
1018
  else {
1020
- const local = use(refresh => {
1021
- return new Subscriber(instance, () => refresh);
1022
- });
1019
+ const local = use(refresh => (new Subscriber(instance, () => refresh)));
1020
+ if (typeof arg == "object") {
1021
+ local.active = false;
1022
+ if (typeof arg2 !== "object")
1023
+ arg2 = getOwnPropertyNames(instance);
1024
+ for (const key of arg2)
1025
+ if (key in arg)
1026
+ instance[key] = arg[key];
1027
+ React__default["default"].useLayoutEffect(() => {
1028
+ local.active = true;
1029
+ });
1030
+ }
1023
1031
  React__default["default"].useLayoutEffect(() => {
1024
1032
  local.commit();
1025
1033
  return () => {
@@ -1102,10 +1110,11 @@ function useTap(source, path, expect) {
1102
1110
  }
1103
1111
  return sub;
1104
1112
  });
1105
- React__default["default"].useLayoutEffect(() => local.commit(), []);
1113
+ React__default["default"].useLayoutEffect(local.commit, []);
1106
1114
  return local.proxy;
1107
1115
  }
1108
1116
 
1117
+ /* istanbul ignore file */
1109
1118
  class MVC extends Model {
1110
1119
  tap(path, expect) {
1111
1120
  return useTap(this, path, expect);
@@ -1210,7 +1219,7 @@ function RenderFunction(props) {
1210
1219
  return {};
1211
1220
  });
1212
1221
  if (hook.commit)
1213
- React__default["default"].useLayoutEffect(() => hook.commit(), []);
1222
+ React__default["default"].useLayoutEffect(hook.commit, []);
1214
1223
  return props.render(hook.proxy);
1215
1224
  }
1216
1225