@sanity/sdk 2.3.1 → 2.5.0
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/dist/index.d.ts +173 -105
- package/dist/index.js +354 -122
- package/dist/index.js.map +1 -1
- package/package.json +12 -11
- package/src/_exports/index.ts +30 -0
- package/src/agent/agentActions.test.ts +81 -0
- package/src/agent/agentActions.ts +139 -0
- package/src/auth/authStore.test.ts +13 -13
- package/src/auth/refreshStampedToken.test.ts +16 -16
- package/src/auth/subscribeToStateAndFetchCurrentUser.test.ts +6 -6
- package/src/auth/subscribeToStorageEventsAndSetToken.test.ts +4 -4
- package/src/auth/utils.ts +36 -0
- package/src/client/clientStore.test.ts +151 -0
- package/src/client/clientStore.ts +39 -1
- package/src/comlink/controller/actions/destroyController.test.ts +2 -2
- package/src/comlink/controller/actions/getOrCreateChannel.test.ts +6 -6
- package/src/comlink/controller/actions/getOrCreateController.test.ts +5 -5
- package/src/comlink/controller/actions/getOrCreateController.ts +1 -1
- package/src/comlink/controller/actions/releaseChannel.test.ts +3 -2
- package/src/comlink/controller/comlinkControllerStore.test.ts +4 -4
- package/src/comlink/node/actions/getOrCreateNode.test.ts +7 -7
- package/src/comlink/node/actions/releaseNode.test.ts +2 -2
- package/src/comlink/node/comlinkNodeStore.test.ts +4 -3
- package/src/config/sanityConfig.ts +49 -3
- package/src/document/actions.test.ts +34 -0
- package/src/document/actions.ts +31 -7
- package/src/document/applyDocumentActions.test.ts +9 -6
- package/src/document/applyDocumentActions.ts +9 -49
- package/src/document/documentStore.test.ts +148 -107
- package/src/document/documentStore.ts +40 -10
- package/src/document/permissions.test.ts +9 -9
- package/src/document/permissions.ts +17 -7
- package/src/document/processActions.test.ts +345 -0
- package/src/document/processActions.ts +185 -2
- package/src/document/reducers.ts +13 -6
- package/src/presence/presenceStore.ts +13 -7
- package/src/preview/previewStore.test.ts +10 -2
- package/src/preview/previewStore.ts +2 -1
- package/src/preview/subscribeToStateAndFetchBatches.test.ts +8 -5
- package/src/preview/subscribeToStateAndFetchBatches.ts +9 -3
- package/src/projection/projectionStore.test.ts +18 -2
- package/src/projection/projectionStore.ts +2 -1
- package/src/projection/subscribeToStateAndFetchBatches.test.ts +6 -5
- package/src/projection/subscribeToStateAndFetchBatches.ts +9 -3
- package/src/query/queryStore.ts +7 -4
- package/src/releases/getPerspectiveState.ts +2 -2
- package/src/releases/releasesStore.ts +10 -4
- package/src/store/createActionBinder.test.ts +8 -6
- package/src/store/createActionBinder.ts +50 -14
- package/src/store/createStateSourceAction.test.ts +12 -11
- package/src/store/createStateSourceAction.ts +6 -6
- package/src/store/createStoreInstance.test.ts +29 -16
- package/src/store/createStoreInstance.ts +6 -5
- package/src/store/defineStore.test.ts +1 -1
- package/src/store/defineStore.ts +12 -7
|
@@ -17,13 +17,13 @@ const DEFAULT_PERSPECTIVE = 'drafts'
|
|
|
17
17
|
// Cache for options
|
|
18
18
|
const optionsCache = new Map<string, Map<string, PerspectiveHandle>>()
|
|
19
19
|
|
|
20
|
-
const selectInstancePerspective = (context: SelectorContext<ReleasesStoreState
|
|
20
|
+
const selectInstancePerspective = (context: SelectorContext<ReleasesStoreState>, _?: unknown) =>
|
|
21
21
|
context.instance.config.perspective
|
|
22
22
|
const selectActiveReleases = (context: SelectorContext<ReleasesStoreState>) =>
|
|
23
23
|
context.state.activeReleases
|
|
24
24
|
const selectOptions = (
|
|
25
25
|
_context: SelectorContext<ReleasesStoreState>,
|
|
26
|
-
options?:
|
|
26
|
+
options: PerspectiveHandle & {projectId?: string; dataset?: string},
|
|
27
27
|
) => options
|
|
28
28
|
|
|
29
29
|
const memoizedOptionsSelector = createSelector(
|
|
@@ -3,7 +3,7 @@ import {type SanityDocument} from '@sanity/types'
|
|
|
3
3
|
import {catchError, EMPTY, retry, switchMap, timer} from 'rxjs'
|
|
4
4
|
|
|
5
5
|
import {getClientState} from '../client/clientStore'
|
|
6
|
-
import {bindActionByDataset} from '../store/createActionBinder'
|
|
6
|
+
import {bindActionByDataset, type BoundDatasetKey} from '../store/createActionBinder'
|
|
7
7
|
import {createStateSourceAction} from '../store/createStateSourceAction'
|
|
8
8
|
import {defineStore, type StoreContext} from '../store/defineStore'
|
|
9
9
|
import {listenQuery} from '../utils/listenQuery'
|
|
@@ -32,7 +32,7 @@ export interface ReleasesStoreState {
|
|
|
32
32
|
error?: unknown
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
-
export const releasesStore = defineStore<ReleasesStoreState>({
|
|
35
|
+
export const releasesStore = defineStore<ReleasesStoreState, BoundDatasetKey>({
|
|
36
36
|
name: 'Releases',
|
|
37
37
|
getInitialState: (): ReleasesStoreState => ({
|
|
38
38
|
activeReleases: undefined,
|
|
@@ -50,17 +50,23 @@ export const releasesStore = defineStore<ReleasesStoreState>({
|
|
|
50
50
|
export const getActiveReleasesState = bindActionByDataset(
|
|
51
51
|
releasesStore,
|
|
52
52
|
createStateSourceAction({
|
|
53
|
-
selector: ({state}) => state.activeReleases,
|
|
53
|
+
selector: ({state}, _?) => state.activeReleases,
|
|
54
54
|
}),
|
|
55
55
|
)
|
|
56
56
|
|
|
57
57
|
const RELEASES_QUERY = 'releases::all()'
|
|
58
58
|
const QUERY_PARAMS = {}
|
|
59
59
|
|
|
60
|
-
const subscribeToReleases = ({
|
|
60
|
+
const subscribeToReleases = ({
|
|
61
|
+
instance,
|
|
62
|
+
state,
|
|
63
|
+
key: {projectId, dataset},
|
|
64
|
+
}: StoreContext<ReleasesStoreState, BoundDatasetKey>) => {
|
|
61
65
|
return getClientState(instance, {
|
|
62
66
|
apiVersion: '2025-04-10',
|
|
63
67
|
perspective: 'raw',
|
|
68
|
+
projectId,
|
|
69
|
+
dataset,
|
|
64
70
|
})
|
|
65
71
|
.observable.pipe(
|
|
66
72
|
switchMap((client: SanityClient) =>
|
|
@@ -12,7 +12,7 @@ beforeEach(() => vi.mocked(createStoreInstance).mockClear())
|
|
|
12
12
|
|
|
13
13
|
describe('createActionBinder', () => {
|
|
14
14
|
it('should bind an action and call it with correct context and parameters, using caching', () => {
|
|
15
|
-
const binder = createActionBinder(() => '')
|
|
15
|
+
const binder = createActionBinder((..._rest) => ({name: ''}))
|
|
16
16
|
const storeDefinition = {
|
|
17
17
|
name: 'TestStore',
|
|
18
18
|
getInitialState: () => ({counter: 0}),
|
|
@@ -37,7 +37,9 @@ describe('createActionBinder', () => {
|
|
|
37
37
|
})
|
|
38
38
|
|
|
39
39
|
it('should create separate store instances for different composite keys', () => {
|
|
40
|
-
const binder = createActionBinder(({projectId, dataset}) =>
|
|
40
|
+
const binder = createActionBinder(({config: {projectId, dataset}}, ..._rest) => ({
|
|
41
|
+
name: `${projectId}.${dataset}`,
|
|
42
|
+
}))
|
|
41
43
|
const storeDefinition = {
|
|
42
44
|
name: 'TestStore',
|
|
43
45
|
getInitialState: () => ({counter: 0}),
|
|
@@ -59,7 +61,7 @@ describe('createActionBinder', () => {
|
|
|
59
61
|
})
|
|
60
62
|
|
|
61
63
|
it('should dispose the store instance when the last instance is disposed', () => {
|
|
62
|
-
const binder = createActionBinder(() => '')
|
|
64
|
+
const binder = createActionBinder((..._rest) => ({name: ''}))
|
|
63
65
|
const storeDefinition = {
|
|
64
66
|
name: 'TestStore',
|
|
65
67
|
getInitialState: () => ({counter: 0}),
|
|
@@ -93,10 +95,10 @@ describe('bindActionByDataset', () => {
|
|
|
93
95
|
name: 'DSStore',
|
|
94
96
|
getInitialState: () => ({counter: 0}),
|
|
95
97
|
}
|
|
96
|
-
const action = vi.fn((_context, value: string) => value)
|
|
98
|
+
const action = vi.fn((_context, {value}: {value: string}) => value)
|
|
97
99
|
const boundAction = bindActionByDataset(storeDefinition, action)
|
|
98
100
|
const instance = createSanityInstance({projectId: 'proj1', dataset: 'ds1'})
|
|
99
|
-
const result = boundAction(instance, 'hello')
|
|
101
|
+
const result = boundAction(instance, {value: 'hello'})
|
|
100
102
|
expect(result).toBe('hello')
|
|
101
103
|
})
|
|
102
104
|
|
|
@@ -105,7 +107,7 @@ describe('bindActionByDataset', () => {
|
|
|
105
107
|
name: 'DSStore',
|
|
106
108
|
getInitialState: () => ({counter: 0}),
|
|
107
109
|
}
|
|
108
|
-
const action = vi.fn((_context) => 'fail')
|
|
110
|
+
const action = vi.fn((_context, _?) => 'fail')
|
|
109
111
|
const boundAction = bindActionByDataset(storeDefinition, action)
|
|
110
112
|
// Instance with missing dataset
|
|
111
113
|
const instance = createSanityInstance({projectId: 'proj1', dataset: ''})
|
|
@@ -1,14 +1,20 @@
|
|
|
1
|
-
import {type
|
|
1
|
+
import {type DocumentSource, SOURCE_ID} from '../config/sanityConfig'
|
|
2
2
|
import {type SanityInstance} from './createSanityInstance'
|
|
3
3
|
import {createStoreInstance, type StoreInstance} from './createStoreInstance'
|
|
4
4
|
import {type StoreState} from './createStoreState'
|
|
5
5
|
import {type StoreContext, type StoreDefinition} from './defineStore'
|
|
6
6
|
|
|
7
|
+
export type BoundDatasetKey = {
|
|
8
|
+
name: string
|
|
9
|
+
projectId: string
|
|
10
|
+
dataset: string
|
|
11
|
+
}
|
|
12
|
+
|
|
7
13
|
/**
|
|
8
14
|
* Defines a store action that operates on a specific state type
|
|
9
15
|
*/
|
|
10
|
-
export type StoreAction<TState, TParams extends unknown[], TReturn> = (
|
|
11
|
-
context: StoreContext<TState>,
|
|
16
|
+
export type StoreAction<TState, TParams extends unknown[], TReturn, TKey = unknown> = (
|
|
17
|
+
context: StoreContext<TState, TKey>,
|
|
12
18
|
...params: TParams
|
|
13
19
|
) => TReturn
|
|
14
20
|
|
|
@@ -43,7 +49,10 @@ export type BoundStoreAction<_TState, TParams extends unknown[], TReturn> = (
|
|
|
43
49
|
* )
|
|
44
50
|
* ```
|
|
45
51
|
*/
|
|
46
|
-
export function createActionBinder
|
|
52
|
+
export function createActionBinder<
|
|
53
|
+
TKey extends {name: string},
|
|
54
|
+
TKeyParams extends unknown[] = unknown[],
|
|
55
|
+
>(keyFn: (instance: SanityInstance, ...params: TKeyParams) => TKey) {
|
|
47
56
|
const instanceRegistry = new Map<string, Set<string>>()
|
|
48
57
|
const storeRegistry = new Map<string, StoreInstance<unknown>>()
|
|
49
58
|
|
|
@@ -54,13 +63,13 @@ export function createActionBinder(keyFn: (config: SanityConfig) => string) {
|
|
|
54
63
|
* @param action - The action to bind
|
|
55
64
|
* @returns A function that executes the action with a Sanity instance
|
|
56
65
|
*/
|
|
57
|
-
return function bindAction<TState, TParams extends
|
|
58
|
-
storeDefinition: StoreDefinition<TState>,
|
|
59
|
-
action: StoreAction<TState, TParams, TReturn>,
|
|
66
|
+
return function bindAction<TState, TParams extends TKeyParams, TReturn>(
|
|
67
|
+
storeDefinition: StoreDefinition<TState, TKey>,
|
|
68
|
+
action: StoreAction<TState, TParams, TReturn, TKey>,
|
|
60
69
|
): BoundStoreAction<TState, TParams, TReturn> {
|
|
61
70
|
return function boundAction(instance: SanityInstance, ...params: TParams) {
|
|
62
|
-
const
|
|
63
|
-
const compositeKey = storeDefinition.name + (
|
|
71
|
+
const key = keyFn(instance, ...params)
|
|
72
|
+
const compositeKey = storeDefinition.name + (key.name ? `:${key.name}` : '')
|
|
64
73
|
|
|
65
74
|
// Get or create instance set for this composite key
|
|
66
75
|
let instances = instanceRegistry.get(compositeKey)
|
|
@@ -87,12 +96,12 @@ export function createActionBinder(keyFn: (config: SanityConfig) => string) {
|
|
|
87
96
|
// Get or create store instance
|
|
88
97
|
let storeInstance = storeRegistry.get(compositeKey)
|
|
89
98
|
if (!storeInstance) {
|
|
90
|
-
storeInstance = createStoreInstance(instance, storeDefinition)
|
|
99
|
+
storeInstance = createStoreInstance(instance, key, storeDefinition)
|
|
91
100
|
storeRegistry.set(compositeKey, storeInstance)
|
|
92
101
|
}
|
|
93
102
|
|
|
94
103
|
// Execute action with store context
|
|
95
|
-
return action({instance, state: storeInstance.state as StoreState<TState
|
|
104
|
+
return action({instance, state: storeInstance.state as StoreState<TState>, key}, ...params)
|
|
96
105
|
}
|
|
97
106
|
}
|
|
98
107
|
}
|
|
@@ -128,11 +137,38 @@ export function createActionBinder(keyFn: (config: SanityConfig) => string) {
|
|
|
128
137
|
* fetchDocument(sanityInstance, 'doc123')
|
|
129
138
|
* ```
|
|
130
139
|
*/
|
|
131
|
-
export const bindActionByDataset = createActionBinder
|
|
140
|
+
export const bindActionByDataset = createActionBinder<
|
|
141
|
+
BoundDatasetKey,
|
|
142
|
+
[(object & {projectId?: string; dataset?: string})?, ...unknown[]]
|
|
143
|
+
>((instance, options) => {
|
|
144
|
+
const projectId = options?.projectId ?? instance.config.projectId
|
|
145
|
+
const dataset = options?.dataset ?? instance.config.dataset
|
|
146
|
+
if (!projectId || !dataset) {
|
|
147
|
+
throw new Error('This API requires a project ID and dataset configured.')
|
|
148
|
+
}
|
|
149
|
+
return {name: `${projectId}.${dataset}`, projectId, dataset}
|
|
150
|
+
})
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Binds an action to a store that's scoped to a specific document source.
|
|
154
|
+
**/
|
|
155
|
+
export const bindActionBySource = createActionBinder<
|
|
156
|
+
{name: string},
|
|
157
|
+
[{source?: DocumentSource}, ...unknown[]]
|
|
158
|
+
>((instance, {source}) => {
|
|
159
|
+
if (source) {
|
|
160
|
+
const id = source[SOURCE_ID]
|
|
161
|
+
if (!id) throw new Error('Invalid source (missing ID information)')
|
|
162
|
+
if (Array.isArray(id)) return {name: id.join(':')}
|
|
163
|
+
return {name: `${id.projectId}.${id.dataset}`}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
const {projectId, dataset} = instance.config
|
|
167
|
+
|
|
132
168
|
if (!projectId || !dataset) {
|
|
133
169
|
throw new Error('This API requires a project ID and dataset configured.')
|
|
134
170
|
}
|
|
135
|
-
return `${projectId}.${dataset}`
|
|
171
|
+
return {name: `${projectId}.${dataset}`}
|
|
136
172
|
})
|
|
137
173
|
|
|
138
174
|
/**
|
|
@@ -173,4 +209,4 @@ export const bindActionByDataset = createActionBinder(({projectId, dataset}) =>
|
|
|
173
209
|
* getCurrentUser(sanityInstance)
|
|
174
210
|
* ```
|
|
175
211
|
*/
|
|
176
|
-
export const bindActionGlobally = createActionBinder(() => 'global')
|
|
212
|
+
export const bindActionGlobally = createActionBinder((..._rest) => ({name: 'global'}))
|
|
@@ -21,7 +21,7 @@ describe('createStateSourceAction', () => {
|
|
|
21
21
|
it('should create a source that provides current state through getCurrent', () => {
|
|
22
22
|
const selector = vi.fn(({state: s}: SelectorContext<CountStoreState>) => s.count)
|
|
23
23
|
const action = createStateSourceAction(selector)
|
|
24
|
-
const source = action({state, instance})
|
|
24
|
+
const source = action({state, instance, key: null})
|
|
25
25
|
|
|
26
26
|
expect(source.getCurrent()).toBe(0)
|
|
27
27
|
state.set('test', {count: 5})
|
|
@@ -33,7 +33,7 @@ describe('createStateSourceAction', () => {
|
|
|
33
33
|
const source = createStateSourceAction({
|
|
34
34
|
selector: ({state: s}: SelectorContext<CountStoreState>) => s.count,
|
|
35
35
|
isEqual: (a, b) => a === b,
|
|
36
|
-
})({state, instance})
|
|
36
|
+
})({state, instance, key: null})
|
|
37
37
|
|
|
38
38
|
const unsubscribe = source.subscribe(onStoreChanged)
|
|
39
39
|
|
|
@@ -53,11 +53,11 @@ describe('createStateSourceAction', () => {
|
|
|
53
53
|
const source = createStateSourceAction({
|
|
54
54
|
selector: ({state: s}: SelectorContext<CountStoreState>) => s.items,
|
|
55
55
|
onSubscribe,
|
|
56
|
-
})({state, instance})
|
|
56
|
+
})({state, instance, key: null})
|
|
57
57
|
|
|
58
58
|
const unsubscribe = source.subscribe()
|
|
59
59
|
expect(onSubscribe).toHaveBeenCalledWith(
|
|
60
|
-
expect.objectContaining({state, instance}),
|
|
60
|
+
expect.objectContaining({state, instance, key: null}),
|
|
61
61
|
// No params in this case
|
|
62
62
|
)
|
|
63
63
|
|
|
@@ -68,7 +68,7 @@ describe('createStateSourceAction', () => {
|
|
|
68
68
|
const action = createStateSourceAction({
|
|
69
69
|
selector: ({state: s}: SelectorContext<CountStoreState>, index: number) => s.items[index],
|
|
70
70
|
})
|
|
71
|
-
const source = action({state, instance}, 0)
|
|
71
|
+
const source = action({state, instance, key: null}, 0)
|
|
72
72
|
|
|
73
73
|
state.set('add', {items: ['first']})
|
|
74
74
|
expect(source.getCurrent()).toBe('first')
|
|
@@ -80,7 +80,7 @@ describe('createStateSourceAction', () => {
|
|
|
80
80
|
selector: () => {
|
|
81
81
|
throw error
|
|
82
82
|
},
|
|
83
|
-
})({state, instance})
|
|
83
|
+
})({state, instance, key: null})
|
|
84
84
|
|
|
85
85
|
const errorHandler = vi.fn()
|
|
86
86
|
source.observable.subscribe({error: errorHandler})
|
|
@@ -94,7 +94,7 @@ describe('createStateSourceAction', () => {
|
|
|
94
94
|
const source = createStateSourceAction({
|
|
95
95
|
selector: ({state: s}: SelectorContext<CountStoreState>) => s.items.map((i) => i.length),
|
|
96
96
|
isEqual,
|
|
97
|
-
})({state, instance})
|
|
97
|
+
})({state, instance, key: null})
|
|
98
98
|
|
|
99
99
|
const onChange = vi.fn()
|
|
100
100
|
source.subscribe(onChange)
|
|
@@ -112,7 +112,7 @@ describe('createStateSourceAction', () => {
|
|
|
112
112
|
const source = createStateSourceAction({
|
|
113
113
|
selector: ({state: s}: SelectorContext<CountStoreState>) => s.count,
|
|
114
114
|
onSubscribe: () => cleanup,
|
|
115
|
-
})({state, instance})
|
|
115
|
+
})({state, instance, key: null})
|
|
116
116
|
|
|
117
117
|
const unsubscribe = source.subscribe()
|
|
118
118
|
unsubscribe()
|
|
@@ -125,6 +125,7 @@ describe('createStateSourceAction', () => {
|
|
|
125
125
|
)({
|
|
126
126
|
state,
|
|
127
127
|
instance,
|
|
128
|
+
key: null,
|
|
128
129
|
})
|
|
129
130
|
|
|
130
131
|
const subscriber1 = vi.fn()
|
|
@@ -144,7 +145,7 @@ describe('createStateSourceAction', () => {
|
|
|
144
145
|
|
|
145
146
|
it('should cache selector context per state object', () => {
|
|
146
147
|
const selector = vi.fn(({state: s}: SelectorContext<CountStoreState>) => s.count)
|
|
147
|
-
const source = createStateSourceAction(selector)({state, instance})
|
|
148
|
+
const source = createStateSourceAction(selector)({state, instance, key: null})
|
|
148
149
|
|
|
149
150
|
// Initial call creates context
|
|
150
151
|
expect(source.getCurrent()).toBe(0)
|
|
@@ -181,10 +182,10 @@ describe('createStateSourceAction', () => {
|
|
|
181
182
|
const secondInstance = createSanityInstance({projectId: 'test2', dataset: 'test2'})
|
|
182
183
|
const selector = vi.fn(({state: s}: SelectorContext<CountStoreState>) => s.count)
|
|
183
184
|
|
|
184
|
-
const source1 = createStateSourceAction(selector)({state, instance})
|
|
185
|
+
const source1 = createStateSourceAction(selector)({state, instance, key: null})
|
|
185
186
|
source1.getCurrent()
|
|
186
187
|
|
|
187
|
-
const source2 = createStateSourceAction(selector)({state, instance: secondInstance})
|
|
188
|
+
const source2 = createStateSourceAction(selector)({state, instance: secondInstance, key: null})
|
|
188
189
|
source2.getCurrent()
|
|
189
190
|
|
|
190
191
|
const context1 = selector.mock.calls[0][0]
|
|
@@ -89,7 +89,7 @@ export type Selector<TState, TParams extends unknown[], TReturn> = (
|
|
|
89
89
|
/**
|
|
90
90
|
* Configuration options for creating a state source action
|
|
91
91
|
*/
|
|
92
|
-
interface StateSourceOptions<TState, TParams extends unknown[], TReturn> {
|
|
92
|
+
interface StateSourceOptions<TState, TParams extends unknown[], TReturn, TKey> {
|
|
93
93
|
/**
|
|
94
94
|
* Selector function that derives the desired value from store state
|
|
95
95
|
*
|
|
@@ -106,7 +106,7 @@ interface StateSourceOptions<TState, TParams extends unknown[], TReturn> {
|
|
|
106
106
|
* @param params - Action parameters provided during invocation
|
|
107
107
|
* @returns Optional cleanup function called when subscription ends
|
|
108
108
|
*/
|
|
109
|
-
onSubscribe?: (context: StoreContext<TState>, ...params: TParams) => void | (() => void)
|
|
109
|
+
onSubscribe?: (context: StoreContext<TState, TKey>, ...params: TParams) => void | (() => void)
|
|
110
110
|
|
|
111
111
|
/**
|
|
112
112
|
* Equality function to prevent unnecessary updates
|
|
@@ -168,9 +168,9 @@ interface StateSourceOptions<TState, TParams extends unknown[], TReturn> {
|
|
|
168
168
|
* })
|
|
169
169
|
* ```
|
|
170
170
|
*/
|
|
171
|
-
export function createStateSourceAction<TState, TParams extends unknown[], TReturn>(
|
|
172
|
-
options: Selector<TState, TParams, TReturn> | StateSourceOptions<TState, TParams, TReturn>,
|
|
173
|
-
): StoreAction<TState, TParams, StateSource<TReturn
|
|
171
|
+
export function createStateSourceAction<TState, TParams extends unknown[], TReturn, TKey = unknown>(
|
|
172
|
+
options: Selector<TState, TParams, TReturn> | StateSourceOptions<TState, TParams, TReturn, TKey>,
|
|
173
|
+
): StoreAction<TState, TParams, StateSource<TReturn>, TKey> {
|
|
174
174
|
const selector = typeof options === 'function' ? options : options.selector
|
|
175
175
|
const subscribeHandler = options && 'onSubscribe' in options ? options.onSubscribe : undefined
|
|
176
176
|
const isEqual = options && 'isEqual' in options ? (options.isEqual ?? Object.is) : Object.is
|
|
@@ -184,7 +184,7 @@ export function createStateSourceAction<TState, TParams extends unknown[], TRetu
|
|
|
184
184
|
* @param context - Store context providing access to state and instance
|
|
185
185
|
* @param params - Parameters provided when invoking the bound action
|
|
186
186
|
*/
|
|
187
|
-
function stateSourceAction(context: StoreContext<TState>, ...params: TParams) {
|
|
187
|
+
function stateSourceAction(context: StoreContext<TState, TKey>, ...params: TParams) {
|
|
188
188
|
const {state, instance} = context
|
|
189
189
|
|
|
190
190
|
const getCurrent = () => {
|
|
@@ -24,36 +24,45 @@ describe('createStoreInstance', () => {
|
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
it('should create store instance with initial state', () => {
|
|
27
|
-
const store = createStoreInstance(instance, storeDef)
|
|
27
|
+
const store = createStoreInstance(instance, {name: 'store'}, storeDef)
|
|
28
28
|
expect(store.state).toBeDefined()
|
|
29
29
|
})
|
|
30
30
|
|
|
31
31
|
it('should call getInitialState with Sanity instance', () => {
|
|
32
32
|
const getInitialState = vi.fn(() => ({count: 0}))
|
|
33
|
-
createStoreInstance(instance, {...storeDef, getInitialState})
|
|
34
|
-
expect(getInitialState).toHaveBeenCalledWith(instance)
|
|
33
|
+
createStoreInstance(instance, {name: 'store'}, {...storeDef, getInitialState})
|
|
34
|
+
expect(getInitialState).toHaveBeenCalledWith(instance, {name: 'store'})
|
|
35
35
|
})
|
|
36
36
|
|
|
37
37
|
it('should call initialize function with context', () => {
|
|
38
38
|
const initialize = vi.fn()
|
|
39
39
|
|
|
40
|
-
const store = createStoreInstance(
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
40
|
+
const store = createStoreInstance(
|
|
41
|
+
instance,
|
|
42
|
+
{name: 'store'},
|
|
43
|
+
{
|
|
44
|
+
...storeDef,
|
|
45
|
+
initialize,
|
|
46
|
+
},
|
|
47
|
+
)
|
|
44
48
|
expect(initialize).toHaveBeenCalledWith({
|
|
45
49
|
state: store.state,
|
|
46
50
|
instance,
|
|
51
|
+
key: {name: 'store'},
|
|
47
52
|
})
|
|
48
53
|
})
|
|
49
54
|
|
|
50
55
|
it('should handle store disposal with cleanup function', () => {
|
|
51
56
|
const disposeMock = vi.fn()
|
|
52
57
|
|
|
53
|
-
const store = createStoreInstance(
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
58
|
+
const store = createStoreInstance(
|
|
59
|
+
instance,
|
|
60
|
+
{name: 'store'},
|
|
61
|
+
{
|
|
62
|
+
...storeDef,
|
|
63
|
+
initialize: () => disposeMock,
|
|
64
|
+
},
|
|
65
|
+
)
|
|
57
66
|
store.dispose()
|
|
58
67
|
|
|
59
68
|
expect(disposeMock).toHaveBeenCalledTimes(1)
|
|
@@ -61,7 +70,7 @@ describe('createStoreInstance', () => {
|
|
|
61
70
|
})
|
|
62
71
|
|
|
63
72
|
it('should handle disposal without initialize function', () => {
|
|
64
|
-
const store = createStoreInstance(instance, storeDef)
|
|
73
|
+
const store = createStoreInstance(instance, {name: 'store'}, storeDef)
|
|
65
74
|
store.dispose()
|
|
66
75
|
expect(store.isDisposed()).toBe(true)
|
|
67
76
|
})
|
|
@@ -69,10 +78,14 @@ describe('createStoreInstance', () => {
|
|
|
69
78
|
it('should prevent multiple disposals', () => {
|
|
70
79
|
const disposeMock = vi.fn()
|
|
71
80
|
|
|
72
|
-
const store = createStoreInstance(
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
81
|
+
const store = createStoreInstance(
|
|
82
|
+
instance,
|
|
83
|
+
{name: 'store'},
|
|
84
|
+
{
|
|
85
|
+
...storeDef,
|
|
86
|
+
initialize: () => disposeMock,
|
|
87
|
+
},
|
|
88
|
+
)
|
|
76
89
|
store.dispose()
|
|
77
90
|
store.dispose()
|
|
78
91
|
|
|
@@ -57,15 +57,16 @@ export interface StoreInstance<TState> {
|
|
|
57
57
|
* instance.dispose()
|
|
58
58
|
* ```
|
|
59
59
|
*/
|
|
60
|
-
export function createStoreInstance<TState>(
|
|
60
|
+
export function createStoreInstance<TState, TKey extends {name: string}>(
|
|
61
61
|
instance: SanityInstance,
|
|
62
|
-
|
|
62
|
+
key: TKey,
|
|
63
|
+
{name, getInitialState, initialize}: StoreDefinition<TState, TKey>,
|
|
63
64
|
): StoreInstance<TState> {
|
|
64
|
-
const state = createStoreState(getInitialState(instance), {
|
|
65
|
+
const state = createStoreState(getInitialState(instance, key), {
|
|
65
66
|
enabled: !!getEnv('DEV'),
|
|
66
|
-
name: `${name}-${
|
|
67
|
+
name: `${name}-${key.name}`,
|
|
67
68
|
})
|
|
68
|
-
const dispose = initialize?.({state, instance})
|
|
69
|
+
const dispose = initialize?.({state, instance, key})
|
|
69
70
|
const disposed = {current: false}
|
|
70
71
|
|
|
71
72
|
return {
|
|
@@ -13,6 +13,6 @@ describe('defineStore', () => {
|
|
|
13
13
|
const result = defineStore(storeDef)
|
|
14
14
|
expect(result).toBe(storeDef)
|
|
15
15
|
expect(result.name).toBe('TestStore')
|
|
16
|
-
expect(result.getInitialState({} as SanityInstance)).toBe(42)
|
|
16
|
+
expect(result.getInitialState({} as SanityInstance, null)).toBe(42)
|
|
17
17
|
})
|
|
18
18
|
})
|
package/src/store/defineStore.ts
CHANGED
|
@@ -4,7 +4,7 @@ import {type StoreState} from './createStoreState'
|
|
|
4
4
|
/**
|
|
5
5
|
* Context object provided to store initialization functions
|
|
6
6
|
*/
|
|
7
|
-
export interface StoreContext<TState> {
|
|
7
|
+
export interface StoreContext<TState, TKey = unknown> {
|
|
8
8
|
/**
|
|
9
9
|
* Sanity instance associated with this store
|
|
10
10
|
*
|
|
@@ -20,6 +20,11 @@ export interface StoreContext<TState> {
|
|
|
20
20
|
* Contains methods for getting/setting state and observing changes
|
|
21
21
|
*/
|
|
22
22
|
state: StoreState<TState>
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* The key used to instantiate the store.
|
|
26
|
+
*/
|
|
27
|
+
key: TKey
|
|
23
28
|
}
|
|
24
29
|
|
|
25
30
|
/**
|
|
@@ -29,7 +34,7 @@ export interface StoreContext<TState> {
|
|
|
29
34
|
* Stores are isolated state containers that can be associated with Sanity instances.
|
|
30
35
|
* Each store definition creates a separate state instance per composite key.
|
|
31
36
|
*/
|
|
32
|
-
export interface StoreDefinition<TState> {
|
|
37
|
+
export interface StoreDefinition<TState, TKey = unknown> {
|
|
33
38
|
/**
|
|
34
39
|
* Unique name for the store
|
|
35
40
|
*
|
|
@@ -47,7 +52,7 @@ export interface StoreDefinition<TState> {
|
|
|
47
52
|
* Called when a new store instance is created. Can use Sanity instance
|
|
48
53
|
* configuration to determine initial state.
|
|
49
54
|
*/
|
|
50
|
-
getInitialState: (instance: SanityInstance) => TState
|
|
55
|
+
getInitialState: (instance: SanityInstance, key: TKey) => TState
|
|
51
56
|
|
|
52
57
|
/**
|
|
53
58
|
* Optional initialization function
|
|
@@ -65,7 +70,7 @@ export interface StoreDefinition<TState> {
|
|
|
65
70
|
* - Cancel pending operations
|
|
66
71
|
* - Dispose external connections
|
|
67
72
|
*/
|
|
68
|
-
initialize?: (context: StoreContext<TState>) => (() => void) | undefined
|
|
73
|
+
initialize?: (context: StoreContext<TState, TKey>) => (() => void) | undefined
|
|
69
74
|
}
|
|
70
75
|
|
|
71
76
|
/**
|
|
@@ -74,8 +79,8 @@ export interface StoreDefinition<TState> {
|
|
|
74
79
|
* @param storeDefinition - Configuration object defining the store
|
|
75
80
|
* @returns The finalized store definition
|
|
76
81
|
*/
|
|
77
|
-
export function defineStore<TState>(
|
|
78
|
-
storeDefinition: StoreDefinition<TState>,
|
|
79
|
-
): StoreDefinition<TState> {
|
|
82
|
+
export function defineStore<TState, TKey = unknown>(
|
|
83
|
+
storeDefinition: StoreDefinition<TState, TKey>,
|
|
84
|
+
): StoreDefinition<TState, TKey> {
|
|
80
85
|
return storeDefinition
|
|
81
86
|
}
|