davideicardi / live-plugin-manager

Plugin manager and installer for Node.JS
MIT License
225 stars 43 forks source link

Async function #19

Closed xxczaki closed 4 years ago

xxczaki commented 4 years ago

Hi,

I installed an AsyncFunction-returning package. Unfortunately, when I try to use it (via manager.require(name).default, it shows [Function], instead of [AsyncFunction].

davideicardi commented 4 years ago

Can you provice me a package example to reproduce the problem. Or best of all a unit test.. Thanks!

xxczaki commented 4 years ago

@davideicardi Here is my use case:

I have a constructor, which receives an array of async functions (plugins) as an argument. I save a list of plugin names in the storage, from which I install all of them:

await manager.install('@parsify/date');

// ...

// Plugins storage (names)
const plugins = await store.get('plugins') || [];

// This array gets passed to the constructor
// I think this is why it returns a synchronous function
const pluginsArray = plugins.map((name: string) => manager.require(name).default);

const parsify = new Parsify([
    examplePlugin(),
    ...pluginsArray
]);

// Let's say I have just 1 custom plugin installed (@parsify/date)
// The AsyncFunction comes from the `examplePlugin()`, where's the Function comes from the array.
console.log(parsify); //=> Parsify { plugins: [ [AsyncFunction], [Function] ] }

Here is the code for the package, which I install: https://github.com/parsify-dev/date/blob/master/src/index.ts

As you can see, it exports an async function.

xxczaki commented 4 years ago

I found a solution, turns out the problem was my fault:

Instead of:

manager.require(name).default

I should have used

manager.require(name).default()

This way the actual exported function is fetched :smile: