Commit 2b5e31cac39d3db81a26461d2d5ea9de451caf07
make for-each/pair public, move lookup accessors to add-lookup-methods
Matt McKegg committed on 9/12/2016, 12:58:57 PMParent: 3d65e26e909eda26a2ae0fd4157022512389dc58
Files changed
concat.js | changed |
dict.js | changed |
lib/add-lookup-methods.js | added |
merge.js | changed |
for-each-pair.js | added |
for-each.js | added |
concat.js | ||
---|---|---|
@@ -1,7 +1,8 @@ | ||
1 | 1 | var resolve = require('./resolve') |
2 | 2 | var addCollectionMethods = require('./lib/add-collection-methods') |
3 | 3 | var computed = require('./computed') |
4 | +var forEach = require('./for-each') | |
4 | 5 | |
5 | 6 | module.exports = function Concat (observables) { |
6 | 7 | var values = [] |
7 | 8 | var rawValues = [] |
@@ -31,14 +32,4 @@ | ||
31 | 32 | addCollectionMethods(result, rawValues, instance.checkUpdated) |
32 | 33 | |
33 | 34 | return result |
34 | 35 | } |
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.js | ||
---|---|---|
@@ -2,28 +2,26 @@ | ||
2 | 2 | var LazyWatcher = require('./lib/lazy-watcher') |
3 | 3 | var isSame = require('./lib/is-same') |
4 | 4 | var resolve = require('./resolve') |
5 | 5 | var isObservable = require('./is-observable') |
6 | +var forEachPair = require('./for-each-pair') | |
7 | +var addLookupMethods = require('./lib/add-lookup-methods') | |
6 | 8 | |
7 | -// TODO: reimplement using LazyWatcher | |
8 | - | |
9 | 9 | module.exports = Dict |
10 | 10 | |
11 | 11 | function Dict (defaultValues, opts) { |
12 | 12 | var object = Object.create({}) |
13 | - var sources = [] | |
14 | - var releases = [] | |
13 | + var sources = {} | |
14 | + var releases = {} | |
15 | 15 | var fixedIndexing = opts && opts.fixedIndexing || false |
16 | 16 | |
17 | 17 | var comparer = opts && opts.comparer || null |
18 | 18 | |
19 | 19 | var binder = LazyWatcher(update, listen, unlisten) |
20 | 20 | binder.value = object |
21 | 21 | |
22 | 22 | if (defaultValues) { |
23 | - Object.keys(defaultValues).forEach(function (key) { | |
24 | - put(key, defaultValues[key]) | |
25 | - }) | |
23 | + forEachPair(defaultValues, put) | |
26 | 24 | } |
27 | 25 | |
28 | 26 | var observable = function MutantDictionary (listener) { |
29 | 27 | if (!listener) { |
@@ -31,23 +29,17 @@ | ||
31 | 29 | } |
32 | 30 | return binder.addListener(listener) |
33 | 31 | } |
34 | 32 | |
33 | + addLookupMethods(observable, sources) | |
34 | + | |
35 | 35 | observable.put = function (key, valueOrObs) { |
36 | 36 | valueOrObs = getObsValue(valueOrObs) |
37 | 37 | put(key, valueOrObs) |
38 | 38 | binder.broadcast() |
39 | 39 | return valueOrObs |
40 | 40 | } |
41 | 41 | |
42 | - observable.get = function (key) { | |
43 | - return sources[key] | |
44 | - } | |
45 | - | |
46 | - observable.keys = function () { | |
47 | - return Object.keys(sources) | |
48 | - } | |
49 | - | |
50 | 42 | observable.clear = function () { |
51 | 43 | Object.keys(sources).forEach(function (key) { |
52 | 44 | tryInvoke(releases[key]) |
53 | 45 | delete sources[key] |
@@ -69,15 +61,17 @@ | ||
69 | 61 | return !!~object.indexOf(valueOrObs) |
70 | 62 | } |
71 | 63 | |
72 | 64 | observable.set = function (values) { |
73 | - var keys = values && Object.keys(values) || [] | |
74 | 65 | if (fixedIndexing) { |
75 | - keys.forEach(function (key) { | |
66 | + var keys = [] | |
67 | + | |
68 | + forEachPair(values, function (key, value) { | |
69 | + keys.push(key) | |
76 | 70 | if (sources[key]) { |
77 | - sources[key].set(values[key]) | |
71 | + sources[key].set(value) | |
78 | 72 | } else { |
79 | - put(key, getObsValue(values[key])) | |
73 | + put(key, getObsValue(value)) | |
80 | 74 | } |
81 | 75 | }) |
82 | 76 | |
83 | 77 | Object.keys(sources).forEach(function (key) { |
@@ -95,12 +89,9 @@ | ||
95 | 89 | delete releases[key] |
96 | 90 | delete object[key] |
97 | 91 | }) |
98 | 92 | |
99 | - keys.forEach(function (key) { | |
100 | - put(key, values[key]) | |
101 | - }) | |
102 | - | |
93 | + forEachPair(values, put) | |
103 | 94 | binder.broadcast() |
104 | 95 | } |
105 | 96 | } |
106 | 97 |
lib/add-lookup-methods.js | ||
---|---|---|
@@ -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.js | ||
---|---|---|
@@ -1,7 +1,9 @@ | ||
1 | 1 | var computed = require('./computed') |
2 | 2 | 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') | |
4 | 6 | |
5 | 7 | module.exports = Merge |
6 | 8 | |
7 | 9 | function Merge (sources) { |
@@ -36,36 +38,8 @@ | ||
36 | 38 | var result = function MutantMerge (listener) { |
37 | 39 | return instance(listener) |
38 | 40 | } |
39 | 41 | |
40 | - result.keys = function () { | |
41 | - instance.checkUpdated() | |
42 | - return Array.from(keys.values()) | |
43 | - } | |
42 | + addLookupMethods(result, raw, instance.checkUpdated) | |
44 | 43 | |
45 | - result.get = function (key) { | |
46 | - instance.checkUpdated() | |
47 | - return raw[key] | |
48 | - } | |
49 | - | |
50 | 44 | return result |
51 | 45 | } |
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.js | ||
---|---|---|
@@ -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 | +} |
Built with git-ssb-web