Comments (7)
@danielroe Can you please help on this issue? 🙏🏼
from hookable.
@pi0 any feedback would be most appreciated 🙂
from hookable.
It would be really helpful to see typescript examples 👀
from hookable.
Just realized that HookCallback
expects a function that returns Promise<void> | void
,
however in the linked example that it seems you can return a value when callHook
resolves.
export type HookCallback = (...args: any) => Promise<void> | void
https://github.com/unjs/hookable/blob/main/src/types.ts#L1
Is that expected?
from hookable.
Also I have noticed that although my JetBrains IDE seems to think that callHook
always returns a Promise, this is simply not the case.
However one would simply go ahead and put a then()
on the end and do something like call a logging method. e.g.
hooks
.callHook(Hook.BEFORE_SERIALIZED, msg)
.then(() => console.log("test"))
This only works if two conditions are met...
- There is a registered hook
- When adding a hook the callBack function is a Promise e.g...
hooks.hook(Hook.BEFORE_SERIALIZED, async (msg) => {
console.log("✅", msg.Message)
})
If any of these conditions are not met then a runtime error occurs (there is no then()
on undefined
).
The type definition
callHook<NameT extends HookNameT>(name: NameT, ...args: Parameters<InferCallback<HooksT, NameT>>): Promise<any>;
should perhaps be
callHook<NameT extends HookNameT>(name: NameT, ...args: Parameters<InferCallback<HooksT, NameT>>): void | Promise<any>;
I edited the type def in Hookable directly in my IDE and the ignored promise warning is gone 🎉
We are building a library that exports the hooks in the config so it's quite normal to have unregistered hooks (noop).
Regarding points 1... The core lib where callHook()
is used cannot be expected to return a Promise, therefore cannot be used with .then()
or await
. Reason: If no hooks are registered then one can not expect a Promise. However this should potentially be the default behavior when callHook()
is called without a registered hook. Simply return a Promise.resolve() by default should this be expected.
Regarding point 2... The hook()
method may be used in another plugin somewhere and should it not use a async method callback then it would break the whole app as I would call .then()
on undefined
.
from hookable.
Could anyone answer this
In some cases, certain operations require consultation with hooks beforehand.
For example:
const msg = await app.callHook('user:login:before');
if (msg === undefined) {
Message.success('Hi, you are logged in!');
} else {
Message.error('Oops!' + msg);
return;
}
// ...
@danielroe 🙏🙏
from hookable.
Some of working sample:
import { Hookable } from 'hookable';
export class Parser extends Hookable<{
/** super tsdoc comment working too */
someOneHook: (rows: string[]) => void;
}> {
rows: string[] = [];
constructor() {
super(); // init instance of Hookable
}
getRows() {
this.rows = ['a', 'b', 'c'];
this.callHook('someOneHook', this.rows);
}
}
import { Parser } from '...';
const parser = new Parser();
// subscribe
const unregisterSomeOneHook = parser.hook('someOneHook', rows => console.log(rows));
parser.getRows();
// unsubscribe
unregisterSomeOneHook();
from hookable.
Related Issues (19)
- Cannot find module 'hable' HOT 1
- Fully typed nested/namespaced hooks (for `addHooks`) HOT 2
- `callHookSync`
- Allow differed hook calling
- hook events
- Action Required: Fix Renovate Configuration
- Dependency Dashboard
- closed
- Debugger
- Is there a way to clear all hooks? HOT 3
- Debugger handling parallel hook calls with same name
- Type 'HooksT' does not satisfy the constraint 'Record<string, any>' HOT 4
- Synchronous Hooks HOT 4
- set caller
- Allow to return value from callback function HOT 1
- Uncaught SyntaxError: Unexpected token { HOT 2
- Typescript: Template class extend Hookable type issue
- Allow register hooks with `enforce: before|after`
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from hookable.