Skip to content

Commit

Permalink
Expose target when creating a proxy (wrap) to make comlink compatible…
Browse files Browse the repository at this point in the history
… with proxy-polyfill
  • Loading branch information
frannunezrivera committed Nov 1, 2019
1 parent 1a518c7 commit 8bd0c3a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/comlink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ function closeEndPoint(endpoint: Endpoint) {
if (isMessagePort(endpoint)) endpoint.close();
}

export function wrap<T>(ep: Endpoint): Remote<T> {
return createProxy<T>(ep) as any;
export function wrap<T>(ep: Endpoint, target?: any): Remote<T> {
return createProxy<T>(ep, [], target) as any;
}

function throwIfProxyReleased(isReleased: boolean) {
Expand All @@ -190,10 +190,11 @@ function throwIfProxyReleased(isReleased: boolean) {

function createProxy<T>(
ep: Endpoint,
path: (string | number | symbol)[] = []
path: (string | number | symbol)[] = [],
target: object = function() {}
): Remote<T> {
let isProxyReleased = false;
const proxy = new Proxy(function() {}, {
const proxy = new Proxy(target, {
get(_target, prop) {
throwIfProxyReleased(isProxyReleased);
if (prop === releaseProxy) {
Expand Down
6 changes: 6 additions & 0 deletions tests/same_window.comlink.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,12 @@ describe("Comlink in the same realm", function() {
await instance[Comlink.releaseProxy]();
expect(() => instance.method()).to.throw();
});

it('can proxy with a given target', async function() {
const thing = Comlink.wrap(this.port1, { value: {} });
Comlink.expose({ value: 4 }, this.port2);
expect(await thing.value).to.equal(4);
});
});

function guardedIt(f) {
Expand Down

0 comments on commit 8bd0c3a

Please sign in to comment.