Skip to content

Commit

Permalink
fix: ajusta lib
Browse files Browse the repository at this point in the history
  • Loading branch information
drusco committed Aug 15, 2023
1 parent 4e90ce4 commit f0f1633
Show file tree
Hide file tree
Showing 19 changed files with 130 additions and 101 deletions.
2 changes: 1 addition & 1 deletion src/Emulator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe("(lib) Emulator", () => {
// with the actual window object
browser.useRef("window", win);

// the api informs the browser context to create a new ref 'window'
// the emulator informs the browser context to create a new ref 'window'
// since the 'window' ref exists in the browser already it will use the existing proxy
// every handler trap call on the api will correspond to the real window target in the browser
const window = nodejs.useRef("window", global);
Expand Down
4 changes: 2 additions & 2 deletions src/Emulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ export default class Emulator implements Exotic.Emulator {
return lib.methods.entries();
}

encode(value: any): Exotic.payload {
return lib.methods.encode(this, value);
encode(value: any): any {
return lib.methods.encode(value);
}

get(value?: any): Promise<any> {
Expand Down
27 changes: 2 additions & 25 deletions src/lib/methods/encode.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,14 @@ const $ = new Emulator();
describe("(method) encode", () => {
it("Encodes a proxy into a payload object", () => {
const proxy = $.use();
const mock: Exotic.payload = { value: 1, encoded: true };
const mock: Exotic.ProxyPayload = ["⁠", 1];
expect($.encode(proxy)).toEqual(mock);
});

it("Encodes proxies into payload objects deeply", () => {
const deep = $.use();
const value = [[[[{ deep }]]]];
const mock: Exotic.payload = {
value: [
{
value: [
{
value: [
{
value: [
{
value: { deep: { value: 2, encoded: true } },
encoded: false,
},
],
encoded: false,
},
],
encoded: false,
},
],
encoded: false,
},
],
encoded: false,
};
const mock = [[[[{ deep: ["⁠", 2] }]]]];
expect($.encode(value)).toEqual(mock);
});
});
24 changes: 3 additions & 21 deletions src/lib/methods/encode.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,5 @@
import Exotic from "../../types/Exotic";
import { getPayload } from "../../utils";
import { encode as encodeValue } from "../../utils";

export default function encode(
scope: Exotic.Emulator,
value: any,
): Exotic.payload {
const isObject = value !== null && typeof value === "object";
const isArray = Array.isArray(value);
const result: Exotic.payload = getPayload(scope, value);

if (isObject) {
const copy = isArray ? [] : {};

for (const key in value) {
copy[key] = encode(scope, value[key]);
}

result.value = copy;
}

return result;
export default function encode(value: any): any {
return encodeValue(value);
}
6 changes: 4 additions & 2 deletions src/lib/methods/parent.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import Exotic from "../../types/Exotic";
import { findProxy, map } from "../../utils";
import decode from "../../utils/decode";

export default function parent(
value?: Exotic.traceable,
): undefined | Exotic.Proxy {
const proxy = findProxy(value);
if (!proxy) return;
const { origin } = map.proxies.get(proxy);
return origin && origin.proxy;
const { origin, scope } = map.proxies.get(proxy);
const decodedOrigin = decode(scope, origin) as Exotic.proxy.origin;
return decodedOrigin && decodedOrigin.proxy;
}
10 changes: 3 additions & 7 deletions src/types/Exotic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ declare namespace Exotic {
type traceable = object;
type key = string | symbol;
type FunctionLike = (...args: any[]) => any;

type payload = {
value: any;
encoded: boolean;
};
type ProxyPayload = [noBreak: "⁠", proxyId: number];

interface Emulator {
use(value?: any): Proxy;
Expand All @@ -20,7 +16,7 @@ declare namespace Exotic {
revokeAll(): void;
isRevoked(value: traceable): boolean;
entries(): IterableIterator<Proxy>;
encode(value: any): payload;
encode(value: any): any;
get(value?: any): Promise<any>;
refs: key[];
active: number;
Expand Down Expand Up @@ -69,7 +65,7 @@ declare namespace Exotic {
scope: Emulator;
sandbox: Record<key, any>;
target?: any;
origin?: origin;
origin?: ProxyPayload;
key?: key;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils/createProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import traps from "./traps";
const createProxy = (
scope: Exotic.Emulator,
target?: any,
origin?: Exotic.proxy.origin,
origin?: Exotic.ProxyPayload,
refKey?: Exotic.key,
): Exotic.Proxy => {
const usableProxy = findProxy(target);
Expand Down
36 changes: 36 additions & 0 deletions src/utils/decode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import Exotic from "../types/Exotic";
import findProxy from "./findProxy";
import findProxyById from "./findProxyById";

function isPayload(value: any): boolean {
if (!Array.isArray(value)) return false;
if (!value.length || value.length > 2) return false;
const [noBreak] = value;
if (noBreak !== "⁠") return false;
return true;
}

export default function decode(scope: Exotic.Emulator, value: any): any {
const isObject = value !== null && typeof value === "object";
const isArray = Array.isArray(value);
const proxy = findProxy(value);
const payload = isPayload(value);

let result = value;

if (proxy) {
result = proxy;
} else if (payload) {
result = findProxyById(scope, value[1]);
} else if (isObject) {
const copy = isArray ? [] : {};

for (const key in value) {
copy[key] = decode(scope, value[key]);
}

result = copy;
}

return result;
}
20 changes: 20 additions & 0 deletions src/utils/encode.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import Emulator from "../Emulator";
import encode from "./encode";

const $ = new Emulator();

describe("(function) encode", () => {
it("Returns a payload object from a proxy", () => {
const proxy = $.use();
const payload = encode(proxy);

expect(payload).toEqual(["⁠", 1]);
});

it("Returns a payload object from a non proxy value", () => {
const value = null;
const payload = encode(value);

expect(payload).toEqual(null);
});
});
25 changes: 25 additions & 0 deletions src/utils/encode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import Exotic from "../types/Exotic";
import findProxy from "./findProxy";
import map from "./map";

export default function encode(value: any): any {
const isObject = value !== null && typeof value === "object";
const isArray = Array.isArray(value);
const proxy = findProxy(value);

let result = value;

if (proxy) {
result = ["⁠", map.proxies.get(proxy).id] as Exotic.ProxyPayload;
} else if (isObject) {
const copy = isArray ? [] : {};

for (const key in value) {
copy[key] = encode(value[key]);
}

result = copy;
}

return result;
}
15 changes: 15 additions & 0 deletions src/utils/findProxyById.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Exotic from "../types/Exotic";
import map from "./map";
import proxyIterator from "./proxyIterator";

export default function findProxyById(
scope: Exotic.Emulator,
id: number,
): void | Exotic.Proxy {
for (const proxy of proxyIterator()) {
const { scope: proxyScope, id: proxyId } = map.proxies.get(proxy);
if (scope === proxyScope && id === proxyId) {
return proxy;
}
}
}
20 changes: 0 additions & 20 deletions src/utils/getPayload.spec.ts

This file was deleted.

11 changes: 0 additions & 11 deletions src/utils/getPayload.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import findProxy from "./findProxy";
import createProxy from "./createProxy";
import proxyIterator from "./proxyIterator";
import revokeProxy from "./revokeProxy";
import getPayload from "./getPayload";
import map from "./map";
import encode from "./encode";
import * as traps from "./traps";

export {
Expand All @@ -15,5 +15,5 @@ export {
createProxy,
revokeProxy,
traps,
getPayload,
encode,
};
7 changes: 5 additions & 2 deletions src/utils/revokeProxy.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Exotic from "../types/Exotic";
import decode from "./decode";
import findProxy from "./findProxy";
import map from "./map";

Expand Down Expand Up @@ -28,9 +29,11 @@ const revokeProxy = (value: Exotic.traceable): boolean => {
proxyData.key = undefined;
}

if (origin) {
const decodedOrigin = decode(scope, origin) as Exotic.proxy.origin;

if (decodedOrigin) {
// remove from proxy's parent references
const { action, key, proxy: parentProxy } = origin;
const { action, key, proxy: parentProxy } = decodedOrigin;
if (action === "get" || action === "set") {
// delete from parent proxy and target
if (parentProxy) {
Expand Down
5 changes: 3 additions & 2 deletions src/utils/traps/apply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Exotic from "../../types/Exotic";
import createProxy from "../createProxy";
import findProxy from "../findProxy";
import map from "../map";
import encode from "../encode";

const apply = (mock: Exotic.Mock, that?: any, args?: any[]): any => {
const proxy = findProxy(mock);
Expand All @@ -20,10 +21,10 @@ const apply = (mock: Exotic.Mock, that?: any, args?: any[]): any => {
value = Reflect.apply(scope.target(target), scope.target(that), args);
}

const argList = args.map((arg) => createProxy(scope, arg, origin));
const argList = args.map((arg) => createProxy(scope, arg));
origin.args = argList;

return createProxy(scope, value, origin);
return createProxy(scope, value, encode(origin));
};

export default apply;
5 changes: 3 additions & 2 deletions src/utils/traps/construct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Exotic from "../../types/Exotic";
import createProxy from "../createProxy";
import findProxy from "../findProxy";
import map from "../map";
import encode from "../encode";

const construct = (mock: Exotic.Mock, args: any[]): object => {
const proxy = findProxy(mock);
Expand All @@ -19,10 +20,10 @@ const construct = (mock: Exotic.Mock, args: any[]): object => {
value = Reflect.construct(target, args);
}

const argList = args.map((arg) => createProxy(scope, arg, origin));
const argList = args.map((arg) => createProxy(scope, arg));
origin.args = argList;

return createProxy(scope, value, origin);
return createProxy(scope, value, encode(origin));
};

export default construct;
3 changes: 2 additions & 1 deletion src/utils/traps/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Exotic from "../../types/Exotic";
import createProxy from "../createProxy";
import findProxy from "../findProxy";
import map from "../map";
import encode from "../encode";

const get = (mock: Exotic.Mock, key: Exotic.key): any => {
const proxy = findProxy(mock);
Expand Down Expand Up @@ -31,7 +32,7 @@ const get = (mock: Exotic.Mock, key: Exotic.key): any => {
value = sandbox[key];
}

sandbox[key] = createProxy(scope, value, origin);
sandbox[key] = createProxy(scope, value, encode(origin));

return Reflect.get(sandbox, key);
};
Expand Down
5 changes: 3 additions & 2 deletions src/utils/traps/set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Exotic from "../../types/Exotic";
import createProxy from "../createProxy";
import findProxy from "../findProxy";
import map from "../map";
import encode from "../encode";

const set = (mock: Exotic.Mock, key: Exotic.key, value: any): boolean => {
const proxy = findProxy(mock);
Expand All @@ -14,8 +15,8 @@ const set = (mock: Exotic.Mock, key: Exotic.key, value: any): boolean => {
value,
};

const newValue = createProxy(scope, value, origin);
origin.value = newValue;
const newValue = createProxy(scope, value, encode(origin));
// origin.value = newValue;

try {
target[key] = scope.target(value);
Expand Down

0 comments on commit f0f1633

Please sign in to comment.