git ssb

1+

Matt McKegg / mutant



Commit 9ddfe1a36b770dd913a4f5649543b64785ffd4cc

add merge and lookup

Matt McKegg committed on 9/10/2016, 6:19:37 AM
Parent: 99538587b3f862c6f3c7fa504500775fc48797de

Files changed

lib/key-value.jsadded
lookup.jsadded
merge.jsadded
lib/key-value.jsView
@@ -1,0 +1,9 @@
1+var Struct = require('../struct')
2+var isObservable = require('../is-observable')
3+
4+module.exports = function MutantKeyValue (key, value) {
5+ var result = Struct({key: key, value: value})
6+ result._type = 'MutantLookupKeyValue'
7+ result.isBound = isObservable(key) || isObservable(value)
8+ return result
9+}
lookup.jsView
@@ -1,0 +1,73 @@
1+var MutantMap = require('./map')
2+var computed = require('./computed')
3+var resolve = require('./resolve')
4+var KeyValue = require('./lib/key-value')
5+var isObservable = require('./is-observable')
6+
7+module.exports = Lookup
8+
9+function Lookup (obs, lambdaOrKey, opts) {
10+ var mapped = MutantMap(obs, function (item, invalidateOn) {
11+ if (typeof lambdaOrKey === 'function') {
12+ var value = lambdaOrKey(item, invalidateOn)
13+ if (isObservable(value) && value._type === 'MutantKeyValue') {
14+ return value // passthru
15+ } else if (Array.isArray(value)) {
16+ return KeyValue(value[0], value[1])
17+ } else {
18+ return KeyValue(value, item)
19+ }
20+ } else if (typeof lambdaOrKey === 'string') {
21+ return KeyValue(item[lambdaOrKey], item)
22+ }
23+ })
24+
25+ var raw = {}
26+ var value = {}
27+ var keys = new Set()
28+
29+ var instance = computed.extended(mapped, function update () {
30+ var currentKeys = []
31+
32+ for (var i = 0; i < mapped.getLength(); i++) {
33+ var item = mapped.get(i)
34+ if (item) {
35+ var key = resolve(item.key)
36+ if (key !== undefined) {
37+ var rawValue = item.value
38+ currentKeys.push(key)
39+ keys.add(key)
40+ raw[key] = rawValue
41+ value[key] = resolve(rawValue)
42+ }
43+ }
44+ }
45+
46+ // remove deleted keys
47+ Array.from(keys.values()).filter(function (k) {
48+ return !currentKeys.includes(k)
49+ }).forEach(function (key) {
50+ keys.delete(key)
51+ delete raw[key]
52+ delete value[key]
53+ })
54+
55+ return value
56+ })
57+
58+ var result = function MutantLookup (listener) {
59+ return instance(listener)
60+ }
61+
62+ result.keys = function () {
63+ instance.checkUpdated()
64+ return Array.from(keys.values())
65+ }
66+
67+ result.get = function (key) {
68+ instance.checkUpdated()
69+ return raw[key]
70+ }
71+
72+ return result
73+}
merge.jsView
@@ -1,0 +1,71 @@
1+var computed = require('./computed')
2+var resolve = require('./resolve')
3+var isObservable = require('./is-observable')
4+
5+module.exports = Merge
6+
7+function Merge (sources) {
8+ var raw = {}
9+ var value = {}
10+ var keys = new Set()
11+
12+ var instance = computed.extended(sources, function update () {
13+ var currentKeys = []
14+
15+ forEach(sources, function (source) {
16+ forEachPair(source, function (key, rawValue) {
17+ currentKeys.push(key)
18+ keys.add(key)
19+ raw[key] = rawValue
20+ value[key] = resolve(rawValue)
21+ })
22+ })
23+
24+ // remove deleted keys
25+ Array.from(keys.values()).filter(function (k) {
26+ return !currentKeys.includes(k)
27+ }).forEach(function (key) {
28+ keys.delete(key)
29+ delete raw[key]
30+ delete value[key]
31+ })
32+
33+ return value
34+ })
35+
36+ var result = function MutantMerge (listener) {
37+ return instance(listener)
38+ }
39+
40+ result.keys = function () {
41+ instance.checkUpdated()
42+ return Array.from(keys.values())
43+ }
44+
45+ result.get = function (key) {
46+ instance.checkUpdated()
47+ return raw[key]
48+ }
49+
50+ return result
51+}
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+}

Built with git-ssb-web