atom.io 0.40.2 → 0.40.3

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.
@@ -44,13 +44,13 @@ export function createReadonlyHeldSelectorFamily<
44
44
  StateLifecycleEvent<ReadonlyHeldSelectorToken<T>>
45
45
  >()
46
46
 
47
- const familyFunction = (key: K): ReadonlyHeldSelectorToken<T> => {
47
+ const create = (key: K): ReadonlyHeldSelectorToken<T> => {
48
48
  const subKey = stringifyJson(key)
49
49
  const family: FamilyMetadata = { key: familyKey, subKey }
50
50
  const fullKey = `${familyKey}(${subKey})`
51
51
  const target = newest(store)
52
52
 
53
- const token = createReadonlyHeldSelector(
53
+ return createReadonlyHeldSelector(
54
54
  target,
55
55
  {
56
56
  key: fullKey,
@@ -59,17 +59,16 @@ export function createReadonlyHeldSelectorFamily<
59
59
  },
60
60
  family,
61
61
  )
62
-
63
- // subject.next({ type: `state_creation`, token, timestamp: Date.now() })
64
- return token
65
62
  }
66
63
 
67
- const readonlySelectorFamily = Object.assign(familyFunction, familyToken, {
64
+ const readonlySelectorFamily: ReadonlyHeldSelectorFamily<T, K> = {
65
+ ...familyToken,
66
+ create,
68
67
  internalRoles,
69
68
  subject,
70
69
  install: (s: RootStore) => createReadonlyHeldSelectorFamily(s, options),
71
70
  default: options.const,
72
- }) satisfies ReadonlyHeldSelectorFamily<T, K>
71
+ }
73
72
 
74
73
  store.families.set(familyKey, readonlySelectorFamily)
75
74
  return familyToken
@@ -45,7 +45,7 @@ export function createReadonlyPureSelectorFamily<T, K extends Canonical, E>(
45
45
  StateLifecycleEvent<ReadonlyPureSelectorToken<T, K, E>>
46
46
  >()
47
47
 
48
- const familyFunction = <Key extends K>(
48
+ const create = <Key extends K>(
49
49
  key: Key,
50
50
  ): ReadonlyPureSelectorToken<T, Key, E> => {
51
51
  const subKey = stringifyJson(key)
@@ -60,16 +60,16 @@ export function createReadonlyPureSelectorFamily<T, K extends Canonical, E>(
60
60
  individualOptions.catch = options.catch
61
61
  }
62
62
 
63
- const token = createReadonlyPureSelector<T, Key, E>(
63
+ return createReadonlyPureSelector<T, Key, E>(
64
64
  target,
65
65
  individualOptions,
66
66
  family,
67
67
  )
68
-
69
- return token
70
68
  }
71
69
 
72
- const readonlySelectorFamily = Object.assign(familyFunction, familyToken, {
70
+ const readonlySelectorFamily: ReadonlyPureSelectorFamily<T, K, E> = {
71
+ ...familyToken,
72
+ create,
73
73
  internalRoles,
74
74
  subject,
75
75
  install: (s: RootStore) => createReadonlyPureSelectorFamily(s, options),
@@ -83,8 +83,7 @@ export function createReadonlyPureSelectorFamily<T, K extends Canonical, E>(
83
83
  json: (token) => getJsonToken(store, token),
84
84
  })
85
85
  },
86
- ...(options.catch ? { catch: options.catch } : {}),
87
- }) satisfies ReadonlyPureSelectorFamily<T, K, E>
86
+ }
88
87
 
89
88
  store.families.set(familyKey, readonlySelectorFamily)
90
89
  return familyToken
@@ -38,9 +38,7 @@ export function createRegularAtomFamily<T, K extends Canonical, E>(
38
38
 
39
39
  const subject = new Subject<StateLifecycleEvent<RegularAtomToken<T, K, E>>>()
40
40
 
41
- const familyFunction = <Key extends K>(
42
- key: Key,
43
- ): RegularAtomToken<T, Key, E> => {
41
+ const create = <Key extends K>(key: Key): RegularAtomToken<T, Key, E> => {
44
42
  const subKey = stringifyJson(key)
45
43
  const family: FamilyMetadata<Key> = { key: options.key, subKey }
46
44
  const fullKey = `${options.key}(${subKey})`
@@ -58,20 +56,17 @@ export function createRegularAtomFamily<T, K extends Canonical, E>(
58
56
  individualOptions.catch = options.catch
59
57
  }
60
58
 
61
- const token = createRegularAtom(target, individualOptions, family)
62
-
63
- // subject.next({ type: `state_creation`, token, timestamp: Date.now() })
64
- return token
59
+ return createRegularAtom(target, individualOptions, family)
65
60
  }
66
61
 
67
- const atomFamily: RegularAtomFamily<T, K, E> = Object.assign(familyFunction, {
62
+ const atomFamily: RegularAtomFamily<T, K, E> = {
68
63
  ...familyToken,
64
+ create,
69
65
  default: options.default,
70
66
  subject,
71
67
  install: (s: RootStore) => createRegularAtomFamily(s, options),
72
68
  internalRoles,
73
- ...(options.catch ? { catch: options.catch } : {}),
74
- })
69
+ }
75
70
 
76
71
  store.families.set(options.key, atomFamily)
77
72
  if (isFn(options.default) === false) {
@@ -44,13 +44,13 @@ export function createWritableHeldSelectorFamily<
44
44
  StateLifecycleEvent<WritableHeldSelectorToken<T>>
45
45
  >()
46
46
 
47
- const familyFunction = (key: K): WritableHeldSelectorToken<T> => {
47
+ const create = (key: K): WritableHeldSelectorToken<T> => {
48
48
  const subKey = stringifyJson(key)
49
49
  const family: FamilyMetadata = { key: familyKey, subKey }
50
50
  const fullKey = `${familyKey}(${subKey})`
51
51
  const target = newest(store)
52
52
 
53
- const token = createWritableHeldSelector(
53
+ return createWritableHeldSelector(
54
54
  target,
55
55
  {
56
56
  key: fullKey,
@@ -60,17 +60,16 @@ export function createWritableHeldSelectorFamily<
60
60
  },
61
61
  family,
62
62
  )
63
-
64
- // subject.next({ type: `state_creation`, token, timestamp: Date.now() })
65
- return token
66
63
  }
67
64
 
68
- const selectorFamily = Object.assign(familyFunction, familyToken, {
65
+ const selectorFamily: WritableHeldSelectorFamily<T, K> = {
66
+ ...familyToken,
67
+ create,
69
68
  internalRoles,
70
69
  subject,
71
70
  install: (s: RootStore) => createWritableHeldSelectorFamily(s, options),
72
71
  default: options.const,
73
- }) satisfies WritableHeldSelectorFamily<T, K>
72
+ }
74
73
 
75
74
  store.families.set(familyKey, selectorFamily)
76
75
  return familyToken
@@ -45,7 +45,7 @@ export function createWritablePureSelectorFamily<T, K extends Canonical, E>(
45
45
  StateLifecycleEvent<WritablePureSelectorToken<T, K, E>>
46
46
  >()
47
47
 
48
- const familyFunction = <Key extends K>(
48
+ const create = <Key extends K>(
49
49
  key: Key,
50
50
  ): WritablePureSelectorToken<T, Key, E> => {
51
51
  const subKey = stringifyJson(key)
@@ -61,17 +61,16 @@ export function createWritablePureSelectorFamily<T, K extends Canonical, E>(
61
61
  individualOptions.catch = options.catch
62
62
  }
63
63
 
64
- const token = createWritablePureSelector<T, Key, E>(
64
+ return createWritablePureSelector<T, Key, E>(
65
65
  target,
66
66
  individualOptions,
67
67
  family,
68
68
  )
69
-
70
- // subject.next({ type: `state_creation`, token, timestamp: Date.now() })
71
- return token
72
69
  }
73
70
 
74
- const selectorFamily = Object.assign(familyFunction, familyToken, {
71
+ const selectorFamily: WritablePureSelectorFamily<T, K, E> = {
72
+ ...familyToken,
73
+ create,
75
74
  internalRoles,
76
75
  subject,
77
76
  install: (s: RootStore) => createWritablePureSelectorFamily(s, options),
@@ -85,8 +84,7 @@ export function createWritablePureSelectorFamily<T, K extends Canonical, E>(
85
84
  json: (token) => getJsonToken(store, token),
86
85
  })
87
86
  },
88
- ...(options.catch ? { catch: options.catch } : {}),
89
- }) satisfies WritablePureSelectorFamily<T, K, E>
87
+ }
90
88
 
91
89
  store.families.set(familyKey, selectorFamily)
92
90
  return familyToken
@@ -52,7 +52,7 @@ export function mintInStore<T, K extends Canonical, Key extends K, E>(
52
52
  `adds member`,
53
53
  typeof key === `string` ? `\`${key}\`` : key,
54
54
  )
55
- token = family(key)
55
+ token = family.create(key)
56
56
  if (molecule) {
57
57
  store.moleculeData.set(stringKey, family.key)
58
58
  }
@@ -23,7 +23,7 @@ import type { Store } from "./store"
23
23
  import type { Subject } from "./subject"
24
24
  import type { Timeline } from "./timeline"
25
25
  import type { RootStore, Transaction } from "./transaction"
26
- import type { Flat } from "./utility-types"
26
+ import type { Ctor, Flat } from "./utility-types"
27
27
 
28
28
  export * from "./arbitrary"
29
29
  export * from "./atom"
@@ -67,7 +67,7 @@ export type RegularAtom<T, E> = Flat<
67
67
  default: T | (() => T)
68
68
  cleanup?: () => void
69
69
  internalRoles?: internalRole[]
70
- catch?: readonly (new () => E)[]
70
+ catch?: readonly Ctor<E>[]
71
71
  }
72
72
  >
73
73
  export type MutableAtom<T extends Transceiver<any, any, any>> = Flat<
@@ -101,14 +101,14 @@ export type WritablePureSelector<T, E> = Flat<
101
101
  type: `writable_pure_selector`
102
102
  getFrom: (target: Store) => E | T
103
103
  setSelf: (newValue: T) => void
104
- catch?: readonly (new () => E)[]
104
+ catch?: readonly Ctor<E>[]
105
105
  }
106
106
  >
107
107
  export type ReadonlyPureSelector<T, E> = Flat<
108
108
  AtomIOState & {
109
109
  type: `readonly_pure_selector`
110
110
  getFrom: (target: Store) => E | T
111
- catch?: readonly (new () => E)[]
111
+ catch?: readonly Ctor<E>[]
112
112
  }
113
113
  >
114
114
  export type ReadonlySelector<T, E> =
@@ -135,13 +135,13 @@ export type RegularAtomFamily<T, K extends Canonical, E = never> =
135
135
  & Flat<
136
136
  & RegularAtomFamilyToken<T, K, E>
137
137
  & {
138
+ create: <Key extends K>(key: Key) => RegularAtomToken<T, Key, E>
138
139
  default: T | ((key: K) => T)
139
140
  install: (store: RootStore) => void
140
141
  internalRoles: string[] | undefined
141
142
  subject: Subject<StateLifecycleEvent<RegularAtomToken<T, K, E>>>
142
143
  }
143
144
  >
144
- & (<Key extends K>(key: Key) => RegularAtomToken<T, Key, E>)
145
145
 
146
146
  // biome-ignore format: intersection
147
147
  export type MutableAtomFamily<
@@ -151,13 +151,13 @@ export type MutableAtomFamily<
151
151
  & Flat<
152
152
  & MutableAtomFamilyToken<T, K>
153
153
  & {
154
+ create: <Key extends K>(key: Key) => MutableAtomToken<T, Key>
154
155
  class: ConstructorOf<T>
155
156
  install: (store: RootStore) => void
156
157
  internalRoles: string[] | undefined
157
158
  subject: Subject<StateLifecycleEvent<MutableAtomToken<T>>>
158
159
  }
159
160
  >
160
- & (<Key extends K>(key: Key) => MutableAtomToken<T, Key>)
161
161
 
162
162
  export type AtomFamily<T, K extends Canonical, E> =
163
163
  | MutableAtomFamily<T extends Transceiver<any, any, any> ? T : never, K>
@@ -168,52 +168,52 @@ export type WritablePureSelectorFamily<T, K extends Canonical, E> =
168
168
  & Flat<
169
169
  & WritablePureSelectorFamilyToken<T, K, E>
170
170
  & {
171
+ create: <Key extends K>(key: Key) => WritablePureSelectorToken<T, Key, E>
171
172
  default: (key: K) => T,
172
173
  install: (store: RootStore) => void
173
174
  internalRoles: string[] | undefined
174
175
  subject: Subject<StateLifecycleEvent<WritablePureSelectorToken<T, K, E>>>
175
176
  }
176
177
  >
177
- & (<Key extends K>(key: Key) => WritablePureSelectorToken<T, Key, E>)
178
178
 
179
179
  // biome-ignore format: intersection
180
180
  export type WritableHeldSelectorFamily<T , K extends Canonical> =
181
181
  & Flat<
182
182
  & WritableHeldSelectorFamilyToken<T, K>
183
183
  & {
184
+ create: <Key extends K>(key: Key) => WritableHeldSelectorToken<T, Key>
184
185
  default: (key: K) => T,
185
186
  install: (store: RootStore) => void
186
187
  internalRoles: string[] | undefined
187
188
  subject: Subject<StateLifecycleEvent<WritableHeldSelectorToken<T, K>>>
188
189
  }
189
190
  >
190
- & (<Key extends K>(key: Key) => WritableHeldSelectorToken<T, Key>)
191
191
 
192
192
  // biome-ignore format: intersection
193
193
  export type ReadonlyPureSelectorFamily<T, K extends Canonical, E> =
194
194
  & Flat<
195
195
  & ReadonlyPureSelectorFamilyToken<T, K, E>
196
196
  & {
197
+ create: <Key extends K>(key: Key) => ReadonlyPureSelectorToken<T, Key, E>
197
198
  default: (key: K) => T,
198
199
  install: (store: RootStore) => void
199
200
  internalRoles: string[] | undefined
200
201
  subject: Subject<StateLifecycleEvent<ReadonlyPureSelectorToken<T, K, E>>>
201
202
  }
202
203
  >
203
- & (<Key extends K>(key: Key) => ReadonlyPureSelectorToken<T, Key, E>)
204
204
 
205
205
  // biome-ignore format: intersection
206
206
  export type ReadonlyHeldSelectorFamily<T , K extends Canonical> =
207
207
  & Flat<
208
208
  & ReadonlyHeldSelectorFamilyToken<T, K>
209
209
  & {
210
+ create: <Key extends K>(key: Key) => ReadonlyHeldSelectorToken<T, Key>
210
211
  default: (key: K) => T,
211
212
  install: (store: RootStore) => void
212
213
  internalRoles: string[] | undefined
213
214
  subject: Subject<StateLifecycleEvent<ReadonlyHeldSelectorToken<T>>>
214
215
  }
215
216
  >
216
- & (<Key extends K>(key: Key) => ReadonlyHeldSelectorToken<T, Key>)
217
217
 
218
218
  export type PureSelectorFamily<T, K extends Canonical, E> =
219
219
  | ReadonlyPureSelectorFamily<T, K, E>
@@ -44,7 +44,7 @@ export function createMutableAtomFamily<
44
44
 
45
45
  const subject = new Subject<StateLifecycleEvent<MutableAtomToken<T>>>()
46
46
 
47
- const familyFunction = (key: K): MutableAtomToken<T> => {
47
+ const create = (key: K): MutableAtomToken<T> => {
48
48
  const subKey = stringifyJson(key)
49
49
  const family: FamilyMetadata = { key: options.key, subKey }
50
50
  const fullKey = `${options.key}(${subKey})`
@@ -58,18 +58,17 @@ export function createMutableAtomFamily<
58
58
  individualOptions.effects = options.effects(key)
59
59
  }
60
60
 
61
- const token = createMutableAtom(target, individualOptions, family)
62
-
63
- // subject.next({ type: `state_creation`, token, timestamp: Date.now() })
64
- return token
61
+ return createMutableAtom(target, individualOptions, family)
65
62
  }
66
63
 
67
- const atomFamily = Object.assign(familyFunction, familyToken, {
64
+ const atomFamily: MutableAtomFamily<T, K> = {
65
+ ...familyToken,
66
+ create,
68
67
  class: options.class,
69
68
  subject,
70
69
  install: (s: RootStore) => createMutableAtomFamily(s, options),
71
70
  internalRoles,
72
- }) satisfies MutableAtomFamily<T, K>
71
+ }
73
72
 
74
73
  store.families.set(options.key, atomFamily)
75
74
 
@@ -1,5 +1,7 @@
1
1
  export type Fn = (...parameters: any[]) => any
2
2
 
3
+ export type Ctor<T> = new (...args: any[]) => T
4
+
3
5
  export type Flat<R extends { [K in PropertyKey]: any }> = {
4
6
  [K in keyof R]: R[K]
5
7
  }
package/src/main/atom.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { ConstructorOf, Transceiver } from "atom.io/internal"
1
+ import type { ConstructorOf, Ctor, Transceiver } from "atom.io/internal"
2
2
  import {
3
3
  createMutableAtom,
4
4
  createMutableAtomFamily,
@@ -25,7 +25,7 @@ export type RegularAtomOptions<T, E = never> = {
25
25
  /** Hooks used to run side effects when the atom is set */
26
26
  effects?: readonly AtomEffect<T>[]
27
27
  /** The classes of errors that might be thrown when deriving the atom's default value */
28
- catch?: readonly (new () => E)[]
28
+ catch?: readonly Ctor<E>[]
29
29
  }
30
30
  /**
31
31
  * Create a regular atom, a global reactive variable in the implicit store
@@ -91,7 +91,7 @@ export type RegularAtomFamilyOptions<T, K extends Canonical, E = never> = {
91
91
  /** Hooks used to run side effects when an atom in the family is set */
92
92
  effects?: (key: K) => AtomEffect<T>[]
93
93
  /** The classes of errors that might be thrown when deriving the atom's default value */
94
- catch?: readonly (new () => E)[]
94
+ catch?: readonly Ctor<E>[]
95
95
  }
96
96
  /**
97
97
  * Create a family of regular atoms, allowing for the dynamic creation and disposal of atoms.
@@ -1,3 +1,4 @@
1
+ import type { Ctor } from "atom.io/internal"
1
2
  import {
2
3
  createSelectorFamily,
3
4
  createStandaloneSelector,
@@ -25,7 +26,7 @@ export type WritablePureSelectorOptions<T, E = never> = {
25
26
  /** For each instantiated selector, a function that sets its value */
26
27
  set: Write<(newValue: T) => void>
27
28
  /** The classes of errors that might be thrown when deriving the atom's default value */
28
- catch?: readonly (new () => E)[]
29
+ catch?: readonly Ctor<E>[]
29
30
  }
30
31
  export type ReadonlyPureSelectorOptions<T, E = never> = {
31
32
  /** The unique identifier of the selector */
@@ -33,7 +34,7 @@ export type ReadonlyPureSelectorOptions<T, E = never> = {
33
34
  /** For each instantiated selector, a function that computes its value */
34
35
  get: Read<() => T>
35
36
  /** The classes of errors that might be thrown when deriving the atom's default value */
36
- catch?: readonly (new () => E)[]
37
+ catch?: readonly Ctor<E>[]
37
38
  }
38
39
  export type ReadonlyHeldSelectorOptions<T extends object> = {
39
40
  /** The unique identifier of the selector */
@@ -154,7 +155,7 @@ export type WritablePureSelectorFamilyOptions<
154
155
  /** For each instantiated family member, a function that sets its value */
155
156
  set: (key: K) => Write<(newValue: T) => void>
156
157
  /** The classes of errors that might be thrown when deriving the atom's default value */
157
- catch?: readonly (new () => E)[]
158
+ catch?: readonly Ctor<E>[]
158
159
  }
159
160
  export type ReadonlyPureSelectorFamilyOptions<
160
161
  T,
@@ -166,7 +167,7 @@ export type ReadonlyPureSelectorFamilyOptions<
166
167
  /** For each instantiated family member, a function that computes its value */
167
168
  get: (key: K) => Read<() => T>
168
169
  /** The classes of errors that might be thrown when deriving the atom's default value */
169
- catch?: readonly (new () => E)[]
170
+ catch?: readonly Ctor<E>[]
170
171
  }
171
172
  export type WritableHeldSelectorFamilyOptions<
172
173
  T extends object,