@unseenco/theatre-dataverse 0.1.4 → 0.1.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/dist/index.js +299 -89
- package/dist/index.js.map +2 -2
- package/dist/index.mjs +1759 -0
- package/dist/index.mjs.map +7 -0
- package/package.json +11 -2
package/dist/index.js
CHANGED
|
@@ -1,31 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
var __defProp = Object.defineProperty;
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
-
var __spreadValues = (a, b) => {
|
|
9
|
-
for (var prop in b || (b = {}))
|
|
10
|
-
if (__hasOwnProp.call(b, prop))
|
|
11
|
-
__defNormalProp(a, prop, b[prop]);
|
|
12
|
-
if (__getOwnPropSymbols)
|
|
13
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
-
if (__propIsEnum.call(b, prop))
|
|
15
|
-
__defNormalProp(a, prop, b[prop]);
|
|
16
|
-
}
|
|
17
|
-
return a;
|
|
18
|
-
};
|
|
19
|
-
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
-
var __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
|
|
21
6
|
var __export = (target, all) => {
|
|
22
|
-
__markAsModule(target);
|
|
23
7
|
for (var name in all)
|
|
24
8
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
25
9
|
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
26
19
|
|
|
27
20
|
// src/index.ts
|
|
28
|
-
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
29
23
|
Atom: () => Atom,
|
|
30
24
|
PointerProxy: () => PointerProxy,
|
|
31
25
|
Ticker: () => Ticker,
|
|
@@ -39,6 +33,7 @@ __export(exports, {
|
|
|
39
33
|
prism: () => prism_default,
|
|
40
34
|
val: () => val
|
|
41
35
|
});
|
|
36
|
+
module.exports = __toCommonJS(src_exports);
|
|
42
37
|
|
|
43
38
|
// ../../node_modules/lodash-es/isArray.js
|
|
44
39
|
var isArray = Array.isArray;
|
|
@@ -189,7 +184,9 @@ var funcProto2 = Function.prototype;
|
|
|
189
184
|
var objectProto3 = Object.prototype;
|
|
190
185
|
var funcToString2 = funcProto2.toString;
|
|
191
186
|
var hasOwnProperty2 = objectProto3.hasOwnProperty;
|
|
192
|
-
var reIsNative = RegExp(
|
|
187
|
+
var reIsNative = RegExp(
|
|
188
|
+
"^" + funcToString2.call(hasOwnProperty2).replace(reRegExpChar, "\\$&").replace(/hasOwnProperty|(function).*?(?=\\\()| for .+?(?=\\\])/g, "$1.*?") + "$"
|
|
189
|
+
);
|
|
193
190
|
function baseIsNative(value) {
|
|
194
191
|
if (!isObject_default(value) || isMasked_default(value)) {
|
|
195
192
|
return false;
|
|
@@ -603,8 +600,8 @@ function last(array) {
|
|
|
603
600
|
var last_default = last;
|
|
604
601
|
|
|
605
602
|
// src/pointer.ts
|
|
606
|
-
var pointerMetaWeakMap = new WeakMap();
|
|
607
|
-
var cachedSubPathPointersWeakMap = new WeakMap();
|
|
603
|
+
var pointerMetaWeakMap = /* @__PURE__ */ new WeakMap();
|
|
604
|
+
var cachedSubPathPointersWeakMap = /* @__PURE__ */ new WeakMap();
|
|
608
605
|
var pointerMetaSymbol = Symbol("pointerMeta");
|
|
609
606
|
var proxyHandler = {
|
|
610
607
|
get(pointerKey, prop) {
|
|
@@ -612,7 +609,7 @@ var proxyHandler = {
|
|
|
612
609
|
return pointerMetaWeakMap.get(pointerKey);
|
|
613
610
|
let subPathPointers = cachedSubPathPointersWeakMap.get(pointerKey);
|
|
614
611
|
if (!subPathPointers) {
|
|
615
|
-
subPathPointers = new Map();
|
|
612
|
+
subPathPointers = /* @__PURE__ */ new Map();
|
|
616
613
|
cachedSubPathPointersWeakMap.set(pointerKey, subPathPointers);
|
|
617
614
|
}
|
|
618
615
|
const existing = subPathPointers.get(prop);
|
|
@@ -633,10 +630,9 @@ var getPointerParts = (p) => {
|
|
|
633
630
|
return { root: root2, path };
|
|
634
631
|
};
|
|
635
632
|
function pointer(args) {
|
|
636
|
-
var _a;
|
|
637
633
|
const meta = {
|
|
638
634
|
root: args.root,
|
|
639
|
-
path:
|
|
635
|
+
path: args.path ?? []
|
|
640
636
|
};
|
|
641
637
|
const pointerKey = {};
|
|
642
638
|
pointerMetaWeakMap.set(pointerKey, meta);
|
|
@@ -675,7 +671,7 @@ var hoop = (s, path, reducer) => {
|
|
|
675
671
|
const newVal = hoop(oldVal, restOfPath, reducer);
|
|
676
672
|
if (oldVal === newVal)
|
|
677
673
|
return s;
|
|
678
|
-
const newS =
|
|
674
|
+
const newS = { ...s, [key]: newVal };
|
|
679
675
|
return newS;
|
|
680
676
|
} else {
|
|
681
677
|
const [key, ...restOfPath] = path;
|
|
@@ -762,7 +758,10 @@ function getSharedMechanism() {
|
|
|
762
758
|
const varName = "__dataverse_discoveryMechanism_sharedStack";
|
|
763
759
|
const root2 = typeof window !== "undefined" ? window : typeof global !== "undefined" ? global : {};
|
|
764
760
|
if (root2) {
|
|
765
|
-
const existingMechanism =
|
|
761
|
+
const existingMechanism = (
|
|
762
|
+
// @ts-ignore ignore
|
|
763
|
+
root2[varName]
|
|
764
|
+
);
|
|
766
765
|
if (existingMechanism && typeof existingMechanism === "object" && existingMechanism.type === "Dataverse_discoveryMechanism") {
|
|
767
766
|
return existingMechanism;
|
|
768
767
|
} else {
|
|
@@ -792,12 +791,28 @@ var HotHandle = class {
|
|
|
792
791
|
this._prismInstance = _prismInstance;
|
|
793
792
|
this._didMarkDependentsAsStale = false;
|
|
794
793
|
this._isFresh = false;
|
|
795
|
-
this._cacheOfDendencyValues = new Map();
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
this.
|
|
794
|
+
this._cacheOfDendencyValues = /* @__PURE__ */ new Map();
|
|
795
|
+
/**
|
|
796
|
+
* @internal
|
|
797
|
+
*/
|
|
798
|
+
this._dependents = /* @__PURE__ */ new Set();
|
|
799
|
+
/**
|
|
800
|
+
* @internal
|
|
801
|
+
*/
|
|
802
|
+
this._dependencies = /* @__PURE__ */ new Set();
|
|
803
|
+
this._possiblyStaleDeps = /* @__PURE__ */ new Set();
|
|
804
|
+
this._scope = new HotScope(
|
|
805
|
+
this
|
|
806
|
+
);
|
|
807
|
+
/**
|
|
808
|
+
* @internal
|
|
809
|
+
*/
|
|
800
810
|
this._lastValue = void 0;
|
|
811
|
+
/**
|
|
812
|
+
* If true, the prism is stale even though its dependencies aren't
|
|
813
|
+
* marked as such. This is used by `prism.source()` and `prism.state()`
|
|
814
|
+
* to mark the prism as stale.
|
|
815
|
+
*/
|
|
801
816
|
this._forciblySetToStale = false;
|
|
802
817
|
this._reactToDependencyGoingStale = (which) => {
|
|
803
818
|
this._possiblyStaleDeps.add(which);
|
|
@@ -854,7 +869,7 @@ var HotHandle = class {
|
|
|
854
869
|
}
|
|
855
870
|
}
|
|
856
871
|
}
|
|
857
|
-
const newDeps = new Set();
|
|
872
|
+
const newDeps = /* @__PURE__ */ new Set();
|
|
858
873
|
this._cacheOfDendencyValues.clear();
|
|
859
874
|
const collector = (observedDep) => {
|
|
860
875
|
newDeps.add(observedDep);
|
|
@@ -869,7 +884,10 @@ var HotHandle = class {
|
|
|
869
884
|
} finally {
|
|
870
885
|
const topOfTheStack = hookScopeStack.pop();
|
|
871
886
|
if (topOfTheStack !== this._scope) {
|
|
872
|
-
console.warn(
|
|
887
|
+
console.warn(
|
|
888
|
+
// @todo guide the user to report the bug in an issue
|
|
889
|
+
`The Prism hook stack has slipped. This is a bug.`
|
|
890
|
+
);
|
|
873
891
|
}
|
|
874
892
|
}
|
|
875
893
|
popCollector(collector);
|
|
@@ -899,12 +917,18 @@ var HotHandle = class {
|
|
|
899
917
|
dependent(this._prismInstance);
|
|
900
918
|
}
|
|
901
919
|
}
|
|
920
|
+
/**
|
|
921
|
+
* @internal
|
|
922
|
+
*/
|
|
902
923
|
_addDependency(d) {
|
|
903
924
|
if (this._dependencies.has(d))
|
|
904
925
|
return;
|
|
905
926
|
this._dependencies.add(d);
|
|
906
927
|
d._addDependent(this._reactToDependencyGoingStale);
|
|
907
928
|
}
|
|
929
|
+
/**
|
|
930
|
+
* @internal
|
|
931
|
+
*/
|
|
908
932
|
_removeDependency(d) {
|
|
909
933
|
if (!this._dependencies.has(d))
|
|
910
934
|
return;
|
|
@@ -916,12 +940,18 @@ var emptyObject = {};
|
|
|
916
940
|
var PrismInstance = class {
|
|
917
941
|
constructor(_fn) {
|
|
918
942
|
this._fn = _fn;
|
|
943
|
+
/**
|
|
944
|
+
* Whether the object is a prism.
|
|
945
|
+
*/
|
|
919
946
|
this.isPrism = true;
|
|
920
947
|
this._state = {
|
|
921
948
|
hot: false,
|
|
922
949
|
handle: void 0
|
|
923
950
|
};
|
|
924
951
|
}
|
|
952
|
+
/**
|
|
953
|
+
* Whether the prism is hot.
|
|
954
|
+
*/
|
|
925
955
|
get isHot() {
|
|
926
956
|
return this._state.hot;
|
|
927
957
|
}
|
|
@@ -929,7 +959,12 @@ var PrismInstance = class {
|
|
|
929
959
|
const dependent = () => {
|
|
930
960
|
ticker.onThisOrNextTick(refresh);
|
|
931
961
|
};
|
|
932
|
-
let lastValue =
|
|
962
|
+
let lastValue = (
|
|
963
|
+
// use an empty object as the initial value so that the listener is called on the first tick.
|
|
964
|
+
// if we were to use, say, undefined, and this.getValue() also returned undefined, the listener
|
|
965
|
+
// would never be called.
|
|
966
|
+
emptyObject
|
|
967
|
+
);
|
|
933
968
|
const refresh = () => {
|
|
934
969
|
const newValue = this.getValue();
|
|
935
970
|
if (newValue === lastValue)
|
|
@@ -949,6 +984,9 @@ var PrismInstance = class {
|
|
|
949
984
|
};
|
|
950
985
|
return unsubscribe;
|
|
951
986
|
}
|
|
987
|
+
/**
|
|
988
|
+
* Calls `callback` every time the prism's state goes from `fresh-\>stale.` Returns an `unsubscribe()` function.
|
|
989
|
+
*/
|
|
952
990
|
onStale(callback) {
|
|
953
991
|
const untap = () => {
|
|
954
992
|
this._removeDependent(fn);
|
|
@@ -957,10 +995,20 @@ var PrismInstance = class {
|
|
|
957
995
|
this._addDependent(fn);
|
|
958
996
|
return untap;
|
|
959
997
|
}
|
|
998
|
+
/**
|
|
999
|
+
* Keep the prism hot, even if there are no tappers (subscribers).
|
|
1000
|
+
*/
|
|
960
1001
|
keepHot() {
|
|
961
1002
|
return this.onStale(() => {
|
|
962
1003
|
});
|
|
963
1004
|
}
|
|
1005
|
+
/**
|
|
1006
|
+
* Add a prism as a dependent of this prism.
|
|
1007
|
+
*
|
|
1008
|
+
* @param d - The prism to be made a dependent of this prism.
|
|
1009
|
+
*
|
|
1010
|
+
* @see _removeDependent
|
|
1011
|
+
*/
|
|
964
1012
|
_addDependent(d) {
|
|
965
1013
|
if (!this._state.hot) {
|
|
966
1014
|
this._goHot();
|
|
@@ -974,6 +1022,13 @@ var PrismInstance = class {
|
|
|
974
1022
|
handle: hotHandle
|
|
975
1023
|
};
|
|
976
1024
|
}
|
|
1025
|
+
/**
|
|
1026
|
+
* Remove a prism as a dependent of this prism.
|
|
1027
|
+
*
|
|
1028
|
+
* @param d - The prism to be removed from as a dependent of this prism.
|
|
1029
|
+
*
|
|
1030
|
+
* @see _addDependent
|
|
1031
|
+
*/
|
|
977
1032
|
_removeDependent(d) {
|
|
978
1033
|
const state2 = this._state;
|
|
979
1034
|
if (!state2.hot) {
|
|
@@ -986,6 +1041,9 @@ var PrismInstance = class {
|
|
|
986
1041
|
handle.destroy();
|
|
987
1042
|
}
|
|
988
1043
|
}
|
|
1044
|
+
/**
|
|
1045
|
+
* Gets the current value of the prism. If the value is stale, it causes the prism to freshen.
|
|
1046
|
+
*/
|
|
989
1047
|
getValue() {
|
|
990
1048
|
reportResolutionStart(this);
|
|
991
1049
|
const state2 = this._state;
|
|
@@ -999,14 +1057,17 @@ var PrismInstance = class {
|
|
|
999
1057
|
return val2;
|
|
1000
1058
|
}
|
|
1001
1059
|
};
|
|
1002
|
-
var HotScope = class {
|
|
1060
|
+
var HotScope = class _HotScope {
|
|
1003
1061
|
constructor(_hotHandle) {
|
|
1004
1062
|
this._hotHandle = _hotHandle;
|
|
1005
|
-
this._refs = new Map();
|
|
1063
|
+
this._refs = /* @__PURE__ */ new Map();
|
|
1006
1064
|
this.isPrismScope = true;
|
|
1065
|
+
// NOTE probably not a great idea to eager-allocate all of these objects/maps for every scope,
|
|
1066
|
+
// especially because most wouldn't get used in the majority of cases. However, back when these
|
|
1067
|
+
// were stored on weakmaps, they were uncomfortable to inspect in the debugger.
|
|
1007
1068
|
this.subs = {};
|
|
1008
|
-
this.effects = new Map();
|
|
1009
|
-
this.memos = new Map();
|
|
1069
|
+
this.effects = /* @__PURE__ */ new Map();
|
|
1070
|
+
this.memos = /* @__PURE__ */ new Map();
|
|
1010
1071
|
}
|
|
1011
1072
|
ref(key, initialValue) {
|
|
1012
1073
|
let ref2 = this._refs.get(key);
|
|
@@ -1042,6 +1103,7 @@ var HotScope = class {
|
|
|
1042
1103
|
if (memo2 === void 0) {
|
|
1043
1104
|
memo2 = {
|
|
1044
1105
|
cachedValue: null,
|
|
1106
|
+
// undefined will always indicate "deps have changed", so we set its initial value as such
|
|
1045
1107
|
deps: void 0
|
|
1046
1108
|
};
|
|
1047
1109
|
this.memos.set(key, memo2);
|
|
@@ -1055,19 +1117,23 @@ var HotScope = class {
|
|
|
1055
1117
|
return memo2.cachedValue;
|
|
1056
1118
|
}
|
|
1057
1119
|
state(key, initialValue) {
|
|
1058
|
-
const { value, setValue } = this.memo(
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
value2
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1120
|
+
const { value, setValue } = this.memo(
|
|
1121
|
+
"state/" + key,
|
|
1122
|
+
() => {
|
|
1123
|
+
const value2 = { current: initialValue };
|
|
1124
|
+
const setValue2 = (newValue) => {
|
|
1125
|
+
value2.current = newValue;
|
|
1126
|
+
this._hotHandle.forceStale();
|
|
1127
|
+
};
|
|
1128
|
+
return { value: value2, setValue: setValue2 };
|
|
1129
|
+
},
|
|
1130
|
+
[]
|
|
1131
|
+
);
|
|
1066
1132
|
return [value.current, setValue];
|
|
1067
1133
|
}
|
|
1068
1134
|
sub(key) {
|
|
1069
1135
|
if (!this.subs[key]) {
|
|
1070
|
-
this.subs[key] = new
|
|
1136
|
+
this.subs[key] = new _HotScope(this._hotHandle);
|
|
1071
1137
|
}
|
|
1072
1138
|
return this.subs[key];
|
|
1073
1139
|
}
|
|
@@ -1079,12 +1145,16 @@ var HotScope = class {
|
|
|
1079
1145
|
}
|
|
1080
1146
|
source(subscribe, getValue2) {
|
|
1081
1147
|
const sourceKey = "$$source/blah";
|
|
1082
|
-
this.effect(
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1148
|
+
this.effect(
|
|
1149
|
+
sourceKey,
|
|
1150
|
+
() => {
|
|
1151
|
+
const unsub = subscribe(() => {
|
|
1152
|
+
this._hotHandle.forceStale();
|
|
1153
|
+
});
|
|
1154
|
+
return unsub;
|
|
1155
|
+
},
|
|
1156
|
+
[subscribe]
|
|
1157
|
+
);
|
|
1088
1158
|
return getValue2();
|
|
1089
1159
|
}
|
|
1090
1160
|
};
|
|
@@ -1179,7 +1249,7 @@ function source(subscribe, getValue2) {
|
|
|
1179
1249
|
var prism = (fn) => {
|
|
1180
1250
|
return new PrismInstance(fn);
|
|
1181
1251
|
};
|
|
1182
|
-
var ColdScope = class {
|
|
1252
|
+
var ColdScope = class _ColdScope {
|
|
1183
1253
|
effect(key, cb, deps) {
|
|
1184
1254
|
console.warn(`prism.effect() does not run in cold prisms`);
|
|
1185
1255
|
}
|
|
@@ -1194,7 +1264,7 @@ var ColdScope = class {
|
|
|
1194
1264
|
return { current: initialValue };
|
|
1195
1265
|
}
|
|
1196
1266
|
sub(key) {
|
|
1197
|
-
return new
|
|
1267
|
+
return new _ColdScope();
|
|
1198
1268
|
}
|
|
1199
1269
|
source(subscribe, getValue2) {
|
|
1200
1270
|
return getValue2();
|
|
@@ -1211,7 +1281,10 @@ function calculateColdPrism(fn) {
|
|
|
1211
1281
|
} finally {
|
|
1212
1282
|
const topOfTheStack = hookScopeStack.pop();
|
|
1213
1283
|
if (topOfTheStack !== scope2) {
|
|
1214
|
-
console.warn(
|
|
1284
|
+
console.warn(
|
|
1285
|
+
// @todo guide the user to report the bug in an issue
|
|
1286
|
+
`The Prism hook stack has slipped. This is a bug.`
|
|
1287
|
+
);
|
|
1215
1288
|
}
|
|
1216
1289
|
}
|
|
1217
1290
|
return value;
|
|
@@ -1228,23 +1301,17 @@ prism.source = source;
|
|
|
1228
1301
|
var prism_default = prism;
|
|
1229
1302
|
|
|
1230
1303
|
// src/Atom.ts
|
|
1231
|
-
var ValueTypes;
|
|
1232
|
-
(function(ValueTypes2) {
|
|
1233
|
-
ValueTypes2[ValueTypes2["Dict"] = 0] = "Dict";
|
|
1234
|
-
ValueTypes2[ValueTypes2["Array"] = 1] = "Array";
|
|
1235
|
-
ValueTypes2[ValueTypes2["Other"] = 2] = "Other";
|
|
1236
|
-
})(ValueTypes || (ValueTypes = {}));
|
|
1237
1304
|
var getTypeOfValue = (v) => {
|
|
1238
1305
|
if (Array.isArray(v))
|
|
1239
|
-
return 1
|
|
1306
|
+
return 1 /* Array */;
|
|
1240
1307
|
if (isPlainObject_default(v))
|
|
1241
|
-
return 0
|
|
1242
|
-
return 2
|
|
1308
|
+
return 0 /* Dict */;
|
|
1309
|
+
return 2 /* Other */;
|
|
1243
1310
|
};
|
|
1244
1311
|
var getKeyOfValue = (v, key, vType = getTypeOfValue(v)) => {
|
|
1245
|
-
if (vType === 0 && typeof key === "string") {
|
|
1312
|
+
if (vType === 0 /* Dict */ && typeof key === "string") {
|
|
1246
1313
|
return v[key];
|
|
1247
|
-
} else if (vType === 1 && isValidArrayIndex(key)) {
|
|
1314
|
+
} else if (vType === 1 /* Array */ && isValidArrayIndex(key)) {
|
|
1248
1315
|
return v[key];
|
|
1249
1316
|
} else {
|
|
1250
1317
|
return void 0;
|
|
@@ -1254,12 +1321,12 @@ var isValidArrayIndex = (key) => {
|
|
|
1254
1321
|
const inNumber = typeof key === "number" ? key : parseInt(key, 10);
|
|
1255
1322
|
return !isNaN(inNumber) && inNumber >= 0 && inNumber < Infinity && (inNumber | 0) === inNumber;
|
|
1256
1323
|
};
|
|
1257
|
-
var Scope = class {
|
|
1324
|
+
var Scope = class _Scope {
|
|
1258
1325
|
constructor(_parent, _path) {
|
|
1259
1326
|
this._parent = _parent;
|
|
1260
1327
|
this._path = _path;
|
|
1261
|
-
this.children = new Map();
|
|
1262
|
-
this.identityChangeListeners = new Set();
|
|
1328
|
+
this.children = /* @__PURE__ */ new Map();
|
|
1329
|
+
this.identityChangeListeners = /* @__PURE__ */ new Set();
|
|
1263
1330
|
}
|
|
1264
1331
|
addIdentityChangeListener(cb) {
|
|
1265
1332
|
this.identityChangeListeners.add(cb);
|
|
@@ -1278,7 +1345,7 @@ var Scope = class {
|
|
|
1278
1345
|
getOrCreateChild(key) {
|
|
1279
1346
|
let child = this.children.get(key);
|
|
1280
1347
|
if (!child) {
|
|
1281
|
-
child = child = new
|
|
1348
|
+
child = child = new _Scope(this, this._path.concat([key]));
|
|
1282
1349
|
this.children.set(key, child);
|
|
1283
1350
|
}
|
|
1284
1351
|
return child;
|
|
@@ -1295,9 +1362,20 @@ var Scope = class {
|
|
|
1295
1362
|
};
|
|
1296
1363
|
var Atom = class {
|
|
1297
1364
|
constructor(initialState) {
|
|
1365
|
+
/**
|
|
1366
|
+
* @internal
|
|
1367
|
+
*/
|
|
1298
1368
|
this.$$isPointerToPrismProvider = true;
|
|
1369
|
+
/**
|
|
1370
|
+
* Convenience property that gives you a pointer to the root of the atom.
|
|
1371
|
+
*
|
|
1372
|
+
* @remarks
|
|
1373
|
+
* Equivalent to `pointer({ root: thisAtom, path: [] })`.
|
|
1374
|
+
*/
|
|
1299
1375
|
this.pointer = pointer_default({ root: this, path: [] });
|
|
1300
|
-
this.prism = this.pointerToPrism(
|
|
1376
|
+
this.prism = this.pointerToPrism(
|
|
1377
|
+
this.pointer
|
|
1378
|
+
);
|
|
1301
1379
|
this._onPointerValueChange = (pointer2, cb) => {
|
|
1302
1380
|
const { path } = getPointerParts(pointer2);
|
|
1303
1381
|
const scope2 = this._getOrCreateScopeForPath(path);
|
|
@@ -1310,6 +1388,11 @@ var Atom = class {
|
|
|
1310
1388
|
this._currentState = initialState;
|
|
1311
1389
|
this._rootScope = new Scope(void 0, []);
|
|
1312
1390
|
}
|
|
1391
|
+
/**
|
|
1392
|
+
* Sets the state of the atom.
|
|
1393
|
+
*
|
|
1394
|
+
* @param newState - The new state of the atom.
|
|
1395
|
+
*/
|
|
1313
1396
|
set(newState) {
|
|
1314
1397
|
const oldState = this._currentState;
|
|
1315
1398
|
this._currentState = newState;
|
|
@@ -1318,23 +1401,62 @@ var Atom = class {
|
|
|
1318
1401
|
get() {
|
|
1319
1402
|
return this._currentState;
|
|
1320
1403
|
}
|
|
1404
|
+
/**
|
|
1405
|
+
* Returns the value at the given pointer
|
|
1406
|
+
*
|
|
1407
|
+
* @param pointerOrFn - A pointer to the desired path. Could also be a function returning a pointer
|
|
1408
|
+
*
|
|
1409
|
+
* Example
|
|
1410
|
+
* ```ts
|
|
1411
|
+
* const atom = atom({ a: { b: 1 } })
|
|
1412
|
+
* atom.getByPointer(atom.pointer.a.b) // 1
|
|
1413
|
+
* atom.getByPointer((p) => p.a.b) // 1
|
|
1414
|
+
* ```
|
|
1415
|
+
*/
|
|
1321
1416
|
getByPointer(pointerOrFn) {
|
|
1322
1417
|
const pointer2 = isPointer(pointerOrFn) ? pointerOrFn : pointerOrFn(this.pointer);
|
|
1323
1418
|
const path = getPointerParts(pointer2).path;
|
|
1324
1419
|
return this._getIn(path);
|
|
1325
1420
|
}
|
|
1421
|
+
/**
|
|
1422
|
+
* Gets the state of the atom at `path`.
|
|
1423
|
+
*/
|
|
1326
1424
|
_getIn(path) {
|
|
1327
1425
|
return path.length === 0 ? this.get() : get_default(this.get(), path);
|
|
1328
1426
|
}
|
|
1329
1427
|
reduce(fn) {
|
|
1330
1428
|
this.set(fn(this.get()));
|
|
1331
1429
|
}
|
|
1430
|
+
/**
|
|
1431
|
+
* Reduces the value at the given pointer
|
|
1432
|
+
*
|
|
1433
|
+
* @param pointerOrFn - A pointer to the desired path. Could also be a function returning a pointer
|
|
1434
|
+
*
|
|
1435
|
+
* Example
|
|
1436
|
+
* ```ts
|
|
1437
|
+
* const atom = atom({ a: { b: 1 } })
|
|
1438
|
+
* atom.reduceByPointer(atom.pointer.a.b, (b) => b + 1) // atom.get().a.b === 2
|
|
1439
|
+
* atom.reduceByPointer((p) => p.a.b, (b) => b + 1) // atom.get().a.b === 2
|
|
1440
|
+
* ```
|
|
1441
|
+
*/
|
|
1332
1442
|
reduceByPointer(pointerOrFn, reducer) {
|
|
1333
1443
|
const pointer2 = isPointer(pointerOrFn) ? pointerOrFn : pointerOrFn(this.pointer);
|
|
1334
1444
|
const path = getPointerParts(pointer2).path;
|
|
1335
1445
|
const newState = updateDeep(this.get(), path, reducer);
|
|
1336
1446
|
this.set(newState);
|
|
1337
1447
|
}
|
|
1448
|
+
/**
|
|
1449
|
+
* Sets the value at the given pointer
|
|
1450
|
+
*
|
|
1451
|
+
* @param pointerOrFn - A pointer to the desired path. Could also be a function returning a pointer
|
|
1452
|
+
*
|
|
1453
|
+
* Example
|
|
1454
|
+
* ```ts
|
|
1455
|
+
* const atom = atom({ a: { b: 1 } })
|
|
1456
|
+
* atom.setByPointer(atom.pointer.a.b, 2) // atom.get().a.b === 2
|
|
1457
|
+
* atom.setByPointer((p) => p.a.b, 2) // atom.get().a.b === 2
|
|
1458
|
+
* ```
|
|
1459
|
+
*/
|
|
1338
1460
|
setByPointer(pointerOrFn, val2) {
|
|
1339
1461
|
this.reduceByPointer(pointerOrFn, () => val2);
|
|
1340
1462
|
}
|
|
@@ -1348,7 +1470,7 @@ var Atom = class {
|
|
|
1348
1470
|
return;
|
|
1349
1471
|
const oldValueType = getTypeOfValue(oldState);
|
|
1350
1472
|
const newValueType = getTypeOfValue(newState);
|
|
1351
|
-
if (oldValueType === 2 && oldValueType === newValueType)
|
|
1473
|
+
if (oldValueType === 2 /* Other */ && oldValueType === newValueType)
|
|
1352
1474
|
return;
|
|
1353
1475
|
for (const [childKey, childScope] of scope2.children) {
|
|
1354
1476
|
const oldChildVal = getKeyOfValue(oldState, childKey, oldValueType);
|
|
@@ -1363,6 +1485,16 @@ var Atom = class {
|
|
|
1363
1485
|
}
|
|
1364
1486
|
return curScope;
|
|
1365
1487
|
}
|
|
1488
|
+
/**
|
|
1489
|
+
* Returns a new prism of the value at the provided path.
|
|
1490
|
+
*
|
|
1491
|
+
* @param pointer - The path to create the prism at.
|
|
1492
|
+
*
|
|
1493
|
+
* ```ts
|
|
1494
|
+
* const pr = atom({ a: { b: 1 } }).pointerToPrism(atom.pointer.a.b)
|
|
1495
|
+
* pr.getValue() // 1
|
|
1496
|
+
* ```
|
|
1497
|
+
*/
|
|
1366
1498
|
pointerToPrism(pointer2) {
|
|
1367
1499
|
const { path } = getPointerParts(pointer2);
|
|
1368
1500
|
const subscribe = (listener) => this._onPointerValueChange(pointer2, listener);
|
|
@@ -1374,7 +1506,7 @@ var Atom = class {
|
|
|
1374
1506
|
};
|
|
1375
1507
|
|
|
1376
1508
|
// src/pointerToPrism.ts
|
|
1377
|
-
var identifyPrismWeakMap = new WeakMap();
|
|
1509
|
+
var identifyPrismWeakMap = /* @__PURE__ */ new WeakMap();
|
|
1378
1510
|
function isPointerToPrismProvider(val2) {
|
|
1379
1511
|
return typeof val2 === "object" && val2 !== null && val2["$$isPointerToPrismProvider"] === true;
|
|
1380
1512
|
}
|
|
@@ -1384,7 +1516,9 @@ var pointerToPrism = (pointer2) => {
|
|
|
1384
1516
|
if (!prismInstance) {
|
|
1385
1517
|
const root2 = meta.root;
|
|
1386
1518
|
if (!isPointerToPrismProvider(root2)) {
|
|
1387
|
-
throw new Error(
|
|
1519
|
+
throw new Error(
|
|
1520
|
+
`Cannot run pointerToPrism() on a pointer whose root is not an PointerToPrismProvider`
|
|
1521
|
+
);
|
|
1388
1522
|
}
|
|
1389
1523
|
prismInstance = root2.pointerToPrism(pointer2);
|
|
1390
1524
|
identifyPrismWeakMap.set(meta, prismInstance);
|
|
@@ -1430,38 +1564,91 @@ function* iterateAndCountTicks(pointerOrPrism) {
|
|
|
1430
1564
|
|
|
1431
1565
|
// src/Ticker.ts
|
|
1432
1566
|
var EMPTY_TICKS_BEFORE_GOING_DORMANT = 60 * 3;
|
|
1433
|
-
var Ticker = class {
|
|
1567
|
+
var Ticker = class _Ticker {
|
|
1434
1568
|
constructor(_conf) {
|
|
1435
1569
|
this._conf = _conf;
|
|
1436
1570
|
this._ticking = false;
|
|
1571
|
+
/**
|
|
1572
|
+
* Whether the Ticker is dormant
|
|
1573
|
+
*/
|
|
1437
1574
|
this._dormant = true;
|
|
1438
1575
|
this._numberOfDormantTicks = 0;
|
|
1576
|
+
/**
|
|
1577
|
+
* Counts up for every tick executed.
|
|
1578
|
+
* Internally, this is used to measure ticks per second.
|
|
1579
|
+
*
|
|
1580
|
+
* This is "public" to TypeScript, because it's a tool for performance measurements.
|
|
1581
|
+
* Consider this as experimental, and do not rely on it always being here in future releases.
|
|
1582
|
+
*/
|
|
1439
1583
|
this.__ticks = 0;
|
|
1440
|
-
this._scheduledForThisOrNextTick = new Set();
|
|
1441
|
-
this._scheduledForNextTick = new Set();
|
|
1584
|
+
this._scheduledForThisOrNextTick = /* @__PURE__ */ new Set();
|
|
1585
|
+
this._scheduledForNextTick = /* @__PURE__ */ new Set();
|
|
1442
1586
|
this._timeAtCurrentTick = 0;
|
|
1443
1587
|
}
|
|
1588
|
+
/**
|
|
1589
|
+
* Whether the Ticker is dormant
|
|
1590
|
+
*/
|
|
1444
1591
|
get dormant() {
|
|
1445
1592
|
return this._dormant;
|
|
1446
1593
|
}
|
|
1594
|
+
/**
|
|
1595
|
+
* Registers for fn to be called either on this tick or the next tick.
|
|
1596
|
+
*
|
|
1597
|
+
* If `onThisOrNextTick()` is called while `Ticker.tick()` is running, the
|
|
1598
|
+
* side effect _will_ be called within the running tick. If you don't want this
|
|
1599
|
+
* behavior, you can use `onNextTick()`.
|
|
1600
|
+
*
|
|
1601
|
+
* Note that `fn` will be added to a `Set()`. Which means, if you call `onThisOrNextTick(fn)`
|
|
1602
|
+
* with the same fn twice in a single tick, it'll only run once.
|
|
1603
|
+
*
|
|
1604
|
+
* @param fn - The function to be registered.
|
|
1605
|
+
*
|
|
1606
|
+
* @see offThisOrNextTick
|
|
1607
|
+
*/
|
|
1447
1608
|
onThisOrNextTick(fn) {
|
|
1448
1609
|
this._scheduledForThisOrNextTick.add(fn);
|
|
1449
1610
|
if (this._dormant) {
|
|
1450
1611
|
this._goActive();
|
|
1451
1612
|
}
|
|
1452
1613
|
}
|
|
1614
|
+
/**
|
|
1615
|
+
* Registers a side effect to be called on the next tick.
|
|
1616
|
+
*
|
|
1617
|
+
* @param fn - The function to be registered.
|
|
1618
|
+
*
|
|
1619
|
+
* @see onThisOrNextTick
|
|
1620
|
+
* @see offNextTick
|
|
1621
|
+
*/
|
|
1453
1622
|
onNextTick(fn) {
|
|
1454
1623
|
this._scheduledForNextTick.add(fn);
|
|
1455
1624
|
if (this._dormant) {
|
|
1456
1625
|
this._goActive();
|
|
1457
1626
|
}
|
|
1458
1627
|
}
|
|
1628
|
+
/**
|
|
1629
|
+
* De-registers a fn to be called either on this tick or the next tick.
|
|
1630
|
+
*
|
|
1631
|
+
* @param fn - The function to be de-registered.
|
|
1632
|
+
*
|
|
1633
|
+
* @see onThisOrNextTick
|
|
1634
|
+
*/
|
|
1459
1635
|
offThisOrNextTick(fn) {
|
|
1460
1636
|
this._scheduledForThisOrNextTick.delete(fn);
|
|
1461
1637
|
}
|
|
1638
|
+
/**
|
|
1639
|
+
* De-registers a fn to be called on the next tick.
|
|
1640
|
+
*
|
|
1641
|
+
* @param fn - The function to be de-registered.
|
|
1642
|
+
*
|
|
1643
|
+
* @see onNextTick
|
|
1644
|
+
*/
|
|
1462
1645
|
offNextTick(fn) {
|
|
1463
1646
|
this._scheduledForNextTick.delete(fn);
|
|
1464
1647
|
}
|
|
1648
|
+
/**
|
|
1649
|
+
* The time at the start of the current tick if there is a tick in progress, otherwise defaults to
|
|
1650
|
+
* `performance.now()`.
|
|
1651
|
+
*/
|
|
1465
1652
|
get time() {
|
|
1466
1653
|
if (this._ticking) {
|
|
1467
1654
|
return this._timeAtCurrentTick;
|
|
@@ -1469,24 +1656,32 @@ var Ticker = class {
|
|
|
1469
1656
|
return performance.now();
|
|
1470
1657
|
}
|
|
1471
1658
|
_goActive() {
|
|
1472
|
-
var _a, _b;
|
|
1473
1659
|
if (!this._dormant)
|
|
1474
1660
|
return;
|
|
1475
1661
|
this._dormant = false;
|
|
1476
|
-
|
|
1662
|
+
this._conf?.onActive?.();
|
|
1477
1663
|
}
|
|
1478
1664
|
_goDormant() {
|
|
1479
|
-
var _a, _b;
|
|
1480
1665
|
if (this._dormant)
|
|
1481
1666
|
return;
|
|
1482
1667
|
this._dormant = true;
|
|
1483
1668
|
this._numberOfDormantTicks = 0;
|
|
1484
|
-
|
|
1485
|
-
}
|
|
1669
|
+
this._conf?.onDormant?.();
|
|
1670
|
+
}
|
|
1671
|
+
/**
|
|
1672
|
+
* Triggers a tick which starts executing the callbacks scheduled for this tick.
|
|
1673
|
+
*
|
|
1674
|
+
* @param t - The time at the tick.
|
|
1675
|
+
*
|
|
1676
|
+
* @see onThisOrNextTick
|
|
1677
|
+
* @see onNextTick
|
|
1678
|
+
*/
|
|
1486
1679
|
tick(t = performance.now()) {
|
|
1487
1680
|
if (process.env.NODE_ENV === "development") {
|
|
1488
|
-
if (!(this instanceof
|
|
1489
|
-
throw new Error(
|
|
1681
|
+
if (!(this instanceof _Ticker)) {
|
|
1682
|
+
throw new Error(
|
|
1683
|
+
'ticker.tick must be called while bound to the ticker. As in, "ticker.tick(time)" or "requestAnimationFrame((t) => ticker.tick(t))" for performance.'
|
|
1684
|
+
);
|
|
1490
1685
|
}
|
|
1491
1686
|
}
|
|
1492
1687
|
this.__ticks++;
|
|
@@ -1517,7 +1712,7 @@ var Ticker = class {
|
|
|
1517
1712
|
throw new Error(`Maximum recursion limit for _tick()`);
|
|
1518
1713
|
}
|
|
1519
1714
|
const oldSet = this._scheduledForThisOrNextTick;
|
|
1520
|
-
this._scheduledForThisOrNextTick = new Set();
|
|
1715
|
+
this._scheduledForThisOrNextTick = /* @__PURE__ */ new Set();
|
|
1521
1716
|
for (const fn of oldSet) {
|
|
1522
1717
|
fn(time);
|
|
1523
1718
|
}
|
|
@@ -1553,18 +1748,33 @@ function* iterateOver(pointerOrPrism) {
|
|
|
1553
1748
|
// src/PointerProxy.ts
|
|
1554
1749
|
var PointerProxy = class {
|
|
1555
1750
|
constructor(currentPointer) {
|
|
1751
|
+
/**
|
|
1752
|
+
* @internal
|
|
1753
|
+
*/
|
|
1556
1754
|
this.$$isPointerToPrismProvider = true;
|
|
1557
1755
|
this._currentPointerBox = new Atom(currentPointer);
|
|
1558
1756
|
this.pointer = pointer_default({ root: this, path: [] });
|
|
1559
1757
|
}
|
|
1758
|
+
/**
|
|
1759
|
+
* Sets the underlying pointer.
|
|
1760
|
+
* @param p - The pointer to be proxied.
|
|
1761
|
+
*/
|
|
1560
1762
|
setPointer(p) {
|
|
1561
1763
|
this._currentPointerBox.set(p);
|
|
1562
1764
|
}
|
|
1765
|
+
/**
|
|
1766
|
+
* Returns a prism of the value at the provided sub-path of the proxied pointer.
|
|
1767
|
+
*
|
|
1768
|
+
* @param path - The path to create the prism at.
|
|
1769
|
+
*/
|
|
1563
1770
|
pointerToPrism(pointer2) {
|
|
1564
1771
|
const { path } = getPointerMeta(pointer2);
|
|
1565
1772
|
return prism_default(() => {
|
|
1566
1773
|
const currentPointer = this._currentPointerBox.prism.getValue();
|
|
1567
|
-
const subPointer = path.reduce(
|
|
1774
|
+
const subPointer = path.reduce(
|
|
1775
|
+
(pointerSoFar, pathItem) => pointerSoFar[pathItem],
|
|
1776
|
+
currentPointer
|
|
1777
|
+
);
|
|
1568
1778
|
return val(subPointer);
|
|
1569
1779
|
});
|
|
1570
1780
|
}
|