Skip to content

Commit a9e6cd1

Browse files
authored
fix(atoms): make Signal passable to functions accepting ZeduxNode (#341)
1 parent 955430d commit a9e6cd1

File tree

12 files changed

+149
-26
lines changed

12 files changed

+149
-26
lines changed

packages/atoms/src/classes/Ecosystem.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,7 +1012,9 @@ export class Ecosystem<Context extends Record<string, any> | undefined = any>
10121012

10131013
const signal = new Signal<{
10141014
Events: EventMap
1015+
Params: undefined
10151016
State: State
1017+
Template: undefined
10161018
}>(this, id, state)
10171019

10181020
this.n.set(id, signal)

packages/atoms/src/classes/MappedSignal.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import {
22
AnyNodeGenerics,
3-
AtomGenerics,
43
InternalEvaluationReason,
54
Mutatable,
5+
NodeGenerics,
66
SendableEvents,
77
Settable,
88
Transaction,
@@ -29,12 +29,11 @@ const getRelevantEvents = (events?: Record<string, any>) => {
2929
export type SignalMap = Record<string, Signal<AnyNodeGenerics> | unknown>
3030

3131
export class MappedSignal<
32-
G extends Pick<AtomGenerics, 'Events' | 'State'> & {
33-
Params?: any
34-
Template?: any
35-
} = {
32+
G extends NodeGenerics = {
3633
Events: any
34+
Params: undefined
3735
State: any
36+
Template: undefined
3837
}
3938
> extends Signal<G> {
4039
/**

packages/atoms/src/classes/Signal.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import {
2-
AtomGenerics,
32
GraphEdge,
43
Mutatable,
54
NodeGenerics,
@@ -21,7 +20,7 @@ import { recursivelyMutate, recursivelyProxy } from './proxies'
2120
import { getEvaluationContext } from '../utils/evaluationContext'
2221
import { schedulerPost, schedulerPre } from '../utils/ecosystem'
2322

24-
export const doMutate = <G extends Pick<NodeGenerics, 'State' | 'Events'>>(
23+
export const doMutate = <G extends NodeGenerics>(
2524
node: Signal<G>,
2625
isWrapperSignal: boolean,
2726
mutatable: Mutatable<G['State']>,
@@ -89,19 +88,13 @@ export const doMutate = <G extends Pick<NodeGenerics, 'State' | 'Events'>>(
8988
}
9089

9190
export class Signal<
92-
G extends Pick<AtomGenerics, 'Events' | 'State'> & {
93-
Params?: any
94-
Template?: any
95-
} = {
91+
G extends NodeGenerics = {
9692
Events: any
93+
Params: undefined
9794
State: any
95+
Template: undefined
9896
}
99-
> extends ZeduxNode<
100-
G & {
101-
Params: G extends { Params: infer P } ? P : undefined
102-
Template: G extends { Template: infer T } ? T : undefined
103-
}
104-
> {
97+
> extends ZeduxNode<G> {
10598
/**
10699
* @see ZeduxNode.o
107100
*/

packages/atoms/src/classes/templates/AtomTemplate.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
import { AtomInstance } from '../instances/AtomInstance'
88
import { Ecosystem } from '../Ecosystem'
99
import { AtomTemplateBase } from './AtomTemplateBase'
10-
import { Signal } from '../Signal'
10+
import { AnySignal } from '@zedux/atoms/types/index'
1111

1212
export type AtomInstanceRecursive<
1313
G extends Omit<AtomGenerics, 'Node' | 'Template'>
@@ -44,7 +44,7 @@ export class AtomTemplate<
4444
public override(
4545
newValue: AtomValueOrFactory<
4646
G & {
47-
Signal: Signal<{ State: G['State']; Events: G['Events'] }> | undefined
47+
Signal: AnySignal<{ State: G['State']; Events: G['Events'] }> | undefined
4848
}
4949
>
5050
): AtomTemplate<G> {

packages/atoms/src/classes/templates/AtomTemplateBase.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
} from '@zedux/atoms/types/index'
88
import { prefix } from '@zedux/atoms/utils/general'
99
import { Ecosystem } from '../Ecosystem'
10-
import { Signal } from '../Signal'
10+
import { AnySignal } from '@zedux/atoms/types/index'
1111
import { getScopeString } from '@zedux/atoms/utils/graph'
1212

1313
export abstract class AtomTemplateBase<
@@ -44,7 +44,7 @@ export abstract class AtomTemplateBase<
4444
*/
4545
public readonly v: AtomValueOrFactory<
4646
G & {
47-
Signal: Signal<{ State: G['State']; Events: G['Events'] }> | undefined
47+
Signal: AnySignal<{ State: G['State']; Events: G['Events'] }> | undefined
4848
}
4949
>,
5050

packages/atoms/src/factories/api.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { AtomApi } from '../classes/AtomApi'
2-
import { AtomApiPromise, None, ResolvedStateOf, StateOf } from '../types/index'
2+
import {
3+
AnySignal,
4+
AtomApiPromise,
5+
None,
6+
ResolvedStateOf,
7+
StateOf,
8+
} from '../types/index'
39
import { Signal } from '../classes/Signal'
410

511
/**
@@ -90,7 +96,7 @@ export const api: {
9096
State = undefined,
9197
Exports extends Record<string, any> = None,
9298
SignalType extends
93-
| Signal<{ Events: any; State: State }>
99+
| AnySignal<{ State: State }>
94100
| undefined = undefined
95101
>(
96102
value: State
@@ -104,7 +110,7 @@ export const api: {
104110
State = undefined,
105111
Exports extends Record<string, any> = None,
106112
SignalType extends
107-
| Signal<{ Events: any; State: State }>
113+
| AnySignal<{ State: State }>
108114
| undefined = undefined,
109115
PromiseType extends AtomApiPromise = undefined
110116
>(

packages/atoms/src/injectors/injectMappedSignal.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@ export function injectMappedSignal<
8787
config?: InjectSignalConfig<EventMap>
8888
): MappedSignal<{
8989
Events: Prettify<EventsOf<S> & EventMap>
90+
Params: undefined
9091
State: StateOf<S>
92+
Template: undefined
9193
}>
9294

9395
export function injectMappedSignal<
@@ -98,7 +100,9 @@ export function injectMappedSignal<
98100
config?: InjectSignalConfig<EventMap>
99101
): MappedSignal<{
100102
Events: Prettify<MapAll<M> & EventMap>
103+
Params: undefined
101104
State: { [K in keyof M]: M[K] extends Signal<any> ? StateOf<M[K]> : M[K] }
105+
Template: undefined
102106
}>
103107

104108
export function injectMappedSignal(

packages/atoms/src/injectors/injectPromise.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@ export interface InjectPromiseAtomApi<
3434
> extends AtomApi<G> {
3535
dataSignal: Signal<{
3636
Events: EventMap
37+
Params: undefined
3738
ResolvedState: Data
3839
State: Data | undefined
40+
Template: undefined
3941
}>
4042

4143
addExports<NewExports extends Record<string, any>>(
@@ -155,7 +157,9 @@ export const injectPromise: {
155157
Promise: Promise<Data>
156158
Signal: MappedSignal<{
157159
Events: EventMap
160+
Params: undefined
158161
State: Omit<PromiseState<Data>, 'data'> & { data: Data }
162+
Template: undefined
159163
}>
160164
State: Omit<PromiseState<Data>, 'data'> & { data: Data }
161165
},
@@ -176,8 +180,10 @@ export const injectPromise: {
176180
Promise: Promise<Data>
177181
Signal: MappedSignal<{
178182
Events: EventMap
183+
Params: undefined
179184
ResolvedState: Omit<PromiseState<Data>, 'data'> & { data: Data }
180185
State: PromiseState<Data>
186+
Template: undefined
181187
}>
182188
State: PromiseState<Data>
183189
},
@@ -204,16 +210,20 @@ export const injectPromise: {
204210

205211
const dataSignal = injectSignal(initialData, signalConfig) as Signal<{
206212
Events: EventMap
213+
Params: undefined
207214
ResolvedState: Data
208215
State: Data | undefined
216+
Template: undefined
209217
}>
210218

211219
const signal = injectMappedSignal({
212220
...getInitialPromiseState<Data>(),
213221
data: dataSignal,
214222
}) as MappedSignal<{
215223
Events: EventMap
224+
Params: undefined
216225
State: PromiseState<Data>
226+
Template: undefined
217227
}>
218228

219229
if (
@@ -290,7 +300,9 @@ export const injectPromise: {
290300
Promise: Promise<Data>
291301
Signal: MappedSignal<{
292302
Events: EventMap
303+
Params: undefined
293304
State: PromiseState<Data>
305+
Template: undefined
294306
}>
295307
State: PromiseState<Data>
296308
},

packages/atoms/src/injectors/injectSignal.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ export const injectSignal = <
5050

5151
const signal = new Signal<{
5252
Events: EventMap
53+
Params: undefined
5354
State: State
55+
Template: undefined
5456
}>(
5557
instance.e,
5658
id,

packages/atoms/src/types/atoms.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,17 @@ export type AnyAtomTemplate<G extends Partial<AtomGenerics> | 'any' = 'any'> =
5353
: any
5454
>
5555

56+
export type AnySignal<G extends Partial<NodeGenerics> | 'any' = 'any'> = Signal<
57+
G extends Partial<NodeGenerics>
58+
? {
59+
Events: G extends { Events: infer E } ? E : any
60+
Params: G extends { Params: infer P } ? P : any
61+
State: G extends { State: infer S } ? S : any
62+
Template: G extends { Template: infer T } ? T : any
63+
}
64+
: any
65+
>
66+
5667
export type AnyNodeGenerics<
5768
G extends Partial<NodeGenerics> = { [K in keyof NodeGenerics]: any }
5869
> = {
@@ -69,7 +80,7 @@ export type AtomApiGenerics = Pick<
6980
export type AtomGenericsToAtomApiGenerics<
7081
G extends Pick<AtomGenerics, 'Events' | 'Exports' | 'Promise' | 'State'>
7182
> = Pick<G, 'Exports' | 'Promise' | 'State'> & {
72-
Signal: Signal<Pick<G, 'Events' | 'State'>> | undefined
83+
Signal: AnySignal<Pick<G, 'Events' | 'State'>> | undefined
7384
}
7485

7586
export interface AtomGenerics {

0 commit comments

Comments
 (0)