@stencil/store
Advanced tools
+4
-4
@@ -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; |
+4
-4
@@ -68,3 +68,3 @@ import { getRenderingRef, forceUpdate } from '@stencil/core'; | ||
| const createObservableMap = (defaultState) => { | ||
| const createObservableMap = (defaultState, shouldUpdate = (a, b) => a !== b) => { | ||
| let states = new Map(Object.entries(defaultState !== null && defaultState !== void 0 ? defaultState : {})); | ||
@@ -93,3 +93,3 @@ const handlers = { | ||
| const oldValue = states.get(propName); | ||
| if (oldValue !== value || typeof value === 'object') { | ||
| if (shouldUpdate(value, oldValue, propName)) { | ||
| states.set(propName, value); | ||
@@ -170,4 +170,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); | ||
@@ -174,0 +174,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>; |
+1
-1
| 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>; |
+1
-1
| { | ||
| "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", |
+9
-1
@@ -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 @@ |
19822
4.97%141
6.02%