Comparing version 0.3.3 to 0.4.0
@@ -21,2 +21,3 @@ import React,{useState,useEffect,useRef,useContext,useMemo}from'react';const shallowEqual = require("fbjs/lib/shallowEqual"); | ||
const produce = Immer.produce; | ||
const applyPatches = Immer.applyPatches; | ||
class Store { | ||
@@ -50,12 +51,22 @@ constructor(initialState) { | ||
} | ||
update(updater) { | ||
update(this, updater); | ||
update(updater, patchesCallback) { | ||
update(this, updater, patchesCallback); | ||
} | ||
applyPatches(patches) { | ||
applyPatchesToStore(this, patches); | ||
} | ||
} | ||
function update(store, updater) { | ||
function applyPatchesToStore(store, patches) { | ||
const currentState = store.getRawState(); | ||
const nextState = produce(currentState, (s) => updater(s, currentState)); | ||
const nextState = applyPatches(currentState, patches); | ||
if (nextState !== currentState) { | ||
store._updateState(nextState); | ||
} | ||
} | ||
function update(store, updater, patchesCallback) { | ||
const currentState = store.getRawState(); | ||
const nextState = produce(currentState, (s) => updater(s, currentState), patchesCallback); | ||
if (nextState !== currentState) { | ||
store._updateState(nextState); | ||
} | ||
}function InjectStoreState({ store, on = s => s, children, }) { | ||
@@ -62,0 +73,0 @@ const state = useStoreState(store, on); |
@@ -21,2 +21,3 @@ 'use strict';Object.defineProperty(exports,'__esModule',{value:true});function _interopDefault(e){return(e&&(typeof e==='object')&&'default'in e)?e['default']:e}var React=require('react'),React__default=_interopDefault(React);const shallowEqual = require("fbjs/lib/shallowEqual"); | ||
const produce = Immer.produce; | ||
const applyPatches = Immer.applyPatches; | ||
class Store { | ||
@@ -50,12 +51,22 @@ constructor(initialState) { | ||
} | ||
update(updater) { | ||
update(this, updater); | ||
update(updater, patchesCallback) { | ||
update(this, updater, patchesCallback); | ||
} | ||
applyPatches(patches) { | ||
applyPatchesToStore(this, patches); | ||
} | ||
} | ||
function update(store, updater) { | ||
function applyPatchesToStore(store, patches) { | ||
const currentState = store.getRawState(); | ||
const nextState = produce(currentState, (s) => updater(s, currentState)); | ||
const nextState = applyPatches(currentState, patches); | ||
if (nextState !== currentState) { | ||
store._updateState(nextState); | ||
} | ||
} | ||
function update(store, updater, patchesCallback) { | ||
const currentState = store.getRawState(); | ||
const nextState = produce(currentState, (s) => updater(s, currentState), patchesCallback); | ||
if (nextState !== currentState) { | ||
store._updateState(nextState); | ||
} | ||
}function InjectStoreState({ store, on = s => s, children, }) { | ||
@@ -62,0 +73,0 @@ const state = useStoreState(store, on); |
@@ -0,1 +1,2 @@ | ||
import { Patch } from "immer"; | ||
export declare type TPullstateUpdateListener = () => void; | ||
@@ -17,4 +18,6 @@ export interface IStoreInternalOptions { | ||
getRawState(): S; | ||
update(updater: (state: S, original?: S) => void): void; | ||
update(updater: (state: S, original?: S) => void, patchesCallback?: (patches: Patch[], inversePatches: Patch[]) => void): void; | ||
applyPatches(patches: Patch[]): void; | ||
} | ||
export declare function update<S = any>(store: Store<S>, updater: (draft: S, original?: S) => void): void; | ||
export declare function applyPatchesToStore<S = any>(store: Store<S>, patches: Patch[]): void; | ||
export declare function update<S = any>(store: Store<S>, updater: (draft: S, original?: S) => void, patchesCallback?: (patches: Patch[], inversePatches: Patch[]) => void): void; |
{ | ||
"name": "pullstate", | ||
"version": "0.3.3", | ||
"version": "0.4.0", | ||
"description": "Simple state stores using immer and React hooks", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
66318
1012