value-enhancer
Advanced tools
Comparing version 3.1.1 to 3.1.2
@@ -1,2 +0,2 @@ | ||
import { R as ReadonlyVal, U as UnwrapVal } from './typings-c9d13ac0.js'; | ||
import { R as ReadonlyVal, U as UnwrapVal } from './typings-c0bbe2e6.js'; | ||
@@ -3,0 +3,0 @@ interface ReactiveCollection<TKey = any, TValue = any> { |
'use strict'; | ||
// src/utils.ts | ||
var defaultCompare = (newValue, oldValue) => newValue === oldValue; | ||
var defaultEqual = Object.is; | ||
var invoke = (fn, value) => { | ||
@@ -516,5 +516,5 @@ try { | ||
*/ | ||
constructor(get, { compare = defaultCompare, eager } = {}, start) { | ||
constructor(get, { equal, compare, eager } = {}, start) { | ||
this.get = get; | ||
this.compare = compare; | ||
this.compare = this.equal = equal || compare || defaultEqual; | ||
this._e = eager; | ||
@@ -527,2 +527,7 @@ this._s = new Subscribers(get, start); | ||
get; | ||
equal; | ||
/** | ||
* @ignore | ||
* @deprecated Use `equal` instead. | ||
*/ | ||
compare; | ||
@@ -592,3 +597,3 @@ reaction(subscriber, eager = this._e) { | ||
const value = getValue(); | ||
if (!this.compare(value, currentValue)) { | ||
if (!this.equal(value, currentValue)) { | ||
this._s.d = true; | ||
@@ -609,5 +614,6 @@ currentValue = value; | ||
super(get, config, () => { | ||
const disposer = listen(notify); | ||
currentValue = getValue(); | ||
dirty = notified = false; | ||
return listen(notify); | ||
return disposer; | ||
}); | ||
@@ -621,3 +627,3 @@ } | ||
constructor(getValue, listen, config) { | ||
const initialCompare = config?.compare; | ||
const initialEqual = config && (config.equal || config.compare); | ||
let currentValue = INIT_VALUE; | ||
@@ -640,3 +646,3 @@ let dirty = false; | ||
const value = computeValue(); | ||
if (!this.compare(value, currentValue)) { | ||
if (!this.equal(value, currentValue)) { | ||
this._s.d = true; | ||
@@ -656,3 +662,3 @@ currentValue = value; | ||
innerDisposer = innerVal && innerVal.$valCompute(notify); | ||
currentCompare = initialCompare || (innerVal ? innerVal.compare : defaultCompare); | ||
currentEqual = initialEqual || (innerVal ? innerVal.equal : defaultEqual); | ||
} | ||
@@ -668,5 +674,2 @@ }; | ||
super(get, config, () => { | ||
updateInnerVal(); | ||
currentValue = innerVal ? innerVal.value : innerMaybeVal; | ||
dirty = notified = false; | ||
const outerDisposer = listen(() => { | ||
@@ -676,2 +679,5 @@ updateInnerVal(); | ||
}); | ||
updateInnerVal(); | ||
currentValue = innerVal ? innerVal.value : innerMaybeVal; | ||
dirty = notified = false; | ||
return () => { | ||
@@ -682,4 +688,4 @@ innerDisposer?.(); | ||
}); | ||
let currentCompare = this.compare; | ||
this.compare = (newValue, oldValue) => currentCompare(newValue, oldValue); | ||
let currentEqual = this.equal; | ||
this.equal = (newValue, oldValue) => currentEqual(newValue, oldValue); | ||
} | ||
@@ -693,3 +699,3 @@ }; | ||
(notify) => collection.watch((k) => { | ||
if (!k || k === key) { | ||
if (k === key || k == null) { | ||
notify(); | ||
@@ -702,3 +708,3 @@ } | ||
(notify) => collection.watch((k) => { | ||
if (!k || k === key) { | ||
if (k === key || k == null) { | ||
notify(); | ||
@@ -705,0 +711,0 @@ } |
@@ -1,3 +0,3 @@ | ||
import { R as ReadonlyVal, V as ValInputsValueTuple, a as ValConfig, b as ValDisposer, c as ValSetValue, U as UnwrapVal, d as Val, e as ValSubscriber } from './typings-c9d13ac0.js'; | ||
export { f as ValCompare } from './typings-c9d13ac0.js'; | ||
import { R as ReadonlyVal, V as ValInputsValueTuple, a as ValConfig, b as ValDisposer, c as ValSetValue, U as UnwrapVal, d as Val, e as ValSubscriber } from './typings-c0bbe2e6.js'; | ||
export { f as ValCompare, g as ValEqual } from './typings-c0bbe2e6.js'; | ||
@@ -127,3 +127,3 @@ /** Returns the value passed in. */ | ||
* public $: Foo$; | ||
* private setVals: { [K in keyof Foo$]: ValSetValue<Foo$[K]> }; | ||
* private setVals: { [K in keyof Foo$]: ValSetValue<UnwrapVal<Foo$[K]>> }; | ||
* | ||
@@ -130,0 +130,0 @@ * public constructor() { |
@@ -5,3 +5,3 @@ 'use strict'; | ||
var identity = (value) => value; | ||
var defaultCompare = (newValue, oldValue) => newValue === oldValue; | ||
var defaultEqual = Object.is; | ||
var getValue = (val2) => val2.value; | ||
@@ -137,5 +137,5 @@ var getValues = (valInputs) => valInputs.map(getValue); | ||
*/ | ||
constructor(get, { compare = defaultCompare, eager } = {}, start) { | ||
constructor(get, { equal, compare, eager } = {}, start) { | ||
this.get = get; | ||
this.compare = compare; | ||
this.compare = this.equal = equal || compare || defaultEqual; | ||
this._e = eager; | ||
@@ -148,2 +148,7 @@ this._s = new Subscribers(get, start); | ||
get; | ||
equal; | ||
/** | ||
* @ignore | ||
* @deprecated Use `equal` instead. | ||
*/ | ||
compare; | ||
@@ -205,3 +210,3 @@ reaction(subscriber, eager = this._e) { | ||
const set = (value2) => { | ||
if (!val2.compare(value2, currentValue)) { | ||
if (!val2.equal(value2, currentValue)) { | ||
currentValue = value2; | ||
@@ -245,3 +250,3 @@ if (subs) { | ||
const value = getValue2(); | ||
if (!this.compare(value, currentValue)) { | ||
if (!this.equal(value, currentValue)) { | ||
this._s.d = true; | ||
@@ -262,5 +267,6 @@ currentValue = value; | ||
super(get, config, () => { | ||
const disposer = listen(notify); | ||
currentValue = getValue2(); | ||
dirty = notified = false; | ||
return listen(notify); | ||
return disposer; | ||
}); | ||
@@ -295,3 +301,3 @@ } | ||
constructor(getValue2, listen, config) { | ||
const initialCompare = config?.compare; | ||
const initialEqual = config && (config.equal || config.compare); | ||
let currentValue = INIT_VALUE; | ||
@@ -314,3 +320,3 @@ let dirty = false; | ||
const value = computeValue(); | ||
if (!this.compare(value, currentValue)) { | ||
if (!this.equal(value, currentValue)) { | ||
this._s.d = true; | ||
@@ -330,3 +336,3 @@ currentValue = value; | ||
innerDisposer = innerVal && innerVal.$valCompute(notify); | ||
currentCompare = initialCompare || (innerVal ? innerVal.compare : defaultCompare); | ||
currentEqual = initialEqual || (innerVal ? innerVal.equal : defaultEqual); | ||
} | ||
@@ -342,5 +348,2 @@ }; | ||
super(get, config, () => { | ||
updateInnerVal(); | ||
currentValue = innerVal ? innerVal.value : innerMaybeVal; | ||
dirty = notified = false; | ||
const outerDisposer = listen(() => { | ||
@@ -350,2 +353,5 @@ updateInnerVal(); | ||
}); | ||
updateInnerVal(); | ||
currentValue = innerVal ? innerVal.value : innerMaybeVal; | ||
dirty = notified = false; | ||
return () => { | ||
@@ -356,4 +362,4 @@ innerDisposer?.(); | ||
}); | ||
let currentCompare = this.compare; | ||
this.compare = (newValue, oldValue) => currentCompare(newValue, oldValue); | ||
let currentEqual = this.equal; | ||
this.equal = (newValue, oldValue) => currentEqual(newValue, oldValue); | ||
} | ||
@@ -378,3 +384,3 @@ }; | ||
this.set = (value) => { | ||
if (!this.compare(value, currentValue)) { | ||
if (!this.equal(value, currentValue)) { | ||
this._s.d = true; | ||
@@ -381,0 +387,0 @@ currentValue = value; |
{ | ||
"name": "value-enhancer", | ||
"version": "3.1.1", | ||
"version": "3.1.2", | ||
"private": false, | ||
@@ -5,0 +5,0 @@ "description": "A tiny library to enhance value with reactive wrapper.", |
@@ -254,3 +254,3 @@ # [value-enhancer](https://github.com/crimx/value-enhancer) | ||
// `===` equality check by default | ||
// `Object.is` equality check by default | ||
count$.set(3); // nothing happened | ||
@@ -343,5 +343,5 @@ | ||
## Custom Compare | ||
## Custom Equal | ||
By default, `===` equality check is used to determine whether a value has changed. You can customize the equality check by passing a `compare` function. | ||
By default, `Object.is` equality check is used to determine whether a value has changed. You can customize the equality check by passing a `equal` function. | ||
@@ -355,4 +355,4 @@ ```js | ||
const xyzPosition$ = val({ x: 0, y: 0, z: 0 }, { compare: isSameXYZPosition }); | ||
const xyPosition$ = derive(xyPosition, { compare: isSameXYPosition }); | ||
const xyzPosition$ = val({ x: 0, y: 0, z: 0 }, { equal: isSameXYZPosition }); | ||
const xyPosition$ = derive(xyPosition, { equal: isSameXYPosition }); | ||
@@ -388,2 +388,3 @@ xyPosition$.set({ x: 0, y: 0, z: 0 }); // nothing happened | ||
type ValSetValue, | ||
type UnwrapVal, | ||
readonlyVal, | ||
@@ -401,3 +402,3 @@ groupVals, | ||
public readonly $: Foo$; | ||
private setVals: { [K in keyof Foo$]: ValSetValue<Foo$[K]> }; | ||
private setVals: { [K in keyof Foo$]: ValSetValue<UnwrapVal<Foo$[K]>> }; | ||
@@ -404,0 +405,0 @@ public constructor() { |
@@ -37,3 +37,3 @@ import type { ReadonlyVal, UnwrapVal } from "../typings"; | ||
collection.watch(k => { | ||
if (!k || k === key) { | ||
if (k === key || k == null) { | ||
notify(); | ||
@@ -81,3 +81,3 @@ } | ||
collection.watch(k => { | ||
if (!k || k === key) { | ||
if (k === key || k == null) { | ||
notify(); | ||
@@ -84,0 +84,0 @@ } |
@@ -20,3 +20,3 @@ import { ReadonlyValImpl } from "./readonly-val"; | ||
const value = getValue(); | ||
if (!this.compare(value, currentValue)) { | ||
if (!this.equal(value, currentValue)) { | ||
this._subs_.dirty_ = true; | ||
@@ -39,5 +39,7 @@ currentValue = value; | ||
super(get, config, () => { | ||
// attach listener first so that upstream value is resolved | ||
const disposer = listen(notify); | ||
currentValue = getValue(); | ||
dirty = notified = false; | ||
return listen(notify); | ||
return disposer; | ||
}); | ||
@@ -44,0 +46,0 @@ } |
@@ -8,2 +8,3 @@ export type { | ||
ValDisposer, | ||
ValEqual, | ||
ValSetValue, | ||
@@ -10,0 +11,0 @@ ValSubscriber, |
@@ -11,3 +11,3 @@ import type { ValOnStart } from "./subscribers"; | ||
import { SubscriberMode, Subscribers } from "./subscribers"; | ||
import { defaultCompare, invoke } from "./utils"; | ||
import { defaultEqual, invoke } from "./utils"; | ||
@@ -33,7 +33,7 @@ /** | ||
get: () => TValue, | ||
{ compare = defaultCompare, eager }: ValConfig<TValue> = {}, | ||
{ equal, compare, eager }: ValConfig<TValue> = {}, | ||
start?: ValOnStart | ||
) { | ||
this.get = get; | ||
this.compare = compare; | ||
this.compare = this.equal = equal || compare || defaultEqual; | ||
this._eager_ = eager; | ||
@@ -49,2 +49,8 @@ this._subs_ = new Subscribers<TValue>(get, start); | ||
public equal: (this: void, newValue: TValue, oldValue: TValue) => boolean; | ||
/** | ||
* @ignore | ||
* @deprecated Use `equal` instead. | ||
*/ | ||
public compare: (this: void, newValue: TValue, oldValue: TValue) => boolean; | ||
@@ -148,3 +154,3 @@ | ||
const set = (value: TValue | undefined): void => { | ||
if (!val.compare(value, currentValue)) { | ||
if (!val.equal(value, currentValue)) { | ||
currentValue = value; | ||
@@ -197,3 +203,3 @@ if (subs) { | ||
* public $: Foo$; | ||
* private setVals: { [K in keyof Foo$]: ValSetValue<Foo$[K]> }; | ||
* private setVals: { [K in keyof Foo$]: ValSetValue<UnwrapVal<Foo$[K]>> }; | ||
* | ||
@@ -200,0 +206,0 @@ * public constructor() { |
@@ -6,3 +6,8 @@ export interface ReadonlyVal<TValue = any> { | ||
get(this: void): TValue; | ||
/** Compare two values. Default `===`. */ | ||
/** Compare two values. Default `Object.is`. */ | ||
equal(this: void, newValue: TValue, oldValue: TValue): boolean; | ||
/** | ||
* @ignore | ||
* @deprecated Use `equal` instead. | ||
*/ | ||
compare(this: void, newValue: TValue, oldValue: TValue): boolean; | ||
@@ -50,2 +55,11 @@ /** | ||
export type ValEqual<TValue = any> = ( | ||
newValue: TValue, | ||
oldValue: TValue | ||
) => boolean; | ||
/** | ||
* @ignore | ||
* @deprecated Use Equal instead. | ||
*/ | ||
export type ValCompare<TValue = any> = ( | ||
@@ -65,6 +79,11 @@ newValue: TValue, | ||
/** | ||
* Compare two values. Default `===`. | ||
* Compare two values. Default `Object.is`. | ||
*/ | ||
compare?: ValCompare<TValue>; | ||
equal?: ValEqual<TValue>; | ||
/** | ||
* @ignore | ||
* @deprecated Use `equal` instead. | ||
*/ | ||
compare?: ValEqual<TValue>; | ||
/** | ||
* Set the default behavior of subscription and reaction. | ||
@@ -71,0 +90,0 @@ * Emission triggers synchronously if `true`. Default `false`. |
import type { ReadonlyVal, UnwrapVal, ValConfig, ValDisposer } from "./typings"; | ||
import { ReadonlyValImpl } from "./readonly-val"; | ||
import { INIT_VALUE, defaultCompare, isVal } from "./utils"; | ||
import { INIT_VALUE, defaultEqual, isVal } from "./utils"; | ||
@@ -15,3 +15,3 @@ class UnwrapFromImpl< | ||
) { | ||
const initialCompare = config?.compare; | ||
const initialEqual = config && (config.equal || config.compare); | ||
@@ -38,3 +38,3 @@ let currentValue = INIT_VALUE as TValue; | ||
const value = computeValue(); | ||
if (!this.compare(value, currentValue)) { | ||
if (!this.equal(value, currentValue)) { | ||
this._subs_.dirty_ = true; | ||
@@ -55,4 +55,4 @@ currentValue = value; | ||
innerDisposer = innerVal && innerVal.$valCompute(notify); | ||
currentCompare = | ||
initialCompare || (innerVal ? innerVal.compare : defaultCompare); | ||
currentEqual = | ||
initialEqual || (innerVal ? innerVal.equal : defaultEqual); | ||
} | ||
@@ -70,6 +70,3 @@ }; | ||
super(get, config, () => { | ||
updateInnerVal(); | ||
currentValue = innerVal ? innerVal.value : (innerMaybeVal as TValue); | ||
dirty = notified = false; | ||
// attach listener first so that upstream value is resolved | ||
const outerDisposer = listen(() => { | ||
@@ -80,2 +77,6 @@ updateInnerVal(); | ||
updateInnerVal(); | ||
currentValue = innerVal ? innerVal.value : (innerMaybeVal as TValue); | ||
dirty = notified = false; | ||
return () => { | ||
@@ -87,5 +88,5 @@ innerDisposer?.(); | ||
let currentCompare = this.compare; | ||
this.compare = (newValue: TValue, oldValue: TValue) => | ||
currentCompare(newValue, oldValue); | ||
let currentEqual = this.equal; | ||
this.equal = (newValue: TValue, oldValue: TValue) => | ||
currentEqual(newValue, oldValue); | ||
} | ||
@@ -92,0 +93,0 @@ } |
@@ -6,6 +6,3 @@ import type { ReadonlyVal, ValInputsValueTuple } from "./typings"; | ||
export const defaultCompare = <TValue = any>( | ||
newValue: TValue, | ||
oldValue: TValue | ||
): boolean => newValue === oldValue; | ||
export const defaultEqual = Object.is; | ||
@@ -12,0 +9,0 @@ const getValue = <TValue>(val: ReadonlyVal<TValue>): TValue => val.value; |
@@ -12,3 +12,3 @@ import type { Val, ValConfig } from "./typings"; | ||
this.set = (value: TValue) => { | ||
if (!this.compare(value, currentValue)) { | ||
if (!this.equal(value, currentValue)) { | ||
this._subs_.dirty_ = true; | ||
@@ -15,0 +15,0 @@ currentValue = value; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
139438
4244
460