Skip to content

Commit

Permalink
fixes after merge with remote
Browse files Browse the repository at this point in the history
  • Loading branch information
nerfZael committed Sep 27, 2022
1 parent dadd0f8 commit 0916400
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 63 deletions.
3 changes: 1 addition & 2 deletions packages/cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

import * as Commands from "./commands";

import { executeMaybeAsyncFunction } from "@polywrap/core-js";
import { program } from "commander";

export const run = async (argv: string[]): Promise<void> => {
for (const command of Object.values(Commands)) {
if ("setup" in command) {
await executeMaybeAsyncFunction(command.setup, program);
await command.setup(program);
}
}

Expand Down
3 changes: 2 additions & 1 deletion packages/cli/src/commands/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { MaybeAsync } from "@polywrap/core-js";
import { Command as Program, Argument } from "commander";

export { Program, Argument };

export interface Command {
setup: (program: Program) => void;
setup: (program: Program) => MaybeAsync<void>;
}
6 changes: 1 addition & 5 deletions packages/cli/src/lib/option-parsers/client-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { intlMsg } from "../intl";
import { importTypescriptModule } from "../system";
import { getTestEnvClientConfig } from "../test-env";

import { executeMaybeAsyncFunction } from "@polywrap/core-js";
import { PolywrapClientConfig } from "@polywrap/client-js";
import path from "path";

Expand Down Expand Up @@ -43,10 +42,7 @@ export async function parseClientConfigOption(
process.exit(1);
}

finalClientConfig = await executeMaybeAsyncFunction(
configModule.getClientConfig,
finalClientConfig
);
finalClientConfig = await configModule.getClientConfig(finalClientConfig);

try {
validateClientConfig(finalClientConfig);
Expand Down
8 changes: 4 additions & 4 deletions packages/cli/src/lib/workflow/JobRunner.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { JobResult, JobStatus, Step } from "./types";

import { PolywrapClient } from "@polywrap/client-js";
import { executeMaybeAsyncFunction, MaybeAsync } from "@polywrap/core-js";
import { Client, MaybeAsync } from "@polywrap/core-js";
import { WorkflowJobs } from "@polywrap/polywrap-manifest-types-js";
import { ClientConfigBuilder } from "@polywrap/client-config-builder-js";
import { PolywrapClient } from "@polywrap/client-js";

export class JobRunner {
private jobOutput: Map<string, JobResult>;

constructor(
private client: PolywrapClient,
private client: Client,
private onExecution?: (id: string, JobResult: JobResult) => MaybeAsync<void>
) {
this.jobOutput = new Map();
Expand Down Expand Up @@ -208,7 +208,7 @@ export class JobRunner {
this.jobOutput.set(absId, result);

if (this.onExecution) {
await executeMaybeAsyncFunction(this.onExecution, absId, result);
await this.onExecution(absId, result);
}
}
}
Expand Down
43 changes: 24 additions & 19 deletions packages/js/core/src/__tests__/MaybeAsync.spec.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,45 @@
import {
MaybeAsync,
executeMaybeAsyncFunction,
isPromise,
} from "..";
import { MaybeAsync, isPromise } from "..";

class ClassInstance {
interface IClassInterface {
normalMethod(arg: string): MaybeAsync<string>;
asyncMethod(arg: string): MaybeAsync<string>;
}

class ClassInstance implements IClassInterface {
constructor(private _prop: string) {}

normalMethod(arg: string): string {
return this._prop + arg;
}

async asyncMethod(arg: string): Promise<string> {
await new Promise((resolve) =>
setTimeout(resolve, 200)
);
await new Promise((resolve) => setTimeout(resolve, 200));

return this._prop + arg;
}
}

describe("MaybeAsync", () => {
const promise: MaybeAsync<string> =
new Promise<string>((resolve, reject) => { return "" });
const testFunction = (args: unknown[]) => { return "foo" };
const testFunctionReturnPromise = (args: unknown[]) => new Promise<string>((resolve) => { resolve("foo") });
const promise: MaybeAsync<string> = new Promise<string>((resolve, reject) => {
return "";
});
const testFunction = (): MaybeAsync<string> => {
return "foo";
};
const testFunctionReturnPromise = (): MaybeAsync<string> =>
new Promise<string>((resolve) => {
resolve("foo");
});

it("sanity", async () => {
expect(isPromise(promise)).toBe(true);
expect(await executeMaybeAsyncFunction(testFunction)).toBe("foo");
expect(await executeMaybeAsyncFunction(testFunctionReturnPromise)).toBe("foo");
expect(await testFunction()).toBe("foo");
expect(await testFunctionReturnPromise()).toBe("foo");
});

it("works with class instances", async () => {
const instance = new ClassInstance("bar");
expect(await executeMaybeAsyncFunction(instance.normalMethod.bind(instance, "foo"))).toBe("barfoo")
expect(await executeMaybeAsyncFunction(instance.asyncMethod.bind(instance, "foo"))).toBe("barfoo")
})
const instance: IClassInterface = new ClassInstance("bar");
expect(await instance.normalMethod("foo")).toBe("barfoo");
expect(await instance.asyncMethod("foo")).toBe("barfoo");
});
});
17 changes: 0 additions & 17 deletions packages/js/core/src/types/MaybeAsync.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1 @@
export type MaybeAsync<T> = Promise<T> | T;

export const isPromise = <T extends unknown>(
test?: MaybeAsync<T>
): test is Promise<T> =>
!!test && typeof (test as Promise<T>).then === "function";

// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
export const executeMaybeAsyncFunction = async <T extends unknown>(
func: (...args: unknown[]) => Promise<T> | T,
...args: unknown[]
): Promise<T> => {
let result = func(...args);
if (isPromise(result)) {
result = await result;
}
return result;
};
6 changes: 2 additions & 4 deletions packages/js/core/src/types/Plugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { Client, MaybeAsync, executeMaybeAsyncFunction } from ".";
import { Client, MaybeAsync } from ".";

import { WrapManifest } from "@polywrap/wrap-manifest-types-js";
import { Result, ResultErr, ResultOk } from "@polywrap/result";
Expand Down Expand Up @@ -60,9 +60,7 @@ export abstract class PluginModule<
);
}

