сторами работать как с редаксом:
import {Readable, readable, derived} from 'svelte/store';
import {noop} from 'svelte/internal';
import {isNotEmptyArray} from './arrays';
type CombinedStoreValues<SR extends Record<string, Readable<any>>> = {
[K in keyof SR]: SR[K] extends Readable<infer T> ? T : never;
};
export function combineStores<SR extends Record<string, Readable<any>>>(storesRecord: SR): Readable<CombinedStoreValues<SR>> & SR {
const names: Array<keyof SR> = Object.keys(storesRecord);
const stores = names.map<Readable<SR[string]>>(name => storesRecord[name]);
if(!isNotEmptyArray(stores)) {
return Object.assign(
readable({} as CombinedStoreValues<SR>, noop),
storesRecord
);
}
const reducer = (acc: Partial<CombinedStoreValues<SR>>, value: SR[string], i: number) => ({
...acc,
[names[i]]: value
});
return Object.assign(
derived(stores, (values) => values.reduce(reducer, {}) as CombinedStoreValues<SR>),
storesRecord
);
}
Выше тут было...
Обсуждают сегодня