@sanity/sdk 3.0.0-rc.2 → 3.0.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/_exports/_internal.d.ts +1 -1
- package/dist/_exports/_internal.js +2 -2
- package/dist/_exports/comlink.d.ts +1 -1
- package/dist/{authStore-DKFX8fn7.d.ts → authStore-C0o1NOxW.d.ts} +29 -43
- package/dist/authStore-C0o1NOxW.d.ts.map +1 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +131 -135
- package/dist/index.js.map +1 -1
- package/dist/{queryStore-DPf9sN3V.js → queryStore-C7yMHPUI.js} +2 -2
- package/dist/queryStore-C7yMHPUI.js.map +1 -0
- package/dist/{telemetryManager-Dq_qCkeI.js → telemetryManager-UIEiMtC0.js} +2 -2
- package/dist/{telemetryManager-Dq_qCkeI.js.map → telemetryManager-UIEiMtC0.js.map} +1 -1
- package/dist/{version-BChjsi4u.js → version-D8RP_Q59.js} +2 -2
- package/dist/{version-BChjsi4u.js.map → version-D8RP_Q59.js.map} +1 -1
- package/package.json +3 -3
- package/src/_exports/index.ts +7 -4
- package/src/datasets/datasets.test.ts +44 -9
- package/src/datasets/datasets.ts +24 -23
- package/src/document/documentConstants.ts +9 -0
- package/src/document/documentStore.test.ts +69 -0
- package/src/document/reducers.test.ts +224 -0
- package/src/document/reducers.ts +108 -25
- package/src/favorites/favorites.test.ts +64 -24
- package/src/favorites/favorites.ts +73 -22
- package/src/store/fetcherStore.test.ts +23 -1
- package/src/store/fetcherStore.ts +3 -3
- package/dist/authStore-DKFX8fn7.d.ts.map +0 -1
- package/dist/queryStore-DPf9sN3V.js.map +0 -1
- package/src/utils/createFetcherStore.test.ts +0 -171
- package/src/utils/createFetcherStore.ts +0 -284
|
@@ -1,284 +0,0 @@
|
|
|
1
|
-
import {asapScheduler, EMPTY, firstValueFrom, from, Observable} from 'rxjs'
|
|
2
|
-
import {
|
|
3
|
-
catchError,
|
|
4
|
-
delay,
|
|
5
|
-
filter,
|
|
6
|
-
first,
|
|
7
|
-
groupBy,
|
|
8
|
-
map,
|
|
9
|
-
mergeMap,
|
|
10
|
-
pairwise,
|
|
11
|
-
startWith,
|
|
12
|
-
switchMap,
|
|
13
|
-
tap,
|
|
14
|
-
} from 'rxjs/operators'
|
|
15
|
-
|
|
16
|
-
import {bindActionGlobally, type BoundStoreAction} from '../store/createActionBinder'
|
|
17
|
-
import {type SanityInstance} from '../store/createSanityInstance'
|
|
18
|
-
import {
|
|
19
|
-
createStateSourceAction,
|
|
20
|
-
type SelectorContext,
|
|
21
|
-
type StateSource,
|
|
22
|
-
} from '../store/createStateSourceAction'
|
|
23
|
-
import {defineStore, type StoreContext} from '../store/defineStore'
|
|
24
|
-
import {insecureRandomId} from '../utils/ids'
|
|
25
|
-
import {omitProperty} from '../utils/object'
|
|
26
|
-
import {setCleanupTimeout} from './setCleanupTimeout'
|
|
27
|
-
|
|
28
|
-
interface CreateFetcherStoreOptions<TParams extends unknown[], TData> {
|
|
29
|
-
/**
|
|
30
|
-
* A unique name for this store (for devtools)
|
|
31
|
-
*/
|
|
32
|
-
name: string
|
|
33
|
-
/**
|
|
34
|
-
* A function that creates the observable that will be used to fetch the data.
|
|
35
|
-
*/
|
|
36
|
-
fetcher: (instance: SanityInstance) => (...params: TParams) => Observable<TData>
|
|
37
|
-
/**
|
|
38
|
-
* The function used to convert the params into keys that state related to
|
|
39
|
-
* those params will be stored.
|
|
40
|
-
*/
|
|
41
|
-
getKey: (instance: SanityInstance, ...params: TParams) => string
|
|
42
|
-
/**
|
|
43
|
-
* Delay in ms before clearing state after the last subscription is removed.
|
|
44
|
-
* This results in react components suspending again due to no previous state
|
|
45
|
-
* to show.
|
|
46
|
-
*/
|
|
47
|
-
stateExpirationDelay?: number
|
|
48
|
-
/**
|
|
49
|
-
* The minimum time (ms) that must elapse since the last fetch for the same key
|
|
50
|
-
*/
|
|
51
|
-
fetchThrottleInternal?: number
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
interface StoreEntry<TParams extends unknown[], TData> {
|
|
55
|
-
params: TParams
|
|
56
|
-
instance: SanityInstance
|
|
57
|
-
key: string
|
|
58
|
-
data?: TData
|
|
59
|
-
error?: unknown
|
|
60
|
-
subscriptions: string[]
|
|
61
|
-
lastFetchInitiatedAt?: string
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Internal helper type
|
|
66
|
-
* @public
|
|
67
|
-
*/
|
|
68
|
-
export interface FetcherStoreState<TParams extends unknown[], TData> {
|
|
69
|
-
stateByParams: {[TSerializedKey in string]?: StoreEntry<TParams, TData>}
|
|
70
|
-
error?: unknown
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* Internal helper type
|
|
75
|
-
* @public
|
|
76
|
-
*/
|
|
77
|
-
export interface FetcherStore<TParams extends unknown[], TData> {
|
|
78
|
-
getState: BoundStoreAction<
|
|
79
|
-
FetcherStoreState<TParams, TData>,
|
|
80
|
-
TParams,
|
|
81
|
-
StateSource<TData | undefined>
|
|
82
|
-
>
|
|
83
|
-
resolveState: BoundStoreAction<FetcherStoreState<TParams, TData>, TParams, Promise<TData>>
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* Creates a store from a function that returns an observable that fetches data
|
|
88
|
-
* that supports parameterized state caching.
|
|
89
|
-
*
|
|
90
|
-
* This function creates a resource store keyed by parameter values (using the
|
|
91
|
-
* provided `getKey` function) and returns a state source (via `getState`)
|
|
92
|
-
* that components can subscribe to. When a new subscription is added, and if
|
|
93
|
-
* enough time has passed since the last fetch (controlled by
|
|
94
|
-
* `fetchThrottleInternal`), it invokes the observable factory (via
|
|
95
|
-
* `getObservable`) to fetch fresh data. The data is stored in state and can be
|
|
96
|
-
* accessed reactively.
|
|
97
|
-
*
|
|
98
|
-
* Additionally, the store provides a `resolveState` function that returns a
|
|
99
|
-
* Promise resolving with the next non-undefined value from the state source.
|
|
100
|
-
*
|
|
101
|
-
* State expiration is implemented: after the last subscription for a key is
|
|
102
|
-
* removed, its state is cleared after `stateExpirationDelay` ms, causing
|
|
103
|
-
* components to suspend until fresh data is fetched.
|
|
104
|
-
*/
|
|
105
|
-
export function createFetcherStore<TParams extends unknown[], TData>({
|
|
106
|
-
name,
|
|
107
|
-
fetcher: getObservable,
|
|
108
|
-
getKey,
|
|
109
|
-
fetchThrottleInternal = 1000,
|
|
110
|
-
stateExpirationDelay = 5000,
|
|
111
|
-
}: CreateFetcherStoreOptions<TParams, TData>): FetcherStore<TParams, TData> {
|
|
112
|
-
const store = defineStore<FetcherStoreState<TParams, TData>>({
|
|
113
|
-
name,
|
|
114
|
-
getInitialState: () => ({
|
|
115
|
-
stateByParams: {},
|
|
116
|
-
}),
|
|
117
|
-
initialize: (context) => {
|
|
118
|
-
const subscription = subscribeToSubscriptionsAndFetch(context)
|
|
119
|
-
return () => subscription.unsubscribe()
|
|
120
|
-
},
|
|
121
|
-
})
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* For each key in state.stateByParams we set up a grouped stream.
|
|
125
|
-
* When a subscription is added (i.e. the subscriptions array length increases)
|
|
126
|
-
* and if enough time has elapsed since the last fetch, we update the timestamp
|
|
127
|
-
* and call the factory function for that key.
|
|
128
|
-
*/
|
|
129
|
-
const subscribeToSubscriptionsAndFetch = ({
|
|
130
|
-
state,
|
|
131
|
-
}: StoreContext<FetcherStoreState<TParams, TData>>) => {
|
|
132
|
-
return state.observable
|
|
133
|
-
.pipe(
|
|
134
|
-
// Map the state to an array of [serialized, entry] pairs.
|
|
135
|
-
switchMap((s: FetcherStoreState<TParams, TData>) => {
|
|
136
|
-
const entries = Object.entries(s.stateByParams)
|
|
137
|
-
return entries.length > 0 ? from(entries) : EMPTY
|
|
138
|
-
}),
|
|
139
|
-
// Group by the serialized key.
|
|
140
|
-
groupBy(([key]) => key),
|
|
141
|
-
mergeMap((group$) =>
|
|
142
|
-
group$.pipe(
|
|
143
|
-
// Emit an initial value for pairwise comparisons.
|
|
144
|
-
startWith<[string, StoreEntry<TParams, TData> | undefined]>([group$.key, undefined]),
|
|
145
|
-
pairwise(),
|
|
146
|
-
// Trigger only when the subscriptions array grows.
|
|
147
|
-
filter(([[, prevEntry], [, currEntry]]) => {
|
|
148
|
-
const prevSubs = prevEntry?.subscriptions ?? []
|
|
149
|
-
const currSubs = currEntry?.subscriptions ?? []
|
|
150
|
-
return currSubs.length > prevSubs.length
|
|
151
|
-
}),
|
|
152
|
-
map(([, [, currEntry]]) => currEntry),
|
|
153
|
-
|
|
154
|
-
// Only trigger if we haven't fetched recently.
|
|
155
|
-
filter((entry) => {
|
|
156
|
-
const lastFetch = entry?.lastFetchInitiatedAt
|
|
157
|
-
if (!lastFetch) return true
|
|
158
|
-
return Date.now() - new Date(lastFetch).getTime() >= fetchThrottleInternal
|
|
159
|
-
}),
|
|
160
|
-
switchMap((entry) => {
|
|
161
|
-
// Retrieve params from the entry
|
|
162
|
-
if (!entry) return EMPTY
|
|
163
|
-
|
|
164
|
-
// Record that a fetch is being initiated.
|
|
165
|
-
state.set('setLastFetchInitiatedAt', (prev: FetcherStoreState<TParams, TData>) => ({
|
|
166
|
-
stateByParams: {
|
|
167
|
-
...prev.stateByParams,
|
|
168
|
-
[entry.key]: {
|
|
169
|
-
...entry,
|
|
170
|
-
...prev.stateByParams[entry.key],
|
|
171
|
-
lastFetchInitiatedAt: new Date().toISOString(),
|
|
172
|
-
},
|
|
173
|
-
},
|
|
174
|
-
}))
|
|
175
|
-
|
|
176
|
-
const factoryFn = getObservable(entry.instance)
|
|
177
|
-
return factoryFn(...entry.params).pipe(
|
|
178
|
-
// the `createStateSourceAction` util requires the update
|
|
179
|
-
// to
|
|
180
|
-
delay(0, asapScheduler),
|
|
181
|
-
tap((data: TData) =>
|
|
182
|
-
state.set('setData', (prev: FetcherStoreState<TParams, TData>) => ({
|
|
183
|
-
stateByParams: {
|
|
184
|
-
...prev.stateByParams,
|
|
185
|
-
[entry.key]: {
|
|
186
|
-
...omitProperty(entry, 'error'),
|
|
187
|
-
...(prev.stateByParams[entry.key]
|
|
188
|
-
? omitProperty(prev.stateByParams[entry.key], 'error')
|
|
189
|
-
: {}),
|
|
190
|
-
data,
|
|
191
|
-
},
|
|
192
|
-
},
|
|
193
|
-
})),
|
|
194
|
-
),
|
|
195
|
-
catchError((error) => {
|
|
196
|
-
state.set('setError', (prev) => ({
|
|
197
|
-
stateByParams: {
|
|
198
|
-
...prev.stateByParams,
|
|
199
|
-
[entry.key]: {
|
|
200
|
-
...entry,
|
|
201
|
-
...prev.stateByParams[entry.key],
|
|
202
|
-
error,
|
|
203
|
-
},
|
|
204
|
-
},
|
|
205
|
-
}))
|
|
206
|
-
|
|
207
|
-
return EMPTY
|
|
208
|
-
}),
|
|
209
|
-
)
|
|
210
|
-
}),
|
|
211
|
-
),
|
|
212
|
-
),
|
|
213
|
-
)
|
|
214
|
-
.subscribe({
|
|
215
|
-
error: (error) => state.set('setError', {error}),
|
|
216
|
-
})
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
const getState = bindActionGlobally(
|
|
220
|
-
store,
|
|
221
|
-
createStateSourceAction({
|
|
222
|
-
selector: (
|
|
223
|
-
{
|
|
224
|
-
instance,
|
|
225
|
-
state: {stateByParams, error},
|
|
226
|
-
}: SelectorContext<FetcherStoreState<TParams, TData>>,
|
|
227
|
-
...params: TParams
|
|
228
|
-
) => {
|
|
229
|
-
if (error) throw error
|
|
230
|
-
const key = getKey(instance, ...params)
|
|
231
|
-
const entry = stateByParams[key]
|
|
232
|
-
if (entry?.error) throw entry.error
|
|
233
|
-
return entry?.data
|
|
234
|
-
},
|
|
235
|
-
onSubscribe: ({instance, state}, ...params: TParams) => {
|
|
236
|
-
const subscriptionId = insecureRandomId()
|
|
237
|
-
const key = getKey(instance, ...params)
|
|
238
|
-
|
|
239
|
-
state.set('addSubscription', (prev: FetcherStoreState<TParams, TData>) => ({
|
|
240
|
-
stateByParams: {
|
|
241
|
-
...prev.stateByParams,
|
|
242
|
-
[key]: {
|
|
243
|
-
...prev.stateByParams[key],
|
|
244
|
-
instance,
|
|
245
|
-
key,
|
|
246
|
-
params: prev.stateByParams[key]?.params || params,
|
|
247
|
-
subscriptions: [...(prev.stateByParams[key]?.subscriptions || []), subscriptionId],
|
|
248
|
-
},
|
|
249
|
-
},
|
|
250
|
-
}))
|
|
251
|
-
|
|
252
|
-
return () => {
|
|
253
|
-
setCleanupTimeout(() => {
|
|
254
|
-
state.set('removeSubscription', (prev: FetcherStoreState<TParams, TData>) => {
|
|
255
|
-
const entry = prev.stateByParams[key]
|
|
256
|
-
if (!entry) return prev
|
|
257
|
-
|
|
258
|
-
const newSubs = (entry.subscriptions || []).filter((id) => id !== subscriptionId)
|
|
259
|
-
if (newSubs.length === 0) {
|
|
260
|
-
return {stateByParams: omitProperty(prev.stateByParams, key)}
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
return {
|
|
264
|
-
stateByParams: {
|
|
265
|
-
...prev.stateByParams,
|
|
266
|
-
[key]: {
|
|
267
|
-
...entry,
|
|
268
|
-
subscriptions: newSubs,
|
|
269
|
-
},
|
|
270
|
-
},
|
|
271
|
-
}
|
|
272
|
-
})
|
|
273
|
-
}, stateExpirationDelay)
|
|
274
|
-
}
|
|
275
|
-
},
|
|
276
|
-
}),
|
|
277
|
-
)
|
|
278
|
-
|
|
279
|
-
const resolveState = bindActionGlobally(store, ({instance}, ...params: TParams) =>
|
|
280
|
-
firstValueFrom(getState(instance, ...params).observable.pipe(first((i) => i !== undefined))),
|
|
281
|
-
)
|
|
282
|
-
|
|
283
|
-
return {getState, resolveState}
|
|
284
|
-
}
|