diff --git a/chrome/content/zotero/containers/tagSelector.jsx b/chrome/content/zotero/containers/tagSelector.jsx index 834f46bfe9..ed6ca94408 100644 --- a/chrome/content/zotero/containers/tagSelector.jsx +++ b/chrome/content/zotero/containers/tagSelector.jsx @@ -26,7 +26,7 @@ Zotero.TagSelector = class TagSelectorContainer extends React.PureComponent { ['collection-item', 'item', 'item-tag', 'tag', 'setting'], 'tagSelector' ); - Zotero.Prefs.registerObserver('fontSize', this.handleFontChange.bind(this)); + this._prefObserverID = Zotero.Prefs.registerObserver('fontSize', this.handleFontChange.bind(this)); this.tagListRef = React.createRef(); this.searchBoxRef = React.createRef(); @@ -731,12 +731,8 @@ Zotero.TagSelector = class TagSelectorContainer extends React.PureComponent { uninit() { ReactDOM.unmountComponentAtNode(this.domEl); - if (this._notifierID) { - Zotero.Notifier.unregisterObserver(this._notifierID); - } - if (this._prefObserverID) { - Zotero.Prefs.unregisterObserver('fontSize', this.handleFontChange.bind(this)); - } + Zotero.Notifier.unregisterObserver(this._notifierID); + Zotero.Prefs.unregisterObserver(this._prefObserverID); } static propTypes = { diff --git a/chrome/content/zotero/xpcom/itemTreeView.js b/chrome/content/zotero/xpcom/itemTreeView.js index 6258e34403..42ed281fa5 100644 --- a/chrome/content/zotero/xpcom/itemTreeView.js +++ b/chrome/content/zotero/xpcom/itemTreeView.js @@ -53,12 +53,13 @@ Zotero.ItemTreeView = function (collectionTreeRow) { this._refreshPromise = Zotero.Promise.resolve(); - this._unregisterID = Zotero.Notifier.registerObserver( + this._notifierObserverID = Zotero.Notifier.registerObserver( this, ['item', 'collection-item', 'item-tag', 'share-items', 'bucket', 'feedItem', 'search'], 'itemTreeView', 50 ); + this._prefObserverID = Zotero.Prefs.registerObserver('recursiveCollections', this.refresh.bind(this)); } Zotero.ItemTreeView.prototype = Object.create(Zotero.LibraryTreeView.prototype); @@ -1019,7 +1020,8 @@ Zotero.ItemTreeView.prototype.notify = Zotero.Promise.coroutine(function* (actio Zotero.ItemTreeView.prototype.unregister = async function() { - Zotero.Notifier.unregisterObserver(this._unregisterID); + Zotero.Notifier.unregisterObserver(this._notifierObserverID); + Zotero.Prefs.unregisterObserver(this._prefObserverID); if (this.collectionTreeRow.onUnload) { await this.collectionTreeRow.onUnload(); diff --git a/chrome/content/zotero/xpcom/prefs.js b/chrome/content/zotero/xpcom/prefs.js index c13926ba0b..a0df9a3c6b 100644 --- a/chrome/content/zotero/xpcom/prefs.js +++ b/chrome/content/zotero/xpcom/prefs.js @@ -274,24 +274,39 @@ Zotero.Prefs = new function(){ } var _observers = {}; - this.registerObserver = function(name, handler) { + var _observersBySymbol = {}; + + /** + * @param {String} name - Preference name on extensions.zotero branch + * @param {Function} handler + * @return {Symbol} - Symbol to pass to unregisterObserver() + */ + this.registerObserver = function (name, handler) { + var symbol = Symbol(); _observers[name] = _observers[name] || []; _observers[name].push(handler); + _observersBySymbol[symbol] = [name, handler]; + return symbol; } - this.unregisterObserver = function(name, handler) { - var obs = _observers[name]; + /** + * @param {Symbol} symbol - Symbol returned from registerObserver() + */ + this.unregisterObserver = function (symbol) { + var obs = _observersBySymbol[symbol]; if (!obs) { - Zotero.debug("No preferences observer registered for " + name); + Zotero.debug("No pref observer registered for given symbol"); return; } + delete _observersBySymbol[symbol]; + + var [name, handler] = obs; var i = obs.indexOf(handler); if (i == -1) { - Zotero.debug("Handler was not registered for preference " + name); + Zotero.debug("Handler was not registered for preference " + name, 2); return; } - obs.splice(i, 1); } } diff --git a/chrome/content/zotero/xpcom/quickCopy.js b/chrome/content/zotero/xpcom/quickCopy.js index f2542c0a83..a279d96d08 100644 --- a/chrome/content/zotero/xpcom/quickCopy.js +++ b/chrome/content/zotero/xpcom/quickCopy.js @@ -40,7 +40,9 @@ Zotero.QuickCopy = new function() { if (!_initialized) { // Make sure export translator code is loaded whenever the output format changes - Zotero.Prefs.registerObserver("export.quickCopy.setting", _loadOutputFormat); + this._prefObserverID = Zotero.Prefs.registerObserver( + "export.quickCopy.setting", _loadOutputFormat + ); _initialized = true; } @@ -71,7 +73,7 @@ Zotero.QuickCopy = new function() { if (_initPromise) { _initPromise.cancel(); } - Zotero.Prefs.unregisterObserver("export.quickCopy.setting", _loadOutputFormat); + Zotero.Prefs.unregisterObserver(this._prefObserverID); };