diff --git a/spec/store/events/off-spec.js b/spec/store/events/off-spec.js index 38a56c7..60eba2c 100644 --- a/spec/store/events/off-spec.js +++ b/spec/store/events/off-spec.js @@ -31,12 +31,12 @@ describe("off", function() { }); store.remove("products", "1"); expect(listener.handler.calls.count()).toEqual(6); - store.off("added", "products", "1", listener.handler, context); - store.off("added", "products", listener.handler, context); - store.off("updated", "products", "1", listener.handler, context); - store.off("updated", "products", listener.handler, context); - store.off("removed", "products", "1", listener.handler, context); - store.off("removed", "products", listener.handler, context); + store.off("added", "products", "1", listener.handler); + store.off("added", "products", listener.handler); + store.off("updated", "products", "1", listener.handler); + store.off("updated", "products", listener.handler); + store.off("removed", "products", "1", listener.handler); + store.off("removed", "products", listener.handler); store.add({ "type": "products", "id": "1" @@ -49,8 +49,16 @@ describe("off", function() { expect(listener.handler.calls.count()).toEqual(6); }); - it("must allow context to be optional"); + it("must throw an error when an unknown event is passed", function () { + expect(function () { + store.off("foo", "products", "1", listener.handler); + }).toThrowError("Unknown event 'foo'"); + }); - it("must throw an error when an unknown event is passed"); + it("must throw an error if the type has not been defined", function () { + expect(function () { + store.off("added", "foo", "1", listener.handler); + }).toThrowError("Unknown type 'foo'"); + }); }); diff --git a/spec/store/events/on-spec.js b/spec/store/events/on-spec.js index 74df3bd..bccdb00 100644 --- a/spec/store/events/on-spec.js +++ b/spec/store/events/on-spec.js @@ -94,6 +94,16 @@ describe("on", function() { expect(listener.handler.calls.count()).toEqual(0); }); - it("must throw an error when an unknown event is passed"); + it("must throw an error when an unknown event is passed", function () { + expect(function () { + store.on("foo", "products", "1", listener.handler, context); + }).toThrowError("Unknown event 'foo'"); + }); + + it("must throw an error if the type has not been defined", function () { + expect(function () { + store.on("added", "foo", "1", listener.handler, context); + }).toThrowError("Unknown type 'foo'"); + }); }); diff --git a/src/.eslintrc b/src/.eslintrc index 598ee8f..74ee87d 100644 --- a/src/.eslintrc +++ b/src/.eslintrc @@ -56,7 +56,7 @@ "no-with": 2, "radix": 2, "semi": 2, - "valid-jsdoc": 1, + "valid-jsdoc": 2, "vars-on-top": 2, "wrap-iife": 2, "yoda": 2 diff --git a/src/store.js b/src/store.js index 6d27968..70e276c 100644 --- a/src/store.js +++ b/src/store.js @@ -202,24 +202,31 @@ export default class Store { * @param {string} type - Name of resource to originally passed to on(). * @param {string} [id] - ID of the resource to originally passed to on(). * @param {function} callback - Function originally passed to on(). - * @param {Object} [context] - Context originally passed to on(). * @return {undefined} - Nothing. */ - off(event, type, id, callback, context) { - if (id && ({}).toString.call(id) === '[object Function]') { - this.off.call(this, event, type, null, id, callback); - } else { - if (id) { - if (this._resourceListeners[event][type] && this._resourceListeners[event][type][id]) { - this._resourceListeners[event][type][id] = this._resourceListeners[event][type][id].filter(x => { - return !(x[0] === callback && x[1] === context); - }); + off(event, type, id, callback) { + if (this._resourceListeners[event] && this._collectionListeners[event]) { + if (this._types[type]) { + if (id && ({}).toString.call(id) === '[object Function]') { + this.off.call(this, event, type, null, id, callback); + } else { + if (id) { + if (this._resourceListeners[event][type] && this._resourceListeners[event][type][id]) { + this._resourceListeners[event][type][id] = this._resourceListeners[event][type][id].filter(x => { + return x[0] !== callback; + }); + } + } else if (this._collectionListeners[event][type]) { + this._collectionListeners[event][type] = this._collectionListeners[event][type].filter(x => { + return x[0] !== callback; + }); + } } - } else if (this._collectionListeners[event][type]) { - this._collectionListeners[event][type] = this._collectionListeners[event][type].filter(x => { - return !(x[0] === callback && x[1] === context); - }); + } else { + throw new Error(`Unknown type '${type}'`); } + } else { + throw new Error(`Unknown event '${event}'`); } } @@ -235,17 +242,25 @@ export default class Store { * @return {undefined} - Nothing. */ on(event, type, id, callback, context) { - if (id && ({}).toString.call(id) === '[object Function]') { - this.on.call(this, event, type, null, id, callback); - } else { - if (id) { - this._resourceListeners[event][type] = this._resourceListeners[event][type] || {}; - this._resourceListeners[event][type][id] = this._resourceListeners[event][type][id] || []; - this._resourceListeners[event][type][id].push([ callback, context ]); + if (this._resourceListeners[event] && this._collectionListeners[event]) { + if (this._types[type]) { + if (id && ({}).toString.call(id) === '[object Function]') { + this.on.call(this, event, type, null, id, callback); + } else { + if (id) { + this._resourceListeners[event][type] = this._resourceListeners[event][type] || {}; + this._resourceListeners[event][type][id] = this._resourceListeners[event][type][id] || []; + this._resourceListeners[event][type][id].push([ callback, context ]); + } else { + this._collectionListeners[event][type] = this._collectionListeners[event][type] || []; + this._collectionListeners[event][type].push([ callback, context ]); + } + } } else { - this._collectionListeners[event][type] = this._collectionListeners[event][type] || []; - this._collectionListeners[event][type].push([ callback, context ]); + throw new Error(`Unknown type '${type}'`); } + } else { + throw new Error(`Unknown event '${event}'`); } }