diff --git a/src/computed.js b/src/computed.js index dbfff1d..a7009ea 100644 --- a/src/computed.js +++ b/src/computed.js @@ -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)); } diff --git a/src/observable.js b/src/observable.js index 66d57ce..6c6180f 100644 --- a/src/observable.js +++ b/src/observable.js @@ -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)); } diff --git a/src/util.js b/src/util.js new file mode 100644 index 0000000..4ce0d59 --- /dev/null +++ b/src/util.js @@ -0,0 +1 @@ +export const eq = (a, b) => a === b;