@stencil/store
Advanced tools
Comparing version 1.2.0 to 1.3.0
@@ -72,3 +72,3 @@ 'use strict'; | ||
const createObservableMap = (defaultState) => { | ||
const createObservableMap = (defaultState, shouldUpdate = (a, b) => a !== b) => { | ||
let states = new Map(Object.entries(defaultState !== null && defaultState !== void 0 ? defaultState : {})); | ||
@@ -97,3 +97,3 @@ const handlers = { | ||
const oldValue = states.get(propName); | ||
if (oldValue !== value || typeof value === 'object') { | ||
if (shouldUpdate(value, oldValue, propName)) { | ||
states.set(propName, value); | ||
@@ -174,4 +174,4 @@ handlers.set.forEach((cb) => cb(propName, value, oldValue)); | ||
const createStore = (defaultState) => { | ||
const map = createObservableMap(defaultState); | ||
const createStore = (defaultState, shouldUpdate) => { | ||
const map = createObservableMap(defaultState, shouldUpdate); | ||
stencilSubscription(map); | ||
@@ -178,0 +178,0 @@ return map; |
import { ObservableMap } from './types'; | ||
export declare const createObservableMap: <T extends { | ||
[key: string]: any; | ||
}>(defaultState?: T) => ObservableMap<T>; | ||
}>(defaultState?: T, shouldUpdate?: (newV: any, oldValue: any, prop: keyof T) => boolean) => ObservableMap<T>; |
import { ObservableMap } from './types'; | ||
export declare const createStore: <T extends { | ||
[key: string]: any; | ||
}>(defaultState?: T) => ObservableMap<T>; | ||
}>(defaultState?: T, shouldUpdate?: (newV: any, oldValue: any, prop: keyof T) => boolean) => ObservableMap<T>; |
{ | ||
"name": "@stencil/store", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "Store is a lightweight shared state library by the StencilJS core team. Implements a simple key/value map that efficiently re-renders components when necessary.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -80,6 +80,14 @@ # @stencil/store | ||
### `createStore<T>(initialState)` | ||
### `createStore<T>(initialState?: T, shouldUpdate?)` | ||
Create a new store with the given initial state. The type is inferred from `initialState`, or can be passed as the generic type `T`. | ||
By default, store performs a exact comparison (`===`) between the new value, and the previous one in order to prevent unnecessary rerenders, however, this behaviour can be changed by providing a `shouldUpdate` function through the second argument. When this function returns `false`, the value won't be updated. By providing a custom `shouldUpdate()` function, applications can create their own fine-grained change detection logic, beyond the default `===`. This may be useful for certain use-cases to avoid any expensive re-rendering. | ||
```ts | ||
const shouldUpdate = (newValue, oldValue, propChanged) => { | ||
return JSON.stringify(newValue) !== JSON.stringify(oldValue); | ||
} | ||
``` | ||
Returns a `store` object with the following properties. | ||
@@ -86,0 +94,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
19822
141