const data = await executeMaybeAsyncFunction<TResult>(
fn.bind(this, args, client)
);
const data = await fn(args, client);
return ResultOk(data);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import {
IUriResolver,
Uri,
Client,
executeMaybeAsyncFunction,
Wrapper,
IUriResolutionContext,
UriPackageOrWrapper,
UriResolutionResult,
Expand Down Expand Up @@ -39,9 +37,7 @@ export class PackageToWrapperCacheResolver implements IUriResolver<Error> {
client: Client,
resolutionContext: IUriResolutionContext
): Promise<Result<UriPackageOrWrapper, Error>> {
const wrapper = await executeMaybeAsyncFunction<Wrapper | undefined>(
this.cache.get.bind(this.cache, uri)
);
const wrapper = await this.cache.get(uri);

if (wrapper) {
const result = UriResolutionResult.ok(uri, wrapper);
Expand Down Expand Up @@ -78,9 +74,7 @@ export class PackageToWrapperCacheResolver implements IUriResolver<Error> {
const wrapper = createResult.value;

for (const uri of resolutionPath) {
await executeMaybeAsyncFunction<Wrapper | undefined>(
this.cache.set.bind(this.cache, uri, wrapper)
);
await this.cache.set(uri, wrapper);
}

result = UriResolutionResult.ok(result.value.uri, wrapper);
Expand All @@ -89,9 +83,7 @@ export class PackageToWrapperCacheResolver implements IUriResolver<Error> {
const resolutionPath: Uri[] = subContext.getResolutionPath();

for (const uri of resolutionPath) {
await executeMaybeAsyncFunction<Wrapper | undefined>(
this.cache.set.bind(this.cache, uri, wrapper)
);
await this.cache.set(uri, wrapper);
}
}
}
Expand Down

0 comments on commit 0916400

Please sign in to comment.