Comparing version 1.3.2 to 1.4.0
@@ -0,1 +1,8 @@ | ||
## 1.4.0 | ||
* Added the ability to listen for change patches on an entire store, using `Store.listenToPatches(patchListener)`. | ||
* Fixed a bug where applying patches to stores didn't trigger the new optimized updates. | ||
* Fixed bug with Reactions running twice | ||
### 1.3.1 | ||
@@ -2,0 +9,0 @@ |
@@ -63,2 +63,3 @@ import React,{useState,useRef,useEffect,useContext,useMemo}from'react';import produce$1 from'immer';const isEqual = require("fast-deep-equal"); | ||
const produce = Immer.produce; | ||
const produceWithPatches = Immer.produceWithPatches; | ||
const applyPatches = Immer.applyPatches; | ||
@@ -77,3 +78,3 @@ function makeSubscriptionFunction(store, watch, listener) { | ||
function makeReactionFunctionCreator(watch, reaction) { | ||
return (store) => { | ||
return store => { | ||
let lastWatchState = watch(store.getRawState()); | ||
@@ -85,13 +86,23 @@ return () => { | ||
if (store._optListenerCount > 0) { | ||
let changePatches; | ||
store._updateStateWithoutReaction(produce(currentState, s => reaction(nextWatchState, s, currentState, lastWatchState), (patches, inversePatches) => { | ||
changePatches = patches; | ||
})); | ||
const [nextState, patches, inversePatches] = produceWithPatches(currentState, s => reaction(nextWatchState, s, currentState, lastWatchState)); | ||
store._updateStateWithoutReaction(nextState); | ||
lastWatchState = nextWatchState; | ||
if (changePatches.length > 0) { | ||
return getChangedPathsFromPatches(changePatches); | ||
if (patches.length > 0) { | ||
store._patchListeners.forEach(listener => listener(patches, inversePatches)); | ||
return getChangedPathsFromPatches(patches); | ||
} | ||
} | ||
store._updateStateWithoutReaction(produce(currentState, s => reaction(nextWatchState, s, currentState, lastWatchState))); | ||
lastWatchState = nextWatchState; | ||
else { | ||
if (store._patchListeners.length > 0) { | ||
const [nextState, patches, inversePatches] = produceWithPatches(currentState, s => reaction(nextWatchState, s, currentState, lastWatchState)); | ||
if (patches.length > 0) { | ||
store._patchListeners.forEach(listener => listener(patches, inversePatches)); | ||
} | ||
store._updateStateWithoutReaction(nextState); | ||
} | ||
else { | ||
store._updateStateWithoutReaction(produce(currentState, s => reaction(nextWatchState, s, currentState, lastWatchState))); | ||
} | ||
lastWatchState = nextWatchState; | ||
} | ||
} | ||
@@ -114,2 +125,3 @@ return []; | ||
this._optListenerCount = 0; | ||
this._patchListeners = []; | ||
this.currentState = initialState; | ||
@@ -191,2 +203,8 @@ this.initialState = initialState; | ||
} | ||
listenToPatches(patchListener) { | ||
this._patchListeners.push(patchListener); | ||
return () => { | ||
this._patchListeners = this._patchListeners.filter(f => f !== patchListener); | ||
}; | ||
} | ||
subscribe(watch, listener) { | ||
@@ -230,3 +248,3 @@ if (!this.ssr) { | ||
if (nextState !== currentState) { | ||
store._updateState(nextState); | ||
store._updateState(nextState, getChangedPathsFromPatches(patches)); | ||
} | ||
@@ -253,15 +271,26 @@ } | ||
if (store._optListenerCount > 0) { | ||
let changePatches; | ||
const nextState = produce(currentState, s => updater(s, currentState), (patches, inversePatches) => { | ||
const [nextState, patches, inversePatches] = produceWithPatches(currentState, s => updater(s, currentState)); | ||
if (patches.length > 0) { | ||
if (patchesCallback) { | ||
patchesCallback(patches, inversePatches); | ||
} | ||
changePatches = patches; | ||
}); | ||
if (changePatches.length > 0) { | ||
store._updateState(nextState, getChangedPathsFromPatches(changePatches)); | ||
store._patchListeners.forEach(listener => listener(patches, inversePatches)); | ||
store._updateState(nextState, getChangedPathsFromPatches(patches)); | ||
} | ||
} | ||
else { | ||
const nextState = produce(currentState, s => updater(s, currentState), patchesCallback); | ||
let nextState; | ||
if (store._patchListeners.length > 0 || patchesCallback) { | ||
const [ns, patches, inversePatches] = produceWithPatches(currentState, s => updater(s, currentState)); | ||
if (patches.length > 0) { | ||
if (patchesCallback) { | ||
patchesCallback(patches, inversePatches); | ||
} | ||
store._patchListeners.forEach(listener => listener(patches, inversePatches)); | ||
} | ||
nextState = ns; | ||
} | ||
else { | ||
nextState = produce(currentState, s => updater(s, currentState)); | ||
} | ||
if (nextState !== currentState) { | ||
@@ -268,0 +297,0 @@ store._updateState(nextState); |
@@ -63,2 +63,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),produce$1=_interopDefault(require('immer'));const isEqual = require("fast-deep-equal"); | ||
const produce = Immer.produce; | ||
const produceWithPatches = Immer.produceWithPatches; | ||
const applyPatches = Immer.applyPatches; | ||
@@ -77,3 +78,3 @@ function makeSubscriptionFunction(store, watch, listener) { | ||
function makeReactionFunctionCreator(watch, reaction) { | ||
return (store) => { | ||
return store => { | ||
let lastWatchState = watch(store.getRawState()); | ||
@@ -85,13 +86,23 @@ return () => { | ||
if (store._optListenerCount > 0) { | ||
let changePatches; | ||
store._updateStateWithoutReaction(produce(currentState, s => reaction(nextWatchState, s, currentState, lastWatchState), (patches, inversePatches) => { | ||
changePatches = patches; | ||
})); | ||
const [nextState, patches, inversePatches] = produceWithPatches(currentState, s => reaction(nextWatchState, s, currentState, lastWatchState)); | ||
store._updateStateWithoutReaction(nextState); | ||
lastWatchState = nextWatchState; | ||
if (changePatches.length > 0) { | ||
return getChangedPathsFromPatches(changePatches); | ||
if (patches.length > 0) { | ||
store._patchListeners.forEach(listener => listener(patches, inversePatches)); | ||
return getChangedPathsFromPatches(patches); | ||
} | ||
} | ||
store._updateStateWithoutReaction(produce(currentState, s => reaction(nextWatchState, s, currentState, lastWatchState))); | ||
lastWatchState = nextWatchState; | ||
else { | ||
if (store._patchListeners.length > 0) { | ||
const [nextState, patches, inversePatches] = produceWithPatches(currentState, s => reaction(nextWatchState, s, currentState, lastWatchState)); | ||
if (patches.length > 0) { | ||
store._patchListeners.forEach(listener => listener(patches, inversePatches)); | ||
} | ||
store._updateStateWithoutReaction(nextState); | ||
} | ||
else { | ||
store._updateStateWithoutReaction(produce(currentState, s => reaction(nextWatchState, s, currentState, lastWatchState))); | ||
} | ||
lastWatchState = nextWatchState; | ||
} | ||
} | ||
@@ -114,2 +125,3 @@ return []; | ||
this._optListenerCount = 0; | ||
this._patchListeners = []; | ||
this.currentState = initialState; | ||
@@ -191,2 +203,8 @@ this.initialState = initialState; | ||
} | ||
listenToPatches(patchListener) { | ||
this._patchListeners.push(patchListener); | ||
return () => { | ||
this._patchListeners = this._patchListeners.filter(f => f !== patchListener); | ||
}; | ||
} | ||
subscribe(watch, listener) { | ||
@@ -230,3 +248,3 @@ if (!this.ssr) { | ||
if (nextState !== currentState) { | ||
store._updateState(nextState); | ||
store._updateState(nextState, getChangedPathsFromPatches(patches)); | ||
} | ||
@@ -253,15 +271,26 @@ } | ||
if (store._optListenerCount > 0) { | ||
let changePatches; | ||
const nextState = produce(currentState, s => updater(s, currentState), (patches, inversePatches) => { | ||
const [nextState, patches, inversePatches] = produceWithPatches(currentState, s => updater(s, currentState)); | ||
if (patches.length > 0) { | ||
if (patchesCallback) { | ||
patchesCallback(patches, inversePatches); | ||
} | ||
changePatches = patches; | ||
}); | ||
if (changePatches.length > 0) { | ||
store._updateState(nextState, getChangedPathsFromPatches(changePatches)); | ||
store._patchListeners.forEach(listener => listener(patches, inversePatches)); | ||
store._updateState(nextState, getChangedPathsFromPatches(patches)); | ||
} | ||
} | ||
else { | ||
const nextState = produce(currentState, s => updater(s, currentState), patchesCallback); | ||
let nextState; | ||
if (store._patchListeners.length > 0 || patchesCallback) { | ||
const [ns, patches, inversePatches] = produceWithPatches(currentState, s => updater(s, currentState)); | ||
if (patches.length > 0) { | ||
if (patchesCallback) { | ||
patchesCallback(patches, inversePatches); | ||
} | ||
store._patchListeners.forEach(listener => listener(patches, inversePatches)); | ||
} | ||
nextState = ns; | ||
} | ||
else { | ||
nextState = produce(currentState, s => updater(s, currentState)); | ||
} | ||
if (nextState !== currentState) { | ||
@@ -268,0 +297,0 @@ store._updateState(nextState); |
@@ -1,2 +0,2 @@ | ||
import { Patch } from "immer"; | ||
import { Patch, PatchListener } from "immer"; | ||
import { DeepKeyOfArray } from "./useStoreStateOpt-types"; | ||
@@ -24,2 +24,3 @@ export declare type TPullstateUpdateListener = () => void; | ||
_optListenerCount: number; | ||
_patchListeners: PatchListener[]; | ||
constructor(initialState: S); | ||
@@ -36,2 +37,3 @@ _setInternalOptions({ ssr, reactionCreators }: IStoreInternalOptions<S>): void; | ||
_removeUpdateListenerOpt(ordKey: string): void; | ||
listenToPatches(patchListener: PatchListener): () => void; | ||
subscribe<T>(watch: (state: S) => T, listener: (watched: T, allState: S, previousWatched: T) => void): () => void; | ||
@@ -38,0 +40,0 @@ createReaction<T>(watch: (state: S) => T, reaction: TReactionFunction<S, T>): () => void; |
{ | ||
"name": "pullstate", | ||
"version": "1.3.2", | ||
"version": "1.4.0", | ||
"description": "Simple state stores using immer and React hooks", | ||
@@ -44,3 +44,3 @@ "main": "dist/index.js", | ||
"in-publish": "^2.0.0", | ||
"immer": "^3.1.1", | ||
"immer": "^3.2.0", | ||
"jest": "24.8.0", | ||
@@ -68,4 +68,4 @@ "jest-environment-jsdom": "24.8.0", | ||
"react": "^16.8.2", | ||
"immer": "^3.1.1" | ||
"immer": "^3.2.0" | ||
} | ||
} |
109211
2152