@phnq/state
Advanced tools
Comparing version 1.10.6 to 1.10.7
@@ -148,3 +148,3 @@ import { ComponentType } from 'react'; | ||
} | ||
interface Options<E = unknown, P = unknown> { | ||
interface Options<S, E = unknown, P = unknown> { | ||
/** | ||
@@ -158,2 +158,3 @@ * Named imported state factories. | ||
mapProvider?: MapProvider<P>; | ||
deepCompare?: (keyof S)[]; | ||
} | ||
@@ -183,3 +184,3 @@ export declare type GetActions<S, A, P extends GenericObject = GenericObject, E extends GenericObject = GenericObject> = (getActionsParams: GetActionsParams<S, E> & P) => Actions<S, A>; | ||
*/ | ||
export declare function createState<S extends object, A extends VoidActions<A>, P extends GenericObject = GenericObject, E extends GenericObject = GenericObject, PR extends keyof S = never>(name: string, options: Options<E, P>, defaultState: State<S>, getActions: GetActions<S, A, P, E>): StateFactory<S, A, PR>; | ||
export declare function createState<S extends object, A extends VoidActions<A>, P extends GenericObject = GenericObject, E extends GenericObject = GenericObject, PR extends keyof S = never>(name: string, options: Options<S, E, P>, defaultState: State<S>, getActions: GetActions<S, A, P, E>): StateFactory<S, A, PR>; | ||
declare global { | ||
@@ -186,0 +187,0 @@ interface Window { |
25
index.js
@@ -34,5 +34,9 @@ "use strict"; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createState = exports.setAllowProxyUsage = void 0; | ||
const log_1 = require("@phnq/log"); | ||
const fast_deep_equal_1 = __importDefault(require("fast-deep-equal")); | ||
const react_1 = __importStar(require("react")); | ||
@@ -45,5 +49,6 @@ const log = (0, log_1.createLogger)('@phnq/state'); | ||
exports.setAllowProxyUsage = setAllowProxyUsage; | ||
let invokationContext = undefined; | ||
function createState(name, ...args) { | ||
const [options, defaultState, getActions] = (args.length === 2 ? [{}, ...args] : args); | ||
const { imported: importedStates, mapProvider } = options; | ||
const { imported: importedStates, mapProvider, deepCompare = [] } = options; | ||
const { stateDerivers, initialState } = processDefaultState(defaultState); | ||
@@ -104,3 +109,3 @@ const derivedProperties = stateDerivers.map(({ key }) => key); | ||
onChangeCount.current += 1; | ||
const changedKeys = Object.keys(deltaState).filter(k => deltaState[k] !== currentState[k]); | ||
const changedKeys = Object.keys(deltaState).filter(k => deepCompare.includes(k) ? !(0, fast_deep_equal_1.default)(deltaState[k], currentState[k]) : deltaState[k] !== currentState[k]); | ||
onChange(changedKeys, { prevState, source, viaExternal: !!source }); | ||
@@ -190,4 +195,5 @@ } | ||
const colorCat = colorize(name); | ||
log(`${colorCat.text} %cACTION%c - %s`, ...colorCat.args, 'font-weight:bold', 'font-weight:normal', k, ...args); | ||
setTimeout(() => __awaiter(this, void 0, void 0, function* () { | ||
log(`${colorCat.text} %cACTION%c - %s`, ...colorCat.args, 'font-weight:bold', 'font-weight:normal', `${String(k)}${invokationContext ? ` (via ${name}.${String(invokationContext.action)})` : ''}`, ...args); | ||
const invokeAction = () => __awaiter(this, void 0, void 0, function* () { | ||
invokationContext = { name, action: k }; | ||
try { | ||
@@ -210,3 +216,12 @@ const numSetStateCallsBefore = numSetStateCalls.current; | ||
} | ||
}), 0); | ||
finally { | ||
invokationContext = undefined; | ||
} | ||
}); | ||
if (implicitActionNames.includes(k) || invokationContext) { | ||
invokeAction(); | ||
} | ||
else { | ||
setTimeout(invokeAction, 0); | ||
} | ||
})); | ||
@@ -213,0 +228,0 @@ }); |
{ | ||
"author": "Patrick Gostovic", | ||
"dependencies": { | ||
"@phnq/log": "^1.1.4" | ||
"@phnq/log": "^1.1.4", | ||
"fast-deep-equal": "^3.1.3" | ||
}, | ||
@@ -25,3 +26,3 @@ "peerDependencies": { | ||
}, | ||
"version": "1.10.6" | ||
"version": "1.10.7" | ||
} |
38742
612
3
+ Addedfast-deep-equal@^3.1.3
+ Addedfast-deep-equal@3.1.3(transitive)