atom.io 0.31.1 → 0.32.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.d.ts +3 -154
- package/data/dist/index.js +11 -559
- package/data/src/index.ts +0 -2
- package/data/src/struct-family.ts +1 -1
- package/data/src/struct.ts +1 -2
- package/dist/chunk-3PQTWLQQ.js +83 -0
- package/dist/chunk-3ZFTRSNG.js +523 -0
- package/dist/chunk-4LWKCEW3.js +14 -0
- package/dist/chunk-KVI5OBF2.js +153 -0
- package/dist/{chunk-Y5MBNTVU.js → chunk-UQEYZ3OI.js} +1814 -721
- package/dist/chunk-UYYKOGZQ.js +1034 -0
- package/dist/chunk-VRJP2PCU.js +631 -0
- package/dist/chunk-X7SD2NXU.js +108 -0
- package/dist/index.d.ts +137 -12
- package/dist/index.js +1 -228
- package/eslint-plugin/dist/index.d.ts +1 -30
- package/eslint-plugin/dist/index.js +3 -149
- package/eslint-plugin/src/index.ts +0 -1
- package/eslint-plugin/src/rules/explicit-state-types.ts +1 -0
- package/eslint-plugin/src/rules/index.ts +0 -1
- package/eslint-plugin/src/rules/synchronous-selector-dependencies.ts +1 -0
- package/eslint-plugin/src/walk.ts +1 -0
- package/internal/dist/index.d.ts +129 -58
- package/internal/dist/index.js +1 -1
- package/internal/src/atom/create-regular-atom.ts +3 -3
- package/internal/src/atom/dispose-atom.ts +4 -13
- package/internal/src/atom/is-default.ts +3 -3
- package/internal/src/caching.ts +5 -5
- package/internal/src/capitalize.ts +3 -0
- package/internal/src/families/create-readonly-selector-family.ts +5 -6
- package/internal/src/families/create-writable-selector-family.ts +1 -4
- package/internal/src/families/dispose-from-store.ts +3 -13
- package/internal/src/get-state/get-from-store.ts +2 -2
- package/internal/src/get-state/read-or-compute-value.ts +1 -1
- package/internal/src/index.ts +2 -0
- package/internal/src/install-into-store.ts +1 -1
- package/internal/src/join/edit-relations-in-store.ts +32 -0
- package/internal/src/join/find-relations-in-store.ts +124 -0
- package/internal/src/join/get-internal-relations-from-store.ts +14 -0
- package/internal/src/join/get-join.ts +31 -0
- package/internal/src/join/index.ts +5 -0
- package/{data/src/join.ts → internal/src/join/join-internal.ts} +21 -430
- package/internal/src/junction.ts +7 -4
- package/internal/src/keys.ts +7 -7
- package/internal/src/mutable/create-mutable-atom-family.ts +1 -1
- package/internal/src/mutable/create-mutable-atom.ts +3 -3
- package/internal/src/mutable/get-json-token.ts +1 -1
- package/internal/src/mutable/tracker-family.ts +19 -17
- package/internal/src/mutable/tracker.ts +8 -8
- package/internal/src/pretty-print.ts +1 -1
- package/internal/src/selector/create-readonly-selector.ts +3 -7
- package/internal/src/selector/create-writable-selector.ts +4 -4
- package/internal/src/selector/dispose-selector.ts +20 -11
- package/internal/src/selector/get-selector-dependency-keys.ts +1 -1
- package/internal/src/selector/register-selector.ts +6 -9
- package/internal/src/selector/trace-selector-atoms.ts +2 -2
- package/internal/src/set-state/copy-mutable-if-needed.ts +1 -1
- package/internal/src/set-state/emit-update.ts +4 -2
- package/internal/src/set-state/evict-downstream.ts +1 -1
- package/internal/src/set-state/set-atom-or-selector.ts +1 -1
- package/internal/src/set-state/set-atom.ts +10 -10
- package/internal/src/set-state/set-into-store.ts +2 -2
- package/internal/src/set-state/stow-update.ts +1 -1
- package/internal/src/store/store.ts +1 -1
- package/internal/src/store/withdraw.ts +22 -22
- package/internal/src/subscribe/recall-state.ts +1 -1
- package/internal/src/subscribe/subscribe-in-store.ts +3 -3
- package/internal/src/subscribe/subscribe-to-root-atoms.ts +3 -3
- package/internal/src/subscribe/subscribe-to-state.ts +5 -5
- package/internal/src/subscribe/subscribe-to-timeline.ts +3 -3
- package/internal/src/subscribe/subscribe-to-transaction.ts +3 -3
- package/internal/src/timeline/create-timeline.ts +19 -38
- package/internal/src/timeline/time-travel.ts +2 -1
- package/internal/src/transaction/act-upon-store.ts +2 -2
- package/internal/src/transaction/apply-transaction.ts +5 -5
- package/internal/src/transaction/assign-transaction-to-continuity.ts +1 -1
- package/internal/src/transaction/build-transaction.ts +5 -8
- package/internal/src/transaction/create-transaction.ts +3 -3
- package/internal/src/transaction/get-epoch-number.ts +3 -3
- package/internal/src/transaction/set-epoch-number.ts +2 -2
- package/introspection/dist/index.js +2 -620
- package/json/dist/index.d.ts +2 -2
- package/json/dist/index.js +1 -80
- package/json/src/select-json-family.ts +3 -14
- package/package.json +31 -49
- package/react/dist/index.js +2 -82
- package/react/src/use-o.ts +1 -1
- package/react/src/use-tl.ts +2 -2
- package/react-devtools/dist/index.css +16 -14
- package/react-devtools/dist/index.js +31 -18
- package/react-devtools/src/Updates.tsx +12 -0
- package/react-devtools/src/devtools.scss +16 -14
- package/react-devtools/src/json-editor/editors-by-type/utilities/cast-to-json.ts +2 -1
- package/realtime/dist/index.d.ts +1 -2
- package/realtime/dist/index.js +2 -107
- package/realtime/src/realtime-continuity.ts +3 -2
- package/realtime/src/shared-room-store.ts +1 -2
- package/realtime-client/dist/index.d.ts +9 -9
- package/realtime-client/dist/index.js +3 -509
- package/realtime-client/src/continuity/register-and-attempt-confirmed-update.ts +3 -3
- package/realtime-client/src/continuity/use-conceal-state.ts +1 -1
- package/realtime-client/src/pull-atom-family-member.ts +2 -2
- package/realtime-client/src/pull-atom.ts +2 -2
- package/realtime-client/src/pull-mutable-atom-family-member.ts +2 -2
- package/realtime-client/src/pull-mutable-atom.ts +2 -2
- package/realtime-client/src/pull-selector-family-member.ts +4 -4
- package/realtime-client/src/pull-selector.ts +4 -4
- package/realtime-client/src/push-state.ts +5 -10
- package/realtime-client/src/server-action.ts +4 -4
- package/realtime-client/src/sync-continuity.ts +6 -6
- package/realtime-react/dist/index.js +5 -154
- package/realtime-react/src/use-pull-atom-family-member.ts +1 -1
- package/realtime-react/src/use-pull-atom.ts +1 -1
- package/realtime-react/src/use-pull-mutable-atom.ts +1 -1
- package/realtime-react/src/use-pull-mutable-family-member.ts +1 -1
- package/realtime-react/src/use-pull-selector-family-member.ts +1 -1
- package/realtime-react/src/use-pull-selector.ts +1 -1
- package/realtime-react/src/use-push.ts +1 -1
- package/realtime-react/src/use-server-action.ts +2 -2
- package/realtime-react/src/use-sync-continuity.ts +1 -1
- package/realtime-server/dist/index.d.ts +2 -4
- package/realtime-server/dist/index.js +3 -1001
- package/realtime-server/src/continuity/prepare-to-serve-transaction-request.ts +1 -1
- package/realtime-server/src/continuity/prepare-to-sync-realtime-continuity.ts +3 -3
- package/realtime-server/src/continuity/subscribe-to-continuity-actions.ts +2 -2
- package/realtime-server/src/continuity/subscribe-to-continuity-perpectives.ts +2 -2
- package/realtime-server/src/ipc-sockets/child-socket.ts +2 -0
- package/realtime-server/src/realtime-action-receiver.ts +1 -1
- package/realtime-server/src/realtime-family-provider.ts +2 -2
- package/realtime-server/src/realtime-mutable-family-provider.ts +2 -2
- package/realtime-server/src/realtime-mutable-provider.ts +2 -2
- package/realtime-server/src/realtime-server-stores/server-room-external-actions.ts +2 -1
- package/realtime-server/src/realtime-server-stores/server-room-external-store.ts +1 -1
- package/realtime-server/src/realtime-server-stores/server-sync-store.ts +10 -2
- package/realtime-server/src/realtime-server-stores/server-user-store.ts +1 -2
- package/realtime-server/src/realtime-state-provider.ts +2 -2
- package/realtime-testing/dist/index.js +20 -22
- package/realtime-testing/src/setup-realtime-test.tsx +2 -1
- package/src/index.ts +4 -0
- package/src/join.ts +218 -0
- package/src/silo.ts +4 -4
- package/src/timeline.ts +1 -1
- package/src/transaction.ts +4 -8
- package/transceivers/set-rtx/dist/index.d.ts +4 -3
- package/transceivers/set-rtx/dist/index.js +1 -215
- package/transceivers/set-rtx/src/set-rtx.ts +4 -7
- package/web/dist/index.js +1 -15
- package/data/src/until.ts +0 -15
- package/ephemeral/dist/index.d.ts +0 -67
- package/ephemeral/dist/index.js +0 -9
- package/ephemeral/package.json +0 -13
- package/ephemeral/src/index.ts +0 -1
- package/eslint-plugin/src/rules/lifespan.ts +0 -203
- package/immortal/dist/index.d.ts +0 -12
- package/immortal/dist/index.js +0 -9
- package/immortal/package.json +0 -13
- package/immortal/src/index.ts +0 -1
- package/immortal/src/seek-state.ts +0 -60
- package/react-devtools/src/json-editor/assets/Untitled-1.ai +2 -1436
- package/react-devtools/src/json-editor/assets/data-vis.ai +1 -1548
- package/react-devtools/src/json-editor/comp/json-editor-sketches.ai +5 -1449
- /package/{ephemeral/src → src}/find-state.ts +0 -0
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { IMPLICIT, setIntoStore, subscribeToState, getFromStore, findInStore, getJsonToken, withdraw, subscribeToTimeline, redo, undo } from './chunk-UQEYZ3OI.js';
|
|
2
|
+
import * as React5 from 'react';
|
|
3
|
+
import { jsx } from 'react/jsx-runtime';
|
|
4
|
+
|
|
5
|
+
var StoreContext = React5.createContext(IMPLICIT.STORE);
|
|
6
|
+
var StoreProvider = ({ children, store = IMPLICIT.STORE }) => /* @__PURE__ */ jsx(StoreContext.Provider, { value: store, children });
|
|
7
|
+
|
|
8
|
+
// react/src/parse-state-overloads.ts
|
|
9
|
+
function parseStateOverloads(store, ...rest) {
|
|
10
|
+
let token;
|
|
11
|
+
if (rest.length === 2) {
|
|
12
|
+
const family = rest[0];
|
|
13
|
+
const key = rest[1];
|
|
14
|
+
token = findInStore(store, family, key);
|
|
15
|
+
} else {
|
|
16
|
+
token = rest[0];
|
|
17
|
+
}
|
|
18
|
+
return token;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// react/src/use-i.ts
|
|
22
|
+
function useI(...params) {
|
|
23
|
+
const store = React5.useContext(StoreContext);
|
|
24
|
+
const token = parseStateOverloads(store, ...params);
|
|
25
|
+
const setter = React5.useRef(null);
|
|
26
|
+
if (setter.current === null) {
|
|
27
|
+
setter.current = (next) => {
|
|
28
|
+
setIntoStore(store, token, next);
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
return setter.current;
|
|
32
|
+
}
|
|
33
|
+
function useO(...params) {
|
|
34
|
+
const store = React5.useContext(StoreContext);
|
|
35
|
+
const token = parseStateOverloads(store, ...params);
|
|
36
|
+
const id = React5.useId();
|
|
37
|
+
return React5.useSyncExternalStore(
|
|
38
|
+
(dispatch) => subscribeToState(store, token, `use-o:${id}`, dispatch),
|
|
39
|
+
() => getFromStore(store, token),
|
|
40
|
+
() => getFromStore(store, token)
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// react/src/use-json.ts
|
|
45
|
+
function useJSON(token, key) {
|
|
46
|
+
const store = React5.useContext(StoreContext);
|
|
47
|
+
const stateToken = token.type === `mutable_atom_family` ? findInStore(store, token, key) : token;
|
|
48
|
+
const jsonToken = getJsonToken(store, stateToken);
|
|
49
|
+
return useO(jsonToken);
|
|
50
|
+
}
|
|
51
|
+
function useTL(token) {
|
|
52
|
+
const store = React5.useContext(StoreContext);
|
|
53
|
+
const id = React5.useId();
|
|
54
|
+
const timeline = withdraw(store, token);
|
|
55
|
+
const tokenRef = React5.useRef(token);
|
|
56
|
+
const rebuildMeta = () => {
|
|
57
|
+
return {
|
|
58
|
+
at: timeline.at,
|
|
59
|
+
length: timeline.history.length,
|
|
60
|
+
undo: () => {
|
|
61
|
+
undo(token);
|
|
62
|
+
},
|
|
63
|
+
redo: () => {
|
|
64
|
+
redo(token);
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
};
|
|
68
|
+
const meta = React5.useRef(rebuildMeta());
|
|
69
|
+
const retrieve = () => {
|
|
70
|
+
if (meta.current.at !== timeline?.at || meta.current.length !== timeline?.history.length || tokenRef.current !== token) {
|
|
71
|
+
tokenRef.current = token;
|
|
72
|
+
meta.current = rebuildMeta();
|
|
73
|
+
}
|
|
74
|
+
return meta.current;
|
|
75
|
+
};
|
|
76
|
+
return React5.useSyncExternalStore(
|
|
77
|
+
(dispatch) => subscribeToTimeline(store, token, `use-tl:${id}`, dispatch),
|
|
78
|
+
retrieve,
|
|
79
|
+
retrieve
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export { StoreContext, StoreProvider, useI, useJSON, useO, useTL };
|
|
@@ -0,0 +1,523 @@
|
|
|
1
|
+
import { persistSync } from './chunk-4LWKCEW3.js';
|
|
2
|
+
import { atom, selector, setIntoStore, getEpochNumberOfContinuity, isRootStore, ingestTransactionUpdate, setEpochNumberOfContinuity, disposeAtom, parseJson, getJsonToken, getUpdateToken, subscribeToState, subscribeToTransaction, getFromStore, assignTransactionToContinuity, actUponStore } from './chunk-UQEYZ3OI.js';
|
|
3
|
+
|
|
4
|
+
// realtime-client/src/continuity/register-and-attempt-confirmed-update.ts
|
|
5
|
+
var useRegisterAndAttemptConfirmedUpdate = (store, continuityKey, socket, optimisticUpdates, confirmedUpdates) => (confirmed) => {
|
|
6
|
+
function reconcileEpoch(optimisticUpdate, confirmedUpdate) {
|
|
7
|
+
store.logger.info(
|
|
8
|
+
`\u{1F9D1}\u200D\u2696\uFE0F`,
|
|
9
|
+
`continuity`,
|
|
10
|
+
continuityKey,
|
|
11
|
+
`reconciling updates`
|
|
12
|
+
);
|
|
13
|
+
setIntoStore(store, optimisticUpdateQueue, (queue) => {
|
|
14
|
+
queue.shift();
|
|
15
|
+
return queue;
|
|
16
|
+
});
|
|
17
|
+
if (optimisticUpdate.id === confirmedUpdate.id) {
|
|
18
|
+
const clientResult = JSON.stringify(optimisticUpdate.updates);
|
|
19
|
+
const serverResult = JSON.stringify(confirmedUpdate.updates);
|
|
20
|
+
if (clientResult === serverResult) {
|
|
21
|
+
store.logger.info(
|
|
22
|
+
`\u2705`,
|
|
23
|
+
`continuity`,
|
|
24
|
+
continuityKey,
|
|
25
|
+
`results for ${optimisticUpdate.id} match between client and server`
|
|
26
|
+
);
|
|
27
|
+
socket.emit(`ack:${continuityKey}`, confirmedUpdate.epoch);
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
} else {
|
|
31
|
+
store.logger.info(
|
|
32
|
+
`\u274C`,
|
|
33
|
+
`continuity`,
|
|
34
|
+
continuityKey,
|
|
35
|
+
`thought update #${confirmedUpdate.epoch} was ${optimisticUpdate.key}:${optimisticUpdate.id}, but it was actually ${confirmedUpdate.key}:${confirmedUpdate.id}`
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
store.logger.info(
|
|
39
|
+
`\u{1F9D1}\u200D\u2696\uFE0F`,
|
|
40
|
+
`continuity`,
|
|
41
|
+
continuityKey,
|
|
42
|
+
`updates do not match`,
|
|
43
|
+
optimisticUpdate,
|
|
44
|
+
confirmedUpdate
|
|
45
|
+
);
|
|
46
|
+
const reversedOptimisticUpdates = optimisticUpdates.toReversed();
|
|
47
|
+
for (const subsequentOptimistic of reversedOptimisticUpdates) {
|
|
48
|
+
ingestTransactionUpdate(`oldValue`, subsequentOptimistic, store);
|
|
49
|
+
}
|
|
50
|
+
store.logger.info(
|
|
51
|
+
`\u23EA`,
|
|
52
|
+
`continuity`,
|
|
53
|
+
continuityKey,
|
|
54
|
+
`undid optimistic updates:`,
|
|
55
|
+
reversedOptimisticUpdates
|
|
56
|
+
);
|
|
57
|
+
ingestTransactionUpdate(`oldValue`, optimisticUpdate, store);
|
|
58
|
+
store.logger.info(
|
|
59
|
+
`\u23EA`,
|
|
60
|
+
`continuity`,
|
|
61
|
+
continuityKey,
|
|
62
|
+
`undid zeroth optimistic update`,
|
|
63
|
+
optimisticUpdate
|
|
64
|
+
);
|
|
65
|
+
ingestTransactionUpdate(`newValue`, confirmedUpdate, store);
|
|
66
|
+
store.logger.info(
|
|
67
|
+
`\u23E9`,
|
|
68
|
+
`continuity`,
|
|
69
|
+
continuityKey,
|
|
70
|
+
`applied confirmed update`,
|
|
71
|
+
confirmedUpdate
|
|
72
|
+
);
|
|
73
|
+
socket.emit(`ack:${continuityKey}`, confirmedUpdate.epoch);
|
|
74
|
+
for (const subsequentOptimistic of optimisticUpdates) {
|
|
75
|
+
const token = {
|
|
76
|
+
type: `transaction`,
|
|
77
|
+
key: subsequentOptimistic.key
|
|
78
|
+
};
|
|
79
|
+
const { id, params } = subsequentOptimistic;
|
|
80
|
+
actUponStore(store, token, id)(...params);
|
|
81
|
+
}
|
|
82
|
+
store.logger.info(
|
|
83
|
+
`\u23E9`,
|
|
84
|
+
`continuity`,
|
|
85
|
+
continuityKey,
|
|
86
|
+
`reapplied subsequent optimistic updates:`,
|
|
87
|
+
optimisticUpdates
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
store.logger.info(
|
|
91
|
+
`\u{1F9D1}\u200D\u2696\uFE0F`,
|
|
92
|
+
`continuity`,
|
|
93
|
+
continuityKey,
|
|
94
|
+
`integrating confirmed update`,
|
|
95
|
+
{ confirmedUpdate: confirmed, confirmedUpdates, optimisticUpdates }
|
|
96
|
+
);
|
|
97
|
+
const zerothOptimisticUpdate = optimisticUpdates[0];
|
|
98
|
+
if (zerothOptimisticUpdate) {
|
|
99
|
+
store.logger.info(
|
|
100
|
+
`\u{1F9D1}\u200D\u2696\uFE0F`,
|
|
101
|
+
`continuity`,
|
|
102
|
+
continuityKey,
|
|
103
|
+
`has optimistic updates to reconcile`
|
|
104
|
+
);
|
|
105
|
+
if (confirmed.epoch === zerothOptimisticUpdate.epoch) {
|
|
106
|
+
store.logger.info(
|
|
107
|
+
`\u{1F9D1}\u200D\u2696\uFE0F`,
|
|
108
|
+
`continuity`,
|
|
109
|
+
continuityKey,
|
|
110
|
+
`epoch of confirmed update #${confirmed.epoch} matches zeroth optimistic update`
|
|
111
|
+
);
|
|
112
|
+
reconcileEpoch(zerothOptimisticUpdate, confirmed);
|
|
113
|
+
for (const nextConfirmed of confirmedUpdates) {
|
|
114
|
+
const nextOptimistic = optimisticUpdates[0];
|
|
115
|
+
if (nextConfirmed.epoch === nextOptimistic?.epoch) {
|
|
116
|
+
reconcileEpoch(nextOptimistic, nextConfirmed);
|
|
117
|
+
} else {
|
|
118
|
+
break;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
} else {
|
|
122
|
+
store.logger.info(
|
|
123
|
+
`\u{1F9D1}\u200D\u2696\uFE0F`,
|
|
124
|
+
`continuity`,
|
|
125
|
+
continuityKey,
|
|
126
|
+
`epoch of confirmed update #${confirmed.epoch} does not match zeroth optimistic update #${zerothOptimisticUpdate.epoch}`
|
|
127
|
+
);
|
|
128
|
+
const confirmedUpdateIsAlreadyEnqueued = confirmedUpdates.some(
|
|
129
|
+
(update) => update.epoch === confirmed.epoch
|
|
130
|
+
);
|
|
131
|
+
if (!confirmedUpdateIsAlreadyEnqueued) {
|
|
132
|
+
store.logger.info(
|
|
133
|
+
`\u{1F448}`,
|
|
134
|
+
`continuity`,
|
|
135
|
+
continuityKey,
|
|
136
|
+
`pushing confirmed update to queue`,
|
|
137
|
+
confirmed
|
|
138
|
+
);
|
|
139
|
+
setIntoStore(store, confirmedUpdateQueue, (queue) => {
|
|
140
|
+
queue.push(confirmed);
|
|
141
|
+
queue.sort((a, b) => a.epoch - b.epoch);
|
|
142
|
+
return queue;
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
} else {
|
|
147
|
+
store.logger.info(
|
|
148
|
+
`\u{1F9D1}\u200D\u2696\uFE0F`,
|
|
149
|
+
`continuity`,
|
|
150
|
+
continuityKey,
|
|
151
|
+
`has no optimistic updates to deal with`
|
|
152
|
+
);
|
|
153
|
+
const continuityEpoch = getEpochNumberOfContinuity(store, continuityKey);
|
|
154
|
+
const isRoot = isRootStore(store);
|
|
155
|
+
if (isRoot && continuityEpoch === confirmed.epoch - 1) {
|
|
156
|
+
store.logger.info(
|
|
157
|
+
`\u2705`,
|
|
158
|
+
`continuity`,
|
|
159
|
+
continuityKey,
|
|
160
|
+
`integrating update #${confirmed.epoch} (${confirmed.key} ${confirmed.id})`
|
|
161
|
+
);
|
|
162
|
+
ingestTransactionUpdate(`newValue`, confirmed, store);
|
|
163
|
+
socket.emit(`ack:${continuityKey}`, confirmed.epoch);
|
|
164
|
+
setEpochNumberOfContinuity(store, continuityKey, confirmed.epoch);
|
|
165
|
+
} else if (isRoot && continuityEpoch !== void 0) {
|
|
166
|
+
store.logger.info(
|
|
167
|
+
`\u{1F9D1}\u200D\u2696\uFE0F`,
|
|
168
|
+
`continuity`,
|
|
169
|
+
continuityKey,
|
|
170
|
+
`received update #${confirmed.epoch} but still waiting for update #${continuityEpoch + 1}`,
|
|
171
|
+
{
|
|
172
|
+
clientEpoch: continuityEpoch,
|
|
173
|
+
serverEpoch: confirmed.epoch
|
|
174
|
+
}
|
|
175
|
+
);
|
|
176
|
+
const confirmedUpdateIsAlreadyEnqueued = confirmedUpdates.some(
|
|
177
|
+
(update) => update.epoch === confirmed.epoch
|
|
178
|
+
);
|
|
179
|
+
if (confirmedUpdateIsAlreadyEnqueued) {
|
|
180
|
+
store.logger.info(
|
|
181
|
+
`\u{1F44D}`,
|
|
182
|
+
`continuity`,
|
|
183
|
+
continuityKey,
|
|
184
|
+
`confirmed update #${confirmed.epoch} is already enqueued`
|
|
185
|
+
);
|
|
186
|
+
} else {
|
|
187
|
+
store.logger.info(
|
|
188
|
+
`\u{1F448}`,
|
|
189
|
+
`continuity`,
|
|
190
|
+
continuityKey,
|
|
191
|
+
`pushing confirmed update #${confirmed.epoch} to queue`
|
|
192
|
+
);
|
|
193
|
+
setIntoStore(store, confirmedUpdateQueue, (queue) => {
|
|
194
|
+
queue.push(confirmed);
|
|
195
|
+
queue.sort((a, b) => a.epoch - b.epoch);
|
|
196
|
+
return queue;
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
// realtime-client/src/continuity/use-conceal-state.ts
|
|
204
|
+
function useConcealState(store) {
|
|
205
|
+
return (concealed) => {
|
|
206
|
+
for (const token of concealed) {
|
|
207
|
+
disposeAtom(store, token);
|
|
208
|
+
}
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// realtime-client/src/continuity/use-reveal-state.ts
|
|
213
|
+
function useRevealState(store) {
|
|
214
|
+
return (revealed) => {
|
|
215
|
+
let i = 0;
|
|
216
|
+
let k;
|
|
217
|
+
let v;
|
|
218
|
+
for (const x of revealed) {
|
|
219
|
+
if (i % 2 === 0) {
|
|
220
|
+
k = x;
|
|
221
|
+
} else {
|
|
222
|
+
v = x;
|
|
223
|
+
setIntoStore(store, k, v);
|
|
224
|
+
}
|
|
225
|
+
i++;
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// realtime-client/src/pull-atom.ts
|
|
231
|
+
function pullAtom(store, socket, token) {
|
|
232
|
+
const setServedValue = (data) => {
|
|
233
|
+
setIntoStore(store, token, data);
|
|
234
|
+
};
|
|
235
|
+
socket.on(`serve:${token.key}`, setServedValue);
|
|
236
|
+
socket.emit(`sub:${token.key}`);
|
|
237
|
+
return () => {
|
|
238
|
+
socket.off(`serve:${token.key}`, setServedValue);
|
|
239
|
+
socket.emit(`unsub:${token.key}`);
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// realtime-client/src/pull-atom-family-member.ts
|
|
244
|
+
function pullAtomFamilyMember(store, socket, token) {
|
|
245
|
+
if (!(`family` in token)) {
|
|
246
|
+
console.error(`Token is not a family member:`, token);
|
|
247
|
+
return () => {
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
const { key: familyKey, subKey: serializedSubKey } = token.family;
|
|
251
|
+
const subKey = parseJson(serializedSubKey);
|
|
252
|
+
socket?.on(`serve:${token.key}`, (data) => {
|
|
253
|
+
setIntoStore(store, token, data);
|
|
254
|
+
});
|
|
255
|
+
socket?.emit(`sub:${familyKey}`, subKey);
|
|
256
|
+
return () => {
|
|
257
|
+
socket?.off(`serve:${token.key}`);
|
|
258
|
+
socket?.emit(`unsub:${token.key}`);
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
// realtime-client/src/pull-mutable-atom.ts
|
|
263
|
+
function pullMutableAtom(store, socket, token) {
|
|
264
|
+
const jsonToken = getJsonToken(store, token);
|
|
265
|
+
const updateToken = getUpdateToken(token);
|
|
266
|
+
socket.on(`init:${token.key}`, (data) => {
|
|
267
|
+
setIntoStore(store, jsonToken, data);
|
|
268
|
+
});
|
|
269
|
+
socket.on(
|
|
270
|
+
`next:${token.key}`,
|
|
271
|
+
(data) => {
|
|
272
|
+
setIntoStore(store, updateToken, data);
|
|
273
|
+
}
|
|
274
|
+
);
|
|
275
|
+
socket.emit(`sub:${token.key}`);
|
|
276
|
+
return () => {
|
|
277
|
+
socket.off(`init:${token.key}`);
|
|
278
|
+
socket.off(`next:${token.key}`);
|
|
279
|
+
socket.emit(`unsub:${token.key}`);
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
// realtime-client/src/pull-mutable-atom-family-member.ts
|
|
284
|
+
function pullMutableAtomFamilyMember(store, socket, token) {
|
|
285
|
+
if (!(`family` in token)) {
|
|
286
|
+
console.error(`Token is not a family member:`, token);
|
|
287
|
+
return () => {
|
|
288
|
+
};
|
|
289
|
+
}
|
|
290
|
+
const { key: familyKey, subKey: serializedSubKey } = token.family;
|
|
291
|
+
const subKey = parseJson(serializedSubKey);
|
|
292
|
+
socket.on(`init:${token.key}`, (data) => {
|
|
293
|
+
const jsonToken = getJsonToken(store, token);
|
|
294
|
+
setIntoStore(store, jsonToken, data);
|
|
295
|
+
});
|
|
296
|
+
socket.on(
|
|
297
|
+
`next:${token.key}`,
|
|
298
|
+
(data) => {
|
|
299
|
+
const trackerToken = getUpdateToken(token);
|
|
300
|
+
setIntoStore(store, trackerToken, data);
|
|
301
|
+
}
|
|
302
|
+
);
|
|
303
|
+
socket.emit(`sub:${familyKey}`, subKey);
|
|
304
|
+
return () => {
|
|
305
|
+
socket.off(`serve:${token.key}`);
|
|
306
|
+
socket.emit(`unsub:${token.key}`);
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
// realtime-client/src/pull-selector.ts
|
|
311
|
+
function pullSelector(store, socket, token) {
|
|
312
|
+
const atomKeys = store.selectorAtoms.getRelatedKeys(token.key);
|
|
313
|
+
const unsubscribes = [];
|
|
314
|
+
if (atomKeys) {
|
|
315
|
+
for (const atomKey of atomKeys) {
|
|
316
|
+
const atom2 = store.atoms.get(atomKey);
|
|
317
|
+
if (!atom2) {
|
|
318
|
+
continue;
|
|
319
|
+
}
|
|
320
|
+
switch (atom2.type) {
|
|
321
|
+
case `atom`: {
|
|
322
|
+
unsubscribes.push(pullAtom(store, socket, atom2));
|
|
323
|
+
break;
|
|
324
|
+
}
|
|
325
|
+
case `mutable_atom`: {
|
|
326
|
+
unsubscribes.push(pullMutableAtom(store, socket, atom2));
|
|
327
|
+
break;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
return () => {
|
|
333
|
+
for (const unsubscribe of unsubscribes) {
|
|
334
|
+
unsubscribe();
|
|
335
|
+
}
|
|
336
|
+
};
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
// realtime-client/src/pull-selector-family-member.ts
|
|
340
|
+
function pullSelectorFamilyMember(store, socket, token) {
|
|
341
|
+
if (!(`family` in token)) {
|
|
342
|
+
console.error(`Token is not a family member:`, token);
|
|
343
|
+
return () => {
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
const atomKeys = store.selectorAtoms.getRelatedKeys(token.key);
|
|
347
|
+
const unsubscribes = [];
|
|
348
|
+
if (atomKeys) {
|
|
349
|
+
for (const atomKey of atomKeys) {
|
|
350
|
+
const atom2 = store.atoms.get(atomKey);
|
|
351
|
+
if (!atom2) {
|
|
352
|
+
continue;
|
|
353
|
+
}
|
|
354
|
+
switch (atom2.type) {
|
|
355
|
+
case `atom`: {
|
|
356
|
+
unsubscribes.push(pullAtomFamilyMember(store, socket, atom2));
|
|
357
|
+
break;
|
|
358
|
+
}
|
|
359
|
+
case `mutable_atom`: {
|
|
360
|
+
unsubscribes.push(pullMutableAtomFamilyMember(store, socket, atom2));
|
|
361
|
+
break;
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
return () => {
|
|
367
|
+
for (const unsubscribe of unsubscribes) {
|
|
368
|
+
unsubscribe();
|
|
369
|
+
}
|
|
370
|
+
};
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
// realtime-client/src/push-state.ts
|
|
374
|
+
function pushState(store, socket, token) {
|
|
375
|
+
socket.emit(`claim:${token.key}`);
|
|
376
|
+
subscribeToState(store, token, `push`, ({ newValue }) => {
|
|
377
|
+
socket.emit(`pub:${token.key}`, newValue);
|
|
378
|
+
});
|
|
379
|
+
return () => {
|
|
380
|
+
socket.off(`pub:${token.key}`);
|
|
381
|
+
socket.emit(`unclaim:${token.key}`);
|
|
382
|
+
};
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
// realtime-client/src/realtime-client-stores/client-main-store.ts
|
|
386
|
+
var myIdState__INTERNAL = atom({
|
|
387
|
+
key: `mySocketId__INTERNAL`,
|
|
388
|
+
default: void 0
|
|
389
|
+
});
|
|
390
|
+
var myIdState = selector({
|
|
391
|
+
key: `mySocketId`,
|
|
392
|
+
get: ({ get }) => get(myIdState__INTERNAL)
|
|
393
|
+
});
|
|
394
|
+
var myUsernameState = atom({
|
|
395
|
+
key: `myName`,
|
|
396
|
+
default: null,
|
|
397
|
+
effects: typeof window === `undefined` ? [] : [persistSync(window.localStorage, JSON, `myUsername`)]
|
|
398
|
+
});
|
|
399
|
+
|
|
400
|
+
// realtime-client/src/realtime-client-stores/client-sync-store.ts
|
|
401
|
+
var optimisticUpdateQueue = atom({
|
|
402
|
+
key: `updateQueue`,
|
|
403
|
+
default: []
|
|
404
|
+
});
|
|
405
|
+
var confirmedUpdateQueue = atom(
|
|
406
|
+
{
|
|
407
|
+
key: `serverConfirmedUpdateQueue`,
|
|
408
|
+
default: []
|
|
409
|
+
}
|
|
410
|
+
);
|
|
411
|
+
|
|
412
|
+
// realtime-client/src/server-action.ts
|
|
413
|
+
function serverAction(store, socket, token) {
|
|
414
|
+
const unsubscribeFromLocalUpdates = subscribeToTransaction(
|
|
415
|
+
store,
|
|
416
|
+
token,
|
|
417
|
+
`tx-run:${token.key}:${socket.id}`,
|
|
418
|
+
(clientUpdate) => {
|
|
419
|
+
socket.emit(`tx-run:${token.key}`, clientUpdate);
|
|
420
|
+
}
|
|
421
|
+
);
|
|
422
|
+
return () => {
|
|
423
|
+
unsubscribeFromLocalUpdates();
|
|
424
|
+
};
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
// realtime-client/src/sync-continuity.ts
|
|
428
|
+
function syncContinuity(store, socket, continuity) {
|
|
429
|
+
const continuityKey = continuity.key;
|
|
430
|
+
const optimisticUpdates = getFromStore(store, optimisticUpdateQueue);
|
|
431
|
+
const confirmedUpdates = getFromStore(store, confirmedUpdateQueue);
|
|
432
|
+
const initializeContinuity = (epoch, payload) => {
|
|
433
|
+
socket.off(`continuity-init:${continuityKey}`, initializeContinuity);
|
|
434
|
+
let i = 0;
|
|
435
|
+
let k;
|
|
436
|
+
let v;
|
|
437
|
+
for (const x of payload) {
|
|
438
|
+
if (i % 2 === 0) {
|
|
439
|
+
k = x;
|
|
440
|
+
} else {
|
|
441
|
+
v = x;
|
|
442
|
+
if (`type` in k && k.type === `mutable_atom`) {
|
|
443
|
+
k = getJsonToken(store, k);
|
|
444
|
+
}
|
|
445
|
+
setIntoStore(store, k, v);
|
|
446
|
+
}
|
|
447
|
+
i++;
|
|
448
|
+
}
|
|
449
|
+
setEpochNumberOfContinuity(store, continuityKey, epoch);
|
|
450
|
+
};
|
|
451
|
+
socket.off(`continuity-init:${continuityKey}`);
|
|
452
|
+
socket.on(`continuity-init:${continuityKey}`, initializeContinuity);
|
|
453
|
+
const registerAndAttemptConfirmedUpdate = useRegisterAndAttemptConfirmedUpdate(
|
|
454
|
+
store,
|
|
455
|
+
continuityKey,
|
|
456
|
+
socket,
|
|
457
|
+
optimisticUpdates,
|
|
458
|
+
confirmedUpdates
|
|
459
|
+
);
|
|
460
|
+
socket.off(`tx-new:${continuityKey}`);
|
|
461
|
+
socket.on(`tx-new:${continuityKey}`, registerAndAttemptConfirmedUpdate);
|
|
462
|
+
const unsubscribeFunctions = continuity.actions.map((transaction) => {
|
|
463
|
+
assignTransactionToContinuity(store, continuityKey, transaction.key);
|
|
464
|
+
const unsubscribeFromTransactionUpdates = subscribeToTransaction(
|
|
465
|
+
store,
|
|
466
|
+
transaction,
|
|
467
|
+
`tx-run:${continuityKey}`,
|
|
468
|
+
(clientUpdate) => {
|
|
469
|
+
store.logger.info(
|
|
470
|
+
`\u{1F91E}`,
|
|
471
|
+
`continuity`,
|
|
472
|
+
continuityKey,
|
|
473
|
+
`enqueuing optimistic update`
|
|
474
|
+
);
|
|
475
|
+
const optimisticUpdateIndex = optimisticUpdates.findIndex(
|
|
476
|
+
(update) => update.id === clientUpdate.id
|
|
477
|
+
);
|
|
478
|
+
if (optimisticUpdateIndex === -1) {
|
|
479
|
+
store.logger.info(
|
|
480
|
+
`\u{1F91E}`,
|
|
481
|
+
`continuity`,
|
|
482
|
+
continuityKey,
|
|
483
|
+
`enqueuing new optimistic update`
|
|
484
|
+
);
|
|
485
|
+
setIntoStore(store, optimisticUpdateQueue, (queue) => {
|
|
486
|
+
queue.push(clientUpdate);
|
|
487
|
+
queue.sort((a, b) => a.epoch - b.epoch);
|
|
488
|
+
return queue;
|
|
489
|
+
});
|
|
490
|
+
} else {
|
|
491
|
+
store.logger.info(
|
|
492
|
+
`\u{1F91E}`,
|
|
493
|
+
`continuity`,
|
|
494
|
+
continuityKey,
|
|
495
|
+
`replacing existing optimistic update at index ${optimisticUpdateIndex}`
|
|
496
|
+
);
|
|
497
|
+
setIntoStore(store, optimisticUpdateQueue, (queue) => {
|
|
498
|
+
queue[optimisticUpdateIndex] = clientUpdate;
|
|
499
|
+
return queue;
|
|
500
|
+
});
|
|
501
|
+
}
|
|
502
|
+
socket.emit(`tx-run:${continuityKey}`, {
|
|
503
|
+
id: clientUpdate.id,
|
|
504
|
+
key: transaction.key,
|
|
505
|
+
params: clientUpdate.params
|
|
506
|
+
});
|
|
507
|
+
}
|
|
508
|
+
);
|
|
509
|
+
return unsubscribeFromTransactionUpdates;
|
|
510
|
+
});
|
|
511
|
+
const revealState = useRevealState(store);
|
|
512
|
+
const concealState = useConcealState(store);
|
|
513
|
+
socket.on(`reveal:${continuityKey}`, revealState);
|
|
514
|
+
socket.on(`conceal:${continuityKey}`, concealState);
|
|
515
|
+
socket.emit(`get:${continuityKey}`);
|
|
516
|
+
return () => {
|
|
517
|
+
socket.off(`continuity-init:${continuityKey}`);
|
|
518
|
+
socket.off(`tx-new:${continuityKey}`);
|
|
519
|
+
for (const unsubscribe of unsubscribeFunctions) unsubscribe();
|
|
520
|
+
};
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
export { confirmedUpdateQueue, myIdState, myIdState__INTERNAL, myUsernameState, optimisticUpdateQueue, pullAtom, pullAtomFamilyMember, pullMutableAtom, pullMutableAtomFamilyMember, pullSelector, pullSelectorFamilyMember, pushState, serverAction, syncContinuity, useConcealState, useRegisterAndAttemptConfirmedUpdate, useRevealState };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// web/src/persist-sync.ts
|
|
2
|
+
var persistSync = (storage, { stringify, parse }, key) => ({ setSelf, onSet }) => {
|
|
3
|
+
const savedValue = storage.getItem(key);
|
|
4
|
+
if (savedValue != null) setSelf(parse(savedValue));
|
|
5
|
+
onSet(({ newValue }) => {
|
|
6
|
+
if (newValue == null) {
|
|
7
|
+
storage.removeItem(key);
|
|
8
|
+
return;
|
|
9
|
+
}
|
|
10
|
+
storage.setItem(key, stringify(newValue));
|
|
11
|
+
});
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export { persistSync };
|