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

fix: support optional async WrapperCache methods #1196

Merged
merged 4 commits into from
Aug 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 4 additions & 1 deletion packages/js/client/src/PolywrapClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
RunOptions,
GetManifestOptions,
SimpleCache,
executeMaybeAsyncFunction,
} from "@polywrap/core-js";
import { msgpackEncode, msgpackDecode } from "@polywrap/msgpack-js";
import { WrapManifest } from "@polywrap/wrap-manifest-types-js";
Expand Down Expand Up @@ -481,7 +482,9 @@ export class PolywrapClient implements Client {
// Update cache for all URIs in the chain
if (cacheWrite && wrapper) {
const uris = uriHistory.getResolutionPath().stack.map((x) => x.sourceUri);
this._wrapperCache.set(uris, wrapper);
await executeMaybeAsyncFunction(
this._wrapperCache.set.bind(this._wrapperCache, uris, wrapper)
);
}

if (shouldClearContext) {
Expand Down
32 changes: 27 additions & 5 deletions packages/js/core/src/__tests__/MaybeAsync.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,37 @@ import {
isPromise,
} from "..";

class ClassInstance {
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)
);

return this._prop + arg;
}
}

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

it("sanity", () => {
it("sanity", async () => {
expect(isPromise(promise)).toBe(true);
expect(executeMaybeAsyncFunction(testFunction)).toBeDefined();
expect(executeMaybeAsyncFunction(testFunctionReturnPromise)).toBeDefined();
expect(await executeMaybeAsyncFunction(testFunction)).toBe("foo");
expect(await executeMaybeAsyncFunction(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")
})
});
10 changes: 5 additions & 5 deletions packages/js/core/src/types/WrapperCache.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Uri, Wrapper } from ".";
import { Uri, Wrapper, MaybeAsync } from ".";

export interface WrapperCache {
get(uri: Uri): Wrapper | undefined;
has(uri: Uri): boolean;
set(uris: Uri[], wrapper: Wrapper): void;
set(uri: Uri, wrapper: Wrapper): void;
get(uri: Uri): MaybeAsync<Wrapper | undefined>;
has(uri: Uri): MaybeAsync<boolean>;
set(uris: Uri[], wrapper: Wrapper): MaybeAsync<void>;
set(uri: Uri, wrapper: Wrapper): MaybeAsync<void>;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { Uri, Client, WrapperCache } from "../../../types";
import {
Uri,
Client,
WrapperCache,
executeMaybeAsyncFunction,
Wrapper,
} from "../../../types";
import { UriResolver, UriResolutionResult } from "../../core";

export class CacheResolver implements UriResolver {
Expand All @@ -11,7 +17,9 @@ export class CacheResolver implements UriResolver {
client: Client,
cache: WrapperCache
): Promise<UriResolutionResult> {
const wrapper = cache.get(uri);
const wrapper = await executeMaybeAsyncFunction<Wrapper | undefined>(
cache.get.bind(cache, uri)
);

return Promise.resolve({
uri: uri,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
WrapperCache,
getImplementations,
coreInterfaceUris,
executeMaybeAsyncFunction,
} from "../../..";
import { CreateWrapperFunc } from "./types/CreateWrapperFunc";
import { UriResolutionResult } from "../../core/types/UriResolutionResult";
Expand Down Expand Up @@ -105,7 +106,11 @@ export class ExtendableUriResolver implements UriResolver {
const implementationsToLoad = new Queue<Uri>();

for (const implementationUri of implementationUris) {
if (!cache.has(implementationUri)) {
if (
!(await executeMaybeAsyncFunction<boolean>(
cache.has.bind(cache, implementationUri)
))
) {
implementationsToLoad.enqueue(implementationUri);
}
}
Expand Down Expand Up @@ -148,7 +153,7 @@ export class ExtendableUriResolver implements UriResolver {
};
}
} else {
cache.set(implementationUri, wrapper);
await cache.set(implementationUri, wrapper);
loadedImplementations.push(implementationUri.uri);
failedAttempts = 0;
}
Expand Down