Giter Site home page Giter Site logo

TypeScript Examples about hookable HOT 7 OPEN

unjs avatar unjs commented on June 20, 2024 1
TypeScript Examples

from hookable.

Comments (7)

pi0 avatar pi0 commented on June 20, 2024 2

@danielroe Can you please help on this issue? 🙏🏼

from hookable.

abarke avatar abarke commented on June 20, 2024 1

@pi0 any feedback would be most appreciated 🙂

from hookable.

MichaelGitArt avatar MichaelGitArt commented on June 20, 2024 1

It would be really helpful to see typescript examples 👀

from hookable.

abarke avatar abarke commented on June 20, 2024

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.

abarke avatar abarke commented on June 20, 2024

Also I have noticed that although my JetBrains IDE seems to think that callHook always returns a Promise, this is simply not the case.

I get this message:
image

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...

  1. There is a registered hook
  2. 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.

kaikaibenkai avatar kaikaibenkai commented on June 20, 2024

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.

affinage-digital avatar affinage-digital commented on June 20, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.