|
@@ -197,7 +197,7 @@ const Spruce = {
|
|
|
|
|
|
this.watchers[name].push(callback)
|
|
|
|
|
|
- return
|
|
|
+ return [() => this.unwatch(name, callback)]
|
|
|
}
|
|
|
|
|
|
const nameParts = name.split('.')
|
|
@@ -228,6 +228,53 @@ const Spruce = {
|
|
|
}
|
|
|
|
|
|
target.__watchers.get(part).add(callback)
|
|
|
+
|
|
|
+ return [() => this.unwatch(name, callback)]
|
|
|
+ },
|
|
|
+
|
|
|
+ unwatch(name, callback) {
|
|
|
+ const nameParts = name.split('.')
|
|
|
+
|
|
|
+ const target = nameParts.reduce((target, part) => {
|
|
|
+ const sub = target[part]
|
|
|
+
|
|
|
+ if (! isNullOrUndefined(sub) && (isObject(sub) || isArray(sub))) {
|
|
|
+ return sub
|
|
|
+ }
|
|
|
+
|
|
|
+ return target
|
|
|
+ }, this.stores)
|
|
|
+
|
|
|
+ const part = Object.is(target, this.get(name)) ? '__self' : nameParts[nameParts.length - 1]
|
|
|
+ const watchers = target.__watchers
|
|
|
+
|
|
|
+ if (! watchers.has(part)) {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ watchers.get(part).delete(callback)
|
|
|
+ },
|
|
|
+
|
|
|
+ watchers(name) {
|
|
|
+ const nameParts = name.split('.')
|
|
|
+
|
|
|
+ const target = nameParts.reduce((target, part) => {
|
|
|
+ const sub = target[part]
|
|
|
+
|
|
|
+ if (! isNullOrUndefined(sub) && (isObject(sub) || isArray(sub))) {
|
|
|
+ return sub
|
|
|
+ }
|
|
|
+
|
|
|
+ return target
|
|
|
+ }, this.stores)
|
|
|
+
|
|
|
+ const part = Object.is(target, this.get(name)) ? '__self' : nameParts[nameParts.length - 1]
|
|
|
+
|
|
|
+ if (! target.__watchers) {
|
|
|
+ return {}
|
|
|
+ }
|
|
|
+
|
|
|
+ return target.__watchers.get(part)
|
|
|
},
|
|
|
|
|
|
runWatchers(target, key, value) {
|