prochain icon indicating copy to clipboard operation
prochain copied to clipboard

[bug] the library is not working with constructors

Open chathurabuddi opened this issue 2 years ago • 1 comments

import { wrap } from 'prochain';

class A {
    /** sync method */
    constructor(output) {
        this.output = output;
    }
    foo() {
        this.output += '1'
        return this
    }

    /** async method */
    async bar() {
        this.output += '2'
        return this
    }

    /** property */
    baz = 3

    /** getter */
    get qux() {
        return this.baz
    }
}

const test = async () => {
    const a = wrap(new A('1'));
    const b = await a.foo().bar().foo().bar().bar();
    console.log(b);
}

test();

Error

return wrap(promise.then(target => Reflect.apply(target, thisArg, args)));
                                          ^
TypeError: Function.prototype.apply was called on undefined, which is a undefined and not a function

chathurabuddi avatar Jan 27 '23 17:01 chathurabuddi

Hey @chathurabuddi, I've created a library that works similarly but not identical to this one. It's called dwait and I've tried to build upon this technique and added support for things like string symbols(for example, the split function which uses a Symbol.split shows the wrong types in the prochain) and a simple way of extracting the native Promise besides using something like unwrap function. There are some performance limitations with this approach(using Proxy objects). Still, it has been done successfully before in the vue reactivity events(for intercepting object updates) using a WeakMap to avoid additional wrapping, so we are going to do a similar thing in the dwait; We will cache and reuse the old instances that are already in use and are not garbage collected.

Other than performance there are some tricky aspects in both javascript and typescript which makes supporting 100% of the specs really hard but for the time being with the use of highly expressive type annotations of typescript, we can catch most of the issues long before they even hit the browser/nodejs.

Most of these issues are actually easy to solve once we detect them, The hard part is writing the full suite of tests that can detect all these issues; Even tho dwait has 100% code coverage I'm sure there are more edge cases yet to be found.

Here is the link to the project. https://github.com/rzvxa/dwait https://www.npmjs.com/package/dwait

It can also be used with another library of mine which helps with error handling with a Rust/Go scheme. https://github.com/rzvxa/tryumph https://www.npmjs.com/package/tryumph

I would love to hear some early feedback from you.

rzvxa avatar Jan 01 '24 06:01 rzvxa