@expressive/mvc 0.4.3 → 0.4.4

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
@@ -172,11 +172,10 @@ const Oops$a = issues({
172
172
  Failed: (parent, property, initial) => `An exception was thrown while ${initial ? "initializing" : "refreshing"} [${parent}.${property}].`
173
173
  });
174
174
  const INFO = new WeakMap();
175
- const USED = new WeakMap();
176
175
  const KEYS = new WeakMap();
176
+ const ORDER = new WeakMap();
177
177
  function from(source, arg1, arg2) {
178
178
  return apply(function from(key) {
179
- const parent = this;
180
179
  const { subject, state } = this;
181
180
  const required = arg2 === true || arg1 === true;
182
181
  let getSource;
@@ -197,14 +196,13 @@ function from(source, arg1, arg2) {
197
196
  else
198
197
  throw Oops$a.BadSource(subject, key, source);
199
198
  const setter = arg1;
200
- const info = { key, parent, priority: 1 };
199
+ const info = { key, parent: this };
201
200
  let sub;
202
- let register = USED.get(parent);
203
- if (!register)
204
- USED.set(parent, register = new Map());
201
+ let order = ORDER.get(this);
202
+ if (!order)
203
+ ORDER.set(this, order = []);
205
204
  state.set(key, undefined);
206
- register.set(key, info);
207
- function compute(initial) {
205
+ const compute = (initial) => {
208
206
  try {
209
207
  return setter.call(sub.proxy, sub.proxy);
210
208
  }
@@ -212,8 +210,8 @@ function from(source, arg1, arg2) {
212
210
  Oops$a.Failed(subject, key, initial).warn();
213
211
  throw err;
214
212
  }
215
- }
216
- function update() {
213
+ };
214
+ const refreshValue = () => {
217
215
  let value;
218
216
  try {
219
217
  value = compute(false);
@@ -223,26 +221,25 @@ function from(source, arg1, arg2) {
223
221
  }
224
222
  finally {
225
223
  if (state.get(key) !== value) {
226
- parent.state.set(key, value);
227
- parent.update(key);
224
+ state.set(key, value);
225
+ this.update(key);
228
226
  return value;
229
227
  }
230
228
  }
231
- }
232
- function defer(_key, from) {
233
- let pending = KEYS.get(from);
234
- if (!pending)
235
- KEYS.set(from, pending = []);
236
- if (info.parent !== from)
237
- update();
238
- else {
239
- const after = pending.findIndex(peer => (info.priority > INFO.get(peer).priority));
240
- pending.splice(after + 1, 0, update);
229
+ };
230
+ const dependancyDidUpdate = (_key, source) => {
231
+ if (source !== this) {
232
+ refreshValue();
233
+ return;
241
234
  }
242
- }
243
- function create() {
235
+ let pending = KEYS.get(this);
236
+ if (!pending)
237
+ KEYS.set(this, pending = []);
238
+ pending.push(refreshValue);
239
+ };
240
+ const create = () => {
244
241
  const control = getSource();
245
- sub = control.subscribe(defer);
242
+ sub = control.subscribe(dependancyDidUpdate);
246
243
  try {
247
244
  const value = compute(true);
248
245
  state.set(key, value);
@@ -253,14 +250,10 @@ function from(source, arg1, arg2) {
253
250
  }
254
251
  finally {
255
252
  sub.commit();
256
- for (const key of control.state.keys()) {
257
- const peer = register.get(key);
258
- if (peer && peer.priority >= info.priority)
259
- info.priority = peer.priority + 1;
260
- }
253
+ order.push(refreshValue);
261
254
  }
262
- }
263
- INFO.set(update, info);
255
+ };
256
+ INFO.set(refreshValue, info);
264
257
  return () => {
265
258
  const value = sub ? state.get(key) : create();
266
259
  if (value === undefined && required)
@@ -273,8 +266,11 @@ function flush(control) {
273
266
  const pending = KEYS.get(control);
274
267
  if (!pending)
275
268
  return;
269
+ const list = ORDER.get(control);
276
270
  while (pending.length) {
277
- const compute = pending.shift();
271
+ const compute = pending
272
+ .sort((a, b) => list.indexOf(b) - list.indexOf(a))
273
+ .pop();
278
274
  const { key } = INFO.get(compute);
279
275
  if (!control.frame.has(key))
280
276
  compute();
@@ -299,18 +295,16 @@ class Subscriber {
299
295
  }
300
296
  });
301
297
  const release = parent.addListener(key => {
302
- if (!this.active || !using.has(key))
298
+ if (!this.active)
303
299
  return;
304
300
  const handler = using.get(key);
305
301
  let notify;
306
302
  if (typeof handler == "function") {
307
303
  const callback = handler();
308
- if (typeof callback == "function")
309
- notify = callback;
310
- else if (callback === true)
304
+ if (callback === true)
311
305
  notify = this.onUpdate(key, parent);
312
306
  }
313
- else
307
+ else if (handler === true)
314
308
  notify = this.onUpdate(key, parent);
315
309
  const getWhy = () => {
316
310
  const update = getUpdate(parent.subject);
@@ -332,7 +326,7 @@ class Subscriber {
332
326
  release();
333
327
  };
334
328
  this.add = (key, value) => {
335
- if (value)
329
+ if (value !== undefined)
336
330
  using.set(key, value);
337
331
  else if (!using.has(key))
338
332
  using.set(key, true);
@@ -406,17 +400,15 @@ class Controller {
406
400
  else if (!frame.size)
407
401
  setTimeout(() => {
408
402
  flush(this);
409
- const callback = Array.from(waiting);
410
- const keys = Array.from(frame);
411
- frame.clear();
412
- waiting.clear();
413
- UPDATE.set(subject, keys);
403
+ UPDATE.set(subject, Array.from(frame));
414
404
  setTimeout(() => {
415
405
  UPDATE.delete(subject);
416
406
  }, 0);
417
- for (const cb of callback)
407
+ frame.clear();
408
+ for (const notify of waiting)
418
409
  try {
419
- cb();
410
+ waiting.delete(notify);
411
+ notify();
420
412
  }
421
413
  catch (err) {
422
414
  console.error(err);
@@ -667,6 +659,14 @@ function apply(fn, label) {
667
659
  const desc = output;
668
660
  if ("value" in desc)
669
661
  state.set(key, desc.value);
662
+ if ("destroy" in output) {
663
+ const { destroy } = output;
664
+ if (destroy)
665
+ this.addListener((key) => {
666
+ if (key == null)
667
+ destroy();
668
+ });
669
+ }
670
670
  const { get: onGet, set: onSet, enumerable, suspense } = desc;
671
671
  const set = onSet !== false
672
672
  ? this.ref(key, onSet)
@@ -948,39 +948,31 @@ const ANY$1 = Symbol("any");
948
948
  function managedMap(control, property, initial) {
949
949
  const managed = new ManagedMap(initial, emit);
950
950
  const context = new WeakMap();
951
- const lastUpdate = new Set();
951
+ const observers = new Set();
952
+ const frozen = new Set();
952
953
  function reset() {
953
- lastUpdate.clear();
954
+ frozen.clear();
954
955
  }
955
956
  function emit(key) {
956
- lastUpdate.add(key);
957
+ observers.forEach(notify => notify(key));
957
958
  control.update(property);
958
959
  control.waiting.add(reset);
959
960
  }
960
- function setValue(next) {
961
- control.state.set(property, next);
962
- managed.source = next;
963
- emit(ANY$1);
964
- }
965
- function getValue(local) {
966
- if (!local)
967
- return managed;
968
- if (context.has(local))
969
- return context.get(local);
970
- const refresh = local.onUpdate(property, control);
961
+ function init(local) {
971
962
  const proxy = create(managed);
972
963
  const using = new Set();
973
- const update = () => {
974
- if (using.has(ANY$1) || lastUpdate.has(ANY$1) && using.size)
975
- refresh();
976
- else
977
- for (const key of lastUpdate)
978
- if (using.has(key)) {
979
- refresh();
980
- break;
981
- }
982
- };
983
- local.add(property, () => update);
964
+ observers.add(key => {
965
+ if (frozen.has(local))
966
+ return;
967
+ if (using.has(key) || using.has(ANY$1) || key === ANY$1 && using.size) {
968
+ const refresh = local.onUpdate(property, control);
969
+ if (refresh) {
970
+ frozen.add(local);
971
+ refresh();
972
+ }
973
+ }
974
+ });
975
+ local.add(property, false);
984
976
  context.set(local, proxy);
985
977
  proxy.watch = (key) => {
986
978
  if (!local.active)
@@ -990,8 +982,21 @@ function managedMap(control, property, initial) {
990
982
  }
991
983
  return {
992
984
  value: initial,
993
- set: setValue,
994
- get: getValue
985
+ get(local) {
986
+ if (!local)
987
+ return managed;
988
+ if (context.has(local))
989
+ return context.get(local);
990
+ return init(local);
991
+ },
992
+ set(next) {
993
+ managed.source = next;
994
+ control.state.set(property, next);
995
+ emit(ANY$1);
996
+ },
997
+ destroy() {
998
+ observers.clear();
999
+ }
995
1000
  };
996
1001
  }
997
1002
  class ManagedMap {
@@ -1053,39 +1058,31 @@ const ANY = Symbol("any");
1053
1058
  function managedSet(control, property, initial) {
1054
1059
  const managed = new ManagedSet(initial, emit);
1055
1060
  const context = new WeakMap();
1056
- const lastUpdate = new Set();
1061
+ const observers = new Set();
1062
+ const frozen = new Set();
1057
1063
  function reset() {
1058
- lastUpdate.clear();
1064
+ frozen.clear();
1059
1065
  }
1060
1066
  function emit(key) {
1061
- lastUpdate.add(key);
1067
+ observers.forEach(notify => notify(key));
1062
1068
  control.update(property);
1063
1069
  control.waiting.add(reset);
1064
1070
  }
1065
- function setValue(next) {
1066
- managed.source = next;
1067
- control.state.set(property, next);
1068
- emit(ANY);
1069
- }
1070
- function getValue(local) {
1071
- if (!local)
1072
- return managed;
1073
- if (context.has(local))
1074
- return context.get(local);
1075
- const refresh = local.onUpdate(property, control);
1071
+ function init(local) {
1076
1072
  const proxy = create(managed);
1077
1073
  const using = new Set();
1078
- const update = () => {
1079
- if (using.has(ANY) || lastUpdate.has(ANY) && using.size)
1080
- refresh();
1081
- else
1082
- for (const key of lastUpdate)
1083
- if (using.has(key)) {
1084
- refresh();
1085
- break;
1086
- }
1087
- };
1088
- local.add(property, () => update);
1074
+ observers.add(key => {
1075
+ if (frozen.has(local))
1076
+ return;
1077
+ if (using.has(key) || using.has(ANY) || key === ANY && using.size) {
1078
+ const refresh = local.onUpdate(property, control);
1079
+ if (refresh) {
1080
+ frozen.add(local);
1081
+ refresh();
1082
+ }
1083
+ }
1084
+ });
1085
+ local.add(property, false);
1089
1086
  context.set(local, proxy);
1090
1087
  proxy.watch = (key) => {
1091
1088
  if (!local.active)
@@ -1095,8 +1092,21 @@ function managedSet(control, property, initial) {
1095
1092
  }
1096
1093
  return {
1097
1094
  value: initial,
1098
- set: setValue,
1099
- get: getValue
1095
+ get(local) {
1096
+ if (!local)
1097
+ return managed;
1098
+ if (context.has(local))
1099
+ return context.get(local);
1100
+ return init(local);
1101
+ },
1102
+ set(next) {
1103
+ managed.source = next;
1104
+ control.state.set(property, next);
1105
+ emit(ANY);
1106
+ },
1107
+ destroy() {
1108
+ observers.clear();
1109
+ }
1100
1110
  };
1101
1111
  }
1102
1112
  class ManagedSet {
@@ -1106,8 +1116,9 @@ class ManagedSet {
1106
1116
  }
1107
1117
  watch(_key) { }
1108
1118
  add(key) {
1119
+ this.source.add(key);
1109
1120
  this.emit(key);
1110
- return this.source.add(key);
1121
+ return this;
1111
1122
  }
1112
1123
  ;
1113
1124
  delete(key) {