UNPKG

@runicjs/runic

Version:

A lightweight, type-safe vanilla JS state management library

39 lines (38 loc) 1.27 kB
import { create } from 'mutative'; import updateStatesWithProducer from '../utils/updateStatesWithProducer'; /** * Update the store using Mutative. * @param store - The store to update. * @param recipe - The function to use to update the store. */ export const updateState = (store, recipe) => { store.setState(create(store.getState(), recipe)); }; /** * Update multiple stores simultaneously using Mutative. * * Example: * type NumState = { num: number }; * type NumsState = { nums: number[] }; * const numStore1 = createStore<NumState>({ num: 1 }); * const numsStore = createStore<NumsState>({ nums: [2, 3, 4] }); * const numStore3 = createStore<NumState>({ num: 5 }); * * // This: * updateStates([numStore1, numsStore, numStore3], ([num1, nums, num3]) => { * // Draft<NumState>, Draft<NumsState>, Draft<NumState> * console.log(num1, nums, num3); * }); * * // is equivalent to this: * updateState(numStore1, (num1) => { * updateState(numsStore, (nums) => { * updateState(numStore3, (num3) => { * console.log(num1, nums, num3); * }); * }); * }); */ export function updateStates(stores, fn) { return updateStatesWithProducer(stores, create, fn); }