Commit f5910e3be7de0ef9ad989e4646cf08a5cc427a82
value: add defaultValue option
Matt McKegg committed on 9/10/2016, 6:07:53 AMParent: 117c25c0033f5aa847498c91de7027643cf61eb7
Files changed
value.js | changed |
value.js | ||
---|---|---|
@@ -1,16 +1,16 @@ | ||
1 | 1 | module.exports = Observable |
2 | 2 | |
3 | -function Observable (value) { | |
3 | +function Observable (value, opts) { | |
4 | 4 | var listeners = [] |
5 | - value = value === undefined ? null : value | |
5 | + value = getValue(value, opts) | |
6 | 6 | |
7 | 7 | observable.set = function (v) { |
8 | - value = v | |
8 | + value = getValue(v, opts) | |
9 | 9 | |
10 | 10 | var cachedListeners = listeners.slice(0) |
11 | 11 | for (var i = 0, len = cachedListeners.length; i < len; i++) { |
12 | - cachedListeners[i](v) | |
12 | + cachedListeners[i](value) | |
13 | 13 | } |
14 | 14 | } |
15 | 15 | |
16 | 16 | return observable |
@@ -35,4 +35,15 @@ | ||
35 | 35 | } |
36 | 36 | } |
37 | 37 | } |
38 | 38 | } |
39 | + | |
40 | +function getValue (value, opts) { | |
41 | + if (value == null) { | |
42 | + if (opts && opts.defaultValue != null) { | |
43 | + value = opts.defaultValue | |
44 | + } else { | |
45 | + value = null | |
46 | + } | |
47 | + } | |
48 | + return value | |
49 | +} |
Built with git-ssb-web