@expressive/mvc 0.4.2 → 0.4.5

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)
@@ -946,202 +946,217 @@ function parent(Expects, required) {
946
946
 
947
947
  const ANY$1 = Symbol("any");
948
948
  function managedMap(control, property, initial) {
949
- const lastUpdate = new Set();
949
+ const managed = new ManagedMap(initial, emit);
950
950
  const context = new WeakMap();
951
- const assoc = new WeakMap();
952
- let managed;
953
- let value;
951
+ const observers = new Set();
952
+ const frozen = new Set();
954
953
  function reset() {
955
- lastUpdate.clear();
954
+ frozen.clear();
956
955
  }
957
956
  function emit(key) {
958
- lastUpdate.add(key);
957
+ observers.forEach(notify => notify(key));
959
958
  control.update(property);
960
959
  control.waiting.add(reset);
961
960
  }
962
- function setValue(next, initial) {
963
- value = next;
964
- managed = create(next);
965
- control.state.set(property, next);
966
- if (!initial)
967
- emit(ANY$1);
968
- const watch = (key, on) => {
969
- const local = assoc.get(on);
970
- if (local)
971
- local(key);
972
- };
973
- managed.set = (k, v) => {
974
- emit(k);
975
- return value.set(k, v);
976
- };
977
- managed.delete = (k) => {
978
- emit(k);
979
- return value.delete(k);
980
- };
981
- managed.clear = () => {
982
- emit(ANY$1);
983
- value.clear();
984
- };
985
- managed.get = function (key) {
986
- watch(key, this);
987
- return value.get(key);
988
- };
989
- managed.has = function (key) {
990
- watch(key, this);
991
- return value.has(key);
992
- };
993
- managed.values = function () {
994
- watch(ANY$1, this);
995
- return value.values();
996
- };
997
- managed.keys = function () {
998
- watch(ANY$1, this);
999
- return value.keys();
1000
- };
1001
- managed.entries = function () {
1002
- watch(ANY$1, this);
1003
- return value.entries();
1004
- };
1005
- managed[Symbol.iterator] = function () {
1006
- watch(ANY$1, this);
1007
- return value[Symbol.iterator]();
1008
- };
1009
- defineProperty(managed, "size", {
1010
- get() {
1011
- watch(ANY$1, this);
1012
- return value.size;
961
+ function init(local) {
962
+ const proxy = create(managed);
963
+ const using = new Set();
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
+ }
1013
973
  }
1014
974
  });
1015
- }
1016
- function getValue(local) {
1017
- if (!local)
1018
- return managed;
1019
- if (context.has(local))
1020
- return context.get(local);
1021
- const refresh = local.onUpdate(property, control);
1022
- const proxy = Object.create(managed);
1023
- const using = new Set();
1024
- const update = () => {
1025
- if (using.has(ANY$1) || lastUpdate.has(ANY$1) && using.size)
1026
- refresh();
1027
- else
1028
- for (const key of lastUpdate)
1029
- if (using.has(key)) {
1030
- refresh();
1031
- break;
1032
- }
1033
- };
975
+ local.add(property, false);
1034
976
  context.set(local, proxy);
1035
- assoc.set(proxy, (key) => {
977
+ proxy.watch = (key) => {
1036
978
  if (!local.active)
1037
979
  using.add(key);
1038
- });
1039
- local.add(property, () => update);
980
+ };
1040
981
  return proxy;
1041
982
  }
1042
- setValue(initial, true);
1043
983
  return {
1044
- set: setValue,
1045
- get: getValue
984
+ value: initial,
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
+ }
1046
1000
  };
1001
+ }
1002
+ class ManagedMap {
1003
+ constructor(source, emit) {
1004
+ this.source = source;
1005
+ this.emit = emit;
1006
+ }
1007
+ watch(_key) { }
1008
+ set(key, value) {
1009
+ this.source.set(key, value);
1010
+ this.emit(key);
1011
+ return this;
1012
+ }
1013
+ ;
1014
+ delete(key) {
1015
+ this.emit(key);
1016
+ return this.source.delete(key);
1017
+ }
1018
+ ;
1019
+ clear() {
1020
+ this.emit(ANY$1);
1021
+ this.source.clear();
1022
+ }
1023
+ get(key) {
1024
+ this.watch(key);
1025
+ return this.source.get(key);
1026
+ }
1027
+ ;
1028
+ has(key) {
1029
+ this.watch(key);
1030
+ return this.source.has(key);
1031
+ }
1032
+ ;
1033
+ values() {
1034
+ this.watch(ANY$1);
1035
+ return this.source.values();
1036
+ }
1037
+ keys() {
1038
+ this.watch(ANY$1);
1039
+ return this.source.keys();
1040
+ }
1041
+ entries() {
1042
+ this.watch(ANY$1);
1043
+ return this.source.entries();
1044
+ }
1045
+ ;
1046
+ [Symbol.iterator]() {
1047
+ this.watch(ANY$1);
1048
+ return this.source[Symbol.iterator]();
1049
+ }
1050
+ ;
1051
+ get size() {
1052
+ this.watch(ANY$1);
1053
+ return this.source.size;
1054
+ }
1047
1055
  }
