-
-
Notifications
You must be signed in to change notification settings - Fork 134
Expand file tree
/
Copy pathwithEntityPlugin.ts
More file actions
35 lines (33 loc) · 981 Bytes
/
Copy pathwithEntityPlugin.ts
File metadata and controls
35 lines (33 loc) · 981 Bytes
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
import type {Setter} from 'solid-js';
import type {GenericStoreApi} from 'statebuilder';
import {makePlugin} from 'statebuilder';
export function withEntityPlugin<T extends unknown[]>() {
type TItem = T[number];
return makePlugin(
(storeApi: GenericStoreApi<T, Setter<T>>) => {
return {
entity: {
updateBy(
matcherFn: (data: TItem) => boolean,
updaterFn: (oldValue: TItem) => TItem,
) {
return storeApi.set(items => {
return items.map(item => {
if (matcherFn(item)) {
return updaterFn(item);
}
return item as TItem;
}) as T;
});
},
removeBy(matcherFn: (data: TItem) => boolean) {
return storeApi.set(
items => items.filter(item => !matcherFn(item)) as T,
);
},
},
};
},
{name: 'withEntityPlugin'},
);
}