@mmckegg/mutant
Advanced tools
Comparing version 3.5.4 to 3.6.0
@@ -22,2 +22,6 @@ var Value = require('./value') | ||
if (opts && opts.nextTick) { | ||
binder.nextTick = true | ||
} | ||
if (defaultValues && defaultValues.length) { | ||
@@ -24,0 +28,0 @@ forEach(defaultValues, add) |
@@ -6,4 +6,7 @@ var addCollectionMethods = require('./lib/add-collection-methods') | ||
var isObservable = require('./is-observable') | ||
var MutantMap = require('./map') | ||
module.exports = function DictToCollection (obs) { | ||
module.exports = DictToCollection | ||
function DictToCollection (obs) { | ||
var value = [] | ||
@@ -46,2 +49,8 @@ var raw = [] | ||
module.exports.values = function (obs) { | ||
return MutantMap(DictToCollection(obs), function (item) { | ||
return item.value | ||
}) | ||
} | ||
function shouldUpdate (newItem, keyValue) { | ||
@@ -48,0 +57,0 @@ if (!keyValue) { |
@@ -22,2 +22,6 @@ var Value = require('./value') | ||
if (opts && opts.nextTick) { | ||
binder.nextTick = true | ||
} | ||
if (defaultValues) { | ||
@@ -24,0 +28,0 @@ forEachPair(defaultValues, put) |
@@ -11,2 +11,7 @@ module.exports = function (target, lookup, checkUpdated) { | ||
} | ||
target.has = function (key) { | ||
checkUpdated && checkUpdated() | ||
return key in lookup | ||
} | ||
} |
module.exports = function (update, onBind, onUnbind) { | ||
var lazy = false | ||
var context = this | ||
var updating = false | ||
var obj = { | ||
live: false, | ||
nextTick: false, | ||
suspended: false, | ||
broadcast: broadcast, | ||
update: update, | ||
@@ -13,2 +14,13 @@ value: null, | ||
broadcast: function () { | ||
if (obj.nextTick) { | ||
if (!updating) { | ||
updating = true | ||
setImmediate(broadcast) | ||
} | ||
} else { | ||
broadcast() | ||
} | ||
}, | ||
transaction: function (value, cb) { | ||
@@ -23,3 +35,15 @@ var originalValue = obj.suspended | ||
onUpdate: function () { | ||
if (update.apply(context)) { | ||
if (obj.nextTick) { | ||
if (!updating) { | ||
updating = true | ||
setImmediate(obj.updateAndBroadcast) | ||
} | ||
} else { | ||
obj.updateAndBroadcast() | ||
} | ||
}, | ||
updateAndBroadcast: function () { | ||
updating = false | ||
if (update.call(context)) { | ||
broadcast() | ||
@@ -30,5 +54,9 @@ } | ||
checkUpdated: function () { | ||
if (!obj.live || lazy) { | ||
if (!obj.live || lazy || updating) { | ||
lazy = false | ||
update.apply(context) | ||
if (obj.nextTick && obj.live && lazy) { | ||
obj.onUpdate() // use cached value to make more responsive | ||
} else { | ||
update.apply(context) | ||
} | ||
} | ||
@@ -35,0 +63,0 @@ }, |
@@ -72,3 +72,8 @@ var MutantMap = require('./map') | ||
result.has = function (key) { | ||
instance.checkUpdated() | ||
return key in raw | ||
} | ||
return result | ||
} |
40
map.js
@@ -15,2 +15,6 @@ var resolve = require('./resolve') | ||
if (opts && opts.nextTick) { | ||
binder.nextTick = true | ||
} | ||
var itemInvalidators = new global.Map() | ||
@@ -209,19 +213,21 @@ var lastValues = new global.Map() | ||
function updateItem (i) { | ||
var item = get(obs, i) | ||
if (!lastValues.has(item) || !isSame(item, item, comparer)) { | ||
if (itemInvalidators.has(item)) { | ||
itemInvalidators.get(item).forEach(invokeRelease) | ||
itemInvalidators.delete(item) | ||
if (i < getLength(obs)) { | ||
var item = get(obs, i) | ||
if (!lastValues.has(item) || !isSame(item, item, comparer)) { | ||
if (itemInvalidators.has(item)) { | ||
itemInvalidators.get(item).forEach(invokeRelease) | ||
itemInvalidators.delete(item) | ||
} | ||
var newValue = lambda(item, addInvalidateCallback(item)) | ||
if (newValue !== raw[i]) { | ||
raw[i] = newValue | ||
} | ||
rawSet.add(newValue) | ||
lastValues.set(item, raw[i]) | ||
rebind(i) | ||
} else { | ||
raw[i] = lastValues.get(item) | ||
} | ||
var newValue = lambda(item, addInvalidateCallback(item)) | ||
if (newValue !== raw[i]) { | ||
raw[i] = newValue | ||
} | ||
rawSet.add(newValue) | ||
lastValues.set(item, raw[i]) | ||
rebind(i) | ||
} else { | ||
raw[i] = lastValues.get(item) | ||
values[i] = resolve(raw[i]) | ||
} | ||
values[i] = resolve(raw[i]) | ||
} | ||
@@ -298,6 +304,2 @@ | ||
function deleteEntry (entry) { | ||
this.delete(entry) | ||
} | ||
function invokeRelease (item) { | ||
@@ -304,0 +306,0 @@ if (item.release) { |
{ | ||
"name": "@mmckegg/mutant", | ||
"version": "3.5.4", | ||
"version": "3.6.0", | ||
"description": "Create observables and map them to DOM elements. Massively inspired by hyperscript and observ-*. No virtual dom, just direct observable bindings. Unnecessary garbage collection is avoided by using mutable objects instead of blasting immutable junk all over the place.", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -7,3 +7,3 @@ var LazyWatcher = require('./lib/lazy-watcher') | ||
function ProxyCollection (source) { | ||
function ProxyCollection (source, opts) { | ||
var releases = [] | ||
@@ -14,2 +14,6 @@ | ||
if (opts && opts.nextTick) { | ||
binder.nextTick = true | ||
} | ||
var observable = function MutantProxyCollection (listener) { | ||
@@ -16,0 +20,0 @@ if (!listener) { |
@@ -7,3 +7,3 @@ var LazyWatcher = require('./lib/lazy-watcher') | ||
function ProxyDict (source) { | ||
function ProxyDict (source, opts) { | ||
var releases = [] | ||
@@ -14,2 +14,6 @@ | ||
if (opts && opts.nextTick) { | ||
binder.nextTick = true | ||
} | ||
var observable = function MutantProxyDict (listener) { | ||
@@ -40,2 +44,10 @@ if (!listener) { | ||
observable.has = function (key) { | ||
if (isObservable(source) && source.has) { | ||
return source.has(key) | ||
} else if (resolve(source)) { | ||
return key in resolve(source) | ||
} | ||
} | ||
observable.set = function (newSource) { | ||
@@ -42,0 +54,0 @@ unlisten() |
@@ -7,3 +7,3 @@ var LazyWatcher = require('./lib/lazy-watcher') | ||
function Proxy (source) { | ||
function Proxy (source, opts) { | ||
var releases = [] | ||
@@ -14,2 +14,6 @@ | ||
if (opts && opts.nextTick) { | ||
binder.nextTick = true | ||
} | ||
var observable = function MutantProxy (listener) { | ||
@@ -16,0 +20,0 @@ if (!listener) { |
@@ -25,2 +25,3 @@ # mutant | ||
Requires an environment that supports: | ||
- `setImmediate(fn)` | ||
- `Map` and `WeakMap` | ||
@@ -27,0 +28,0 @@ - `MutationObserver` (optional, only for root `html-element` binding support) |
10
set.js
@@ -5,4 +5,4 @@ var LazyWatcher = require('./lib/lazy-watcher') | ||
function Set (defaultValues) { | ||
var instance = new ProtoSet(defaultValues) | ||
function Set (defaultValues, opts) { | ||
var instance = new ProtoSet(defaultValues, opts) | ||
var observable = instance.MutantSet.bind(instance) | ||
@@ -20,3 +20,3 @@ observable.add = instance.add.bind(instance) | ||
// optimise memory usage | ||
function ProtoSet (defaultValues) { | ||
function ProtoSet (defaultValues, opts) { | ||
var self = this | ||
@@ -29,2 +29,6 @@ self.object = [] | ||
if (opts && opts.nextTick) { | ||
self.binder.nextTick = true | ||
} | ||
if (defaultValues && defaultValues.length) { | ||
@@ -31,0 +35,0 @@ defaultValues.forEach(function (valueOrObs) { |
@@ -19,2 +19,6 @@ var Value = require('./value') | ||
if (opts && opts.nextTick) { | ||
binder.nextTick = true | ||
} | ||
var comparer = opts && opts.comparer || null | ||
@@ -21,0 +25,0 @@ |
71838
2488
93