mirror of
https://github.com/atom/atom.git
synced 2026-04-28 03:01:47 -04:00
Pull out ConfigObserver & Subscriber mixins; Add unobserveConfig
This commit is contained in:
@@ -20,7 +20,7 @@ describe "SpacePen extensions", ->
|
||||
view.observeConfig "foo.bar", observeHandler
|
||||
expect(view.hasParent()).toBeTruthy()
|
||||
|
||||
it "observes the keyPath and destroys the subscription when unsubscribe is called", ->
|
||||
it "observes the keyPath and destroys the subscription when `.unobserveConfig()` is called", ->
|
||||
expect(observeHandler).toHaveBeenCalledWith(undefined)
|
||||
observeHandler.reset()
|
||||
|
||||
@@ -29,13 +29,13 @@ describe "SpacePen extensions", ->
|
||||
expect(observeHandler).toHaveBeenCalledWith("hello")
|
||||
observeHandler.reset()
|
||||
|
||||
view.unsubscribe()
|
||||
view.unobserveConfig()
|
||||
|
||||
config.set("foo.bar", "goodbye")
|
||||
|
||||
expect(observeHandler).not.toHaveBeenCalled()
|
||||
|
||||
it "unsubscribes when the view is removed", ->
|
||||
it "unobserves when the view is removed", ->
|
||||
observeHandler.reset()
|
||||
parent.remove()
|
||||
config.set("foo.bar", "hello")
|
||||
|
||||
8
src/stdlib/config-observer.coffee
Normal file
8
src/stdlib/config-observer.coffee
Normal file
@@ -0,0 +1,8 @@
|
||||
module.exports =
|
||||
observeConfig: (keyPath, callback) ->
|
||||
@configSubscriptions ?= {}
|
||||
@configSubscriptions[keyPath] = config.observe(keyPath, callback)
|
||||
|
||||
unobserveConfig: ->
|
||||
for keyPath, subscription of @configSubscriptions ? {}
|
||||
subscription.cancel()
|
||||
@@ -1,29 +1,16 @@
|
||||
_ = require 'underscore'
|
||||
{View} = require 'space-pen'
|
||||
jQuery = require 'jquery'
|
||||
ConfigObserver = require 'config-observer'
|
||||
Subscriber = require 'subscriber'
|
||||
|
||||
originalRemove = View.prototype.remove
|
||||
|
||||
_.extend View.prototype,
|
||||
observeConfig: (keyPath, callback) ->
|
||||
@addSubscription(config.observe(keyPath, callback))
|
||||
|
||||
subscribe: (eventEmitter, eventName, callback) ->
|
||||
eventEmitter.on eventName, callback
|
||||
@addSubscription(cancel: -> eventEmitter.off eventName, callback)
|
||||
|
||||
addSubscription: (subscription) ->
|
||||
@subscriptions ?= []
|
||||
@subscriptions.push(subscription)
|
||||
|
||||
unsubscribe: ->
|
||||
subscription.cancel() for subscription in @subscriptions ? []
|
||||
|
||||
remove: (args...) ->
|
||||
@unsubscribe()
|
||||
originalRemove.apply(this, args)
|
||||
_.extend View.prototype, ConfigObserver
|
||||
_.extend View.prototype, Subscriber
|
||||
|
||||
originalCleanData = jQuery.cleanData
|
||||
jQuery.cleanData = (elements) ->
|
||||
jQuery(element).view()?.unsubscribe?() for element in elements
|
||||
for element in elements
|
||||
if view = jQuery(element).view()
|
||||
view.unobserveConfig()
|
||||
view.unsubscribe()
|
||||
originalCleanData(elements)
|
||||
|
||||
8
src/stdlib/subscriber.coffee
Normal file
8
src/stdlib/subscriber.coffee
Normal file
@@ -0,0 +1,8 @@
|
||||
module.exports =
|
||||
subscribe: (eventEmitter, eventName, callback) ->
|
||||
eventEmitter.on eventName, callback
|
||||
@subscriptions ?= []
|
||||
@subscriptions.push(cancel: -> eventEmitter.off eventName, callback)
|
||||
|
||||
unsubscribe: ->
|
||||
subscription.cancel() for subscription in @subscriptions ? []
|
||||
Reference in New Issue
Block a user