Files: f3288b0f154b91b996580e0c68b99ea612698769 / merge.js
1048 bytesRaw
1 | var computed = require('./computed') |
2 | var resolve = require('./resolve') |
3 | var forEach = require('./for-each') |
4 | var forEachPair = require('./for-each-pair') |
5 | var addLookupMethods = require('./lib/add-lookup-methods') |
6 | |
7 | module.exports = Merge |
8 | |
9 | function Merge (sources) { |
10 | var raw = {} |
11 | var value = {} |
12 | var keys = new Set() |
13 | |
14 | var instance = computed.extended(sources, function update () { |
15 | var currentKeys = [] |
16 | |
17 | forEach(sources, function (source) { |
18 | forEachPair(source, function (key, rawValue) { |
19 | currentKeys.push(key) |
20 | keys.add(key) |
21 | raw[key] = rawValue |
22 | value[key] = resolve(rawValue) |
23 | }) |
24 | }) |
25 | |
26 | // remove deleted keys |
27 | Array.from(keys.values()).filter(function (k) { |
28 | return !currentKeys.includes(k) |
29 | }).forEach(function (key) { |
30 | keys.delete(key) |
31 | delete raw[key] |
32 | delete value[key] |
33 | }) |
34 | |
35 | return value |
36 | }) |
37 | |
38 | var result = function MutantMerge (listener) { |
39 | return instance(listener) |
40 | } |
41 | |
42 | addLookupMethods(result, raw, instance.checkUpdated) |
43 | |
44 | return result |
45 | } |
46 |
Built with git-ssb-web