Comparing version 1.13.1 to 1.13.2
@@ -0,1 +1,5 @@ | ||
### 1.13.2 | ||
More fixes for run() when using `respectCache: true`. Prevents the cacheBreakHook from clearing the current async state, even if the action hasn't finished yet. | ||
### 1.13.1 | ||
@@ -2,0 +6,0 @@ |
@@ -95,6 +95,6 @@ import isEqual from'fast-deep-equal/es6';import React,{useRef,useState,useEffect,useContext,useMemo}from'react';import produce$1,{produceWithPatches,produce,applyPatches}from'immer';function useStoreState(store, getSubState, deps) { | ||
let lastWatchState = watch(store.getRawState()); | ||
return () => { | ||
return (forceRun = false) => { | ||
const currentState = store.getRawState(); | ||
const nextWatchState = watch(currentState); | ||
if (!isEqual(nextWatchState, lastWatchState)) { | ||
if (forceRun || !isEqual(nextWatchState, lastWatchState)) { | ||
if (store._optListenerCount > 0) { | ||
@@ -235,3 +235,3 @@ const [nextState, patches, inversePatches] = produceWithPatches(currentState, (s) => reaction(nextWatchState, s, currentState, lastWatchState)); | ||
} | ||
createReaction(watch, reaction) { | ||
createReaction(watch, reaction, { runNow = false, runNowWithSideEffects = false } = {}) { | ||
const creator = makeReactionFunctionCreator(watch, reaction); | ||
@@ -241,2 +241,8 @@ this.reactionCreators.push(creator); | ||
this.reactions.push(func); | ||
if (runNow || runNowWithSideEffects) { | ||
func(true); | ||
if (runNowWithSideEffects && !this.ssr) { | ||
this._updateState(this.currentState); | ||
} | ||
} | ||
return () => { | ||
@@ -246,3 +252,2 @@ this.reactions = this.reactions.filter(f => f !== func); | ||
} | ||
createPathReaction(path, reaction) { } | ||
getRawState() { | ||
@@ -505,3 +510,4 @@ if (this.batchState !== undefined) { | ||
const cacheBreakLoop = cacheBreakWatcher.hasOwnProperty(key) && cacheBreakWatcher[key] > 2; | ||
if (cacheBreakEnabled && | ||
if (cache.results[key][1] && | ||
cacheBreakEnabled && | ||
cacheBreakHook !== undefined && | ||
@@ -508,0 +514,0 @@ cacheBreakHook({ |
@@ -95,6 +95,6 @@ 'use strict';Object.defineProperty(exports,'__esModule',{value:true});function _interopDefault(e){return(e&&(typeof e==='object')&&'default'in e)?e['default']:e}var isEqual=_interopDefault(require('fast-deep-equal/es6')),React=require('react'),React__default=_interopDefault(React),produce=require('immer'),produce__default=_interopDefault(produce);function useStoreState(store, getSubState, deps) { | ||
let lastWatchState = watch(store.getRawState()); | ||
return () => { | ||
return (forceRun = false) => { | ||
const currentState = store.getRawState(); | ||
const nextWatchState = watch(currentState); | ||
if (!isEqual(nextWatchState, lastWatchState)) { | ||
if (forceRun || !isEqual(nextWatchState, lastWatchState)) { | ||
if (store._optListenerCount > 0) { | ||
@@ -235,3 +235,3 @@ const [nextState, patches, inversePatches] = produce.produceWithPatches(currentState, (s) => reaction(nextWatchState, s, currentState, lastWatchState)); | ||
} | ||
createReaction(watch, reaction) { | ||
createReaction(watch, reaction, { runNow = false, runNowWithSideEffects = false } = {}) { | ||
const creator = makeReactionFunctionCreator(watch, reaction); | ||
@@ -241,2 +241,8 @@ this.reactionCreators.push(creator); | ||
this.reactions.push(func); | ||
if (runNow || runNowWithSideEffects) { | ||
func(true); | ||
if (runNowWithSideEffects && !this.ssr) { | ||
this._updateState(this.currentState); | ||
} | ||
} | ||
return () => { | ||
@@ -246,3 +252,2 @@ this.reactions = this.reactions.filter(f => f !== func); | ||
} | ||
createPathReaction(path, reaction) { } | ||
getRawState() { | ||
@@ -503,3 +508,4 @@ if (this.batchState !== undefined) { | ||
const cacheBreakLoop = cacheBreakWatcher.hasOwnProperty(key) && cacheBreakWatcher[key] > 2; | ||
if (cacheBreakEnabled && | ||
if (cache.results[key][1] && | ||
cacheBreakEnabled && | ||
cacheBreakHook !== undefined && | ||
@@ -506,0 +512,0 @@ cacheBreakHook({ |
import { Patch, PatchListener } from "immer"; | ||
import { DeepKeyOfArray, TAllPathsParameter } from "./useStoreStateOpt-types"; | ||
import { DeepKeyOfArray } from "./useStoreStateOpt-types"; | ||
export declare type TPullstateUpdateListener = () => void; | ||
@@ -9,6 +9,9 @@ export interface IStoreInternalOptions<S> { | ||
export declare type TUpdateFunction<S> = (draft: S, original: S) => void; | ||
declare type TPathReactionFunction<S> = (paths: TAllPathsParameter<S>, draft: S, original: S) => void; | ||
declare type TReactionFunction<S, T> = (watched: T, draft: S, original: S, previousWatched: T) => void; | ||
declare type TRunReactionFunction = () => string[]; | ||
declare type TRunReactionFunction = (forceRun?: boolean) => string[]; | ||
declare type TReactionCreator<S> = (store: Store<S>) => TRunReactionFunction; | ||
interface ICreateReactionOptions { | ||
runNow?: boolean; | ||
runNowWithSideEffects?: boolean; | ||
} | ||
export declare type TStoreActionUpdate<S> = (updater: TUpdateFunction<S> | TUpdateFunction<S>[], patchesCallback?: (patches: Patch[], inversePatches: Patch[]) => void) => void; | ||
@@ -43,4 +46,3 @@ export declare type TStoreAction<S> = (update: TStoreActionUpdate<S>) => void; | ||
subscribe<T>(watch: (state: S) => T, listener: (watched: T, allState: S, previousWatched: T) => void): () => void; | ||
createReaction<T>(watch: (state: S) => T, reaction: TReactionFunction<S, T>): () => void; | ||
createPathReaction<T>(path: TAllPathsParameter<S>, reaction: TPathReactionFunction<S>): void; | ||
createReaction<T>(watch: (state: S) => T, reaction: TReactionFunction<S, T>, { runNow, runNowWithSideEffects }?: ICreateReactionOptions): () => void; | ||
getRawState(): S; | ||
@@ -47,0 +49,0 @@ useState(): S; |
{ | ||
"name": "pullstate", | ||
"version": "1.13.1", | ||
"version": "1.13.2", | ||
"description": "Simple state stores using immer and React hooks", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
144139
2748