-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathindex.d.ts
315 lines (291 loc) · 7.73 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import type { SyncMapValues } from '@logux/actions'
import type { Action, Meta } from '@logux/core'
import type { MapCreator, MapStore, ReadableAtom, StoreValue } from 'nanostores'
import type { Client } from '../client/index.js'
import type { FilterValue, LoadedFilterValue } from '../create-filter/index.js'
interface SyncMapStoreExt {
/**
* Logux Client instance.
*/
readonly client: Client
/**
* Meta from create action if the store was created locally.
*/
createdAt?: Meta
/**
* Mark that store was deleted.
*/
deleted?: true
/**
* While store is loading initial data from server or log.
*/
readonly loading: Promise<void>
/**
* Does store keep data in the log after store is destroyed.
*/
offline: boolean
/**
* Name of map class.
*/
readonly plural: string
/**
* Does store use server to load and save data.
*/
remote: boolean
}
export type LoadedSyncMapValue<Value extends SyncMapValues> = {
id: string
isLoading: false
} & Value
export type SyncMapValue<Value extends SyncMapValues> =
| { id: string; isLoading: true }
| LoadedSyncMapValue<Value>
export type SyncMapStore<Value extends SyncMapValues = any> = MapStore<
SyncMapValue<Value>
> &
SyncMapStoreExt
export interface SyncMapTemplate<
Value extends SyncMapValues = any,
StoreExt = {}
> extends MapCreator {
(
id: string,
client: Client,
...args: [] | [Action, Meta, boolean | undefined]
): StoreExt & SyncMapStore<Value>
cache: {
[id: string]: StoreExt & SyncMapStore<Value>
}
offline: boolean
readonly plural: string
remote: boolean
}
export interface SyncMapTemplateLike<
Value extends object = any,
Args extends any[] = []
> {
(id: string, client: Client, ...args: Args): MapStore<Value>
}
/**
* CRDT LWW Map. It can use server validation or be fully offline.
*
* The best option for classic case with server and many clients.
* Store will resolve client’s edit conflicts with last write wins strategy.
*
* ```ts
* import { syncMapTemplate } from '@logux/client'
*
* export const User = syncMapTemplate<{
* login: string,
* name?: string,
* isAdmin: boolean
* }>('users')
* ```
*
* @param plural Plural store name. It will be used in action type
* and channel name.
* @param opts Options to disable server validation or keep actions in log
* for offline support.
*/
export function syncMapTemplate<Value extends SyncMapValues>(
plural: string,
opts?: {
offline?: boolean
remote?: boolean
}
): SyncMapTemplate<Value>
/**
* Send create action to the server or to the log.
*
* Server will create a row in database on this action. {@link FilterStore}
* will update the list.
*
* ```js
* import { createSyncMap } from '@logux/client'
*
* showLoader()
* await createSyncMap(client, User, {
* id: nanoid(),
* login: 'test'
* })
* hideLoader()
* ```
*
* @param client Logux Client instance.
* @param Template Store template from {@link syncMapTemplate}.
* @param value Initial value.
* @return Promise until server validation for remote classes
* or saving action to the log of fully offline classes.
*/
export function createSyncMap<Value extends SyncMapValues>(
client: Client,
Template: SyncMapTemplate<Value>,
value: { id: string } & Value
): Promise<void>
/**
* Send create action and build store instance.
*
* ```js
* import { buildNewSyncMap } from '@logux/client'
*
* let userStore = buildNewSyncMap(client, User, {
* id: nanoid(),
* login: 'test'
* })
* ```
*
* @param client Logux Client instance.
* @param Template Store template from {@link syncMapTemplate}.
* @param value Initial value.
* @return Promise with store instance.
*/
export function buildNewSyncMap<Value extends SyncMapValues>(
client: Client,
Template: SyncMapTemplate<Value>,
value: { id: string } & Value
): Promise<SyncMapStore<Value>>
/**
* Change store without store instance just by store ID.
*
* ```js
* import { changeSyncMapById } from '@logux/client'
*
* let userStore = changeSyncMapById(client, User, 'user:4hs2jd83mf', {
* name: 'New name'
* })
* ```
*
* @param client Logux Client instance.
* @param Template Store template from {@link syncMapTemplate}.
* @param id Store’s ID.
* @param diff Store’s changes.
* @return Promise until server validation for remote classes
* or saving action to the log of fully offline classes.
*/
export function changeSyncMapById<Value extends SyncMapValues>(
client: Client,
Template: SyncMapTemplate<Value>,
id: string,
diff: Partial<Value>
): Promise<void>
export function changeSyncMapById<
Value extends SyncMapValues,
ValueKey extends keyof Value
>(
client: Client,
Template: SyncMapTemplate<Value>,
id: string,
key: ValueKey,
value: Value[ValueKey]
): Promise<void>
/**
* Change keys in the store’s value.
*
* ```js
* import { changeSyncMap } from '@logux/client'
*
* showLoader()
* await changeSyncMap(userStore, { name: 'New name' })
* hideLoader()
* ```
*
* @param store Store’s instance.
* @param diff Store’s changes.
* @return Promise until server validation for remote classes
* or saving action to the log of fully offline classes.
*/
export function changeSyncMap<Value extends SyncMapValues>(
store: SyncMapStore<Value>,
diff: Partial<Omit<Value, 'id'>>
): Promise<void>
export function changeSyncMap<
Value extends SyncMapValues,
ValueKey extends Exclude<keyof Value, 'id'>
>(
store: SyncMapStore<Value>,
key: ValueKey,
value: Value[ValueKey]
): Promise<void>
/**
* Delete store without store instance just by store ID.
*
* ```js
* import { deleteSyncMapById } from '@logux/client'
*
* showLoader()
* await deleteSyncMapById(client, User, 'user:4hs2jd83mf')
* ```
*
* @param client Logux Client instance.
* @param Template Store template from {@link syncMapTemplate}.
* @param id Store’s ID.
* @return Promise until server validation for remote classes
* or saving action to the log of fully offline classes.
*/
export function deleteSyncMapById(
client: Client,
Template: SyncMapTemplate,
id: string
): Promise<void>
/**
* Delete store.
*
* ```js
* import { deleteSyncMap } from '@logux/client'
*
* showLoader()
* await deleteSyncMap(User)
* ```
*
* @param store Store’s instance.
* @return Promise until server validation for remote classes
* or saving action to the log of fully offline classes.
*/
export function deleteSyncMap(store: SyncMapStore): Promise<void>
/**
* Change store’s value type to value with `isLoaded: false`.
*
* If store is still loading, this function will trow an error.
*
* Use it for tests written on TypeScript.
*
* ```js
* import { ensureLoaded } from '@logux/client'
*
* expect(ensureLoaded($currentUser)).toEqual({ id: 1, name: 'User' })
* ```
*
* @param value Store’s value.
*/
export function ensureLoaded<Value extends SyncMapValues>(
value: SyncMapValue<Value>
): LoadedSyncMapValue<Value>
export function ensureLoaded<Value extends SyncMapValues>(
value: FilterValue<Value>
): LoadedFilterValue<Value>
export type LoadedValue<Type extends { isLoading: boolean }> = {
isLoading: false
} & Type
type LoadableStore = {
readonly loading: Promise<unknown>
} & ReadableAtom<{ isLoading: boolean }>
/**
* Return store’s value if store is loaded or wait until store will be loaded
* and return its value.
*
* Returns `undefined` on 404.
*
* ```js
* import { loadValue } from '@logux/client'
*
* let user = loadValue($currentUser)
* ```
*
* @param store Store to load.
*/
export function loadValue<Store extends SyncMapStore>(
store: Store
): Promise<LoadedValue<StoreValue<Store>> | undefined>
export function loadValue<Store extends LoadableStore>(
store: Store
): Promise<LoadedValue<StoreValue<Store>>>