-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathstores.ts
More file actions
141 lines (128 loc) · 3.27 KB
/
stores.ts
File metadata and controls
141 lines (128 loc) · 3.27 KB
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
// Helpers to work with Nano Stores and Logux to simplify code
// by moving complexity to helper.
import type { Action } from '@logux/core'
import {
type MapStore,
onMount,
type ReadableAtom,
type StoreValue
} from 'nanostores'
import { client } from '../client.ts'
export type OptionalId<Value> = { id?: string } & Omit<Value, 'id'>
type NumberKeys<T> = {
[K in keyof T]: T[K] extends number ? K : never
}[keyof T]
export function increaseKey<Store extends MapStore>(
store: Store,
key: NumberKeys<StoreValue<Store>>
): void {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
let value = store.get()[key] as number
store.setKey(key, value + 1)
}
/**
* Return promise which wait until store stop to have `false`.
*
* It is useful in tests for stores like `page.loading`
* to avoid flaky `setTimeout`.
*/
export function waitLoading(store: ReadableAtom): Promise<void> {
return new Promise<void>(resolve => {
/* node:coverage ignore next 4 */
if (store.get() === false) {
resolve()
return
}
let unbind = store.subscribe(state => {
if (state === false) {
unbind()
resolve()
}
})
})
}
export type LoadedValue<Type extends { isLoading: boolean }> = {
isLoading: false
} & Type
export type LoadableStore = {
readonly loading: Promise<unknown>
} & ReadableAtom<{ isLoading: boolean }>
/**
* Return promise which wait until `store.isLoading` is stop to have `true`.
*/
export async function waitSyncLoading<Store extends LoadableStore>(
store: Store
): Promise<ReadableAtom<LoadedValue<StoreValue<Store>>>> {
let value = store.get()
if (value.isLoading) {
let unbind = store.listen(() => {})
try {
await store.loading
} finally {
unbind()
}
}
return store as unknown as ReadableAtom<LoadedValue<StoreValue<Store>>>
}
/**
* Run callback when store got first listener.
*
* It is like `onMount` in Nano Stores but work with multiple stores.
* Callback runs on first listener for any of store.
*/
export function onMountAny(stores: ReadableAtom[], cb: () => () => void): void {
let listeners = 0
let unbind = (): void => {}
function watching(): () => void {
listeners++
if (listeners === 1) {
unbind = cb()
}
return () => {
listeners--
if (listeners === 0) {
unbind()
}
}
}
for (let store of stores) {
onMount(store, () => watching())
}
}
/**
* Run callback on every Logux action.
*
* It hides complexity of client re-creating on sign-in to re-subscribing.
*/
export function onLogAction(cb: (action: Action) => void): () => void {
let unbindLog: (() => void) | undefined
let unbindClient = client.subscribe(loguxClient => {
unbindLog?.()
unbindLog = undefined
if (loguxClient) {
unbindLog = loguxClient.on('add', cb)
}
})
return () => {
unbindLog?.()
unbindClient()
}
}
/**
* Subscribe to store and run callback on every store’s change until callback
* return `true`.
*
* Abstraction to simplify complex code.
*/
export function subscribeUntil<Value>(
store: ReadableAtom<Value>,
cb: (value: Value) => boolean | undefined
): void {
if (!cb(store.get())) {
let unbind = store.listen(value => {
if (cb(value)) {
unbind()
}
})
}
}