Files: 89e85f9009db15cde0d4a19d0f6fd5167a25d808 / mapped-dict.js
822 bytesRaw
1 | var MutantLookup = require('./lookup') |
2 | var MutantDict = require('./dict') |
3 | var DictToCollection = require('./dict-to-collection') |
4 | |
5 | module.exports = MutantMappedDict |
6 | |
7 | function MutantMappedDict (defaultItems, lambda, opts) { |
8 | opts = opts || {} |
9 | |
10 | var list = MutantDict(defaultItems, { |
11 | comparer: opts.comparer |
12 | }) |
13 | |
14 | var observable = MutantLookup(DictToCollection(list), function (item, invalidateOn) { |
15 | var value = lambda(item.key, item.value, invalidateOn) |
16 | if (value[0] === item.key && value[1] === item.value) { |
17 | return item // passthru |
18 | } else { |
19 | return value |
20 | } |
21 | }, opts) |
22 | |
23 | observable.set = list.set |
24 | |
25 | observable.put = function (key, item) { |
26 | list.put(key, item) |
27 | return observable.get(key) |
28 | } |
29 | |
30 | observable.delete = function (key) { |
31 | list.delete(key) |
32 | } |
33 | |
34 | return observable |
35 | } |
36 |
Built with git-ssb-web