diff --git a/package.json b/package.json index 6f3241d..0016fd1 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "jdomjs", - "version": "3.1.13", - + "version": "3.1.14", + "description": "A wrapper for query selector and html elements", "main": "./index.js", "exports": { diff --git a/src/Hook.js b/src/Hook.js index 8c8ab73..c4a86e2 100644 --- a/src/Hook.js +++ b/src/Hook.js @@ -110,8 +110,7 @@ export default class Hook { dispatchListener(oldVal) { for (let listener of this.listeners) { try { - if (listener.call(this, this._value, oldVal) === true) - break + listener.call(this, this._value, oldVal) } catch (e) {} } } @@ -136,6 +135,32 @@ export default class Hook { return `${this.value}` } + /** + * computed((val) => `Hello ${val}`) + * + * @param {(val: T) => any} fn + * @return {Hook} + */ + computed(fn) { + const computedHook = new Hook(fn(this.value)) + + this.addListener(() => { + computedHook.value = fn(this.value) + }) + + return computedHook + } + + /** + * shorthand for computed + * + * @param {(val: T) => any} fn + * @return {Hook} + */ + $(fn) { + return this.computed(fn) + } + /** * @param {Hook} hook */