@expressive/mvc 0.4.5 → 0.4.6

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
@@ -174,7 +174,7 @@ const Oops$a = issues({
174
174
  const INFO = new WeakMap();
175
175
  const KEYS = new WeakMap();
176
176
  const ORDER = new WeakMap();
177
- function from(source, arg1, arg2) {
177
+ function get(source, arg1, arg2) {
178
178
  return apply(function from(key) {
179
179
  const { subject, state } = this;
180
180
  const required = arg2 === true || arg1 === true;
@@ -944,9 +944,9 @@ function parent(Expects, required) {
944
944
  });
945
945
  }
946
946
 
947
- const ANY$1 = Symbol("any");
948
- function managedMap(control, property, initial) {
949
- const managed = new ManagedMap(initial, emit);
947
+ const ANY = Symbol("any");
948
+ function keyed(control, property, initial) {
949
+ const managed = new Stateful(initial, emit);
950
950
  const context = new WeakMap();
951
951
  const observers = new Set();
952
952
  const frozen = new Set();
@@ -954,150 +954,48 @@ function managedMap(control, property, initial) {
954
954
  frozen.clear();
955
955
  }
956
956
  function emit(key) {
957
- observers.forEach(notify => notify(key));
958
- control.update(property);
959
957
  control.waiting.add(reset);
958
+ control.update(property);
959
+ observers.forEach(notify => notify(key));
960
960
  }
961
961
  function init(local) {
962
962
  const proxy = create(managed);
963
963
  const using = new Set();
964
- observers.add(key => {
964
+ function onEvent(key) {
965
965
  if (frozen.has(local))
966
966
  return;
967
- if (using.has(key) || using.has(ANY$1) || key === ANY$1 && using.size) {
967
+ if (key === ANY || using.has(key) || using.has(ANY)) {
968
968
  const refresh = local.onUpdate(property, control);
969
969
  if (refresh) {
970
970
  frozen.add(local);
971
971
  refresh();
972
972
  }
973
973
  }
974
- });
975
- local.add(property, false);
974
+ }
976
975
  context.set(local, proxy);
977
976
  proxy.watch = (key) => {
978
977
  if (!local.active)
979
978
  using.add(key);
980
979
  };
981
- return proxy;
982
- }
983
- return {
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
- }
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
- }
1055
- }
1056
-
1057
- const ANY = Symbol("any");
1058
- function managedSet(control, property, initial) {
1059
- const managed = new ManagedSet(initial, emit);
1060
- const context = new WeakMap();
1061
- const observers = new Set();
1062
- const frozen = new Set();
1063
- function reset() {
1064
- frozen.clear();
1065
- }
1066
- function emit(key) {
1067
- observers.forEach(notify => notify(key));
1068
- control.update(property);
1069
- control.waiting.add(reset);
1070
- }
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
- }
980
+ local.add(property, false);
981
+ local.dependant.add({
982
+ commit() {
983
+ if (using.size === 0)
984
+ using.add(ANY);
985
+ observers.add(onEvent);
986
+ },
987
+ release() {
988
+ observers.delete(onEvent);
1083
989
  }
1084
990
  });
1085
- local.add(property, false);
1086
- context.set(local, proxy);
1087
- proxy.watch = (key) => {
1088
- if (!local.active)
1089
- using.add(key);
1090
- };
1091
991
  return proxy;
1092
992
  }
1093
993
  return {
1094
994
  value: initial,
1095
995
  get(local) {
1096
- if (!local)
1097
- return managed;
1098
- if (context.has(local))
1099
- return context.get(local);
1100
- return init(local);
996
+ return local ?
997
+ context.get(local) || init(local) :
998
+ managed;
1101
999
  },
1102
1000
  set(next) {
1103
1001
  managed.source = next;
@@ -1109,16 +1007,35 @@ function managedSet(control, property, initial) {
1109
1007
  }
1110
1008
  };
1111
1009
  }
1112
- class ManagedSet {
1010
+ class Stateful {
1113
1011
  constructor(source, emit) {
1114
1012
  this.source = source;
1115
1013
  this.emit = emit;
1014
+ if (source instanceof Set)
1015
+ assign(this, {
1016
+ add(key) {
1017
+ source.add(key);
1018
+ emit(key);
1019
+ return this;
1020
+ }
1021
+ });
1022
+ else
1023
+ assign(this, {
1024
+ get(key) {
1025
+ this.watch(key);
1026
+ return source.get(key);
1027
+ },
1028
+ set(key, value) {
1029
+ source.set(key, value);
1030
+ emit(key);
1031
+ return this;
1032
+ }
1033
+ });
1116
1034
  }
1117
1035
  watch(_key) { }
1118
- add(key) {
1119
- this.source.add(key);
1120
- this.emit(key);
1121
- return this;
1036
+ has(key) {
1037
+ this.watch(key);
1038
+ return this.source.has(key);
1122
1039
  }
1123
1040
  ;
1124
1041
  delete(key) {
@@ -1130,33 +1047,28 @@ class ManagedSet {
1130
1047
  this.emit(ANY);
1131
1048
  this.source.clear();
1132
1049
  }
1133
- has(key) {
1134
- this.watch(key);
1135
- return this.source.has(key);
1050
+ keys() {
1051
+ this.watch(ANY);
1052
+ return this.source.keys();
1136
1053
  }
1137
- ;
1138
1054
  values() {
1139
1055
  this.watch(ANY);
1140
1056
  return this.source.values();
1141
1057
  }
1142
- keys() {
1143
- this.watch(ANY);
1144
- return this.source.keys();
1145
- }
1146
1058
  entries() {
1147
1059
  this.watch(ANY);
1148
1060
  return this.source.entries();
1149
1061
  }
1150
1062
  ;
1063
+ get size() {
1064
+ this.watch(ANY);
1065
+ return this.source.size;
1066
+ }
1151
1067
  [Symbol.iterator]() {
1152
1068
  this.watch(ANY);
1153
1069
  return this.source[Symbol.iterator]();
1154
1070
  }
1155
1071
  ;
1156
- get size() {
1157
- this.watch(ANY);
1158
- return this.source.size;
1159
- }
1160
1072
  }
1161
1073
 
1162
1074
  const Oops$5 = issues({
@@ -1171,10 +1083,8 @@ function use$1(input, argument) {
1171
1083
  : input();
1172
1084
  else if (input && typeof input !== "object")
1173
1085
  throw Oops$5.BadArgument(typeof input);
1174
- if (input instanceof Map)
1175
- return managedMap(this, key, input);
1176
- if (input instanceof Set)
1177
- return managedSet(this, key, input);
1086
+ if (input instanceof Map || input instanceof Set)
1087
+ return keyed(this, key, input);
1178
1088
  function onUpdate(next) {
1179
1089
  state.set(key, next);
1180
1090
  if (next) {
@@ -1629,7 +1539,8 @@ exports.act = act;
1629
1539
  exports.apply = apply;
1630
1540
  exports.array = array;
1631
1541
  exports["default"] = MVC;
1632
- exports.from = from;
1542
+ exports.from = get;
1543
+ exports.get = get;
1633
1544
  exports.parent = parent;
1634
1545
  exports.put = put;
1635
1546
  exports.ref = ref;