@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.d.ts CHANGED
@@ -239,7 +239,7 @@ declare namespace Instruction {
239
239
  */
240
240
  declare function apply<T = any>(instruction: Instruction<T>, name?: string): T;
241
241
 
242
- declare namespace from {
242
+ declare namespace get {
243
243
  type Function<T, O = any> = (this: O, on: O) => T;
244
244
  type Factory<T, O = any> = (key: string) => Function<T, O>;
245
245
  type Getter = (controller: Controller, key: string) => any;
@@ -251,16 +251,16 @@ declare namespace from {
251
251
  * @param compute - Compute function. Bound to a subscriber-proxy of source, returns output value. Will update automatically as input values change.
252
252
  * @param suspend - Value will throw suspense when evaulating to undefined.
253
253
  */
254
- declare function from<R, T>(source: T, compute: (this: T, on: T) => R, suspend: true): Exclude<R, undefined>;
255
- declare function from<R, T>(source: T, compute: (this: T, on: T) => R, suspend?: boolean): R;
254
+ declare function get<R, T>(source: T, compute: (this: T, on: T) => R, suspend: true): Exclude<R, undefined>;
255
+ declare function get<R, T>(source: T, compute: (this: T, on: T) => R, suspend?: boolean): R;
256
256
  /**
257
257
  * Implement a computed value; output is returned by function from provided factory.
258
258
  *
259
259
  * @param compute - Factory function to generate a getter to subscribe dependancies.
260
260
  * @param suspend - Value will throw suspense when evaulating to undefined.
261
261
  */
262
- declare function from<R, T>(compute: (property: string) => (this: T, state: T) => R, suspend: true): Exclude<R, undefined>;
263
- declare function from<R, T>(compute: (property: string) => (this: T, state: T) => R, suspend?: boolean): R;
262
+ declare function get<R, T>(compute: (property: string) => (this: T, state: T) => R, suspend: true): Exclude<R, undefined>;
263
+ declare function get<R, T>(compute: (property: string) => (this: T, state: T) => R, suspend?: boolean): R;
264
264
 
265
265
  declare namespace ref {
266
266
  type Callback<T, S = any> = (this: S, argument: T) => ((next: T) => void) | Promise<void> | void | boolean;
@@ -627,4 +627,4 @@ declare function useTap<T extends Stateful, K extends Model.Field<T>>(source: T
627
627
  declare function useTap<T extends Stateful, R>(source: T | (() => T) | useTap.Tappable, compute: (this: T, from: T) => R, expect: true): Exclude<R, undefined>;
628
628
  declare function useTap<T extends Stateful, R>(source: T | (() => T) | useTap.Tappable, compute: (this: T, from: T) => R, expect?: boolean): R;
629
629
 
630
- export { CONTROL, Consumer, Global, LOCAL, MVC as Model, Provider, STATE, Stateful, WHY, act, apply, array, MVC as default, from, parent, put, ref, set, tap, use, useFrom, useLocal, useModel, useTap };
630
+ export { CONTROL, Consumer, Global, LOCAL, MVC as Model, Provider, STATE, Stateful, WHY, act, apply, array, MVC as default, get as from, get, parent, put, ref, set, tap, use, useFrom, useLocal, useModel, useTap };
package/lib/index.esm.js CHANGED
@@ -166,7 +166,7 @@ const Oops$a = issues({
166
166
  const INFO = new WeakMap();
167
167
  const KEYS = new WeakMap();
168
168
  const ORDER = new WeakMap();
169
- function from(source, arg1, arg2) {
169
+ function get(source, arg1, arg2) {
170
170
  return apply(function from(key) {
171
171
  const { subject, state } = this;
172
172
  const required = arg2 === true || arg1 === true;
@@ -936,9 +936,9 @@ function parent(Expects, required) {
936
936
  });
937
937
  }
938
938
 
939
- const ANY$1 = Symbol("any");
940
- function managedMap(control, property, initial) {
941
- const managed = new ManagedMap(initial, emit);
939
+ const ANY = Symbol("any");
940
+ function keyed(control, property, initial) {
941
+ const managed = new Stateful(initial, emit);
942
942
  const context = new WeakMap();
943
943
  const observers = new Set();
944
944
  const frozen = new Set();
@@ -946,150 +946,48 @@ function managedMap(control, property, initial) {
946
946
  frozen.clear();
947
947
  }
948
948
  function emit(key) {
949
- observers.forEach(notify => notify(key));
950
- control.update(property);
951
949
  control.waiting.add(reset);
950
+ control.update(property);
951
+ observers.forEach(notify => notify(key));
952
952
  }
953
953
  function init(local) {
954
954
  const proxy = create(managed);
955
955
  const using = new Set();
956
- observers.add(key => {
956
+ function onEvent(key) {
957
957
  if (frozen.has(local))
958
958
  return;
959
- if (using.has(key) || using.has(ANY$1) || key === ANY$1 && using.size) {
959
+ if (key === ANY || using.has(key) || using.has(ANY)) {
960
960
  const refresh = local.onUpdate(property, control);
961
961
  if (refresh) {
962
962
  frozen.add(local);
963
963
  refresh();
964
964
  }
965
965
  }
966
- });
967
- local.add(property, false);
966
+ }
968
967
  context.set(local, proxy);
969
968
  proxy.watch = (key) => {
970
969
  if (!local.active)
971
970
  using.add(key);
972
971
  };
973
- return proxy;
974
- }
975
- return {
976
- value: initial,
977
- get(local) {
978
- if (!local)
979
- return managed;
980
- if (context.has(local))
981
- return context.get(local);
982
- return init(local);
983
- },
984
- set(next) {
985
- managed.source = next;
986
- control.state.set(property, next);
987
- emit(ANY$1);
988
- },
989
- destroy() {
990
- observers.clear();
991
- }
992
- };
993
- }
994
- class ManagedMap {
995
- constructor(source, emit) {
996
- this.source = source;
997
- this.emit = emit;
998
- }
999
- watch(_key) { }
1000
- set(key, value) {
1001
- this.source.set(key, value);
1002
- this.emit(key);
1003
- return this;
1004
- }
1005
- ;
1006
- delete(key) {
1007
- this.emit(key);
1008
- return this.source.delete(key);
1009
- }
1010
- ;
1011
- clear() {
1012
- this.emit(ANY$1);
1013
- this.source.clear();
1014
- }
1015
- get(key) {
1016
- this.watch(key);
1017
- return this.source.get(key);
1018
- }
1019
- ;
1020
- has(key) {
1021
- this.watch(key);
1022
- return this.source.has(key);
1023
- }
1024
- ;
1025
- values() {
1026
- this.watch(ANY$1);
1027
- return this.source.values();
1028
- }
1029
- keys() {
1030
- this.watch(ANY$1);
1031
- return this.source.keys();
1032
- }
1033
- entries() {
1034
- this.watch(ANY$1);
1035
- return this.source.entries();
1036
- }
1037
- ;
1038
- [Symbol.iterator]() {
1039
- this.watch(ANY$1);
1040
- return this.source[Symbol.iterator]();
1041
- }
1042
- ;
1043
- get size() {
1044
- this.watch(ANY$1);
1045
- return this.source.size;
1046
- }
1047
- }
1048
-
1049
- const ANY = Symbol("any");
1050
- function managedSet(control, property, initial) {
1051
- const managed = new ManagedSet(initial, emit);
1052
- const context = new WeakMap();
1053
- const observers = new Set();
1054
- const frozen = new Set();
1055
- function reset() {
1056
- frozen.clear();
1057
- }
1058
- function emit(key) {
1059
- observers.forEach(notify => notify(key));
1060
- control.update(property);
1061
- control.waiting.add(reset);
1062
- }
1063
- function init(local) {
1064
- const proxy = create(managed);
1065
- const using = new Set();
1066
- observers.add(key => {
1067
- if (frozen.has(local))
1068
- return;
1069
- if (using.has(key) || using.has(ANY) || key === ANY && using.size) {
1070
- const refresh = local.onUpdate(property, control);
1071
- if (refresh) {
1072
- frozen.add(local);
1073
- refresh();
1074
- }
972
+ local.add(property, false);
973
+ local.dependant.add({
974
+ commit() {
975
+ if (using.size === 0)
976
+ using.add(ANY);
977
+ observers.add(onEvent);
978
+ },
979
+ release() {
980
+ observers.delete(onEvent);
1075
981
  }
1076
982
  });
1077
- local.add(property, false);
1078
- context.set(local, proxy);
1079
- proxy.watch = (key) => {
1080
- if (!local.active)
1081
- using.add(key);
1082
- };
1083
983
  return proxy;
1084
984
  }
1085
985
  return {
1086
986
  value: initial,
1087
987
  get(local) {
1088
- if (!local)
1089
- return managed;
1090
- if (context.has(local))
1091
- return context.get(local);
1092
- return init(local);
988
+ return local ?
989
+ context.get(local) || init(local) :
990
+ managed;
1093
991
  },
1094
992
  set(next) {
1095
993
  managed.source = next;
@@ -1101,16 +999,35 @@ function managedSet(control, property, initial) {
1101
999
  }
1102
1000
  };
1103
1001
  }
1104
- class ManagedSet {
1002
+ class Stateful {
1105
1003
  constructor(source, emit) {
1106
1004
  this.source = source;
1107
1005
  this.emit = emit;
1006
+ if (source instanceof Set)
1007
+ assign(this, {
1008
+ add(key) {
1009
+ source.add(key);
1010
+ emit(key);
1011
+ return this;
1012
+ }
1013
+ });
1014
+ else
1015
+ assign(this, {
1016
+ get(key) {
1017
+ this.watch(key);
1018
+ return source.get(key);
1019
+ },
1020
+ set(key, value) {
1021
+ source.set(key, value);
1022
+ emit(key);
1023
+ return this;
1024
+ }
1025
+ });
1108
1026
  }
1109
1027
  watch(_key) { }
1110
- add(key) {
1111
- this.source.add(key);
1112
- this.emit(key);
1113
- return this;
1028
+ has(key) {
1029
+ this.watch(key);
1030
+ return this.source.has(key);
1114
1031
  }
1115
1032
  ;
1116
1033
  delete(key) {
@@ -1122,33 +1039,28 @@ class ManagedSet {
1122
1039
  this.emit(ANY);
1123
1040
  this.source.clear();
1124
1041
  }
1125
- has(key) {
1126
- this.watch(key);
1127
- return this.source.has(key);
1042
+ keys() {
1043
+ this.watch(ANY);
1044
+ return this.source.keys();
1128
1045
  }
1129
- ;
1130
1046
  values() {
1131
1047
  this.watch(ANY);
1132
1048
  return this.source.values();
1133
1049
  }
1134
- keys() {
1135
- this.watch(ANY);
1136
- return this.source.keys();
1137
- }
1138
1050
  entries() {
1139
1051
  this.watch(ANY);
1140
1052
  return this.source.entries();
1141
1053
  }
1142
1054
  ;
1055
+ get size() {
1056
+ this.watch(ANY);
1057
+ return this.source.size;
1058
+ }
1143
1059
  [Symbol.iterator]() {
1144
1060
  this.watch(ANY);
1145
1061
  return this.source[Symbol.iterator]();
1146
1062
  }
1147
1063
  ;
1148
- get size() {
1149
- this.watch(ANY);
1150
- return this.source.size;
1151
- }
1152
1064
  }
1153
1065
 
1154
1066
  const Oops$5 = issues({
@@ -1163,10 +1075,8 @@ function use$1(input, argument) {
1163
1075
  : input();
1164
1076
  else if (input && typeof input !== "object")
1165
1077
  throw Oops$5.BadArgument(typeof input);
1166
- if (input instanceof Map)
1167
- return managedMap(this, key, input);
1168
- if (input instanceof Set)
1169
- return managedSet(this, key, input);
1078
+ if (input instanceof Map || input instanceof Set)
1079
+ return keyed(this, key, input);
1170
1080
  function onUpdate(next) {
1171
1081
  state.set(key, next);
1172
1082
  if (next) {
@@ -1609,5 +1519,5 @@ class Global extends MVC {
1609
1519
  }
1610
1520
  }
1611
1521
 
1612
- export { CONTROL, Consumer, Global, LOCAL, MVC as Model, Provider, STATE, WHY, act, apply, array, MVC as default, from, parent, put, ref, set, tap, use$1 as use, useFrom, useLocal, useModel, useTap };
1522
+ export { CONTROL, Consumer, Global, LOCAL, MVC as Model, Provider, STATE, WHY, act, apply, array, MVC as default, get as from, get, parent, put, ref, set, tap, use$1 as use, useFrom, useLocal, useModel, useTap };
1613
1523
  //# sourceMappingURL=index.esm.js.map