Skip to content

Commit

Permalink
Adding computed helper on hook.
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianFun123 committed Nov 29, 2024
1 parent a39ea7c commit 1fc8493
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
29 changes: 27 additions & 2 deletions src/Hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}
}
}
Expand All @@ -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
*/
Expand Down

0 comments on commit 1fc8493

Please sign in to comment.