atom.io 0.22.0 → 0.23.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.
Files changed (82) hide show
  1. package/data/dist/index.cjs +17 -1
  2. package/data/dist/index.js +1 -1
  3. package/data/src/join.ts +30 -1
  4. package/dist/chunk-6MLFYN32.js +18 -0
  5. package/dist/{chunk-JA4V7TJY.js → chunk-7DT3PVS3.js} +18 -2
  6. package/dist/chunk-OAYGID5B.js +27 -0
  7. package/dist/index.cjs +2 -11
  8. package/dist/index.d.ts +51 -23
  9. package/dist/index.js +2 -11
  10. package/ephemeral/dist/index.d.ts +12 -0
  11. package/eslint-plugin/dist/index.cjs +0 -1
  12. package/eslint-plugin/dist/index.d.ts +73 -0
  13. package/eslint-plugin/dist/index.js +0 -1
  14. package/eslint-plugin/src/rules/lifespan.ts +0 -1
  15. package/immortal/dist/index.cjs +180 -20
  16. package/immortal/dist/index.d.ts +103 -0
  17. package/immortal/dist/index.js +134 -19
  18. package/immortal/src/index.ts +1 -0
  19. package/immortal/src/make-molecule.ts +222 -0
  20. package/immortal/src/molecule.ts +49 -16
  21. package/immortal/src/seek-state.ts +15 -2
  22. package/internal/dist/index.cjs +1119 -754
  23. package/internal/dist/index.d.ts +109 -12
  24. package/internal/dist/index.js +1098 -760
  25. package/internal/src/atom/create-regular-atom.ts +0 -2
  26. package/internal/src/atom/create-standalone-atom.ts +6 -2
  27. package/internal/src/atom/dispose-atom.ts +22 -2
  28. package/internal/src/families/create-readonly-selector-family.ts +7 -2
  29. package/internal/src/families/create-regular-atom-family.ts +6 -2
  30. package/internal/src/families/create-writable-selector-family.ts +7 -2
  31. package/internal/src/families/dispose-from-store.ts +22 -0
  32. package/internal/src/families/find-in-store.ts +0 -1
  33. package/internal/src/families/index.ts +1 -0
  34. package/internal/src/families/init-family-member.ts +22 -1
  35. package/internal/src/families/seek-in-store.ts +23 -6
  36. package/internal/src/ingest-updates/index.ts +1 -0
  37. package/internal/src/ingest-updates/ingest-creation-disposal.ts +104 -0
  38. package/internal/src/ingest-updates/ingest-transaction-update.ts +26 -4
  39. package/internal/src/mutable/create-mutable-atom-family.ts +6 -2
  40. package/internal/src/mutable/create-mutable-atom.ts +0 -2
  41. package/internal/src/mutable/get-json-token.ts +0 -1
  42. package/internal/src/mutable/tracker-family.ts +7 -7
  43. package/internal/src/not-found-error.ts +5 -0
  44. package/internal/src/selector/create-readonly-selector.ts +2 -3
  45. package/internal/src/selector/create-standalone-selector.ts +6 -2
  46. package/internal/src/selector/create-writable-selector.ts +2 -3
  47. package/internal/src/selector/dispose-selector.ts +32 -5
  48. package/internal/src/selector/register-selector.ts +2 -0
  49. package/internal/src/set-state/stow-update.ts +5 -1
  50. package/internal/src/store/deposit.ts +41 -7
  51. package/internal/src/store/store.ts +11 -0
  52. package/internal/src/store/withdraw.ts +28 -1
  53. package/internal/src/timeline/add-atom-to-timeline.ts +206 -182
  54. package/internal/src/timeline/create-timeline.ts +181 -60
  55. package/internal/src/timeline/time-travel.ts +20 -0
  56. package/internal/src/transaction/apply-transaction.ts +2 -12
  57. package/internal/src/transaction/build-transaction.ts +11 -2
  58. package/introspection/dist/index.cjs +2 -1
  59. package/introspection/dist/index.js +2 -1
  60. package/introspection/src/attach-timeline-family.ts +1 -0
  61. package/json/dist/index.cjs +3 -3
  62. package/json/dist/index.js +6 -5
  63. package/json/src/select-json-family.ts +3 -4
  64. package/package.json +8 -5
  65. package/react-devtools/dist/index.cjs +58 -47
  66. package/react-devtools/dist/index.js +60 -48
  67. package/react-devtools/src/TimelineIndex.tsx +15 -13
  68. package/react-devtools/src/Updates.tsx +41 -32
  69. package/realtime-server/dist/index.cjs +21 -10
  70. package/realtime-server/dist/index.d.ts +1 -1
  71. package/realtime-server/dist/index.js +21 -11
  72. package/realtime-server/src/realtime-server-stores/server-sync-store.ts +21 -11
  73. package/realtime-testing/dist/index.cjs +1 -0
  74. package/realtime-testing/dist/index.js +1 -1
  75. package/src/atom.ts +9 -3
  76. package/src/dispose-state.ts +3 -12
  77. package/src/index.ts +4 -0
  78. package/src/selector.ts +3 -3
  79. package/src/subscribe.ts +8 -4
  80. package/src/timeline.ts +18 -1
  81. package/src/transaction.ts +56 -4
  82. package/dist/chunk-BF4MVQF6.js +0 -44
