Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add resolver.resolveAsync and multiArgs #315

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 37 additions & 4 deletions lib/Resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,17 +225,22 @@ class Resolver {
* @param {object} context context information object
* @param {string} path context path
* @param {string} request request string
* @returns {string | false} result
* @param {ResolveContext} [resolveContext] resolve context
* @param {boolean} [multiArgs] multiArgs
* @returns {string | false | [ string | false, ResolveRequest | undefined]} result
*/
resolveSync(context, path, request) {
resolveSync(context, path, request, resolveContext, multiArgs) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why we need multiArgs?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alexander-akait

  1. Callback wrapper, like bluebird, wrap callback base api into promise api, optionally accept multiArgs.
Promise.promisify(
    function(any arguments..., function callback) nodeFunction,
    [Object {
        multiArgs: boolean=false,
        context: any=this
    } options]
) -> function

So, I think wrap a callback to .resolveAsync accept multiArgs should be better.

  1. Because of opinion 1, for api compact, I think .resolveSync accept Parameters<Resolver['resolveAsync']>, and return Awaited<ReturnType<Resolver['resolveAsync']>>. So, I think wrap a callback to .resolveSync should accept multiArgs.
  2. Also resolveRequest contains meta like .descriptionFileData, useful for further sideEffects or else meta parsing

/** @type {Error | null | undefined} */
let err = undefined;
/** @type {string | false | undefined} */
let result = undefined;
/** @type {ResolveRequest | undefined} */
let resolveRequest = undefined;
let sync = false;
this.resolve(context, path, request, {}, (e, r) => {
this.resolve(context, path, request, resolveContext ?? {}, (e, r, rr) => {
err = e;
result = r;
resolveRequest = rr;
sync = true;
});
if (!sync) {
Expand All @@ -245,7 +250,35 @@ class Resolver {
}
if (err) throw err;
if (result === undefined) throw new Error("No result");
return result;
if (multiArgs) {
return [result, resolveRequest];
} else {
return result;
}
}

/**
* @param {object} context context information object
* @param {string} path context path
* @param {string} request request string
* @param {ResolveContext} [resolveContext] resolve context
* @param {boolean} [multiArgs] multiArgs
* @returns {Promise<string | false | [ string | false, ResolveRequest | undefined]>}
*/
resolveAsync(context, path, request, resolveContext, multiArgs) {
return new Promise((resolve, reject) => {
this.resolve(context, path, request, resolveContext ?? {}, (err, result, resolveRequest) => {
if(err) {
reject(err)
} else if (result === undefined) {
reject(new Error("No result"))
} else if (multiArgs) {
resolve([result, resolveRequest])
} else {
resolve(result)
}
})
})
}

/**
Expand Down
66 changes: 66 additions & 0 deletions test/multiArgs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
require("should");

var { Volume } = require("memfs");
var { ResolverFactory } = require("../");

describe("multiArgs", function () {
/**
* @type {ReturnType<ResolverFactory['createResolver']>}
*/
var resolver;

beforeEach(function () {
var fileSystem = Volume.fromJSON(
{
"/a/index": "",
"/a/dir/index": "",
"/recursive/index": "",
"/recursive/dir/index": "",
"/b/index": "",
"/b/dir/index": "",
"/c/index": "",
"/c/dir/index": "",
"/d/index.js": "",
"/d/dir/.empty": "",
"/e/index": "",
"/e/anotherDir/index": "",
"/e/dir/file": ""
},
"/"
);
resolver = ResolverFactory.createResolver({
alias: {
aliasA: "a",
b$: "a/index",
c$: "/a/index",
multiAlias: ["b", "c", "d", "e", "a"],
recursive: "recursive/dir",
"/d/dir": "/c/dir",
"/d/index.js": "/c/index",
ignored: false
},
modules: "/",
useSyncFileSystemCalls: true,
fileSystem: fileSystem
});
});

it("should resolver.resolveSync parse multiArgs", () => {
resolver.resolveSync({}, "/", "a").should.be.eql("/a/index");
resolver.resolveSync({}, "/", "a", {}).should.be.eql("/a/index");
resolver.resolveSync({}, "/", "a", {}, false).should.be.eql("/a/index");

Array.isArray(resolver.resolveSync({}, "/", "a", {}, true)).should.be.eql(true);
resolver.resolveSync({}, "/", "a", {}, true)[0].should.be.eql("/a/index");
resolver.resolveSync({}, "/", "a", {}, true).length.should.be.eql(2);
});
it("should resolver.resolveAsync parse multiArgs", async () => {
(await resolver.resolveAsync({}, "/", "a")).should.be.eql("/a/index");
(await resolver.resolveAsync({}, "/", "a", {})).should.be.eql("/a/index");
(await resolver.resolveAsync({}, "/", "a", {}, false)).should.be.eql("/a/index");

Array.isArray((await resolver.resolveAsync({}, "/", "a", {}, true))).should.be.eql(true);
(await resolver.resolveAsync({}, "/", "a", {}, true))[0].should.be.eql("/a/index");
(await resolver.resolveAsync({}, "/", "a", {}, true)).length.should.be.eql(2);
});
});
32 changes: 31 additions & 1 deletion types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,37 @@ declare abstract class Resolver {
[ResolveRequest, ResolveContext],
null | ResolveRequest
>;
resolveSync(context: object, path: string, request: string): string | false;

resolveSync(
context: object,
path: string,
request: string,
resolveContext?: ResolveContext,
multiArgs?: false
): string | false;
resolveSync(
context: object,
path: string,
request: string,
resolveContext: ResolveContext,
multiArgs: true
): [result: string | false, ressolveRequest: ResolveRequest | undefined];

resolveAsync(
context: object,
path: string,
request: string,
resolveContext?: ResolveContext,
multiArgs?: false
): Promise<string | false>
resolveAsync(
context: object,
path: string,
request: string,
resolveContext: ResolveContext,
multiArgs: true
): Promise<[result: string | false, ressolveRequest: ResolveRequest | undefined]>

resolve(
context: object,
path: string,
Expand Down