storage.js: Don't use .bind() to define a callbacks context

This commit is contained in:
Johannes Meyer 2019-07-08 13:34:38 +02:00
parent 03fc052749
commit 8377a2d096

View File

@ -96,11 +96,12 @@
* *
* @param {string} key * @param {string} key
* @param {function} callback * @param {function} callback
* @param {object} context
* *
* @returns {void} * @returns {void}
*/ */
subscribe: function(key, callback) { subscribe: function(key, callback, context) {
this.subscribers[this.prefixKey(key)] = callback; this.subscribers[this.prefixKey(key)] = [callback, context];
}, },
/** /**
@ -110,7 +111,8 @@
*/ */
onStorage: function(event) { onStorage: function(event) {
if (typeof this.subscribers[event.key] !== 'undefined') { if (typeof this.subscribers[event.key] !== 'undefined') {
this.subscribers[event.key](JSON.parse(event.oldValue), JSON.parse(event.newValue)); var subscriber = this.subscribers[event.key];
subscriber[0].call(subscriber[1], JSON.parse(event.newValue), JSON.parse(event.oldValue), event);
} }
}, },
@ -198,7 +200,7 @@
this.storage = storage; this.storage = storage;
this.key = key; this.key = key;
storage.subscribe(key, this.onChange.bind(this)); storage.subscribe(key, this.onChange, this);
return this; return this;
}, },
@ -214,10 +216,9 @@
/** /**
* Update the set * Update the set
* *
* @param {Array} oldValue
* @param {Array} newValue * @param {Array} newValue
*/ */
onChange: function(oldValue, newValue) { onChange: function(newValue) {
// Check for deletions first // Check for deletions first
this.values().forEach(function (value) { this.values().forEach(function (value) {
if (newValue.indexOf(value) < 0) { if (newValue.indexOf(value) < 0) {