@@ -1,42 +1,96 @@
1
1
  'use strict';
2
2
 
3
- var atom_io = require('atom.io');
4
- var data = require('atom.io/data');
5
- var internal = require('atom.io/internal');
3
+ var Internal = require('atom.io/internal');
6
4
  var json = require('atom.io/json');
5
+ var data = require('atom.io/data');
6
+
7
+ function _interopNamespace(e) {
8
+ if (e && e.__esModule) return e;
9
+ var n = Object.create(null);
10
+ if (e) {
11
+ Object.keys(e).forEach(function (k) {
12
+ if (k !== 'default') {
13
+ var d = Object.getOwnPropertyDescriptor(e, k);
14
+ Object.defineProperty(n, k, d.get ? d : {
15
+ enumerable: true,
16
+ get: function () { return e[k]; }
17
+ });
18
+ }
19
+ });
20
+ }
21
+ n.default = e;
22
+ return Object.freeze(n);
23
+ }
24
+
25
+ var Internal__namespace = /*#__PURE__*/_interopNamespace(Internal);
7
26
 
8
- // immortal/src/molecule.ts
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));
9
46
  var Molecule = class _Molecule {
10
- constructor(key, above = [], store = internal.IMPLICIT.STORE) {
11
- this.key = key;
12
- this.above = above;
47
+ constructor(store, above, token) {
13
48
  this.store = store;
49
+ this.token = token;
50
+ this.type = `molecule`;
14
51
  this.below = [];
15
52
  this.tokens = [];
16
53
  this.joins = [];
54
+ this.subject = new Internal.Subject();
17
55
  this.dispose = this[Symbol.dispose];
18
- store.molecules.set(json.stringifyJson(key), this);
19
- for (const parent of above) {
20
- parent.below.push(this);
56
+ if (above) {
57
+ if (Array.isArray(above)) {
58
+ this.above = above;
59
+ for (const parent of above) {
60
+ parent.below.push(this);
61
+ }
62
+ } else {
63
+ this.above = [above];
64
+ above.below.push(this);
65
+ }
66
+ } else {
67
+ this.above = [];
21
68
  }
22
69
  }
70
+ get key() {
71
+ return this.token.key;
72
+ }
23
73
  bond(token) {
24
- const state = internal.initFamilyMember(token, this.key, this.store);
74
+ const state = Internal.initFamilyMember(token, this.token.key, this.store);
25
75
  if (token.type === `mutable_atom_family`) {
26
- const jsonFamily = internal.getJsonFamily(token, this.store);
27
- const jsonState = internal.initFamilyMember(jsonFamily, this.key, this.store);
76
+ const jsonFamily = Internal.getJsonFamily(token, this.store);
77
+ const jsonState = Internal.initFamilyMember(jsonFamily, this.token.key, this.store);
28
78
  this.tokens.push(jsonState);
29
79
  }
30
80
  this.tokens.push(state);
81
+ this.subject.next({ type: `state_creation`, token: state });
31
82
  return state;
32
83
  }
33
84
  spawn(key) {
34
- const child = new _Molecule(key, [this], this.store);
85
+ const child = new _Molecule(this.store, this, { key, type: `molecule` });
35
86
  return child;
36
87
  }
37
88
  with(molecule) {
38
89
  return (key) => {
39
- const child = new _Molecule(key, [this, molecule], this.store);
90
+ const child = new _Molecule(this.store, [this, molecule], {
91
+ key,
92
+ type: `molecule`
93
+ });
40
94
  return child;
41
95
  };
42
96
  }
@@ -68,33 +122,139 @@ var Molecule = class _Molecule {
68
122
  while (this.tokens.length > 0) {
69
123
  const token = this.tokens.pop();
70
124
  if (token) {
71
- atom_io.disposeState(token, this.store);
125
+ Internal.disposeFromStore(token, this.store);
72
126
  }
73
127
  }
74
128
  while (this.joins.length > 0) {
75
129
  const join = this.joins.pop();
76
130
  if (join) {
77
- join.molecules.delete(json.stringifyJson(this.key));
131
+ join.molecules.delete(json.stringifyJson(this.token.key));
78
132
  }
79
133
  }
80
134
  }
81
135
  join(token) {
82
136
  const join = data.getJoin(token, this.store);
83
- join.molecules.set(json.stringifyJson(this.key), this);
137
+ join.molecules.set(json.stringifyJson(this.token.key), this);
84
138
  this.joins.push(join);
85
139
  }
86
140
  [Symbol.dispose]() {
87
141
  this.clear();
88
- this.store.molecules.delete(json.stringifyJson(this.key));
142
+ const target = Internal.newest(this.store);
143
+ target.molecules.delete(json.stringifyJson(this.token.key));
89
144
  for (const parent of this.above) {
90
145
  parent.detach(this);
91
146
  }
92
147
  }
93
148
  };
149
+
150
+ // immortal/src/make-molecule.ts
151
+ function createMoleculeFamily(options, store) {
152
+ const subject = new Internal__namespace.Subject();
153
+ const token = {
154
+ key: options.key,
155
+ type: `molecule_family`
156
+ };
157
+ const family = Object.assign(options.new(store), __spreadProps(__spreadValues({}, token), { subject }));
158
+ store.moleculeFamilies.set(options.key, family);
159
+ return token;
160
+ }
161
+ function moleculeFamily(options) {
162
+ return createMoleculeFamily(options, Internal__namespace.IMPLICIT.STORE);
163
+ }
164
+ function makeMoleculeInStore(store, context, family, key, ...params) {
165
+ const target = Internal__namespace.newest(store);
166
+ const token = {
167
+ type: `molecule`,
168
+ key,
169
+ family
170
+ };
171
+ const contextArray = Array.isArray(context) ? context : [context];
172
+ const owners = contextArray.map((ctx) => store.molecules.get(json.stringifyJson(ctx.key))).filter((m) => m !== void 0);
173
+ const Formula = Internal__namespace.withdraw(family, store);
174
+ const molecule = new Formula(owners, token, ...params);
175
+ target.molecules.set(json.stringifyJson(key), molecule);
176
+ const update = {
177
+ type: `molecule_creation`,
178
+ token,
179
+ family,
180
+ context: contextArray,
181
+ params
182
+ };
183
+ const isTransaction = Internal__namespace.isChildStore(target) && target.transactionMeta.phase === `building`;
184
+ if (isTransaction) {
185
+ target.transactionMeta.update.updates.push(update);
186
+ } else {
187
+ Formula.subject.next(update);
188
+ }
189
+ return token;
190
+ }
191
+ function makeMolecule(context, family, key, ...params) {
192
+ return makeMoleculeInStore(
193
+ Internal__namespace.IMPLICIT.STORE,
194
+ context,
195
+ family,
196
+ key,
197
+ ...params
198
+ );
199
+ }
200
+ function useMoleculeFromStore(token, store) {
201
+ const molecule = store.molecules.get(json.stringifyJson(token.key));
202
+ return molecule;
203
+ }
204
+ function useMolecule(token) {
205
+ return useMoleculeFromStore(token, Internal__namespace.IMPLICIT.STORE);
206
+ }
207
+ function disposeMolecule(token, store) {
208
+ const mole = useMoleculeFromStore(token, store);
209
+ if (!mole || !token.family) {
210
+ return;
211
+ }
212
+ const { family } = token;
213
+ const Formula = Internal__namespace.withdraw(family, store);
214
+ const disposalEvent = {
215
+ type: `molecule_disposal`,
216
+ token,
217
+ family,
218
+ context: mole.above,
219
+ familyKeys: mole.tokens.map((t) => {
220
+ var _a;
221
+ return (_a = t.family) == null ? void 0 : _a.key;
222
+ }).filter((k) => typeof k === `string`)
223
+ };
224
+ if (token.family) {
225
+ disposalEvent.family = token.family;
226
+ }
227
+ const isTransaction = Internal__namespace.isChildStore(store) && store.transactionMeta.phase === `building`;
228
+ if (isTransaction) {
229
+ store.transactionMeta.update.updates.push(disposalEvent);
230
+ } else {
231
+ Formula.subject.next(disposalEvent);
232
+ }
233
+ mole.dispose();
234
+ }
235
+ function makeRootMolecule(key, store = Internal__namespace.IMPLICIT.STORE) {
236
+ const molecule = new Molecule(store, void 0, { key, type: `molecule` });
237
+ store.molecules.set(json.stringifyJson(key), molecule);
238
+ return {
239
+ key,
240
+ type: `molecule`
241
+ };
242
+ }
94
243
  function seekState(token, key) {
95
- const state = internal.seekInStore(token, key, internal.IMPLICIT.STORE);
244
+ if (token.type === `molecule_family`) {
245
+ return Internal.seekInStore(token, key, Internal.IMPLICIT.STORE);
246
+ }
247
+ const state = Internal.seekInStore(token, key, Internal.IMPLICIT.STORE);
96
248
  return state;
97
249
  }
98
250
 
99
251
  exports.Molecule = Molecule;
252
+ exports.createMoleculeFamily = createMoleculeFamily;
253
+ exports.disposeMolecule = disposeMolecule;
254
+ exports.makeMolecule = makeMolecule;
255
+ exports.makeMoleculeInStore = makeMoleculeInStore;
256
+ exports.makeRootMolecule = makeRootMolecule;
257
+ exports.moleculeFamily = moleculeFamily;
100
258
  exports.seekState = seekState;
259
+ exports.useMolecule = useMolecule;
260
+ exports.useMoleculeFromStore = useMoleculeFromStore;
@@ -0,0 +1,103 @@
1
+ import { ReadableToken, StateCreation, StateDisposal, MutableAtomFamilyToken, MutableAtomToken, RegularAtomFamilyToken, RegularAtomToken, WritableSelectorFamilyToken, WritableSelectorToken, ReadonlySelectorFamilyToken, ReadonlySelectorToken, WritableFamilyToken, WritableToken, ReadableFamilyToken, Flat, MoleculeCreation, MoleculeDisposal } from 'atom.io';
2
+ import * as Internal from 'atom.io/internal';
3
+ import { Store, Subject, Transceiver } from 'atom.io/internal';
4
+ import { Json } from 'atom.io/json';
5
+ import { Join, JoinToken } from 'atom.io/data';
6
+
7
+ declare class Molecule<Key extends Json.Serializable> {
8
+ readonly store: Store;
9
+ readonly token: MoleculeToken<Key, any, any>;
10
+ readonly type = "molecule";
11
+ get key(): Key;
12
+ readonly family?: MoleculeFamilyToken<Key, any, any>;
13
+ readonly above: Molecule<any>[];
14
+ readonly below: Molecule<any>[];
15
+ readonly tokens: ReadableToken<any>[];
16
+ readonly joins: Join<any, any, any, any>[];
17
+ readonly subject: Subject<StateCreation<any> | StateDisposal<any>>;
18
+ constructor(store: Store, above: Molecule<any> | Molecule<any>[] | undefined, token: MoleculeToken<Key, any, any>);
19
+ bond<T extends Transceiver<any>, J extends Json.Serializable, K extends string>(token: MutableAtomFamilyToken<T, J, K>): MutableAtomToken<T, J>;
20
+ bond<T, K extends Key>(token: RegularAtomFamilyToken<T, K>): RegularAtomToken<T>;
21
+ bond<T, K extends Json.Serializable>(token: WritableSelectorFamilyToken<T, K>): WritableSelectorToken<T>;
22
+ bond<T, K extends Json.Serializable>(token: ReadonlySelectorFamilyToken<T, K>): ReadonlySelectorToken<T>;
23
+ bond<T, K extends Json.Serializable>(token: WritableFamilyToken<T, K>): WritableToken<T>;
24
+ bond<T, K extends Json.Serializable>(token: ReadableFamilyToken<T, K>): ReadableToken<T>;
25
+ spawn<K extends Json.Serializable>(key: K): Molecule<K>;
26
+ with(molecule: Molecule<any>): (key: string) => Molecule<any>;
27
+ detach(child: Molecule<any>): void;
28
+ claim(child: Molecule<any>): void;
29
+ clear(): void;
30
+ join(token: JoinToken<any, any, any, any>): void;
31
+ private [Symbol.dispose];
32
+ dispose: () => void;
33
+ }
34
+
35
+ type MoleculeConstructor<Key extends Json.Serializable, Struct extends {
36
+ [key: string]: any;
37
+ }, Params extends any[]> = new (context: Molecule<any>[], token: MoleculeToken<Key, Struct, Params>, ...params: Params) => Molecule<Key> & Struct;
38
+ type MoleculeFamilyToken<Key extends Json.Serializable, Struct extends {
39
+ [key: string]: any;
40
+ }, Params extends any[]> = {
41
+ key: string;
42
+ type: `molecule_family`;
43
+ __K?: Key;
44
+ __S?: Struct;
45
+ __P?: Params;
46
+ };
47
+ type MoleculeFamily<Key extends Json.Serializable, Struct extends {
48
+ [key: string]: any;
49
+ }, Params extends any[]> = Flat<MoleculeFamilyToken<Key, Struct, Params> & {
50
+ subject: Internal.Subject<MoleculeCreation<Key> | MoleculeDisposal<Key>>;
51
+ }> & MoleculeConstructor<Key, Struct, Params>;
52
+ type MoleculeToken<Key extends Json.Serializable, Struct extends {
53
+ [key: string]: any;
54
+ }, Params extends any[]> = {
55
+ key: Key;
56
+ type: `molecule`;
57
+ family?: MoleculeFamilyToken<Key, Struct, Params>;
58
+ __S?: Struct;
59
+ __P?: Params;
60
+ };
61
+ type MoleculeFamilyOptions<Key extends Json.Serializable, Struct extends {
62
+ [key: string]: any;
63
+ }, Params extends any[]> = {
64
+ key: string;
65
+ new: (store: Internal.Store) => MoleculeConstructor<Key, Struct, Params>;
66
+ };
67
+ declare function createMoleculeFamily<Key extends Json.Serializable, Struct extends {
68
+ [key: string]: any;
69
+ }, Params extends any[]>(options: MoleculeFamilyOptions<Key, Struct, Params>, store: Internal.Store): MoleculeFamilyToken<Key, Struct, Params>;
70
+ declare function moleculeFamily<Key extends Json.Serializable, Struct extends {
71
+ [key: string]: any;
72
+ }, Params extends any[]>(options: MoleculeFamilyOptions<Key, Struct, Params>): MoleculeFamilyToken<Key, Struct, Params>;
73
+ declare function makeMoleculeInStore<Key extends Json.Serializable, Struct extends {
74
+ [key: string]: any;
75
+ }, Params extends any[]>(store: Internal.Store, context: MoleculeToken<any, any, any> | MoleculeToken<any, any, any>[], family: MoleculeFamilyToken<Key, Struct, Params>, key: Key, ...params: Params): MoleculeToken<Key, Struct, Params>;
76
+ declare function makeMolecule<Key extends Json.Serializable, Struct extends {
77
+ [key: string]: any;
78
+ }, Params extends any[]>(context: MoleculeToken<any, any, any> | MoleculeToken<any, any, any>[], family: MoleculeFamilyToken<Key, Struct, Params>, key: Key, ...params: Params): MoleculeToken<Key, Struct, Params>;
79
+ declare function useMoleculeFromStore<Key extends Json.Serializable, Struct extends {
80
+ [key: string]: any;
81
+ }, Params extends any[]>(token: MoleculeToken<Key, Struct, Params>, store: Internal.Store): (Molecule<Key> & Struct) | undefined;
82
+ declare function useMolecule<Key extends Json.Serializable, Struct extends {
83
+ [key: string]: any;
84
+ }, Params extends any[]>(token: MoleculeToken<Key, Struct, Params>): (Molecule<Key> & Struct) | undefined;
85
+ declare function disposeMolecule<Key extends Json.Serializable, Struct extends {
86
+ [key: string]: any;
87
+ }, Params extends any[]>(token: MoleculeToken<Key, Struct, Params>, store: Internal.Store): void;
88
+ type MoleculeType<M extends MoleculeFamilyToken<any, any, any>> = M extends MoleculeFamilyToken<any, infer T, any> ? T : never;
89
+ declare function makeRootMolecule(key: Json.Serializable, store?: Internal.Store): MoleculeToken<Json.Serializable, {
90
+ [key: string]: unknown;
91
+ }, []>;
92
+
93
+ declare function seekState<T extends Transceiver<any>, J extends Json.Serializable, K extends Json.Serializable, Key extends K>(token: MutableAtomFamilyToken<T, J, K>, key: Key): MutableAtomToken<T, J> | undefined;
94
+ declare function seekState<T, K extends Json.Serializable, Key extends K>(token: RegularAtomFamilyToken<T, K>, key: Key): RegularAtomToken<T> | undefined;
95
+ declare function seekState<T, K extends Json.Serializable, Key extends K>(token: WritableSelectorFamilyToken<T, K>, key: Key): WritableSelectorToken<T> | undefined;
96
+ declare function seekState<T, K extends Json.Serializable, Key extends K>(token: ReadonlySelectorFamilyToken<T, K>, key: Key): ReadonlySelectorToken<T> | undefined;
97
+ declare function seekState<T, K extends Json.Serializable, Key extends K>(token: WritableFamilyToken<T, K>, key: Key): WritableToken<T> | undefined;
98
+ declare function seekState<T, K extends Json.Serializable, Key extends K>(token: ReadableFamilyToken<T, K>, key: Key): ReadableToken<T> | undefined;
99
+ declare function seekState<K extends Json.Serializable, S extends {
100
+ [key: string]: any;
101
+ }>(token: MoleculeFamilyToken<K, S, any[]>, key: K): MoleculeToken<K, S, any[]> | undefined;
102
+
103
+ export { Molecule, type MoleculeConstructor, type MoleculeFamily, type MoleculeFamilyOptions, type MoleculeFamilyToken, type MoleculeToken, type MoleculeType, createMoleculeFamily, disposeMolecule, makeMolecule, makeMoleculeInStore, makeRootMolecule, moleculeFamily, seekState, useMolecule, useMoleculeFromStore };
@@ -1,40 +1,57 @@
1
- import '../../dist/chunk-F2X4B4VY.js';
2
- import { disposeState } from 'atom.io';
3
- import { getJoin } from 'atom.io/data';
4
- import { IMPLICIT, initFamilyMember, getJsonFamily, seekInStore } from 'atom.io/internal';
1
+ import { __spreadProps, __spreadValues } from '../../dist/chunk-F2X4B4VY.js';
2
+ import * as Internal from 'atom.io/internal';
3
+ import { Subject, initFamilyMember, getJsonFamily, disposeFromStore, newest, seekInStore, IMPLICIT } from 'atom.io/internal';
5
4
  import { stringifyJson } from 'atom.io/json';
5
+ import { getJoin } from 'atom.io/data';
6
6
 
7
7
  var Molecule = class _Molecule {
8
- constructor(key, above = [], store = IMPLICIT.STORE) {
9
- this.key = key;
10
- this.above = above;
8
+ constructor(store, above, token) {
11
9
  this.store = store;
10
+ this.token = token;
11
+ this.type = `molecule`;
12
12
  this.below = [];
13
13
  this.tokens = [];
14
14
  this.joins = [];
15
+ this.subject = new Subject();
15
16
  this.dispose = this[Symbol.dispose];
16
- store.molecules.set(stringifyJson(key), this);
17
- for (const parent of above) {
18
- parent.below.push(this);
17
+ if (above) {
18
+ if (Array.isArray(above)) {
19
+ this.above = above;
20
+ for (const parent of above) {
21
+ parent.below.push(this);
22
+ }
23
+ } else {
24
+ this.above = [above];
25
+ above.below.push(this);
26
+ }
27
+ } else {
28
+ this.above = [];
19
29
  }
20
30
  }
31
+ get key() {
32
+ return this.token.key;
33
+ }
21
34
  bond(token) {
22
- const state = initFamilyMember(token, this.key, this.store);
35
+ const state = initFamilyMember(token, this.token.key, this.store);
23
36
  if (token.type === `mutable_atom_family`) {
24
37
  const jsonFamily = getJsonFamily(token, this.store);
25
- const jsonState = initFamilyMember(jsonFamily, this.key, this.store);
38
+ const jsonState = initFamilyMember(jsonFamily, this.token.key, this.store);
26
39
  this.tokens.push(jsonState);
27
40
  }
28
41
  this.tokens.push(state);
42
+ this.subject.next({ type: `state_creation`, token: state });
29
43
  return state;
30
44
  }
31
45
  spawn(key) {
32
- const child = new _Molecule(key, [this], this.store);
46
+ const child = new _Molecule(this.store, this, { key, type: `molecule` });
33
47
  return child;
34
48
  }
35
49
  with(molecule) {
36
50
  return (key) => {
37
- const child = new _Molecule(key, [this, molecule], this.store);
51
+ const child = new _Molecule(this.store, [this, molecule], {
52
+ key,
53
+ type: `molecule`
54
+ });
38
55
  return child;
39
56
  };
40
57
  }
@@ -66,32 +83,130 @@ var Molecule = class _Molecule {
66
83
  while (this.tokens.length > 0) {
67
84
  const token = this.tokens.pop();
68
85
  if (token) {
69
- disposeState(token, this.store);
86
+ disposeFromStore(token, this.store);
70
87
  }
71
88
  }
72
89
  while (this.joins.length > 0) {
73
90
  const join = this.joins.pop();
74
91
  if (join) {
75
- join.molecules.delete(stringifyJson(this.key));
92
+ join.molecules.delete(stringifyJson(this.token.key));
76
93
  }
77
94
  }
78
95
  }
79
96
  join(token) {
80
97
  const join = getJoin(token, this.store);
81
- join.molecules.set(stringifyJson(this.key), this);
98
+ join.molecules.set(stringifyJson(this.token.key), this);
82
99
  this.joins.push(join);
83
100
  }
84
101
  [Symbol.dispose]() {
85
102
  this.clear();
86
- this.store.molecules.delete(stringifyJson(this.key));
103
+ const target = newest(this.store);
104
+ target.molecules.delete(stringifyJson(this.token.key));
87
105
  for (const parent of this.above) {
88
106
  parent.detach(this);
89
107
  }
90
108
  }
91
109
  };
110
+
111
+ // immortal/src/make-molecule.ts
112
+ function createMoleculeFamily(options, store) {
113
+ const subject = new Internal.Subject();
114
+ const token = {
115
+ key: options.key,
116
+ type: `molecule_family`
117
+ };
118
+ const family = Object.assign(options.new(store), __spreadProps(__spreadValues({}, token), { subject }));
119
+ store.moleculeFamilies.set(options.key, family);
120
+ return token;
121
+ }
122
+ function moleculeFamily(options) {
123
+ return createMoleculeFamily(options, Internal.IMPLICIT.STORE);
124
+ }
125
+ function makeMoleculeInStore(store, context, family, key, ...params) {
126
+ const target = Internal.newest(store);
127
+ const token = {
128
+ type: `molecule`,
129
+ key,
130
+ family
131
+ };
132
+ const contextArray = Array.isArray(context) ? context : [context];
133
+ const owners = contextArray.map((ctx) => store.molecules.get(stringifyJson(ctx.key))).filter((m) => m !== void 0);
134
+ const Formula = Internal.withdraw(family, store);
135
+ const molecule = new Formula(owners, token, ...params);
136
+ target.molecules.set(stringifyJson(key), molecule);
137
+ const update = {
138
+ type: `molecule_creation`,
139
+ token,
140
+ family,
141
+ context: contextArray,
142
+ params
143
+ };
144
+ const isTransaction = Internal.isChildStore(target) && target.transactionMeta.phase === `building`;
145
+ if (isTransaction) {
146
+ target.transactionMeta.update.updates.push(update);
147
+ } else {
148
+ Formula.subject.next(update);
149
+ }
150
+ return token;
151
+ }
152
+ function makeMolecule(context, family, key, ...params) {
153
+ return makeMoleculeInStore(
154
+ Internal.IMPLICIT.STORE,
155
+ context,
156
+ family,
157
+ key,
158
+ ...params
159
+ );
160
+ }
161
+ function useMoleculeFromStore(token, store) {
162
+ const molecule = store.molecules.get(stringifyJson(token.key));
163
+ return molecule;
164
+ }
165
+ function useMolecule(token) {
166
+ return useMoleculeFromStore(token, Internal.IMPLICIT.STORE);
167
+ }
168
+ function disposeMolecule(token, store) {
169
+ const mole = useMoleculeFromStore(token, store);
170
+ if (!mole || !token.family) {
171
+ return;
172
+ }
173
+ const { family } = token;
174
+ const Formula = Internal.withdraw(family, store);
175
+ const disposalEvent = {
176
+ type: `molecule_disposal`,
177
+ token,
178
+ family,
179
+ context: mole.above,
180
+ familyKeys: mole.tokens.map((t) => {
181
+ var _a;
182
+ return (_a = t.family) == null ? void 0 : _a.key;
183
+ }).filter((k) => typeof k === `string`)
184
+ };
185
+ if (token.family) {
186
+ disposalEvent.family = token.family;
187
+ }
188
+ const isTransaction = Internal.isChildStore(store) && store.transactionMeta.phase === `building`;
189
+ if (isTransaction) {
190
+ store.transactionMeta.update.updates.push(disposalEvent);
191
+ } else {
192
+ Formula.subject.next(disposalEvent);
193
+ }
194
+ mole.dispose();
195
+ }
196
+ function makeRootMolecule(key, store = Internal.IMPLICIT.STORE) {
197
+ const molecule = new Molecule(store, void 0, { key, type: `molecule` });
198
+ store.molecules.set(stringifyJson(key), molecule);
199
+ return {
200
+ key,
201
+ type: `molecule`
202
+ };
203
+ }
92
204
  function seekState(token, key) {
205
+ if (token.type === `molecule_family`) {
206
+ return seekInStore(token, key, IMPLICIT.STORE);
207
+ }
93
208
  const state = seekInStore(token, key, IMPLICIT.STORE);
94
209
  return state;
95
210
  }
96
211
 
97
- export { Molecule, seekState };
212
+ export { Molecule, createMoleculeFamily, disposeMolecule, makeMolecule, makeMoleculeInStore, makeRootMolecule, moleculeFamily, seekState, useMolecule, useMoleculeFromStore };
@@ -1,2 +1,3 @@
1
+ export * from "./make-molecule"
1
2
  export * from "./molecule"
2
3
  export * from "./seek-state"