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:
parent
eb50067a41
commit
6f721098f8
4 changed files with 32 additions and 17 deletions
|
@ -26,7 +26,7 @@ Zotero.TagSelector = class TagSelectorContainer extends React.PureComponent {
|
||||||
['collection-item', 'item', 'item-tag', 'tag', 'setting'],
|
['collection-item', 'item', 'item-tag', 'tag', 'setting'],
|
||||||
'tagSelector'
|
'tagSelector'
|
||||||
);
|
);
|
||||||
Zotero.Prefs.registerObserver('fontSize', this.handleFontChange.bind(this));
|
this._prefObserverID = Zotero.Prefs.registerObserver('fontSize', this.handleFontChange.bind(this));
|
||||||
|
|
||||||
this.tagListRef = React.createRef();
|
this.tagListRef = React.createRef();
|
||||||
this.searchBoxRef = React.createRef();
|
this.searchBoxRef = React.createRef();
|
||||||
|
@ -731,12 +731,8 @@ Zotero.TagSelector = class TagSelectorContainer extends React.PureComponent {
|
||||||
|
|
||||||
uninit() {
|
uninit() {
|
||||||
ReactDOM.unmountComponentAtNode(this.domEl);
|
ReactDOM.unmountComponentAtNode(this.domEl);
|
||||||
if (this._notifierID) {
|
Zotero.Notifier.unregisterObserver(this._notifierID);
|
||||||
Zotero.Notifier.unregisterObserver(this._notifierID);
|
Zotero.Prefs.unregisterObserver(this._prefObserverID);
|
||||||
}
|
|
||||||
if (this._prefObserverID) {
|
|
||||||
Zotero.Prefs.unregisterObserver('fontSize', this.handleFontChange.bind(this));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static propTypes = {
|
static propTypes = {
|
||||||
|
|
|
@ -53,12 +53,13 @@ Zotero.ItemTreeView = function (collectionTreeRow) {
|
||||||
|
|
||||||
this._refreshPromise = Zotero.Promise.resolve();
|
this._refreshPromise = Zotero.Promise.resolve();
|
||||||
|
|
||||||
this._unregisterID = Zotero.Notifier.registerObserver(
|
this._notifierObserverID = Zotero.Notifier.registerObserver(
|
||||||
this,
|
this,
|
||||||
['item', 'collection-item', 'item-tag', 'share-items', 'bucket', 'feedItem', 'search'],
|
['item', 'collection-item', 'item-tag', 'share-items', 'bucket', 'feedItem', 'search'],
|
||||||
'itemTreeView',
|
'itemTreeView',
|
||||||
50
|
50
|
||||||
);
|
);
|
||||||
|
this._prefObserverID = Zotero.Prefs.registerObserver('recursiveCollections', this.refresh.bind(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
Zotero.ItemTreeView.prototype = Object.create(Zotero.LibraryTreeView.prototype);
|
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.ItemTreeView.prototype.unregister = async function() {
|
||||||
Zotero.Notifier.unregisterObserver(this._unregisterID);
|
Zotero.Notifier.unregisterObserver(this._notifierObserverID);
|
||||||
|
Zotero.Prefs.unregisterObserver(this._prefObserverID);
|
||||||
|
|
||||||
if (this.collectionTreeRow.onUnload) {
|
if (this.collectionTreeRow.onUnload) {
|
||||||
await this.collectionTreeRow.onUnload();
|
await this.collectionTreeRow.onUnload();
|
||||||
|
|
|
@ -274,24 +274,39 @@ Zotero.Prefs = new function(){
|
||||||
}
|
}
|
||||||
|
|
||||||
var _observers = {};
|
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] = _observers[name] || [];
|
||||||
_observers[name].push(handler);
|
_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) {
|
if (!obs) {
|
||||||
Zotero.debug("No preferences observer registered for " + name);
|
Zotero.debug("No pref observer registered for given symbol");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
delete _observersBySymbol[symbol];
|
||||||
|
|
||||||
|
var [name, handler] = obs;
|
||||||
var i = obs.indexOf(handler);
|
var i = obs.indexOf(handler);
|
||||||
if (i == -1) {
|
if (i == -1) {
|
||||||
Zotero.debug("Handler was not registered for preference " + name);
|
Zotero.debug("Handler was not registered for preference " + name, 2);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
obs.splice(i, 1);
|
obs.splice(i, 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,7 +40,9 @@ Zotero.QuickCopy = new function() {
|
||||||
|
|
||||||
if (!_initialized) {
|
if (!_initialized) {
|
||||||
// Make sure export translator code is loaded whenever the output format changes
|
// 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;
|
_initialized = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -71,7 +73,7 @@ Zotero.QuickCopy = new function() {
|
||||||
if (_initPromise) {
|
if (_initPromise) {
|
||||||
_initPromise.cancel();
|
_initPromise.cancel();
|
||||||
}
|
}
|
||||||
Zotero.Prefs.unregisterObserver("export.quickCopy.setting", _loadOutputFormat);
|
Zotero.Prefs.unregisterObserver(this._prefObserverID);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue