Skip to content

Commit

Permalink
Added Draft<T> type wrapper to draft state in update function to allo…
Browse files Browse the repository at this point in the history
…w updates to readonly types
  • Loading branch information
lostpebble committed Jun 30, 2020
1 parent fbe617f commit 3e545b1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pullstate",
"version": "1.15.2",
"version": "1.15.3",
"description": "Simple state stores using immer and React hooks",
"main": "dist/index.js",
"module": "dist/index.es.js",
Expand Down
16 changes: 8 additions & 8 deletions src/Store.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @ts-ignore
import { applyPatches, enablePatches, Patch, PatchListener, produce, produceWithPatches } from "immer";
import { applyPatches, Draft, enablePatches, Patch, PatchListener, produce, produceWithPatches } from "immer";
import { useStoreState } from "./useStoreState";
import { DeepKeyOfArray, TAllPathsParameter } from "./useStoreStateOpt-types";
import { DeepKeyOfArray } from "./useStoreStateOpt-types";

import isEqual from "fast-deep-equal/es6";

Expand All @@ -23,8 +23,8 @@ export interface IStoreInternalOptions<S> {
reactionCreators?: TReactionCreator<S>[];
}

export type TUpdateFunction<S> = (draft: S, original: S) => void;
type TPathReactionFunction<S> = (paths: TAllPathsParameter<S>, draft: S, original: S) => void;
export type TUpdateFunction<S> = (draft: Draft<S>, original: S) => void;
// type TPathReactionFunction<S> = (paths: TAllPathsParameter<S>, draft: S, original: S) => void;
type TReactionFunction<S, T> = (watched: T, draft: S, original: S, previousWatched: T) => void;
type TRunReactionFunction = (forceRun?: boolean) => string[];
type TRunSubscriptionFunction = () => void;
Expand Down Expand Up @@ -405,10 +405,10 @@ function runUpdates<S>(
func: boolean
): [S, Patch[], Patch[]] {
return func
? (produceWithPatches(currentState, (s: S) => (updater as TUpdateFunction<S>)(s, currentState)) as any)
? (produceWithPatches(currentState, (s: S) => (updater as TUpdateFunction<S>)(s as Draft<S>, currentState)) as any)
: ((updater as TUpdateFunction<S>[]).reduce(
([nextState, patches, inversePatches], currentValue) => {
const resp = produceWithPatches(nextState as any, (s: S) => currentValue(s, nextState)) as any;
const resp = produceWithPatches(nextState as any, (s: S) => currentValue(s as Draft<S>, nextState)) as any;
patches.push(...resp[1]);
inversePatches.push(...resp[2]);
return [resp[0], patches, inversePatches];
Expand Down Expand Up @@ -455,9 +455,9 @@ export function update<S = any>(
} else {
nextState = produce(currentState as any, (s: S) =>
func
? (updater as TUpdateFunction<S>)(s, currentState)
? (updater as TUpdateFunction<S>)(s as Draft<S>, currentState)
: (updater as TUpdateFunction<S>[]).reduce((previousValue, currentUpdater) => {
return produce(previousValue as any, (s: S) => currentUpdater(s, previousValue)) as any;
return produce(previousValue as any, (s: S) => currentUpdater(s as Draft<S>, previousValue)) as any;
}, currentState)
) as any;
}
Expand Down

0 comments on commit 3e545b1

Please sign in to comment.