feat: make zustand store logic ready for classes

This commit is contained in:
thecodrr
2020-03-28 13:51:18 +05:00
parent f0f17c3f76
commit 0e9fcaf542

View File

@@ -1,13 +1,25 @@
import produce from "immer";
import produce, { immerable } from "immer";
import create from "zustand";
function immer(config) {
return function(set, get, api) {
return config(fn => set(produce(fn)), get, api);
const obj = config(
fn =>
set(() =>
produce(get(), state => {
fn(state);
})
),
get,
api
);
obj[immerable] = true;
return obj;
};
}
function createStore(store) {
store = store.new ? store.new.bind(store) : store;
return create(immer(store));
}