git ssb

1+

Matt McKegg / mutant



Commit 2b5e31cac39d3db81a26461d2d5ea9de451caf07

make for-each/pair public, move lookup accessors to add-lookup-methods

Matt McKegg committed on 9/12/2016, 12:58:57 PM
Parent: 3d65e26e909eda26a2ae0fd4157022512389dc58

Files changed

concat.jschanged
dict.jschanged
lib/add-lookup-methods.jsadded
merge.jschanged
for-each-pair.jsadded
for-each.jsadded
concat.jsView
@@ -1,7 +1,8 @@
11 var resolve = require('./resolve')
22 var addCollectionMethods = require('./lib/add-collection-methods')
33 var computed = require('./computed')
4+var forEach = require('./for-each')
45
56 module.exports = function Concat (observables) {
67 var values = []
78 var rawValues = []
@@ -31,14 +32,4 @@
3132 addCollectionMethods(result, rawValues, instance.checkUpdated)
3233
3334 return result
3435 }
35-
36-function forEach (sources, fn) {
37- if (sources && !sources.forEach) {
38- sources = resolve(sources)
39- }
40-
41- if (sources && sources.forEach) {
42- sources.forEach(fn)
43- }
44-}
dict.jsView
@@ -2,28 +2,26 @@
22 var LazyWatcher = require('./lib/lazy-watcher')
33 var isSame = require('./lib/is-same')
44 var resolve = require('./resolve')
55 var isObservable = require('./is-observable')
6+var forEachPair = require('./for-each-pair')
7+var addLookupMethods = require('./lib/add-lookup-methods')
68
7-// TODO: reimplement using LazyWatcher
8-
99 module.exports = Dict
1010
1111 function Dict (defaultValues, opts) {
1212 var object = Object.create({})
13- var sources = []
14- var releases = []
13+ var sources = {}
14+ var releases = {}
1515 var fixedIndexing = opts && opts.fixedIndexing || false
1616
1717 var comparer = opts && opts.comparer || null
1818
1919 var binder = LazyWatcher(update, listen, unlisten)
2020 binder.value = object
2121
2222 if (defaultValues) {
23- Object.keys(defaultValues).forEach(function (key) {
24- put(key, defaultValues[key])
25- })
23+ forEachPair(defaultValues, put)
2624 }
2725
2826 var observable = function MutantDictionary (listener) {
2927 if (!listener) {
@@ -31,23 +29,17 @@
3129 }
3230 return binder.addListener(listener)
3331 }
3432
33+ addLookupMethods(observable, sources)
34+
3535 observable.put = function (key, valueOrObs) {
3636 valueOrObs = getObsValue(valueOrObs)
3737 put(key, valueOrObs)
3838 binder.broadcast()
3939 return valueOrObs
4040 }
4141
42- observable.get = function (key) {
43- return sources[key]
44- }
45-
46- observable.keys = function () {
47- return Object.keys(sources)
48- }
49-
5042 observable.clear = function () {
5143 Object.keys(sources).forEach(function (key) {
5244 tryInvoke(releases[key])
5345 delete sources[key]
@@ -69,15 +61,17 @@
6961 return !!~object.indexOf(valueOrObs)
7062 }
7163
7264 observable.set = function (values) {
73- var keys = values && Object.keys(values) || []
7465 if (fixedIndexing) {
75- keys.forEach(function (key) {
66+ var keys = []
67+
68+ forEachPair(values, function (key, value) {
69+ keys.push(key)
7670 if (sources[key]) {
77- sources[key].set(values[key])
71+ sources[key].set(value)
7872 } else {
79- put(key, getObsValue(values[key]))
73+ put(key, getObsValue(value))
8074 }
8175 })
8276
8377 Object.keys(sources).forEach(function (key) {
@@ -95,12 +89,9 @@
9589 delete releases[key]
9690 delete object[key]
9791 })
9892
99- keys.forEach(function (key) {
100- put(key, values[key])
101- })
102-
93+ forEachPair(values, put)
10394 binder.broadcast()
10495 }
10596 }
10697
lib/add-lookup-methods.jsView
@@ -1,0 +1,11 @@
1+module.exports = function (target, lookup, checkUpdated) {
2+ target.keys = function () {
3+ checkUpdated && checkUpdated()
4+ return Object.keys(lookup)
5+ }
6+
7+ target.get = function (key) {
8+ checkUpdated && checkUpdated()
9+ return lookup[key]
10+ }
11+}
merge.jsView
@@ -1,7 +1,9 @@
11 var computed = require('./computed')
22 var resolve = require('./resolve')
3-var isObservable = require('./is-observable')
3+var forEach = require('./for-each')
4+var forEachPair = require('./for-each-pair')
5+var addLookupMethods = require('./lib/add-lookup-methods')
46
57 module.exports = Merge
68
79 function Merge (sources) {
@@ -36,36 +38,8 @@
3638 var result = function MutantMerge (listener) {
3739 return instance(listener)
3840 }
3941
40- result.keys = function () {
41- instance.checkUpdated()
42- return Array.from(keys.values())
43- }
42+ addLookupMethods(result, raw, instance.checkUpdated)
4443
45- result.get = function (key) {
46- instance.checkUpdated()
47- return raw[key]
48- }
49-
5044 return result
5145 }
52-
53-function forEach (sources, fn) {
54- if (sources && !sources.forEach) {
55- sources = resolve(sources)
56- }
57-
58- if (sources && sources.forEach) {
59- sources.forEach(fn)
60- }
61-}
62-
63-function forEachPair (source, fn) {
64- if (source) {
65- if (isObservable(source) && source.keys && source.get) {
66- resolve(source.keys).forEach(function (key) {
67- fn(key, source.get(key))
68- })
69- }
70- }
71-}
for-each-pair.jsView
@@ -1,0 +1,19 @@
1+var isObservable = require('./is-observable')
2+var resolve = require('./resolve')
3+
4+module.exports = function forEachPair (source, fn) {
5+ if (source) {
6+ if (isObservable(source) && source.keys && source.get) {
7+ resolve(source.keys).forEach(function (key) {
8+ fn(key, source.get(key))
9+ })
10+ } else {
11+ var values = resolve(source)
12+ if (values) {
13+ Object.keys(values).forEach(function (key) {
14+ fn(key, values[key])
15+ })
16+ }
17+ }
18+ }
19+}
for-each.jsView
@@ -1,0 +1,11 @@
1+var resolve = require('./resolve')
2+
3+module.exports = function forEach (sources, fn) {
4+ if (sources && !sources.forEach) {
5+ sources = resolve(sources)
6+ }
7+
8+ if (sources && sources.forEach) {
9+ sources.forEach(fn)
10+ }
11+}

Built with git-ssb-web