atom.io 0.14.0 → 0.14.1
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/data/dist/index.cjs +1473 -52
- package/data/dist/index.cjs.map +1 -1
- package/data/dist/index.d.cts +315 -16
- package/data/dist/index.js +10 -267
- package/data/dist/index.js.map +1 -1
- package/data/package.json +5 -5
- package/data/src/join.ts +3 -3
- package/dist/chunk-5VJ77LZE.js +176 -0
- package/dist/chunk-5VJ77LZE.js.map +1 -0
- package/dist/chunk-CK7GNCU5.js +309 -0
- package/dist/chunk-CK7GNCU5.js.map +1 -0
- package/dist/chunk-KW7RA7IM.js +67 -0
- package/dist/chunk-KW7RA7IM.js.map +1 -0
- package/dist/chunk-LFXB7Y6M.js +68 -0
- package/dist/chunk-LFXB7Y6M.js.map +1 -0
- package/dist/chunk-NYCVSXQB.js +181 -0
- package/dist/chunk-NYCVSXQB.js.map +1 -0
- package/dist/chunk-TE3ZSTQ6.js +2516 -0
- package/dist/chunk-TE3ZSTQ6.js.map +1 -0
- package/dist/index.cjs +2244 -55
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +268 -21
- package/dist/index.js +1 -152
- package/dist/index.js.map +1 -1
- package/dist/metafile-cjs.json +1 -1
- package/internal/dist/index.cjs +208 -79
- package/internal/dist/index.cjs.map +1 -1
- package/internal/dist/index.d.cts +226 -27
- package/internal/dist/index.js +1 -2240
- package/internal/dist/index.js.map +1 -1
- package/internal/package.json +5 -5
- package/introspection/dist/index.cjs +1618 -46
- package/introspection/dist/index.cjs.map +1 -1
- package/introspection/dist/index.d.cts +374 -4
- package/introspection/dist/index.js +2 -316
- package/introspection/dist/index.js.map +1 -1
- package/introspection/package.json +5 -5
- package/json/dist/index.cjs +1560 -8
- package/json/dist/index.cjs.map +1 -1
- package/json/dist/index.d.cts +371 -7
- package/json/dist/index.js +1 -76
- package/json/dist/index.js.map +1 -1
- package/json/package.json +5 -5
- package/package.json +5 -4
- package/react/dist/index.cjs +1604 -14
- package/react/dist/index.cjs.map +1 -1
- package/react/dist/index.d.cts +375 -4
- package/react/dist/index.js +2 -68
- package/react/dist/index.js.map +1 -1
- package/react/package.json +5 -5
- package/react-devtools/dist/index.cjs +2268 -198
- package/react-devtools/dist/index.cjs.map +1 -1
- package/react-devtools/dist/index.css +2 -2
- package/react-devtools/dist/index.d.cts +148 -24
- package/react-devtools/dist/index.js +57 -297
- package/react-devtools/dist/index.js.map +1 -1
- package/react-devtools/package.json +5 -5
- package/realtime-client/dist/index.cjs +1585 -35
- package/realtime-client/dist/index.cjs.map +1 -1
- package/realtime-client/dist/index.d.cts +382 -12
- package/realtime-client/dist/index.js +2 -165
- package/realtime-client/dist/index.js.map +1 -1
- package/realtime-client/package.json +5 -5
- package/realtime-react/dist/index.cjs +1782 -35
- package/realtime-react/dist/index.cjs.map +1 -1
- package/realtime-react/dist/index.d.cts +49 -9
- package/realtime-react/dist/index.js +4 -66
- package/realtime-react/dist/index.js.map +1 -1
- package/realtime-react/package.json +5 -5
- package/realtime-server/dist/index.cjs +1604 -58
- package/realtime-server/dist/index.cjs.map +1 -1
- package/realtime-server/dist/index.d.cts +391 -11
- package/realtime-server/dist/index.js +27 -21
- package/realtime-server/dist/index.js.map +1 -1
- package/realtime-server/package.json +6 -6
- package/realtime-testing/dist/index.cjs +2352 -16
- package/realtime-testing/dist/index.cjs.map +1 -1
- package/realtime-testing/dist/index.d.cts +486 -3
- package/realtime-testing/dist/index.js +10 -31
- package/realtime-testing/dist/index.js.map +1 -1
- package/realtime-testing/package.json +5 -5
- package/transceivers/set-rtx/dist/index.cjs +31 -10
- package/transceivers/set-rtx/dist/index.cjs.map +1 -1
- package/transceivers/set-rtx/dist/index.d.cts +29 -3
- package/transceivers/set-rtx/dist/index.js +2 -3
- package/transceivers/set-rtx/dist/index.js.map +1 -1
- package/transceivers/set-rtx/package.json +6 -6
- package/data/dist/index.d.ts +0 -158
- package/data/dist/metafile-cjs.json +0 -1
- package/data/dist/metafile-esm.json +0 -1
- package/dist/index.d.ts +0 -243
- package/dist/metafile-esm.json +0 -1
- package/internal/dist/index.d.ts +0 -421
- package/internal/dist/metafile-cjs.json +0 -1
- package/internal/dist/metafile-esm.json +0 -1
- package/introspection/dist/index.d.ts +0 -24
- package/introspection/dist/metafile-cjs.json +0 -1
- package/introspection/dist/metafile-esm.json +0 -1
- package/json/dist/index.d.ts +0 -51
- package/json/dist/metafile-cjs.json +0 -1
- package/json/dist/metafile-esm.json +0 -1
- package/react/dist/index.d.ts +0 -23
- package/react/dist/metafile-cjs.json +0 -1
- package/react/dist/metafile-esm.json +0 -1
- package/react-devtools/dist/index.d.ts +0 -341
- package/react-devtools/dist/metafile-cjs.json +0 -1
- package/react-devtools/dist/metafile-esm.json +0 -1
- package/realtime-client/dist/index.d.ts +0 -22
- package/realtime-client/dist/metafile-cjs.json +0 -1
- package/realtime-client/dist/metafile-esm.json +0 -1
- package/realtime-react/dist/index.d.ts +0 -27
- package/realtime-react/dist/metafile-cjs.json +0 -1
- package/realtime-react/dist/metafile-esm.json +0 -1
- package/realtime-server/dist/index.d.ts +0 -25
- package/realtime-server/dist/metafile-cjs.json +0 -1
- package/realtime-server/dist/metafile-esm.json +0 -1
- package/realtime-testing/dist/index.d.ts +0 -49
- package/realtime-testing/dist/metafile-cjs.json +0 -1
- package/realtime-testing/dist/metafile-esm.json +0 -1
- package/transceivers/set-rtx/dist/index.d.ts +0 -40
- package/transceivers/set-rtx/dist/metafile-cjs.json +0 -1
- package/transceivers/set-rtx/dist/metafile-esm.json +0 -1
|
@@ -1,11 +1,8 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
4
|
-
var RTC = require('atom.io/realtime-client');
|
|
5
|
-
var React6 = require('react');
|
|
6
|
-
var socket_ioClient = require('socket.io-client');
|
|
3
|
+
var React8 = require('react');
|
|
7
4
|
var jsxRuntime = require('react/jsx-runtime');
|
|
8
|
-
var
|
|
5
|
+
var socket_ioClient = require('socket.io-client');
|
|
9
6
|
|
|
10
7
|
function _interopNamespace(e) {
|
|
11
8
|
if (e && e.__esModule) return e;
|
|
@@ -25,17 +22,1767 @@ function _interopNamespace(e) {
|
|
|
25
22
|
return Object.freeze(n);
|
|
26
23
|
}
|
|
27
24
|
|
|
28
|
-
var
|
|
29
|
-
|
|
30
|
-
var
|
|
25
|
+
var React8__namespace = /*#__PURE__*/_interopNamespace(React8);
|
|
26
|
+
|
|
27
|
+
var __defProp = Object.defineProperty;
|
|
28
|
+
var __defProps = Object.defineProperties;
|
|
29
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
30
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
31
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
32
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
33
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
34
|
+
var __spreadValues = (a, b) => {
|
|
35
|
+
for (var prop in b || (b = {}))
|
|
36
|
+
if (__hasOwnProp.call(b, prop))
|
|
37
|
+
__defNormalProp(a, prop, b[prop]);
|
|
38
|
+
if (__getOwnPropSymbols)
|
|
39
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
40
|
+
if (__propIsEnum.call(b, prop))
|
|
41
|
+
__defNormalProp(a, prop, b[prop]);
|
|
42
|
+
}
|
|
43
|
+
return a;
|
|
44
|
+
};
|
|
45
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
46
|
+
|
|
47
|
+
// src/atom.ts
|
|
48
|
+
function atom(options) {
|
|
49
|
+
if (`mutable` in options) {
|
|
50
|
+
return createMutableAtom(options, IMPLICIT.STORE);
|
|
51
|
+
}
|
|
52
|
+
return createAtom(options, void 0, IMPLICIT.STORE);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// src/get-state.ts
|
|
56
|
+
function getState(token, store = IMPLICIT.STORE) {
|
|
57
|
+
var _a;
|
|
58
|
+
const state = (_a = withdraw(token, store)) != null ? _a : withdrawNewFamilyMember(token, store);
|
|
59
|
+
if (state === void 0) {
|
|
60
|
+
throw new NotFoundError(token, store);
|
|
61
|
+
}
|
|
62
|
+
return readOrComputeValue(state, store);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// src/logger.ts
|
|
66
|
+
var simpleLog = (logLevel) => (icon, tokenType, tokenKey, message, ...rest) => {
|
|
67
|
+
console[logLevel](`${icon} ${tokenType} "${tokenKey}" ${message}`, ...rest);
|
|
68
|
+
};
|
|
69
|
+
var simpleLogger = {
|
|
70
|
+
error: simpleLog(`error`),
|
|
71
|
+
info: simpleLog(`info`),
|
|
72
|
+
warn: simpleLog(`warn`)
|
|
73
|
+
};
|
|
74
|
+
var AtomIOLogger = class {
|
|
75
|
+
constructor(logLevel, filter, logger = simpleLogger) {
|
|
76
|
+
this.logLevel = logLevel;
|
|
77
|
+
this.filter = filter;
|
|
78
|
+
this.logger = logger;
|
|
79
|
+
this.error = (...args) => {
|
|
80
|
+
var _a, _b;
|
|
81
|
+
if (((_b = (_a = this.filter) == null ? void 0 : _a.call(this, ...args)) != null ? _b : true) && this.logLevel !== null) {
|
|
82
|
+
this.logger.error(...args);
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
this.info = (...args) => {
|
|
86
|
+
var _a, _b;
|
|
87
|
+
if (((_b = (_a = this.filter) == null ? void 0 : _a.call(this, ...args)) != null ? _b : true) && this.logLevel === `info`) {
|
|
88
|
+
this.logger.info(...args);
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
this.warn = (...args) => {
|
|
92
|
+
var _a, _b;
|
|
93
|
+
if (((_b = (_a = this.filter) == null ? void 0 : _a.call(this, ...args)) != null ? _b : true) && this.logLevel !== `error` && this.logLevel !== null) {
|
|
94
|
+
this.logger.warn(...args);
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
// src/selector.ts
|
|
101
|
+
function selector(options) {
|
|
102
|
+
return createSelector(options, void 0, IMPLICIT.STORE);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// src/set-state.ts
|
|
106
|
+
function setState(token, value, store = IMPLICIT.STORE) {
|
|
107
|
+
var _a;
|
|
108
|
+
const rejection = openOperation(token, store);
|
|
109
|
+
if (rejection) {
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
const state = (_a = withdraw(token, store)) != null ? _a : withdrawNewFamilyMember(token, store);
|
|
113
|
+
if (state === void 0) {
|
|
114
|
+
throw new NotFoundError(token, store);
|
|
115
|
+
}
|
|
116
|
+
setAtomOrSelector(state, value, store);
|
|
117
|
+
closeOperation(store);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// src/transaction.ts
|
|
121
|
+
var runTransaction = (token, store = IMPLICIT.STORE) => (...parameters) => {
|
|
122
|
+
const tx = withdraw(token, store);
|
|
123
|
+
if (tx) {
|
|
124
|
+
return tx.run(...parameters);
|
|
125
|
+
}
|
|
126
|
+
throw new Error(
|
|
127
|
+
`Cannot run transaction "${token.key}": transaction not found in store "${store.config.name}".`
|
|
128
|
+
);
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
// internal/src/future.ts
|
|
132
|
+
var Future = class extends Promise {
|
|
133
|
+
constructor(executor) {
|
|
134
|
+
super((resolve, reject) => {
|
|
135
|
+
const pass = (value) => this.isCanceled ? reject(`canceled`) : resolve(value);
|
|
136
|
+
const fail = (reason) => this.isCanceled ? reject(`canceled`) : reject(reason);
|
|
137
|
+
if (typeof executor === `function`) {
|
|
138
|
+
executor(pass, fail);
|
|
139
|
+
} else {
|
|
140
|
+
executor.then(pass, fail);
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
this.isCanceled = false;
|
|
144
|
+
}
|
|
145
|
+
cancel() {
|
|
146
|
+
this.isCanceled = true;
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
// internal/src/lineage.ts
|
|
151
|
+
function newest(scion) {
|
|
152
|
+
while (scion.child !== null) {
|
|
153
|
+
scion = scion.child;
|
|
154
|
+
}
|
|
155
|
+
return scion;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// internal/src/caching.ts
|
|
159
|
+
function cacheValue(key, value, subject, store) {
|
|
160
|
+
const target = newest(store);
|
|
161
|
+
const currentValue = target.valueMap.get(key);
|
|
162
|
+
if (currentValue instanceof Future) {
|
|
163
|
+
currentValue.cancel();
|
|
164
|
+
}
|
|
165
|
+
if (value instanceof Promise) {
|
|
166
|
+
const future = new Future(value);
|
|
167
|
+
newest(store).valueMap.set(key, future);
|
|
168
|
+
future.then((resolved) => {
|
|
169
|
+
if (future.isCanceled) {
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
cacheValue(key, resolved, subject, store);
|
|
173
|
+
subject.next({ newValue: resolved, oldValue: future });
|
|
174
|
+
}).catch((thrown) => {
|
|
175
|
+
if (thrown !== `canceled`) {
|
|
176
|
+
store.logger.error(`\u{1F4A5}`, `state`, key, `rejected:`, thrown);
|
|
177
|
+
}
|
|
178
|
+
});
|
|
179
|
+
return future;
|
|
180
|
+
}
|
|
181
|
+
target.valueMap.set(key, value);
|
|
182
|
+
return value;
|
|
183
|
+
}
|
|
184
|
+
var readCachedValue = (key, store) => {
|
|
185
|
+
return newest(store).valueMap.get(key);
|
|
186
|
+
};
|
|
187
|
+
var isValueCached = (key, store) => {
|
|
188
|
+
return newest(store).valueMap.has(key);
|
|
189
|
+
};
|
|
190
|
+
var evictCachedValue = (key, store) => {
|
|
191
|
+
const core = newest(store);
|
|
192
|
+
const currentValue = core.valueMap.get(key);
|
|
193
|
+
if (currentValue instanceof Future) {
|
|
194
|
+
currentValue.cancel();
|
|
195
|
+
}
|
|
196
|
+
if (core.operation.open) {
|
|
197
|
+
core.operation.prev.set(key, currentValue);
|
|
198
|
+
}
|
|
199
|
+
core.valueMap.delete(key);
|
|
200
|
+
store.logger.info(`\u{1F5D1}`, `state`, key, `evicted`);
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
// ../anvl/src/json/index.ts
|
|
204
|
+
var parseJson = (str) => JSON.parse(str);
|
|
205
|
+
|
|
206
|
+
// json/src/select-json.ts
|
|
207
|
+
var selectJson = (atom2, transform, store = IMPLICIT.STORE) => {
|
|
208
|
+
return createSelector(
|
|
209
|
+
{
|
|
210
|
+
key: `${atom2.key}:JSON`,
|
|
211
|
+
get: ({ get }) => transform.toJson(get(atom2)),
|
|
212
|
+
set: ({ set }, newValue) => set(atom2, transform.fromJson(newValue))
|
|
213
|
+
},
|
|
214
|
+
void 0,
|
|
215
|
+
store
|
|
216
|
+
);
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
// internal/src/read-or-compute-value.ts
|
|
220
|
+
var readOrComputeValue = (state, store) => {
|
|
221
|
+
if (isValueCached(state.key, store)) {
|
|
222
|
+
store.logger.info(`\u{1F4D6}`, state.type, state.key, `reading cached value`);
|
|
223
|
+
return readCachedValue(state.key, store);
|
|
224
|
+
}
|
|
225
|
+
if (state.type !== `atom`) {
|
|
226
|
+
store.logger.info(`\u{1F9EE}`, state.type, state.key, `computing value`);
|
|
227
|
+
return state.get();
|
|
228
|
+
}
|
|
229
|
+
const fallback = state.default instanceof Function ? state.default() : state.default;
|
|
230
|
+
store.logger.info(
|
|
231
|
+
`\u{1F481}`,
|
|
232
|
+
`atom`,
|
|
233
|
+
state.key,
|
|
234
|
+
`could not find cached value; using default`,
|
|
235
|
+
fallback
|
|
236
|
+
);
|
|
237
|
+
return state.default instanceof Function ? state.default() : state.default;
|
|
238
|
+
};
|
|
239
|
+
|
|
240
|
+
// internal/src/operation.ts
|
|
241
|
+
var openOperation = (token, store) => {
|
|
242
|
+
const target = newest(store);
|
|
243
|
+
if (target.operation.open) {
|
|
244
|
+
store.logger.error(
|
|
245
|
+
`\u274C`,
|
|
246
|
+
token.type,
|
|
247
|
+
token.key,
|
|
248
|
+
`failed to setState during a setState for "${target.operation.token.key}"`
|
|
249
|
+
);
|
|
250
|
+
return `rejection`;
|
|
251
|
+
}
|
|
252
|
+
target.operation = {
|
|
253
|
+
open: true,
|
|
254
|
+
done: /* @__PURE__ */ new Set(),
|
|
255
|
+
prev: /* @__PURE__ */ new Map(),
|
|
256
|
+
time: Date.now(),
|
|
257
|
+
token
|
|
258
|
+
};
|
|
259
|
+
store.logger.info(
|
|
260
|
+
`\u2B55`,
|
|
261
|
+
token.type,
|
|
262
|
+
token.key,
|
|
263
|
+
`operation start in store "${store.config.name}"${target.transactionMeta === null ? `` : ` ${target.transactionMeta.phase} "${target.transactionMeta.update.key}"`}`
|
|
264
|
+
);
|
|
265
|
+
};
|
|
266
|
+
var closeOperation = (store) => {
|
|
267
|
+
const target = newest(store);
|
|
268
|
+
if (target.operation.open) {
|
|
269
|
+
store.logger.info(
|
|
270
|
+
`\u{1F534}`,
|
|
271
|
+
target.operation.token.type,
|
|
272
|
+
target.operation.token.key,
|
|
273
|
+
`operation done in store "${store.config.name}"`
|
|
274
|
+
);
|
|
275
|
+
}
|
|
276
|
+
target.operation = { open: false };
|
|
277
|
+
store.subject.operationStatus.next(target.operation);
|
|
278
|
+
};
|
|
279
|
+
var isDone = (key, store) => {
|
|
280
|
+
const target = newest(store);
|
|
281
|
+
if (!target.operation.open) {
|
|
282
|
+
store.logger.warn(
|
|
283
|
+
`\u{1F41E}`,
|
|
284
|
+
`unknown`,
|
|
285
|
+
key,
|
|
286
|
+
`isDone called outside of an operation. This is probably a bug.`
|
|
287
|
+
);
|
|
288
|
+
return true;
|
|
289
|
+
}
|
|
290
|
+
return target.operation.done.has(key);
|
|
291
|
+
};
|
|
292
|
+
var markDone = (key, store) => {
|
|
293
|
+
const target = newest(store);
|
|
294
|
+
if (!target.operation.open) {
|
|
295
|
+
store.logger.warn(
|
|
296
|
+
`\u{1F41E}`,
|
|
297
|
+
`unknown`,
|
|
298
|
+
key,
|
|
299
|
+
`markDone called outside of an operation. This is probably a bug.`
|
|
300
|
+
);
|
|
301
|
+
return;
|
|
302
|
+
}
|
|
303
|
+
target.operation.done.add(key);
|
|
304
|
+
};
|
|
305
|
+
|
|
306
|
+
// internal/src/set-state/become.ts
|
|
307
|
+
var become = (nextVersionOfThing) => (originalThing) => nextVersionOfThing instanceof Function ? nextVersionOfThing(
|
|
308
|
+
originalThing instanceof Function ? originalThing() : originalThing
|
|
309
|
+
) : nextVersionOfThing;
|
|
310
|
+
|
|
311
|
+
// internal/src/subject.ts
|
|
312
|
+
var Subject = class {
|
|
313
|
+
constructor() {
|
|
314
|
+
this.subscribers = /* @__PURE__ */ new Map();
|
|
315
|
+
}
|
|
316
|
+
subscribe(key, subscriber) {
|
|
317
|
+
this.subscribers.set(key, subscriber);
|
|
318
|
+
const unsubscribe = () => this.unsubscribe(key);
|
|
319
|
+
return unsubscribe;
|
|
320
|
+
}
|
|
321
|
+
unsubscribe(key) {
|
|
322
|
+
this.subscribers.delete(key);
|
|
323
|
+
}
|
|
324
|
+
next(value) {
|
|
325
|
+
for (const subscriber of this.subscribers.values()) {
|
|
326
|
+
subscriber(value);
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
};
|
|
330
|
+
var StatefulSubject = class extends Subject {
|
|
331
|
+
constructor(initialState) {
|
|
332
|
+
super();
|
|
333
|
+
this.state = initialState;
|
|
334
|
+
}
|
|
335
|
+
next(value) {
|
|
336
|
+
this.state = value;
|
|
337
|
+
super.next(value);
|
|
338
|
+
}
|
|
339
|
+
};
|
|
340
|
+
|
|
341
|
+
// internal/src/set-state/copy-mutable-if-needed.ts
|
|
342
|
+
function copyMutableIfNeeded(atom2, transform, origin, target) {
|
|
343
|
+
const originValue = origin.valueMap.get(atom2.key);
|
|
344
|
+
const targetValue = target.valueMap.get(atom2.key);
|
|
345
|
+
if (originValue === targetValue) {
|
|
346
|
+
origin.logger.info(`\u{1F4C3}`, `atom`, `${atom2.key}`, `copying`);
|
|
347
|
+
const jsonValue = transform.toJson(originValue);
|
|
348
|
+
const copiedValue = transform.fromJson(jsonValue);
|
|
349
|
+
target.valueMap.set(atom2.key, copiedValue);
|
|
350
|
+
new Tracker(atom2, origin);
|
|
351
|
+
return copiedValue;
|
|
352
|
+
}
|
|
353
|
+
return targetValue;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
// internal/src/set-state/copy-mutable-in-transaction.ts
|
|
357
|
+
function copyMutableIfWithinTransaction(oldValue, atom2, store) {
|
|
358
|
+
const target = newest(store);
|
|
359
|
+
const parent = target.parent;
|
|
360
|
+
if (parent !== null) {
|
|
361
|
+
if (`toJson` in atom2 && `fromJson` in atom2) {
|
|
362
|
+
const copiedValue = copyMutableIfNeeded(atom2, atom2, parent, target);
|
|
363
|
+
return copiedValue;
|
|
364
|
+
}
|
|
365
|
+
if (`family` in atom2) {
|
|
366
|
+
const family = parent.families.get(atom2.family.key);
|
|
367
|
+
if (family && family.type === `atom_family`) {
|
|
368
|
+
const result = copyMutableFamilyMemberWithinTransaction(
|
|
369
|
+
atom2,
|
|
370
|
+
family,
|
|
371
|
+
parent,
|
|
372
|
+
target
|
|
373
|
+
);
|
|
374
|
+
if (result) {
|
|
375
|
+
return result;
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
return oldValue;
|
|
381
|
+
}
|
|
382
|
+
function copyMutableFamilyMemberWithinTransaction(atom2, family, origin, target) {
|
|
383
|
+
if (`toJson` in family && `fromJson` in family) {
|
|
384
|
+
const copyCreated = copyMutableIfNeeded(atom2, family, origin, target);
|
|
385
|
+
return copyCreated;
|
|
386
|
+
}
|
|
387
|
+
return null;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
// internal/src/set-state/emit-update.ts
|
|
391
|
+
var emitUpdate = (state, update, store) => {
|
|
392
|
+
store.logger.info(
|
|
393
|
+
`\u{1F4E2}`,
|
|
394
|
+
state.type,
|
|
395
|
+
state.key,
|
|
396
|
+
`went (`,
|
|
397
|
+
update.oldValue,
|
|
398
|
+
`->`,
|
|
399
|
+
update.newValue,
|
|
400
|
+
`) subscribers:`,
|
|
401
|
+
state.subject.subscribers
|
|
402
|
+
);
|
|
403
|
+
state.subject.next(update);
|
|
404
|
+
};
|
|
405
|
+
|
|
406
|
+
// internal/src/set-state/evict-downstream.ts
|
|
407
|
+
var evictDownStream = (atom2, store) => {
|
|
408
|
+
const target = newest(store);
|
|
409
|
+
const downstreamKeys = target.selectorAtoms.getRelatedKeys(atom2.key);
|
|
410
|
+
store.logger.info(
|
|
411
|
+
`\u{1F9F9}`,
|
|
412
|
+
atom2.type,
|
|
413
|
+
atom2.key,
|
|
414
|
+
downstreamKeys ? `evicting ${downstreamKeys.size} states downstream:` : `no downstream states`,
|
|
415
|
+
downstreamKeys != null ? downstreamKeys : `to evict`
|
|
416
|
+
);
|
|
417
|
+
if (downstreamKeys) {
|
|
418
|
+
if (target.operation.open) {
|
|
419
|
+
store.logger.info(
|
|
420
|
+
`\u{1F9F9}`,
|
|
421
|
+
atom2.type,
|
|
422
|
+
atom2.key,
|
|
423
|
+
`[ ${[...target.operation.done].join(`, `)} ] already done`
|
|
424
|
+
);
|
|
425
|
+
}
|
|
426
|
+
for (const key of downstreamKeys) {
|
|
427
|
+
if (isDone(key, store)) {
|
|
428
|
+
continue;
|
|
429
|
+
}
|
|
430
|
+
evictCachedValue(key, store);
|
|
431
|
+
markDone(key, store);
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
};
|
|
435
|
+
|
|
436
|
+
// internal/src/set-state/stow-update.ts
|
|
437
|
+
function shouldUpdateBeStowed(key, update) {
|
|
438
|
+
if (isTransceiver(update.newValue)) {
|
|
439
|
+
return false;
|
|
440
|
+
}
|
|
441
|
+
if (key.includes(`\u{1F441}\u200D\u{1F5E8}`)) {
|
|
442
|
+
return false;
|
|
443
|
+
}
|
|
444
|
+
return true;
|
|
445
|
+
}
|
|
446
|
+
var stowUpdate = (state, update, store) => {
|
|
447
|
+
const { key } = state;
|
|
448
|
+
const target = newest(store);
|
|
449
|
+
if (target.transactionMeta === null || target.transactionMeta.phase !== `building`) {
|
|
450
|
+
store.logger.error(
|
|
451
|
+
`\u{1F41E}`,
|
|
452
|
+
`atom`,
|
|
453
|
+
key,
|
|
454
|
+
`stowUpdate called outside of a transaction. This is probably a bug.`
|
|
455
|
+
);
|
|
456
|
+
return;
|
|
457
|
+
}
|
|
458
|
+
const shouldStow = shouldUpdateBeStowed(key, update);
|
|
459
|
+
if (!shouldStow) {
|
|
460
|
+
return;
|
|
461
|
+
}
|
|
462
|
+
const atomUpdate = __spreadValues({ key }, update);
|
|
463
|
+
if (state.family) {
|
|
464
|
+
atomUpdate.family = state.family;
|
|
465
|
+
}
|
|
466
|
+
target.transactionMeta.update.updates.push(atomUpdate);
|
|
467
|
+
store.logger.info(
|
|
468
|
+
`\u{1F4C1}`,
|
|
469
|
+
`atom`,
|
|
470
|
+
key,
|
|
471
|
+
`stowed (`,
|
|
472
|
+
update.oldValue,
|
|
473
|
+
`->`,
|
|
474
|
+
update.newValue,
|
|
475
|
+
`)`
|
|
476
|
+
);
|
|
477
|
+
};
|
|
478
|
+
|
|
479
|
+
// internal/src/set-state/set-atom.ts
|
|
480
|
+
var setAtom = (atom2, next, store) => {
|
|
481
|
+
const target = newest(store);
|
|
482
|
+
const oldValue = readOrComputeValue(atom2, store);
|
|
483
|
+
let newValue = copyMutableIfWithinTransaction(oldValue, atom2, store);
|
|
484
|
+
newValue = become(next)(newValue);
|
|
485
|
+
store.logger.info(`\u{1F4DD}`, `atom`, atom2.key, `set to`, newValue);
|
|
486
|
+
newValue = cacheValue(atom2.key, newValue, atom2.subject, store);
|
|
487
|
+
if (isAtomDefault(atom2.key, store)) {
|
|
488
|
+
markAtomAsNotDefault(atom2.key, store);
|
|
489
|
+
}
|
|
490
|
+
markDone(atom2.key, store);
|
|
491
|
+
evictDownStream(atom2, store);
|
|
492
|
+
const update = { oldValue, newValue };
|
|
493
|
+
if (target.transactionMeta === null || target.transactionMeta.phase === `applying`) {
|
|
494
|
+
emitUpdate(atom2, update, store);
|
|
495
|
+
} else {
|
|
496
|
+
stowUpdate(atom2, update, store);
|
|
497
|
+
}
|
|
498
|
+
};
|
|
499
|
+
|
|
500
|
+
// internal/src/set-state/set-atom-or-selector.ts
|
|
501
|
+
var setAtomOrSelector = (state, value, store) => {
|
|
502
|
+
if (state.type === `selector`) {
|
|
503
|
+
state.set(value);
|
|
504
|
+
} else {
|
|
505
|
+
setAtom(state, value, store);
|
|
506
|
+
}
|
|
507
|
+
};
|
|
508
|
+
|
|
509
|
+
// internal/src/store/deposit.ts
|
|
510
|
+
function deposit(state) {
|
|
511
|
+
const token = {
|
|
512
|
+
key: state.key,
|
|
513
|
+
type: state.type
|
|
514
|
+
};
|
|
515
|
+
if (`family` in state) {
|
|
516
|
+
token.family = state.family;
|
|
517
|
+
}
|
|
518
|
+
return token;
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
// ../rel8/junction/src/junction.ts
|
|
522
|
+
var Junction = class {
|
|
523
|
+
constructor(data, config) {
|
|
524
|
+
this.relations = /* @__PURE__ */ new Map();
|
|
525
|
+
this.contents = /* @__PURE__ */ new Map();
|
|
526
|
+
this.makeContentKey = (a, b) => `${a}:${b}`;
|
|
527
|
+
var _a, _b, _c, _d;
|
|
528
|
+
this.a = data.between[0];
|
|
529
|
+
this.b = data.between[1];
|
|
530
|
+
this.cardinality = data.cardinality;
|
|
531
|
+
if (!(config == null ? void 0 : config.externalStore)) {
|
|
532
|
+
this.relations = new Map((_a = data.relations) == null ? void 0 : _a.map(([a, b]) => [a, new Set(b)]));
|
|
533
|
+
this.contents = new Map(data.contents);
|
|
534
|
+
}
|
|
535
|
+
this.isContent = (_b = config == null ? void 0 : config.isContent) != null ? _b : null;
|
|
536
|
+
if (config == null ? void 0 : config.makeContentKey) {
|
|
537
|
+
this.makeContentKey = config.makeContentKey;
|
|
538
|
+
}
|
|
539
|
+
if (config == null ? void 0 : config.externalStore) {
|
|
540
|
+
const externalStore = config.externalStore;
|
|
541
|
+
this.has = (a, b) => externalStore.has(a, b);
|
|
542
|
+
this.addRelation = (a, b) => {
|
|
543
|
+
externalStore.addRelation(a, b);
|
|
544
|
+
};
|
|
545
|
+
this.deleteRelation = (a, b) => {
|
|
546
|
+
externalStore.deleteRelation(a, b);
|
|
547
|
+
};
|
|
548
|
+
this.replaceRelationsSafely = (a, bs) => {
|
|
549
|
+
externalStore.replaceRelationsSafely(a, bs);
|
|
550
|
+
};
|
|
551
|
+
this.replaceRelationsUnsafely = (a, bs) => {
|
|
552
|
+
externalStore.replaceRelationsUnsafely(a, bs);
|
|
553
|
+
};
|
|
554
|
+
this.getRelatedKeys = (key) => externalStore.getRelatedKeys(key);
|
|
555
|
+
if (externalStore.getContent) {
|
|
556
|
+
this.getContentInternal = (contentKey) => {
|
|
557
|
+
return externalStore.getContent(contentKey);
|
|
558
|
+
};
|
|
559
|
+
this.setContent = (contentKey, content) => {
|
|
560
|
+
externalStore.setContent(contentKey, content);
|
|
561
|
+
};
|
|
562
|
+
this.deleteContent = (contentKey) => {
|
|
563
|
+
externalStore.deleteContent(contentKey);
|
|
564
|
+
};
|
|
565
|
+
}
|
|
566
|
+
for (const [x, ys] of (_c = data.relations) != null ? _c : []) {
|
|
567
|
+
for (const y of ys)
|
|
568
|
+
this.addRelation(x, y);
|
|
569
|
+
}
|
|
570
|
+
for (const [contentKey, content] of (_d = data.contents) != null ? _d : []) {
|
|
571
|
+
this.setContent(contentKey, content);
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
getRelatedKeys(key) {
|
|
576
|
+
return this.relations.get(key);
|
|
577
|
+
}
|
|
578
|
+
addRelation(a, b) {
|
|
579
|
+
let aRelations = this.relations.get(a);
|
|
580
|
+
let bRelations = this.relations.get(b);
|
|
581
|
+
if (aRelations) {
|
|
582
|
+
aRelations.add(b);
|
|
583
|
+
} else {
|
|
584
|
+
aRelations = /* @__PURE__ */ new Set([b]);
|
|
585
|
+
this.relations.set(a, aRelations);
|
|
586
|
+
}
|
|
587
|
+
if (bRelations) {
|
|
588
|
+
bRelations.add(a);
|
|
589
|
+
} else {
|
|
590
|
+
bRelations = /* @__PURE__ */ new Set([a]);
|
|
591
|
+
this.relations.set(b, bRelations);
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
deleteRelation(a, b) {
|
|
595
|
+
const aRelations = this.relations.get(a);
|
|
596
|
+
if (aRelations) {
|
|
597
|
+
aRelations.delete(b);
|
|
598
|
+
if (aRelations.size === 0) {
|
|
599
|
+
this.relations.delete(a);
|
|
600
|
+
}
|
|
601
|
+
const bRelations = this.relations.get(b);
|
|
602
|
+
if (bRelations) {
|
|
603
|
+
bRelations.delete(a);
|
|
604
|
+
if (bRelations.size === 0) {
|
|
605
|
+
this.relations.delete(b);
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
replaceRelationsUnsafely(a, bs) {
|
|
611
|
+
this.relations.set(a, new Set(bs));
|
|
612
|
+
for (const b of bs) {
|
|
613
|
+
const bRelations = /* @__PURE__ */ new Set([a]);
|
|
614
|
+
this.relations.set(b, bRelations);
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
replaceRelationsSafely(a, bs) {
|
|
618
|
+
const aRelationsPrev = this.relations.get(a);
|
|
619
|
+
if (aRelationsPrev) {
|
|
620
|
+
for (const b of aRelationsPrev) {
|
|
621
|
+
const bRelations = this.relations.get(b);
|
|
622
|
+
if (bRelations) {
|
|
623
|
+
if (bRelations.size === 1) {
|
|
624
|
+
this.relations.delete(b);
|
|
625
|
+
} else {
|
|
626
|
+
bRelations.delete(a);
|
|
627
|
+
}
|
|
628
|
+
this.contents.delete(this.makeContentKey(a, b));
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
this.relations.set(a, new Set(bs));
|
|
633
|
+
for (const b of bs) {
|
|
634
|
+
let bRelations = this.relations.get(b);
|
|
635
|
+
if (bRelations) {
|
|
636
|
+
bRelations.add(a);
|
|
637
|
+
} else {
|
|
638
|
+
bRelations = /* @__PURE__ */ new Set([a]);
|
|
639
|
+
this.relations.set(b, bRelations);
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
getContentInternal(contentKey) {
|
|
644
|
+
return this.contents.get(contentKey);
|
|
645
|
+
}
|
|
646
|
+
setContent(contentKey, content) {
|
|
647
|
+
this.contents.set(contentKey, content);
|
|
648
|
+
}
|
|
649
|
+
deleteContent(contentKey) {
|
|
650
|
+
this.contents.delete(contentKey);
|
|
651
|
+
}
|
|
652
|
+
toJSON() {
|
|
653
|
+
return {
|
|
654
|
+
between: [this.a, this.b],
|
|
655
|
+
cardinality: this.cardinality,
|
|
656
|
+
relations: [...this.relations.entries()].map(([a, b]) => [a, [...b]]),
|
|
657
|
+
contents: [...this.contents.entries()]
|
|
658
|
+
};
|
|
659
|
+
}
|
|
660
|
+
set(a, ...rest) {
|
|
661
|
+
var _a;
|
|
662
|
+
const b = typeof rest[0] === `string` ? rest[0] : a[this.b];
|
|
663
|
+
const content = ((_a = rest[1]) != null ? _a : typeof rest[0] === `string`) ? void 0 : rest[0];
|
|
664
|
+
a = typeof a === `string` ? a : a[this.a];
|
|
665
|
+
switch (this.cardinality) {
|
|
666
|
+
case `1:1`: {
|
|
667
|
+
const bPrev = this.getRelatedKey(a);
|
|
668
|
+
if (bPrev && bPrev !== b)
|
|
669
|
+
this.delete(bPrev, a);
|
|
670
|
+
}
|
|
671
|
+
case `1:n`: {
|
|
672
|
+
const aPrev = this.getRelatedKey(b);
|
|
673
|
+
if (aPrev && aPrev !== a)
|
|
674
|
+
this.delete(aPrev, b);
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
if (content) {
|
|
678
|
+
const contentKey = this.makeContentKey(a, b);
|
|
679
|
+
this.setContent(contentKey, content);
|
|
680
|
+
}
|
|
681
|
+
this.addRelation(a, b);
|
|
682
|
+
return this;
|
|
683
|
+
}
|
|
684
|
+
delete(x, b) {
|
|
685
|
+
b = typeof b === `string` ? b : x[this.b];
|
|
686
|
+
const a = typeof x === `string` ? x : x[this.a];
|
|
687
|
+
if (a === void 0 && typeof b === `string`) {
|
|
688
|
+
const bRelations = this.getRelatedKeys(b);
|
|
689
|
+
if (bRelations) {
|
|
690
|
+
for (const a2 of bRelations) {
|
|
691
|
+
this.delete(a2, b);
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
if (typeof a === `string` && b === void 0) {
|
|
696
|
+
const aRelations = this.getRelatedKeys(a);
|
|
697
|
+
if (aRelations) {
|
|
698
|
+
for (const b2 of aRelations) {
|
|
699
|
+
this.delete(a, b2);
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
if (typeof a === `string` && typeof b === `string`) {
|
|
704
|
+
this.deleteRelation(a, b);
|
|
705
|
+
const contentKey = this.makeContentKey(a, b);
|
|
706
|
+
this.deleteContent(contentKey);
|
|
707
|
+
}
|
|
708
|
+
return this;
|
|
709
|
+
}
|
|
710
|
+
getRelatedKey(key) {
|
|
711
|
+
const relations = this.getRelatedKeys(key);
|
|
712
|
+
if (relations) {
|
|
713
|
+
if (relations.size > 1) {
|
|
714
|
+
console.warn(
|
|
715
|
+
`${relations.size} related keys were found for key "${key}": (${[
|
|
716
|
+
...relations
|
|
717
|
+
].map((k) => `"${k}"`).join(`, `)}). Only one related key was expected.`
|
|
718
|
+
);
|
|
719
|
+
}
|
|
720
|
+
for (const relation of relations) {
|
|
721
|
+
return relation;
|
|
722
|
+
}
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
replaceRelations(a, relations, config) {
|
|
726
|
+
const hasContent = !Array.isArray(relations);
|
|
727
|
+
const bs = hasContent ? Object.keys(relations) : relations;
|
|
728
|
+
if (config == null ? void 0 : config.reckless) {
|
|
729
|
+
this.replaceRelationsUnsafely(a, bs);
|
|
730
|
+
} else {
|
|
731
|
+
this.replaceRelationsSafely(a, bs);
|
|
732
|
+
}
|
|
733
|
+
if (hasContent) {
|
|
734
|
+
for (const b of bs) {
|
|
735
|
+
const contentKey = this.makeContentKey(a, b);
|
|
736
|
+
const content = relations[b];
|
|
737
|
+
this.setContent(contentKey, content);
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
return this;
|
|
741
|
+
}
|
|
742
|
+
getContent(a, b) {
|
|
743
|
+
const contentKey = this.makeContentKey(a, b);
|
|
744
|
+
return this.getContentInternal(contentKey);
|
|
745
|
+
}
|
|
746
|
+
getRelationEntries(input) {
|
|
747
|
+
const a = input[this.a];
|
|
748
|
+
const b = input[this.b];
|
|
749
|
+
if (a !== void 0 && b === void 0) {
|
|
750
|
+
const aRelations = this.getRelatedKeys(a);
|
|
751
|
+
if (aRelations) {
|
|
752
|
+
return [...aRelations].map((b2) => {
|
|
753
|
+
var _a;
|
|
754
|
+
return [b2, (_a = this.getContent(a, b2)) != null ? _a : null];
|
|
755
|
+
});
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
if (a === void 0 && b !== void 0) {
|
|
759
|
+
const bRelations = this.getRelatedKeys(b);
|
|
760
|
+
if (bRelations) {
|
|
761
|
+
return [...bRelations].map((a2) => {
|
|
762
|
+
var _a;
|
|
763
|
+
return [a2, (_a = this.getContent(a2, b)) != null ? _a : null];
|
|
764
|
+
});
|
|
765
|
+
}
|
|
766
|
+
}
|
|
767
|
+
return [];
|
|
768
|
+
}
|
|
769
|
+
has(a, b) {
|
|
770
|
+
var _a;
|
|
771
|
+
if (b) {
|
|
772
|
+
const setA = this.getRelatedKeys(a);
|
|
773
|
+
return (_a = setA == null ? void 0 : setA.has(b)) != null ? _a : false;
|
|
774
|
+
}
|
|
775
|
+
return this.relations.has(a);
|
|
776
|
+
}
|
|
777
|
+
};
|
|
778
|
+
|
|
779
|
+
// internal/src/store/store.ts
|
|
780
|
+
var Store = class {
|
|
781
|
+
constructor(name, store = null) {
|
|
782
|
+
this.parent = null;
|
|
783
|
+
this.child = null;
|
|
784
|
+
this.valueMap = /* @__PURE__ */ new Map();
|
|
785
|
+
this.atoms = /* @__PURE__ */ new Map();
|
|
786
|
+
this.selectors = /* @__PURE__ */ new Map();
|
|
787
|
+
this.readonlySelectors = /* @__PURE__ */ new Map();
|
|
788
|
+
this.trackers = /* @__PURE__ */ new Map();
|
|
789
|
+
this.families = /* @__PURE__ */ new Map();
|
|
790
|
+
this.timelines = /* @__PURE__ */ new Map();
|
|
791
|
+
this.transactions = /* @__PURE__ */ new Map();
|
|
792
|
+
this.atomsThatAreDefault = /* @__PURE__ */ new Set();
|
|
793
|
+
this.timelineAtoms = new Junction({
|
|
794
|
+
between: [`timelineKey`, `atomKey`],
|
|
795
|
+
cardinality: `1:n`
|
|
796
|
+
});
|
|
797
|
+
this.selectorAtoms = new Junction({
|
|
798
|
+
between: [`selectorKey`, `atomKey`],
|
|
799
|
+
cardinality: `n:n`
|
|
800
|
+
});
|
|
801
|
+
this.selectorGraph = new Junction(
|
|
802
|
+
{
|
|
803
|
+
between: [`upstreamSelectorKey`, `downstreamSelectorKey`],
|
|
804
|
+
cardinality: `n:n`
|
|
805
|
+
},
|
|
806
|
+
{
|
|
807
|
+
makeContentKey: (...keys) => keys.sort().join(`:`)
|
|
808
|
+
}
|
|
809
|
+
);
|
|
810
|
+
this.subject = {
|
|
811
|
+
atomCreation: new Subject(),
|
|
812
|
+
selectorCreation: new Subject(),
|
|
813
|
+
transactionCreation: new Subject(),
|
|
814
|
+
timelineCreation: new Subject(),
|
|
815
|
+
transactionApplying: new StatefulSubject(null),
|
|
816
|
+
operationStatus: new Subject()
|
|
817
|
+
};
|
|
818
|
+
this.operation = { open: false };
|
|
819
|
+
this.transactionMeta = null;
|
|
820
|
+
this.config = {
|
|
821
|
+
name: `IMPLICIT_STORE`
|
|
822
|
+
};
|
|
823
|
+
this.loggers = [
|
|
824
|
+
new AtomIOLogger(`warn`, (_, __, key) => !key.includes(`\u{1F441}\u200D\u{1F5E8}`))
|
|
825
|
+
];
|
|
826
|
+
this.logger = {
|
|
827
|
+
error: (...messages) => {
|
|
828
|
+
for (const logger of this.loggers)
|
|
829
|
+
logger.error(...messages);
|
|
830
|
+
},
|
|
831
|
+
info: (...messages) => {
|
|
832
|
+
for (const logger of this.loggers)
|
|
833
|
+
logger.info(...messages);
|
|
834
|
+
},
|
|
835
|
+
warn: (...messages) => {
|
|
836
|
+
for (const logger of this.loggers)
|
|
837
|
+
logger.warn(...messages);
|
|
838
|
+
}
|
|
839
|
+
};
|
|
840
|
+
if (store !== null) {
|
|
841
|
+
this.valueMap = new Map(store == null ? void 0 : store.valueMap);
|
|
842
|
+
this.operation = __spreadValues({}, store == null ? void 0 : store.operation);
|
|
843
|
+
this.transactionMeta = (store == null ? void 0 : store.transactionMeta) ? __spreadValues({}, store == null ? void 0 : store.transactionMeta) : null;
|
|
844
|
+
this.config = __spreadProps(__spreadValues({}, store == null ? void 0 : store.config), {
|
|
845
|
+
name
|
|
846
|
+
});
|
|
847
|
+
for (const [, atom2] of store.atoms) {
|
|
848
|
+
atom2.install(this);
|
|
849
|
+
}
|
|
850
|
+
for (const [, selector2] of store.readonlySelectors) {
|
|
851
|
+
selector2.install(this);
|
|
852
|
+
}
|
|
853
|
+
for (const [, selector2] of store.selectors) {
|
|
854
|
+
selector2.install(this);
|
|
855
|
+
}
|
|
856
|
+
for (const [, tx] of store.transactions) {
|
|
857
|
+
tx.install(this);
|
|
858
|
+
}
|
|
859
|
+
for (const [, timeline] of store.timelines) {
|
|
860
|
+
timeline.install(this);
|
|
861
|
+
}
|
|
862
|
+
}
|
|
863
|
+
}
|
|
864
|
+
};
|
|
865
|
+
var IMPLICIT = {
|
|
866
|
+
STORE_INTERNAL: void 0,
|
|
867
|
+
get STORE() {
|
|
868
|
+
var _a;
|
|
869
|
+
return (_a = this.STORE_INTERNAL) != null ? _a : this.STORE_INTERNAL = new Store(`IMPLICIT_STORE`);
|
|
870
|
+
}
|
|
871
|
+
};
|
|
872
|
+
|
|
873
|
+
// internal/src/store/withdraw.ts
|
|
874
|
+
function withdraw(token, store) {
|
|
875
|
+
var _a, _b, _c, _d;
|
|
876
|
+
const target = newest(store);
|
|
877
|
+
const state = (_d = (_c = (_b = (_a = target.atoms.get(token.key)) != null ? _a : target.selectors.get(token.key)) != null ? _b : target.readonlySelectors.get(token.key)) != null ? _c : target.transactions.get(token.key)) != null ? _d : target.timelines.get(token.key);
|
|
878
|
+
if (state) {
|
|
879
|
+
return state;
|
|
880
|
+
}
|
|
881
|
+
return void 0;
|
|
882
|
+
}
|
|
883
|
+
|
|
884
|
+
// internal/src/store/withdraw-new-family-member.ts
|
|
885
|
+
function withdrawNewFamilyMember(token, store) {
|
|
886
|
+
if (token.family) {
|
|
887
|
+
store.logger.info(
|
|
888
|
+
`\u{1F46A}`,
|
|
889
|
+
token.type,
|
|
890
|
+
token.key,
|
|
891
|
+
`creating new family member in store "${store.config.name}"`
|
|
892
|
+
);
|
|
893
|
+
const target = newest(store);
|
|
894
|
+
const family = target.families.get(token.family.key);
|
|
895
|
+
if (family) {
|
|
896
|
+
const jsonSubKey = JSON.parse(token.family.subKey);
|
|
897
|
+
family(jsonSubKey);
|
|
898
|
+
const state = withdraw(token, store);
|
|
899
|
+
return state;
|
|
900
|
+
}
|
|
901
|
+
}
|
|
902
|
+
return void 0;
|
|
903
|
+
}
|
|
904
|
+
|
|
905
|
+
// internal/src/keys.ts
|
|
906
|
+
var isAtomKey = (key, store) => newest(store).atoms.has(key);
|
|
907
|
+
var isSelectorKey = (key, store) => newest(store).selectors.has(key);
|
|
908
|
+
var isReadonlySelectorKey = (key, store) => newest(store).readonlySelectors.has(key);
|
|
909
|
+
var isStateKey = (key, store) => isAtomKey(key, store) || isSelectorKey(key, store) || isReadonlySelectorKey(key, store);
|
|
910
|
+
|
|
911
|
+
// internal/src/selector/get-selector-dependency-keys.ts
|
|
912
|
+
var getSelectorDependencyKeys = (key, store) => {
|
|
913
|
+
const sources = newest(store).selectorGraph.getRelationEntries({ downstreamSelectorKey: key }).filter(([_, { source }]) => source !== key).map(([_, { source }]) => source).filter((source) => isStateKey(source, store));
|
|
914
|
+
return sources;
|
|
915
|
+
};
|
|
916
|
+
|
|
917
|
+
// internal/src/selector/trace-selector-atoms.ts
|
|
918
|
+
var traceSelectorAtoms = (selectorKey, directDependencyKey, store) => {
|
|
919
|
+
const rootKeys = [];
|
|
920
|
+
const indirectDependencyKeys = getSelectorDependencyKeys(
|
|
921
|
+
directDependencyKey,
|
|
922
|
+
store
|
|
923
|
+
);
|
|
924
|
+
let depth = 0;
|
|
925
|
+
while (indirectDependencyKeys.length > 0) {
|
|
926
|
+
const indirectDependencyKey = indirectDependencyKeys.shift();
|
|
927
|
+
++depth;
|
|
928
|
+
if (depth > 99999) {
|
|
929
|
+
throw new Error(
|
|
930
|
+
`Maximum selector dependency depth exceeded (> 99999) in selector "${selectorKey}". This is likely due to a circular dependency.`
|
|
931
|
+
);
|
|
932
|
+
}
|
|
933
|
+
if (!isAtomKey(indirectDependencyKey, store)) {
|
|
934
|
+
indirectDependencyKeys.push(
|
|
935
|
+
...getSelectorDependencyKeys(indirectDependencyKey, store)
|
|
936
|
+
);
|
|
937
|
+
} else if (!rootKeys.includes(indirectDependencyKey)) {
|
|
938
|
+
rootKeys.push(indirectDependencyKey);
|
|
939
|
+
}
|
|
940
|
+
}
|
|
941
|
+
return rootKeys;
|
|
942
|
+
};
|
|
943
|
+
var traceAllSelectorAtoms = (selectorKey, store) => {
|
|
944
|
+
const directDependencyKeys = getSelectorDependencyKeys(selectorKey, store);
|
|
945
|
+
return directDependencyKeys.flatMap(
|
|
946
|
+
(depKey) => isAtomKey(depKey, store) ? depKey : traceSelectorAtoms(selectorKey, depKey, store)
|
|
947
|
+
);
|
|
948
|
+
};
|
|
949
|
+
|
|
950
|
+
// internal/src/selector/update-selector-atoms.ts
|
|
951
|
+
var updateSelectorAtoms = (selectorKey, dependency, store) => {
|
|
952
|
+
const target = newest(store);
|
|
953
|
+
if (dependency.type === `atom`) {
|
|
954
|
+
target.selectorAtoms.set({
|
|
955
|
+
selectorKey,
|
|
956
|
+
atomKey: dependency.key
|
|
957
|
+
});
|
|
958
|
+
store.logger.info(
|
|
959
|
+
`\u{1F50D}`,
|
|
960
|
+
`selector`,
|
|
961
|
+
selectorKey,
|
|
962
|
+
`discovers root atom "${dependency.key}"`
|
|
963
|
+
);
|
|
964
|
+
} else {
|
|
965
|
+
const rootKeys = traceSelectorAtoms(selectorKey, dependency.key, store);
|
|
966
|
+
store.logger.info(
|
|
967
|
+
`\u{1F50D}`,
|
|
968
|
+
`selector`,
|
|
969
|
+
selectorKey,
|
|
970
|
+
`discovers root atoms: [ ${rootKeys.map((key) => `"${key}"`).join(`, `)} ]`
|
|
971
|
+
);
|
|
972
|
+
for (const atomKey of rootKeys) {
|
|
973
|
+
target.selectorAtoms = target.selectorAtoms.set({
|
|
974
|
+
selectorKey,
|
|
975
|
+
atomKey
|
|
976
|
+
});
|
|
977
|
+
}
|
|
978
|
+
}
|
|
979
|
+
};
|
|
980
|
+
|
|
981
|
+
// internal/src/selector/register-selector.ts
|
|
982
|
+
var registerSelector = (selectorKey, store) => ({
|
|
983
|
+
get: (dependency) => {
|
|
984
|
+
const target = newest(store);
|
|
985
|
+
const alreadyRegistered = target.selectorGraph.getRelationEntries({ downstreamSelectorKey: selectorKey }).some(([_, { source }]) => source === dependency.key);
|
|
986
|
+
const dependencyState = withdraw(dependency, store);
|
|
987
|
+
if (dependencyState === void 0) {
|
|
988
|
+
throw new Error(
|
|
989
|
+
`State "${dependency.key}" not found in this store. Did you forget to initialize with the "atom" or "selector" function?`
|
|
990
|
+
);
|
|
991
|
+
}
|
|
992
|
+
const dependencyValue = readOrComputeValue(dependencyState, store);
|
|
993
|
+
store.logger.info(
|
|
994
|
+
`\u{1F50C}`,
|
|
995
|
+
`selector`,
|
|
996
|
+
selectorKey,
|
|
997
|
+
`registers dependency ( "${dependency.key}" =`,
|
|
998
|
+
dependencyValue,
|
|
999
|
+
`)`
|
|
1000
|
+
);
|
|
1001
|
+
if (!alreadyRegistered) {
|
|
1002
|
+
target.selectorGraph.set(
|
|
1003
|
+
{
|
|
1004
|
+
upstreamSelectorKey: dependency.key,
|
|
1005
|
+
downstreamSelectorKey: selectorKey
|
|
1006
|
+
},
|
|
1007
|
+
{
|
|
1008
|
+
source: dependency.key
|
|
1009
|
+
}
|
|
1010
|
+
);
|
|
1011
|
+
}
|
|
1012
|
+
updateSelectorAtoms(selectorKey, dependency, store);
|
|
1013
|
+
return dependencyValue;
|
|
1014
|
+
},
|
|
1015
|
+
set: (stateToken, newValue) => {
|
|
1016
|
+
const state = withdraw(stateToken, store);
|
|
1017
|
+
if (state === void 0) {
|
|
1018
|
+
throw new Error(
|
|
1019
|
+
`State "${stateToken.key}" not found in this store. Did you forget to initialize with the "atom" or "selector" function?`
|
|
1020
|
+
);
|
|
1021
|
+
}
|
|
1022
|
+
setAtomOrSelector(state, newValue, store);
|
|
1023
|
+
}
|
|
1024
|
+
});
|
|
1025
|
+
|
|
1026
|
+
// internal/src/selector/create-read-write-selector.ts
|
|
1027
|
+
var createReadWriteSelector = (options, family, store) => {
|
|
1028
|
+
const target = newest(store);
|
|
1029
|
+
const subject = new Subject();
|
|
1030
|
+
const { get, set } = registerSelector(options.key, store);
|
|
1031
|
+
const getSelf = () => {
|
|
1032
|
+
const value = options.get({ get });
|
|
1033
|
+
cacheValue(options.key, value, subject, store);
|
|
1034
|
+
return value;
|
|
1035
|
+
};
|
|
1036
|
+
const setSelf = (next) => {
|
|
1037
|
+
const oldValue = getSelf();
|
|
1038
|
+
const newValue = become(next)(oldValue);
|
|
1039
|
+
store.logger.info(
|
|
1040
|
+
`\u{1F4DD}`,
|
|
1041
|
+
`selector`,
|
|
1042
|
+
options.key,
|
|
1043
|
+
`set (`,
|
|
1044
|
+
oldValue,
|
|
1045
|
+
`->`,
|
|
1046
|
+
newValue,
|
|
1047
|
+
`)`
|
|
1048
|
+
);
|
|
1049
|
+
cacheValue(options.key, newValue, subject, store);
|
|
1050
|
+
markDone(options.key, store);
|
|
1051
|
+
if (target.transactionMeta === null) {
|
|
1052
|
+
subject.next({ newValue, oldValue });
|
|
1053
|
+
}
|
|
1054
|
+
options.set({ get, set }, newValue);
|
|
1055
|
+
};
|
|
1056
|
+
const mySelector = __spreadValues(__spreadProps(__spreadValues({}, options), {
|
|
1057
|
+
subject,
|
|
1058
|
+
install: (s) => createSelector(options, family, s),
|
|
1059
|
+
get: getSelf,
|
|
1060
|
+
set: setSelf,
|
|
1061
|
+
type: `selector`
|
|
1062
|
+
}), family && { family });
|
|
1063
|
+
target.selectors.set(options.key, mySelector);
|
|
1064
|
+
const initialValue = getSelf();
|
|
1065
|
+
store.logger.info(`\u2728`, mySelector.type, mySelector.key, `=`, initialValue);
|
|
1066
|
+
const token = {
|
|
1067
|
+
key: options.key,
|
|
1068
|
+
type: `selector`
|
|
1069
|
+
};
|
|
1070
|
+
if (family) {
|
|
1071
|
+
token.family = family;
|
|
1072
|
+
}
|
|
1073
|
+
store.subject.selectorCreation.next(token);
|
|
1074
|
+
return token;
|
|
1075
|
+
};
|
|
1076
|
+
|
|
1077
|
+
// internal/src/selector/create-readonly-selector.ts
|
|
1078
|
+
var createReadonlySelector = (options, family, store) => {
|
|
1079
|
+
const target = newest(store);
|
|
1080
|
+
const subject = new Subject();
|
|
1081
|
+
const { get } = registerSelector(options.key, store);
|
|
1082
|
+
const getSelf = () => {
|
|
1083
|
+
const value = options.get({ get });
|
|
1084
|
+
cacheValue(options.key, value, subject, store);
|
|
1085
|
+
return value;
|
|
1086
|
+
};
|
|
1087
|
+
const readonlySelector = __spreadValues(__spreadProps(__spreadValues({}, options), {
|
|
1088
|
+
subject,
|
|
1089
|
+
install: (s) => createSelector(options, family, s),
|
|
1090
|
+
get: getSelf,
|
|
1091
|
+
type: `readonly_selector`
|
|
1092
|
+
}), family && { family });
|
|
1093
|
+
target.readonlySelectors.set(options.key, readonlySelector);
|
|
1094
|
+
const initialValue = getSelf();
|
|
1095
|
+
store.logger.info(
|
|
1096
|
+
`\u2728`,
|
|
1097
|
+
readonlySelector.type,
|
|
1098
|
+
readonlySelector.key,
|
|
1099
|
+
`=`,
|
|
1100
|
+
initialValue
|
|
1101
|
+
);
|
|
1102
|
+
const token = {
|
|
1103
|
+
key: options.key,
|
|
1104
|
+
type: `readonly_selector`
|
|
1105
|
+
};
|
|
1106
|
+
if (family) {
|
|
1107
|
+
token.family = family;
|
|
1108
|
+
}
|
|
1109
|
+
store.subject.selectorCreation.next(token);
|
|
1110
|
+
return token;
|
|
1111
|
+
};
|
|
1112
|
+
|
|
1113
|
+
// internal/src/selector/create-selector.ts
|
|
1114
|
+
function createSelector(options, family, store) {
|
|
1115
|
+
const target = newest(store);
|
|
1116
|
+
const existingWritable = target.selectors.get(options.key);
|
|
1117
|
+
const existingReadonly = target.readonlySelectors.get(options.key);
|
|
1118
|
+
if (existingWritable || existingReadonly) {
|
|
1119
|
+
store.logger.error(
|
|
1120
|
+
`\u274C`,
|
|
1121
|
+
existingReadonly ? `readonly_selector` : `selector`,
|
|
1122
|
+
options.key,
|
|
1123
|
+
`Tried to create selector, but it already exists in the store. (Ignore if you are in development using hot module replacement.)`
|
|
1124
|
+
);
|
|
1125
|
+
}
|
|
1126
|
+
if (`set` in options) {
|
|
1127
|
+
return createReadWriteSelector(options, family, store);
|
|
1128
|
+
}
|
|
1129
|
+
return createReadonlySelector(options, family, store);
|
|
1130
|
+
}
|
|
1131
|
+
|
|
1132
|
+
// internal/src/selector/delete-selector.ts
|
|
1133
|
+
function deleteSelector(selectorToken, store) {
|
|
1134
|
+
const target = newest(store);
|
|
1135
|
+
const { key } = selectorToken;
|
|
1136
|
+
switch (selectorToken.type) {
|
|
1137
|
+
case `selector`:
|
|
1138
|
+
target.selectors.delete(key);
|
|
1139
|
+
break;
|
|
1140
|
+
case `readonly_selector`:
|
|
1141
|
+
target.readonlySelectors.delete(key);
|
|
1142
|
+
break;
|
|
1143
|
+
}
|
|
1144
|
+
target.valueMap.delete(key);
|
|
1145
|
+
target.selectorAtoms.delete(key);
|
|
1146
|
+
const downstreamTokens = target.selectorGraph.getRelationEntries({ upstreamSelectorKey: key }).filter(([_, { source }]) => source === key).map(
|
|
1147
|
+
([downstreamSelectorKey]) => {
|
|
1148
|
+
var _a;
|
|
1149
|
+
return (_a = target.selectors.get(downstreamSelectorKey)) != null ? _a : target.readonlySelectors.get(downstreamSelectorKey);
|
|
1150
|
+
}
|
|
1151
|
+
);
|
|
1152
|
+
for (const downstreamToken of downstreamTokens) {
|
|
1153
|
+
if (downstreamToken) {
|
|
1154
|
+
deleteSelector(downstreamToken, store);
|
|
1155
|
+
}
|
|
1156
|
+
}
|
|
1157
|
+
target.selectorGraph.delete(key);
|
|
1158
|
+
store.logger.info(`\u{1F525}`, selectorToken.type, `${key}`, `deleted`);
|
|
1159
|
+
}
|
|
1160
|
+
|
|
1161
|
+
// internal/src/subscribe/recall-state.ts
|
|
1162
|
+
var recallState = (state, store) => {
|
|
1163
|
+
const target = newest(store);
|
|
1164
|
+
if (!target.operation.open) {
|
|
1165
|
+
return target.valueMap.get(state.key);
|
|
1166
|
+
}
|
|
1167
|
+
return target.operation.prev.get(state.key);
|
|
1168
|
+
};
|
|
1169
|
+
|
|
1170
|
+
// internal/src/subscribe/subscribe-to-root-atoms.ts
|
|
1171
|
+
var subscribeToRootAtoms = (state, store) => {
|
|
1172
|
+
const dependencySubscriptions = `default` in state ? null : traceAllSelectorAtoms(state.key, store).map((atomKey) => {
|
|
1173
|
+
const atom2 = store.atoms.get(atomKey);
|
|
1174
|
+
if (atom2 === void 0) {
|
|
1175
|
+
throw new Error(
|
|
1176
|
+
`Atom "${atomKey}", a dependency of selector "${state.key}", not found in store "${store.config.name}".`
|
|
1177
|
+
);
|
|
1178
|
+
}
|
|
1179
|
+
return atom2.subject.subscribe(
|
|
1180
|
+
`${state.type}:${state.key}`,
|
|
1181
|
+
(atomChange) => {
|
|
1182
|
+
store.logger.info(
|
|
1183
|
+
`\u{1F4E2}`,
|
|
1184
|
+
state.type,
|
|
1185
|
+
state.key,
|
|
1186
|
+
`root`,
|
|
1187
|
+
atomKey,
|
|
1188
|
+
`went`,
|
|
1189
|
+
atomChange.oldValue,
|
|
1190
|
+
`->`,
|
|
1191
|
+
atomChange.newValue
|
|
1192
|
+
);
|
|
1193
|
+
const oldValue = recallState(state, store);
|
|
1194
|
+
const newValue = readOrComputeValue(state, store);
|
|
1195
|
+
store.logger.info(
|
|
1196
|
+
`\u2728`,
|
|
1197
|
+
state.type,
|
|
1198
|
+
state.key,
|
|
1199
|
+
`went`,
|
|
1200
|
+
oldValue,
|
|
1201
|
+
`->`,
|
|
1202
|
+
newValue
|
|
1203
|
+
);
|
|
1204
|
+
state.subject.next({ newValue, oldValue });
|
|
1205
|
+
}
|
|
1206
|
+
);
|
|
1207
|
+
});
|
|
1208
|
+
return dependencySubscriptions;
|
|
1209
|
+
};
|
|
1210
|
+
|
|
1211
|
+
// internal/src/subscribe/subscribe-to-state.ts
|
|
1212
|
+
function subscribeToState(token, handleUpdate, key, store) {
|
|
1213
|
+
const state = withdraw(token, store);
|
|
1214
|
+
if (state === void 0) {
|
|
1215
|
+
throw new Error(
|
|
1216
|
+
`State "${token.key}" not found in this store. Did you forget to initialize with the "atom" or "selector" function?`
|
|
1217
|
+
);
|
|
1218
|
+
}
|
|
1219
|
+
const unsubFunction = state.subject.subscribe(key, handleUpdate);
|
|
1220
|
+
store.logger.info(`\u{1F440}`, state.type, state.key, `Adding subscription "${key}"`);
|
|
1221
|
+
const dependencyUnsubFunctions = state.type !== `atom` ? subscribeToRootAtoms(state, store) : null;
|
|
1222
|
+
const unsubscribe = dependencyUnsubFunctions === null ? () => {
|
|
1223
|
+
store.logger.info(
|
|
1224
|
+
`\u{1F648}`,
|
|
1225
|
+
state.type,
|
|
1226
|
+
state.key,
|
|
1227
|
+
`Removing subscription "${key}"`
|
|
1228
|
+
);
|
|
1229
|
+
unsubFunction();
|
|
1230
|
+
} : () => {
|
|
1231
|
+
store.logger.info(
|
|
1232
|
+
`\u{1F648}`,
|
|
1233
|
+
state.type,
|
|
1234
|
+
state.key,
|
|
1235
|
+
`Removing subscription "${key}"`
|
|
1236
|
+
);
|
|
1237
|
+
unsubFunction();
|
|
1238
|
+
for (const unsubFromDependency of dependencyUnsubFunctions) {
|
|
1239
|
+
unsubFromDependency();
|
|
1240
|
+
}
|
|
1241
|
+
};
|
|
1242
|
+
return unsubscribe;
|
|
1243
|
+
}
|
|
31
1244
|
|
|
32
|
-
// src/
|
|
33
|
-
var
|
|
1245
|
+
// internal/src/subscribe/subscribe-to-timeline.ts
|
|
1246
|
+
var subscribeToTimeline = (token, handleUpdate, key, store) => {
|
|
1247
|
+
const tl = withdraw(token, store);
|
|
1248
|
+
if (tl === void 0) {
|
|
1249
|
+
throw new Error(
|
|
1250
|
+
`Cannot subscribe to timeline "${token.key}": timeline not found in store "${store.config.name}".`
|
|
1251
|
+
);
|
|
1252
|
+
}
|
|
1253
|
+
store.logger.info(`\u{1F440}`, `timeline`, token.key, `Adding subscription "${key}"`);
|
|
1254
|
+
const unsubscribe = tl.subject.subscribe(key, handleUpdate);
|
|
1255
|
+
return () => {
|
|
1256
|
+
store.logger.info(
|
|
1257
|
+
`\u{1F648}`,
|
|
1258
|
+
`timeline`,
|
|
1259
|
+
token.key,
|
|
1260
|
+
`Removing subscription "${key}" from timeline`
|
|
1261
|
+
);
|
|
1262
|
+
unsubscribe();
|
|
1263
|
+
};
|
|
1264
|
+
};
|
|
1265
|
+
|
|
1266
|
+
// internal/src/subscribe/subscribe-to-transaction.ts
|
|
1267
|
+
var subscribeToTransaction = (token, handleUpdate, key, store) => {
|
|
1268
|
+
const tx = withdraw(token, store);
|
|
1269
|
+
if (tx === void 0) {
|
|
1270
|
+
throw new Error(
|
|
1271
|
+
`Cannot subscribe to transaction "${token.key}": transaction not found in store "${store.config.name}".`
|
|
1272
|
+
);
|
|
1273
|
+
}
|
|
1274
|
+
store.logger.info(
|
|
1275
|
+
`\u{1F440}`,
|
|
1276
|
+
`transaction`,
|
|
1277
|
+
token.key,
|
|
1278
|
+
`Adding subscription "${key}"`
|
|
1279
|
+
);
|
|
1280
|
+
const unsubscribe = tx.subject.subscribe(key, handleUpdate);
|
|
1281
|
+
return () => {
|
|
1282
|
+
store.logger.info(
|
|
1283
|
+
`\u{1F648}`,
|
|
1284
|
+
`transaction`,
|
|
1285
|
+
token.key,
|
|
1286
|
+
`Removing subscription "${key}"`
|
|
1287
|
+
);
|
|
1288
|
+
unsubscribe();
|
|
1289
|
+
};
|
|
1290
|
+
};
|
|
1291
|
+
|
|
1292
|
+
// internal/src/mutable/tracker.ts
|
|
1293
|
+
var Tracker = class {
|
|
1294
|
+
constructor(mutableState, store) {
|
|
1295
|
+
this.unsubscribeFromInnerValue = null;
|
|
1296
|
+
this.mutableState = mutableState;
|
|
1297
|
+
const target = newest(store);
|
|
1298
|
+
this.latestUpdateState = this.initializeState(mutableState, target);
|
|
1299
|
+
this.observeCore(mutableState, this.latestUpdateState, target);
|
|
1300
|
+
this.updateCore(mutableState, this.latestUpdateState, target);
|
|
1301
|
+
target.trackers.set(mutableState.key, this);
|
|
1302
|
+
}
|
|
1303
|
+
initializeState(mutableState, store) {
|
|
1304
|
+
const latestUpdateStateKey = `*${mutableState.key}`;
|
|
1305
|
+
deleteAtom({ type: `atom`, key: latestUpdateStateKey }, store);
|
|
1306
|
+
const familyMetaData = mutableState.family ? {
|
|
1307
|
+
key: `*${mutableState.family.key}`,
|
|
1308
|
+
subKey: mutableState.family.subKey
|
|
1309
|
+
} : void 0;
|
|
1310
|
+
const latestUpdateState = createAtom(
|
|
1311
|
+
{
|
|
1312
|
+
key: latestUpdateStateKey,
|
|
1313
|
+
default: null
|
|
1314
|
+
},
|
|
1315
|
+
familyMetaData,
|
|
1316
|
+
store
|
|
1317
|
+
);
|
|
1318
|
+
return latestUpdateState;
|
|
1319
|
+
}
|
|
1320
|
+
observeCore(mutableState, latestUpdateState, store) {
|
|
1321
|
+
const originalInnerValue = getState(mutableState, store);
|
|
1322
|
+
const target = newest(store);
|
|
1323
|
+
this.unsubscribeFromInnerValue = originalInnerValue.subscribe(
|
|
1324
|
+
`tracker:${store.config.name}:${target.transactionMeta === null ? `main` : target.transactionMeta.update.key}`,
|
|
1325
|
+
(update) => {
|
|
1326
|
+
const unsubscribe = store.subject.operationStatus.subscribe(
|
|
1327
|
+
mutableState.key,
|
|
1328
|
+
() => {
|
|
1329
|
+
unsubscribe();
|
|
1330
|
+
setState(latestUpdateState, update, store);
|
|
1331
|
+
}
|
|
1332
|
+
);
|
|
1333
|
+
}
|
|
1334
|
+
);
|
|
1335
|
+
subscribeToState(
|
|
1336
|
+
mutableState,
|
|
1337
|
+
(update) => {
|
|
1338
|
+
var _a;
|
|
1339
|
+
if (update.newValue !== update.oldValue) {
|
|
1340
|
+
(_a = this.unsubscribeFromInnerValue) == null ? void 0 : _a.call(this);
|
|
1341
|
+
const target2 = newest(store);
|
|
1342
|
+
this.unsubscribeFromInnerValue = update.newValue.subscribe(
|
|
1343
|
+
`tracker:${store.config.name}:${target2.transactionMeta === null ? `main` : target2.transactionMeta.update.key}`,
|
|
1344
|
+
(update2) => {
|
|
1345
|
+
const unsubscribe = store.subject.operationStatus.subscribe(
|
|
1346
|
+
mutableState.key,
|
|
1347
|
+
() => {
|
|
1348
|
+
unsubscribe();
|
|
1349
|
+
setState(latestUpdateState, update2, store);
|
|
1350
|
+
}
|
|
1351
|
+
);
|
|
1352
|
+
}
|
|
1353
|
+
);
|
|
1354
|
+
}
|
|
1355
|
+
},
|
|
1356
|
+
`${store.config.name}: tracker observing inner value`,
|
|
1357
|
+
store
|
|
1358
|
+
);
|
|
1359
|
+
}
|
|
1360
|
+
updateCore(mutableState, latestUpdateState, store) {
|
|
1361
|
+
subscribeToState(
|
|
1362
|
+
latestUpdateState,
|
|
1363
|
+
({ newValue, oldValue }) => {
|
|
1364
|
+
const timelineId = store.timelineAtoms.getRelatedKey(
|
|
1365
|
+
latestUpdateState.key
|
|
1366
|
+
);
|
|
1367
|
+
if (timelineId) {
|
|
1368
|
+
const timelineData = store.timelines.get(timelineId);
|
|
1369
|
+
if (timelineData == null ? void 0 : timelineData.timeTraveling) {
|
|
1370
|
+
const unsubscribe2 = subscribeToTimeline(
|
|
1371
|
+
{ key: timelineId, type: `timeline` },
|
|
1372
|
+
(update) => {
|
|
1373
|
+
unsubscribe2();
|
|
1374
|
+
setState(
|
|
1375
|
+
mutableState,
|
|
1376
|
+
(transceiver) => {
|
|
1377
|
+
if (update === `redo` && newValue) {
|
|
1378
|
+
transceiver.do(newValue);
|
|
1379
|
+
} else if (update === `undo` && oldValue) {
|
|
1380
|
+
transceiver.undo(oldValue);
|
|
1381
|
+
}
|
|
1382
|
+
return transceiver;
|
|
1383
|
+
},
|
|
1384
|
+
store
|
|
1385
|
+
);
|
|
1386
|
+
},
|
|
1387
|
+
`${mutableState.key}: tracker observing timeline`,
|
|
1388
|
+
store
|
|
1389
|
+
);
|
|
1390
|
+
return;
|
|
1391
|
+
}
|
|
1392
|
+
}
|
|
1393
|
+
const unsubscribe = store.subject.operationStatus.subscribe(
|
|
1394
|
+
latestUpdateState.key,
|
|
1395
|
+
() => {
|
|
1396
|
+
unsubscribe();
|
|
1397
|
+
const mutable = getState(mutableState, store);
|
|
1398
|
+
const updateNumber = mutable.getUpdateNumber(newValue);
|
|
1399
|
+
const eventOffset = updateNumber - mutable.cacheUpdateNumber;
|
|
1400
|
+
if (newValue && eventOffset === 1) {
|
|
1401
|
+
setState(
|
|
1402
|
+
mutableState,
|
|
1403
|
+
(transceiver) => (transceiver.do(newValue), transceiver),
|
|
1404
|
+
store
|
|
1405
|
+
);
|
|
1406
|
+
}
|
|
1407
|
+
}
|
|
1408
|
+
);
|
|
1409
|
+
},
|
|
1410
|
+
`${store.config.name}: tracker observing latest update`,
|
|
1411
|
+
store
|
|
1412
|
+
);
|
|
1413
|
+
}
|
|
1414
|
+
};
|
|
1415
|
+
|
|
1416
|
+
// internal/src/mutable/create-mutable-atom.ts
|
|
1417
|
+
function createMutableAtom(options, store) {
|
|
1418
|
+
store.logger.info(
|
|
1419
|
+
`\u{1F527}`,
|
|
1420
|
+
`atom`,
|
|
1421
|
+
options.key,
|
|
1422
|
+
`creating in store "${store.config.name}"`
|
|
1423
|
+
);
|
|
1424
|
+
const coreState = createAtom(options, void 0, store);
|
|
1425
|
+
new Tracker(coreState, store);
|
|
1426
|
+
const jsonState = selectJson(coreState, options, store);
|
|
1427
|
+
const target = newest(store);
|
|
1428
|
+
subscribeToState(
|
|
1429
|
+
jsonState,
|
|
1430
|
+
() => {
|
|
1431
|
+
const trackerHasBeenInitialized = newest(store).trackers.has(coreState.key);
|
|
1432
|
+
if (!trackerHasBeenInitialized) {
|
|
1433
|
+
new Tracker(coreState, store);
|
|
1434
|
+
}
|
|
1435
|
+
},
|
|
1436
|
+
`tracker-initializer:${store == null ? void 0 : store.config.name}:${target.transactionMeta === null ? `main` : `${target.transactionMeta.update.key}`}`,
|
|
1437
|
+
store
|
|
1438
|
+
);
|
|
1439
|
+
return coreState;
|
|
1440
|
+
}
|
|
1441
|
+
|
|
1442
|
+
// internal/src/mutable/get-json-token.ts
|
|
1443
|
+
var getJsonToken = (mutableAtomToken) => {
|
|
1444
|
+
const key = mutableAtomToken.family ? `${mutableAtomToken.family.key}:JSON(${mutableAtomToken.family.subKey})` : `${mutableAtomToken.key}:JSON`;
|
|
1445
|
+
const jsonToken = { type: `selector`, key };
|
|
1446
|
+
if (mutableAtomToken.family) {
|
|
1447
|
+
jsonToken.family = {
|
|
1448
|
+
key: `${mutableAtomToken.family.key}:JSON`,
|
|
1449
|
+
subKey: mutableAtomToken.family.subKey
|
|
1450
|
+
};
|
|
1451
|
+
}
|
|
1452
|
+
return jsonToken;
|
|
1453
|
+
};
|
|
1454
|
+
|
|
1455
|
+
// internal/src/mutable/get-update-token.ts
|
|
1456
|
+
var getUpdateToken = (mutableAtomToken) => {
|
|
1457
|
+
const key = `*${mutableAtomToken.key}`;
|
|
1458
|
+
const updateToken = { type: `atom`, key };
|
|
1459
|
+
if (mutableAtomToken.family) {
|
|
1460
|
+
updateToken.family = {
|
|
1461
|
+
key: `*${mutableAtomToken.family.key}`,
|
|
1462
|
+
subKey: mutableAtomToken.family.subKey
|
|
1463
|
+
};
|
|
1464
|
+
}
|
|
1465
|
+
return updateToken;
|
|
1466
|
+
};
|
|
1467
|
+
|
|
1468
|
+
// internal/src/mutable/transceiver.ts
|
|
1469
|
+
function isTransceiver(value) {
|
|
1470
|
+
return typeof value === `object` && value !== null && `do` in value && `undo` in value && `subscribe` in value;
|
|
1471
|
+
}
|
|
1472
|
+
|
|
1473
|
+
// internal/src/atom/is-default.ts
|
|
1474
|
+
var isAtomDefault = (key, store) => {
|
|
1475
|
+
const core = newest(store);
|
|
1476
|
+
return core.atomsThatAreDefault.has(key);
|
|
1477
|
+
};
|
|
1478
|
+
var markAtomAsDefault = (key, store) => {
|
|
1479
|
+
const core = newest(store);
|
|
1480
|
+
core.atomsThatAreDefault = new Set(core.atomsThatAreDefault).add(key);
|
|
1481
|
+
};
|
|
1482
|
+
var markAtomAsNotDefault = (key, store) => {
|
|
1483
|
+
const core = newest(store);
|
|
1484
|
+
core.atomsThatAreDefault = new Set(newest(store).atomsThatAreDefault);
|
|
1485
|
+
core.atomsThatAreDefault.delete(key);
|
|
1486
|
+
};
|
|
1487
|
+
|
|
1488
|
+
// internal/src/atom/create-atom.ts
|
|
1489
|
+
function createAtom(options, family, store) {
|
|
1490
|
+
store.logger.info(
|
|
1491
|
+
`\u{1F528}`,
|
|
1492
|
+
`atom`,
|
|
1493
|
+
options.key,
|
|
1494
|
+
`creating in store "${store.config.name}"`
|
|
1495
|
+
);
|
|
1496
|
+
const target = newest(store);
|
|
1497
|
+
const existing = target.atoms.get(options.key);
|
|
1498
|
+
if (existing) {
|
|
1499
|
+
store.logger.error(
|
|
1500
|
+
`\u274C`,
|
|
1501
|
+
`atom`,
|
|
1502
|
+
options.key,
|
|
1503
|
+
`Tried to create atom, but it already exists in the store.`,
|
|
1504
|
+
`(Ignore if you are in development using hot module replacement.)`
|
|
1505
|
+
);
|
|
1506
|
+
return deposit(existing);
|
|
1507
|
+
}
|
|
1508
|
+
const subject = new Subject();
|
|
1509
|
+
const newAtom = __spreadValues(__spreadProps(__spreadValues({}, options), {
|
|
1510
|
+
type: `atom`,
|
|
1511
|
+
install: (store2) => {
|
|
1512
|
+
store2.logger.info(
|
|
1513
|
+
`\u{1F6E0}\uFE0F`,
|
|
1514
|
+
`atom`,
|
|
1515
|
+
options.key,
|
|
1516
|
+
`installing in store "${store2.config.name}"`
|
|
1517
|
+
);
|
|
1518
|
+
return `mutable` in options ? createMutableAtom(options, store2) : createAtom(options, void 0, store2);
|
|
1519
|
+
},
|
|
1520
|
+
subject
|
|
1521
|
+
}), family && { family });
|
|
1522
|
+
let initialValue = options.default;
|
|
1523
|
+
if (options.default instanceof Function) {
|
|
1524
|
+
initialValue = options.default();
|
|
1525
|
+
}
|
|
1526
|
+
target.atoms.set(newAtom.key, newAtom);
|
|
1527
|
+
markAtomAsDefault(options.key, store);
|
|
1528
|
+
cacheValue(options.key, initialValue, subject, store);
|
|
1529
|
+
const token = deposit(newAtom);
|
|
1530
|
+
if (options.effects) {
|
|
1531
|
+
let effectIndex = 0;
|
|
1532
|
+
const cleanupFunctions = [];
|
|
1533
|
+
for (const effect of options.effects) {
|
|
1534
|
+
const cleanup = effect({
|
|
1535
|
+
setSelf: (next) => setState(token, next, store),
|
|
1536
|
+
onSet: (handle) => subscribeToState(token, handle, `effect[${effectIndex}]`, store)
|
|
1537
|
+
});
|
|
1538
|
+
if (cleanup) {
|
|
1539
|
+
cleanupFunctions.push(cleanup);
|
|
1540
|
+
}
|
|
1541
|
+
++effectIndex;
|
|
1542
|
+
}
|
|
1543
|
+
newAtom.cleanup = () => {
|
|
1544
|
+
for (const cleanup of cleanupFunctions) {
|
|
1545
|
+
cleanup();
|
|
1546
|
+
}
|
|
1547
|
+
};
|
|
1548
|
+
}
|
|
1549
|
+
store.subject.atomCreation.next(token);
|
|
1550
|
+
return token;
|
|
1551
|
+
}
|
|
1552
|
+
|
|
1553
|
+
// internal/src/atom/delete-atom.ts
|
|
1554
|
+
function deleteAtom(atomToken, store) {
|
|
1555
|
+
var _a, _b;
|
|
1556
|
+
const target = newest(store);
|
|
1557
|
+
const { key } = atomToken;
|
|
1558
|
+
const atom2 = target.atoms.get(key);
|
|
1559
|
+
if (!atom2) {
|
|
1560
|
+
store.logger.error(
|
|
1561
|
+
`\u274C`,
|
|
1562
|
+
`atom`,
|
|
1563
|
+
`${key}`,
|
|
1564
|
+
`Tried to delete atom, but it does not exist in the store.`
|
|
1565
|
+
);
|
|
1566
|
+
}
|
|
1567
|
+
(_a = atom2 == null ? void 0 : atom2.cleanup) == null ? void 0 : _a.call(atom2);
|
|
1568
|
+
target.atoms.delete(key);
|
|
1569
|
+
target.valueMap.delete(key);
|
|
1570
|
+
const selectorKeys = target.selectorAtoms.getRelatedKeys(key);
|
|
1571
|
+
if (selectorKeys) {
|
|
1572
|
+
for (const selectorKey of selectorKeys) {
|
|
1573
|
+
const token = (_b = target.selectors.get(selectorKey)) != null ? _b : target.readonlySelectors.get(selectorKey);
|
|
1574
|
+
if (token) {
|
|
1575
|
+
deleteSelector(token, store);
|
|
1576
|
+
}
|
|
1577
|
+
}
|
|
1578
|
+
}
|
|
1579
|
+
target.selectorAtoms.delete(key);
|
|
1580
|
+
target.atomsThatAreDefault.delete(key);
|
|
1581
|
+
target.timelineAtoms.delete(key);
|
|
1582
|
+
store.logger.info(`\u{1F525}`, `atom`, `${key}`, `deleted`);
|
|
1583
|
+
}
|
|
1584
|
+
|
|
1585
|
+
// internal/src/not-found-error.ts
|
|
1586
|
+
var capitalize = (str) => str[0].toUpperCase() + str.slice(1);
|
|
1587
|
+
function prettyPrintTokenType(token) {
|
|
1588
|
+
if (token.type === `readonly_selector`) {
|
|
1589
|
+
return `Readonly Selector`;
|
|
1590
|
+
}
|
|
1591
|
+
return capitalize(token.type);
|
|
1592
|
+
}
|
|
1593
|
+
var NotFoundError = class extends Error {
|
|
1594
|
+
constructor(token, store) {
|
|
1595
|
+
super(
|
|
1596
|
+
`${prettyPrintTokenType(token)} "${token.key}" not found in store "${store.config.name}".`
|
|
1597
|
+
);
|
|
1598
|
+
}
|
|
1599
|
+
};
|
|
1600
|
+
var StoreContext = React8__namespace.createContext(IMPLICIT.STORE);
|
|
1601
|
+
function useI(token) {
|
|
1602
|
+
const store = React8__namespace.useContext(StoreContext);
|
|
1603
|
+
const setter = React8__namespace.useRef(null);
|
|
1604
|
+
if (setter.current === null) {
|
|
1605
|
+
setter.current = (next) => setState(token, next, store);
|
|
1606
|
+
}
|
|
1607
|
+
return setter.current;
|
|
1608
|
+
}
|
|
1609
|
+
|
|
1610
|
+
// realtime-client/src/realtime-state.ts
|
|
1611
|
+
var myIdState__INTERNAL = atom({
|
|
1612
|
+
key: `myId__INTERNAL`,
|
|
1613
|
+
default: null
|
|
1614
|
+
});
|
|
1615
|
+
selector({
|
|
1616
|
+
key: `myId`,
|
|
1617
|
+
get: ({ get }) => get(myIdState__INTERNAL)
|
|
1618
|
+
});
|
|
1619
|
+
|
|
1620
|
+
// realtime-client/src/use-pull.ts
|
|
1621
|
+
function pullState(token, socket, store) {
|
|
1622
|
+
socket.on(`serve:${token.key}`, (data) => {
|
|
1623
|
+
setState(token, data, store);
|
|
1624
|
+
});
|
|
1625
|
+
socket.emit(`sub:${token.key}`);
|
|
1626
|
+
return () => {
|
|
1627
|
+
socket.off(`serve:${token.key}`);
|
|
1628
|
+
socket.emit(`unsub:${token.key}`);
|
|
1629
|
+
};
|
|
1630
|
+
}
|
|
1631
|
+
|
|
1632
|
+
// realtime-client/src/use-pull-family-member.ts
|
|
1633
|
+
function pullFamilyMember(token, socket, store) {
|
|
1634
|
+
if (!(`family` in token)) {
|
|
1635
|
+
console.error(`Token is not a family member:`, token);
|
|
1636
|
+
return () => {
|
|
1637
|
+
};
|
|
1638
|
+
}
|
|
1639
|
+
const { key: familyKey, subKey: serializedSubKey } = token.family;
|
|
1640
|
+
const subKey = parseJson(serializedSubKey);
|
|
1641
|
+
socket == null ? void 0 : socket.on(`serve:${token.key}`, (data) => {
|
|
1642
|
+
setState(token, data, store);
|
|
1643
|
+
});
|
|
1644
|
+
socket == null ? void 0 : socket.emit(`sub:${familyKey}`, subKey);
|
|
1645
|
+
return () => {
|
|
1646
|
+
socket == null ? void 0 : socket.off(`serve:${token.key}`);
|
|
1647
|
+
socket == null ? void 0 : socket.emit(`unsub:${token.key}`);
|
|
1648
|
+
};
|
|
1649
|
+
}
|
|
1650
|
+
|
|
1651
|
+
// realtime-client/src/use-pull-mutable.ts
|
|
1652
|
+
function pullMutableState(token, socket, store) {
|
|
1653
|
+
const jsonToken = getJsonToken(token);
|
|
1654
|
+
const updateToken = getUpdateToken(token);
|
|
1655
|
+
socket.on(`init:${token.key}`, (data) => {
|
|
1656
|
+
setState(jsonToken, data, store);
|
|
1657
|
+
});
|
|
1658
|
+
socket.on(
|
|
1659
|
+
`next:${token.key}`,
|
|
1660
|
+
(data) => {
|
|
1661
|
+
setState(updateToken, data, store);
|
|
1662
|
+
}
|
|
1663
|
+
);
|
|
1664
|
+
socket.emit(`sub:${token.key}`);
|
|
1665
|
+
return () => {
|
|
1666
|
+
socket.off(`init:${token.key}`);
|
|
1667
|
+
socket.off(`next:${token.key}`);
|
|
1668
|
+
socket.emit(`unsub:${token.key}`);
|
|
1669
|
+
};
|
|
1670
|
+
}
|
|
1671
|
+
|
|
1672
|
+
// realtime-client/src/use-pull-mutable-family-member.ts
|
|
1673
|
+
function pullMutableFamilyMember(token, socket, store) {
|
|
1674
|
+
if (!(`family` in token)) {
|
|
1675
|
+
console.error(`Token is not a family member:`, token);
|
|
1676
|
+
return () => {
|
|
1677
|
+
};
|
|
1678
|
+
}
|
|
1679
|
+
const { key: familyKey, subKey: serializedSubKey } = token.family;
|
|
1680
|
+
const subKey = parseJson(serializedSubKey);
|
|
1681
|
+
socket == null ? void 0 : socket.on(`init:${token.key}`, (data) => {
|
|
1682
|
+
const jsonToken = getJsonToken(token);
|
|
1683
|
+
setState(jsonToken, data, store);
|
|
1684
|
+
});
|
|
1685
|
+
socket == null ? void 0 : socket.on(
|
|
1686
|
+
`next:${token.key}`,
|
|
1687
|
+
(data) => {
|
|
1688
|
+
const trackerToken = getUpdateToken(token);
|
|
1689
|
+
setState(trackerToken, data, store);
|
|
1690
|
+
}
|
|
1691
|
+
);
|
|
1692
|
+
socket == null ? void 0 : socket.emit(`sub:${familyKey}`, subKey);
|
|
1693
|
+
return () => {
|
|
1694
|
+
socket == null ? void 0 : socket.off(`serve:${token.key}`);
|
|
1695
|
+
socket == null ? void 0 : socket.emit(`unsub:${token.key}`);
|
|
1696
|
+
};
|
|
1697
|
+
}
|
|
1698
|
+
|
|
1699
|
+
// realtime-client/src/use-push.ts
|
|
1700
|
+
function pushState(token, socket, subscriptionKey, store) {
|
|
1701
|
+
socket.emit(`claim:${token.key}`);
|
|
1702
|
+
subscribeToState(
|
|
1703
|
+
token,
|
|
1704
|
+
({ newValue }) => {
|
|
1705
|
+
socket.emit(`pub:${token.key}`, newValue);
|
|
1706
|
+
},
|
|
1707
|
+
subscriptionKey,
|
|
1708
|
+
store
|
|
1709
|
+
);
|
|
1710
|
+
return () => {
|
|
1711
|
+
socket.off(`pub:${token.key}`);
|
|
1712
|
+
socket.emit(`unclaim:${token.key}`);
|
|
1713
|
+
};
|
|
1714
|
+
}
|
|
1715
|
+
|
|
1716
|
+
// realtime-client/src/use-server-action.ts
|
|
1717
|
+
var TX_SUBS = /* @__PURE__ */ new Map();
|
|
1718
|
+
function synchronizeTransactionResults(token, socket, store) {
|
|
1719
|
+
var _a;
|
|
1720
|
+
const count = (_a = TX_SUBS.get(token.key)) != null ? _a : 0;
|
|
1721
|
+
TX_SUBS.set(token.key, count + 1);
|
|
1722
|
+
const unsubscribe = count === 0 ? subscribeToTransaction(
|
|
1723
|
+
token,
|
|
1724
|
+
(clientUpdate) => {
|
|
1725
|
+
const transactionId = Math.random().toString(36).slice(2);
|
|
1726
|
+
const clientResult = JSON.stringify(clientUpdate);
|
|
1727
|
+
const topic = `tx:sync:${transactionId}`;
|
|
1728
|
+
const sync = (serverUpdate) => {
|
|
1729
|
+
store.logger.info(
|
|
1730
|
+
`\u{1F504}`,
|
|
1731
|
+
`transaction`,
|
|
1732
|
+
token.key,
|
|
1733
|
+
`syncing client and server`
|
|
1734
|
+
);
|
|
1735
|
+
socket.off(topic, sync);
|
|
1736
|
+
const serverResult = JSON.stringify(serverUpdate);
|
|
1737
|
+
if (clientResult !== serverResult) {
|
|
1738
|
+
store.logger.error(
|
|
1739
|
+
`\u274C`,
|
|
1740
|
+
`transaction`,
|
|
1741
|
+
token.key,
|
|
1742
|
+
`results do not match between client and server`
|
|
1743
|
+
);
|
|
1744
|
+
store.logger.error(
|
|
1745
|
+
`\u274C`,
|
|
1746
|
+
`transaction`,
|
|
1747
|
+
token.key,
|
|
1748
|
+
`client:`,
|
|
1749
|
+
clientResult
|
|
1750
|
+
);
|
|
1751
|
+
store.logger.error(
|
|
1752
|
+
`\u274C`,
|
|
1753
|
+
`transaction`,
|
|
1754
|
+
token.key,
|
|
1755
|
+
`server:`,
|
|
1756
|
+
serverResult
|
|
1757
|
+
);
|
|
1758
|
+
} else {
|
|
1759
|
+
store.logger.info(
|
|
1760
|
+
`\u2705`,
|
|
1761
|
+
`transaction`,
|
|
1762
|
+
token.key,
|
|
1763
|
+
`results match between client and server`
|
|
1764
|
+
);
|
|
1765
|
+
}
|
|
1766
|
+
};
|
|
1767
|
+
socket.on(topic, sync);
|
|
1768
|
+
socket.emit(`tx:${token.key}`, clientUpdate, transactionId);
|
|
1769
|
+
},
|
|
1770
|
+
`use-server-action`,
|
|
1771
|
+
store
|
|
1772
|
+
) : () => null;
|
|
1773
|
+
return () => {
|
|
1774
|
+
var _a2;
|
|
1775
|
+
const newCount = (_a2 = TX_SUBS.get(token.key)) != null ? _a2 : 0;
|
|
1776
|
+
TX_SUBS.set(token.key, newCount - 1);
|
|
1777
|
+
unsubscribe();
|
|
1778
|
+
};
|
|
1779
|
+
}
|
|
1780
|
+
var RealtimeContext = React8__namespace.createContext({
|
|
34
1781
|
socket: socket_ioClient.io()
|
|
35
1782
|
});
|
|
36
1783
|
var RealtimeProvider = ({ children, socket }) => {
|
|
37
|
-
const setMyId =
|
|
38
|
-
|
|
1784
|
+
const setMyId = useI(myIdState__INTERNAL);
|
|
1785
|
+
React8__namespace.useEffect(() => {
|
|
39
1786
|
socket.on(`connect`, () => {
|
|
40
1787
|
setMyId(socket.id);
|
|
41
1788
|
});
|
|
@@ -46,45 +1793,45 @@ var RealtimeProvider = ({ children, socket }) => {
|
|
|
46
1793
|
return /* @__PURE__ */ jsxRuntime.jsx(RealtimeContext.Provider, { value: { socket }, children });
|
|
47
1794
|
};
|
|
48
1795
|
function usePull(token) {
|
|
49
|
-
const { socket } =
|
|
50
|
-
const store =
|
|
51
|
-
|
|
1796
|
+
const { socket } = React8__namespace.useContext(RealtimeContext);
|
|
1797
|
+
const store = React8__namespace.useContext(StoreContext);
|
|
1798
|
+
React8__namespace.useEffect(() => pullState(token, socket, store), [token.key]);
|
|
52
1799
|
}
|
|
53
1800
|
function usePullFamilyMember(token) {
|
|
54
|
-
const { socket } =
|
|
55
|
-
const store =
|
|
56
|
-
|
|
1801
|
+
const { socket } = React8__namespace.useContext(RealtimeContext);
|
|
1802
|
+
const store = React8__namespace.useContext(StoreContext);
|
|
1803
|
+
React8__namespace.useEffect(() => pullFamilyMember(token, socket, store), [token.key]);
|
|
57
1804
|
}
|
|
58
1805
|
function usePullMutable(token) {
|
|
59
|
-
const { socket } =
|
|
60
|
-
const store =
|
|
61
|
-
|
|
1806
|
+
const { socket } = React8__namespace.useContext(RealtimeContext);
|
|
1807
|
+
const store = React8__namespace.useContext(StoreContext);
|
|
1808
|
+
React8__namespace.useEffect(() => pullMutableState(token, socket, store), [token.key]);
|
|
62
1809
|
}
|
|
63
1810
|
function usePullMutableFamilyMember(token) {
|
|
64
|
-
const { socket } =
|
|
65
|
-
const store =
|
|
66
|
-
|
|
67
|
-
() =>
|
|
1811
|
+
const { socket } = React8__namespace.useContext(RealtimeContext);
|
|
1812
|
+
const store = React8__namespace.useContext(StoreContext);
|
|
1813
|
+
React8__namespace.useEffect(
|
|
1814
|
+
() => pullMutableFamilyMember(token, socket, store),
|
|
68
1815
|
[token.key]
|
|
69
1816
|
);
|
|
70
1817
|
}
|
|
71
1818
|
function usePush(token) {
|
|
72
|
-
const { socket } =
|
|
73
|
-
const store =
|
|
74
|
-
const id =
|
|
75
|
-
|
|
76
|
-
() =>
|
|
1819
|
+
const { socket } = React8__namespace.useContext(RealtimeContext);
|
|
1820
|
+
const store = React8__namespace.useContext(StoreContext);
|
|
1821
|
+
const id = React8__namespace.useId();
|
|
1822
|
+
React8__namespace.useEffect(
|
|
1823
|
+
() => pushState(token, socket, `use-push:${id}`, store),
|
|
77
1824
|
[token.key]
|
|
78
1825
|
);
|
|
79
1826
|
}
|
|
80
1827
|
function useServerAction(token) {
|
|
81
|
-
const store =
|
|
82
|
-
const { socket } =
|
|
83
|
-
|
|
84
|
-
() =>
|
|
1828
|
+
const store = React8__namespace.useContext(StoreContext);
|
|
1829
|
+
const { socket } = React8__namespace.useContext(RealtimeContext);
|
|
1830
|
+
React8__namespace.useEffect(
|
|
1831
|
+
() => synchronizeTransactionResults(token, socket, store),
|
|
85
1832
|
[token.key]
|
|
86
1833
|
);
|
|
87
|
-
return
|
|
1834
|
+
return runTransaction(token, store);
|
|
88
1835
|
}
|
|
89
1836
|
|
|
90
1837
|
exports.RealtimeContext = RealtimeContext;
|