Use Symbol for pref observer deregistration

Zotero.Prefs.registerObserver() now returns a Symbol that can be passed
to Zotero.Prefs.unregisterObserver().
This commit is contained in:
Dan Stillman 2019-08-12 13:21:18 -04:00
parent eb50067a41
commit 6f721098f8
4 changed files with 32 additions and 17 deletions

View file

@ -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 = {

View file

@ -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();

View file

@ -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);
}
}

View file

@ -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);
};