git ssb

1+

Matt McKegg / mutant



Commit f5910e3be7de0ef9ad989e4646cf08a5cc427a82

value: add defaultValue option

Matt McKegg committed on 9/10/2016, 6:07:53 AM
Parent: 117c25c0033f5aa847498c91de7027643cf61eb7

Files changed

value.jschanged
value.jsView
@@ -1,16 +1,16 @@
11 module.exports = Observable
22
3-function Observable (value) {
3+function Observable (value, opts) {
44 var listeners = []
5- value = value === undefined ? null : value
5+ value = getValue(value, opts)
66
77 observable.set = function (v) {
8- value = v
8+ value = getValue(v, opts)
99
1010 var cachedListeners = listeners.slice(0)
1111 for (var i = 0, len = cachedListeners.length; i < len; i++) {
12- cachedListeners[i](v)
12+ cachedListeners[i](value)
1313 }
1414 }
1515
1616 return observable
@@ -35,4 +35,15 @@
3535 }
3636 }
3737 }
3838 }
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