Skip to content

Commit

Permalink
feat: agrega getId y elimina resolve
Browse files Browse the repository at this point in the history
  • Loading branch information
drusco committed Aug 6, 2023
1 parent c98c7a7 commit 797aa63
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 47 deletions.
31 changes: 2 additions & 29 deletions src/Emulator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import lib from "./lib";
import Exotic from "./types/Exotic";
import { findProxy, map } from "./utils";
import { EventEmitter } from "events";

export default class Emulator extends EventEmitter implements Exotic.Emulator {
Expand Down Expand Up @@ -69,33 +68,7 @@ export default class Emulator extends EventEmitter implements Exotic.Emulator {
return lib.methods.entriesAfter(this, value);
}

// faltando

useId() {}

encode(value: unknown): unknown {
if (findProxy(value)) {
const { id } = map.proxies.get(this.use(value));
return { id, encoded: true }; // TODO: usar Symbol para saber si es encoded o no
}

if (typeof value === "object" && value) {
const copy = Array.isArray(value) ? [] : {};

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

value = copy;
}

return value;
}

resolve(value: any): Exotic.proxy.public {
const proxy = findProxy(value);
if (!proxy) return value;
const { id, target } = map.proxies.get(proxy);
return { id, target };
getId(value: Exotic.traceable): number {
return lib.methods.getId(this, value);
}
}
13 changes: 13 additions & 0 deletions src/lib/methods/getId.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Emulator from "../../Emulator";
const $ = new Emulator();

describe("(method) getId", () => {
it("Returns the proxy's id ", () => {
const totalProxies = $.length;
const proxy = $.use();
const another = $.use();

expect($.getId(proxy)).toBe(totalProxies + 1);
expect($.getId(another)).toBe(totalProxies + 2);
});
});
12 changes: 12 additions & 0 deletions src/lib/methods/getId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Exotic from "../../types/Exotic";
import { findProxy, map } from "../../utils";

export default function getId(
scope: Exotic.Emulator,
value: Exotic.traceable,
): number {
const proxy = findProxy(value);
if (!proxy) return NaN;
const { id } = map.proxies.get(proxy);
return id;
}
2 changes: 2 additions & 0 deletions src/lib/methods/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import isRevoked from "./isRevoked";
import entries from "./entries";
import entriesBefore from "./entriesBefore";
import entriesAfter from "./entriesAfter";
import getId from "./getId";

export default {
useRef,
Expand All @@ -22,4 +23,5 @@ export default {
entries,
entriesBefore,
entriesAfter,
getId,
};
29 changes: 11 additions & 18 deletions src/types/Exotic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ declare namespace Exotic {
target(value?: any): any;
parent(value?: traceable): undefined | Proxy;
children(value?: traceable): Proxy[];
ownKeys(value?: Exotic.traceable): Exotic.key[];
revoke(value: Exotic.traceable): boolean;
isRevoked(value: Exotic.traceable): boolean;
entries(): Iterable<Exotic.Proxy>;
entriesBefore(value: Exotic.traceable): Iterable<Exotic.Proxy>;
entriesAfter(value: Exotic.traceable): Iterable<Exotic.Proxy>;
ownKeys(value?: traceable): key[];
revoke(value: traceable): boolean;
isRevoked(value: traceable): boolean;
entries(): Iterable<Proxy>;
entriesBefore(value: traceable): Iterable<Proxy>;
entriesAfter(value: traceable): Iterable<Proxy>;
getId(value: traceable): number;
refs: key[];
active: number;
revoked: number;
Expand Down Expand Up @@ -49,16 +50,13 @@ declare namespace Exotic {
args?: any[];
}

interface public {
interface data {
id: number;
target?: any;
}

interface data extends public {
revoke(): void;
revoked: boolean;
mock: Mock;
origin?: proxy.origin | undefined;
origin?: origin | undefined;
scope: Emulator;
sandbox: sandbox;
refKey: key;
Expand All @@ -69,13 +67,8 @@ declare namespace Exotic {

// eslint-disable-next-line @typescript-eslint/no-namespace
namespace emulator {
interface options {
[x: string]: any;
}

interface refs {
[x: key]: Proxy;
}
type options = Record<string, any>;
type refs = Record<key, Proxy>;

interface data {
options: options;
Expand Down

0 comments on commit 797aa63

Please sign in to comment.