1048
1056
 
1049
1057
  const ANY = Symbol("any");
1050
1058
  function managedSet(control, property, initial) {
1051
- const lastUpdate = new Set();
1059
+ const managed = new ManagedSet(initial, emit);
1052
1060
  const context = new WeakMap();
1053
- const assoc = new WeakMap();
1054
- let managed;
1055
- let value;
1061
+ const observers = new Set();
1062
+ const frozen = new Set();
1056
1063
  function reset() {
1057
- lastUpdate.clear();
1064
+ frozen.clear();
1058
1065
  }
1059
1066
  function emit(key) {
1060
- lastUpdate.add(key);
1067
+ observers.forEach(notify => notify(key));
1061
1068
  control.update(property);
1062
1069
  control.waiting.add(reset);
1063
1070
  }
1064
- function setValue(next, initial) {
1065
- value = next;
1066
- managed = create(next);
1067
- control.state.set(property, next);
1068
- if (!initial)
1069
- emit(ANY);
1070
- const watch = (key, on) => {
1071
- const local = assoc.get(on);
1072
- if (local)
1073
- local(key);
1074
- };
1075
- managed.add = (k) => {
1076
- emit(k);
1077
- return value.add(k);
1078
- };
1079
- managed.delete = (k) => {
1080
- emit(k);
1081
- return value.delete(k);
1082
- };
1083
- managed.clear = () => {
1084
- emit(ANY);
1085
- value.clear();
1086
- };
1087
- managed.has = function (key) {
1088
- watch(key, this);
1089
- return value.has(key);
1090
- };
1091
- managed.values = function () {
1092
- watch(ANY, this);
1093
- return value.values();
1094
- };
1095
- managed.keys = function () {
1096
- watch(ANY, this);
1097
- return value.keys();
1098
- };
1099
- managed.entries = function () {
1100
- watch(ANY, this);
1101
- return value.entries();
1102
- };
1103
- managed[Symbol.iterator] = function () {
1104
- watch(ANY, this);
1105
- return value[Symbol.iterator]();
1106
- };
1107
- defineProperty(managed, "size", {
1108
- get() {
1109
- watch(ANY, this);
1110
- return value.size;
1071
+ function init(local) {
1072
+ const proxy = create(managed);
1073
+ const using = new Set();
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
+ }
1111
1083
  }
1112
1084
  });
1113
- }
1114
- function getValue(local) {
1115
- if (!local)
1116
- return managed;
1117
- if (context.has(local))
1118
- return context.get(local);
1119
- const refresh = local.onUpdate(property, control);
1120
- const proxy = Object.create(managed);
1121
- const using = new Set();
1122
- const update = () => {
1123
- if (using.has(ANY) || lastUpdate.has(ANY) && using.size)
1124
- refresh();
1125
- else
1126
- for (const key of lastUpdate)
1127
- if (using.has(key)) {
1128
- refresh();
1129
- break;
1130
- }
1131
- };
1085
+ local.add(property, false);
1132
1086
  context.set(local, proxy);
1133
- assoc.set(proxy, (key) => {
1087
+ proxy.watch = (key) => {
1134
1088
  if (!local.active)
1135
1089
  using.add(key);
1136
- });
1137
- local.add(property, () => update);
1090
+ };
1138
1091
  return proxy;
1139
1092
  }
1140
- setValue(initial, true);
1141
1093
  return {
1142
- set: setValue,
1143
- get: getValue
1094
+ value: initial,
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
+ }
1144
1110
  };
1111
+ }
1112
+ class ManagedSet {
1113
+ constructor(source, emit) {
1114
+ this.source = source;
1115
+ this.emit = emit;
1116
+ }
1117
+ watch(_key) { }
1118
+ add(key) {
1119
+ this.source.add(key);
1120
+ this.emit(key);
1121
+ return this;
1122
+ }
1123
+ ;
1124
+ delete(key) {
1125
+ this.emit(key);
1126
+ return this.source.delete(key);
1127
+ }
1128
+ ;
1129
+ clear() {
1130
+ this.emit(ANY);
1131
+ this.source.clear();
1132
+ }
1133
+ has(key) {
1134
+ this.watch(key);
1135
+ return this.source.has(key);
1136
+ }
1137
+ ;
1138
+ values() {
1139
+ this.watch(ANY);
1140
+ return this.source.values();
1141
+ }
1142
+ keys() {
1143
+ this.watch(ANY);
1144
+ return this.source.keys();
1145
+ }
1146
+ entries() {
1147
+ this.watch(ANY);
1148
+ return this.source.entries();
1149
+ }
1150
+ ;
1151
+ [Symbol.iterator]() {
1152
+ this.watch(ANY);
1153
+ return this.source[Symbol.iterator]();
1154
+ }
1155
+ ;
1156
+ get size() {
1157
+ this.watch(ANY);
1158
+ return this.source.size;
1159
+ }
1145
1160
  }
1146
1161
 
1147
1162
  const Oops$5 = issues({