Rudimentary support for comparators.
This commit is contained in:
parent
cb63685e7e
commit
1e10b27f69
@ -1,4 +1,4 @@
|
||||
export function computed(fn, dependencies = []) {
|
||||
export function computed(fn, dependencies = [], comparator = eq) {
|
||||
const subscribers = new Set();
|
||||
const dependents = new Set();
|
||||
let isDirty = true;
|
||||
@ -21,7 +21,7 @@ export function computed(fn, dependencies = []) {
|
||||
if (isDirty) {
|
||||
const newVal = fn.apply(null, dependencies.map(runParam));
|
||||
isDirty = false;
|
||||
if (newVal !== val) {
|
||||
if (!comparator(newVal, val)) {
|
||||
val = newVal;
|
||||
subscribers.forEach(s => s(val));
|
||||
}
|
||||
|
||||
@ -1,8 +1,10 @@
|
||||
export function observable(store) {
|
||||
import { eq } from './util.js';
|
||||
|
||||
export function observable(store, comparator = eq) {
|
||||
const subscribers = new Set();
|
||||
|
||||
const accessor = function _observable(newVal) {
|
||||
if (newVal !== undefined && store !== newVal) {
|
||||
if (newVal !== undefined && !comparator(store, newVal)) {
|
||||
store = newVal;
|
||||
subscribers.forEach(s => s(store));
|
||||
}
|
||||
|
||||
1
packages/frptools/src/util.js
Normal file
1
packages/frptools/src/util.js
Normal file
@ -0,0 +1 @@
|
||||
export const eq = (a, b) => a === b;
|
||||
Loading…
x
Reference in New Issue
Block a user