Skip to content

Commit

Permalink
fix: ajusta classe y tipos
Browse files Browse the repository at this point in the history
  • Loading branch information
drusco committed Jul 24, 2023
1 parent 04b941c commit 756fef0
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 74 deletions.
123 changes: 53 additions & 70 deletions src/Emulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,45 +71,41 @@ export default class Emulator
return itemCount;
}

contains(a: EmulatorNS.traceable, b: EmulatorNS.traceable): boolean {
if (!this.exists(a) || !this.exists(b)) return false;

let origin: EmulatorNS.origin;

if (typeof b === "function") {
const item = proxies.get(b as EmulatorNS.proxy);
origin = item.origin;
}

while (origin) {
const { item } = origin;
if (item === a) return true;
origin = null;
if (typeof item === "function" && this.exists(item)) {
origin = proxies.get(item as EmulatorNS.proxy).origin;
}
// no estoy seguro si tiene algun uso
// voy a comentar para aprovechar la logica de busqueda por origen

// contains(a: EmulatorNS.traceable, b: EmulatorNS.traceable): boolean {
// if (!exists(a)) return false;
// if (!exists(b)) return false;

// let origin: EmulatorNS.origin;

// const item = exists(b) ? this.use(b) : null;
// origin = item?.origin;

// while (origin) {
// const { item } = origin;
// if (item === a) return true;
// origin = null;
// if (exists(item)) {
// origin = proxies.get(item as EmulatorNS.proxy).origin;
// }
// }

// return false;
// }

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

return false;
}

encode(value: EmulatorNS.traceable, callback?: EmulatorNS.proxy): object {
if (typeof value === "function" && proxies.has(value as EmulatorNS.proxy)) {
const { id } = proxies.get(value as EmulatorNS.proxy);

if (typeof callback == "function") {
callback(value);
}

return { id, encoded: true };
}

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

for (const key in value) {
if (!value[key]) continue;
copy[key] = this.encode(value[key], callback);
copy[key] = this.encode(value[key]);
}

value = copy;
Expand All @@ -118,15 +114,8 @@ export default class Emulator
return value;
}

exists(value: any): boolean {
if (proxies.has(value) || targets.has(value)) {
return true;
}
return false;
}

getId(value: EmulatorNS.traceable): number {
if (!this.exists(value)) return -1;
if (!exists(value)) return -1;
return proxies.get(this.use(value)).id;
}

Expand Down Expand Up @@ -166,19 +155,10 @@ export default class Emulator
secret.delete(this);
}

isTraceable(value: any): boolean {
if (value === null) return false;
if (this.exists(value)) return false;
if (!(typeof value === "object" || typeof value === "function")) {
return false;
}
return true;
}

async resolve(item: EmulatorNS.traceable): Promise<void> {
// Access the real value of a proxy

if (!this.exists(item)) return;
if (!exists(item)) return;

return new Promise((resolve): void => {
// 'resolve' event listener must exists
Expand All @@ -190,7 +170,7 @@ export default class Emulator
async resolveId(item: EmulatorNS.traceable): Promise<void> {
// Access the external id of a proxy

if (!this.exists(item) && typeof item != "object") return;
if (!exists(item) && typeof item != "object") return;

return new Promise((resolve): void => {
// 'resolveId' event listener must exists
Expand All @@ -208,27 +188,13 @@ export default class Emulator

return false;
}

static getEventNames(): string[] {
return [...events];
}
}

const secret = new WeakMap<EmulatorNS.EmulatorClass, EmulatorNS.private>();
const dummies = new WeakMap<EmulatorNS.proxy, EmulatorNS.proxy>();
const targets = new WeakMap<EmulatorNS.traceable, EmulatorNS.proxy>();
const proxies = new WeakMap<EmulatorNS.proxy, EmulatorNS.item>();

const events = [
"proxy",
"action",
"revoke",
"namespace",
"deleteGroup",
"resolve",
"resolveId",
];

const traps = {
get(dummy: EmulatorNS.proxy, key: string): unknown {
const item = dummies.get(dummy);
Expand All @@ -246,7 +212,7 @@ const traps = {

if (!sandboxKeys.includes(key)) {
if (targets.has(target) && target[key] !== undefined) {
if (scope.isTraceable(target[key])) {
if (isTraceable(target[key])) {
sandbox[key] = createProxy(scope, target[key], group, origin);
} else {
sandbox[key] = target[key];
Expand All @@ -262,7 +228,7 @@ const traps = {
set(dummy: EmulatorNS.proxy, key: string, value: unknown): boolean {
const item = dummies.get(dummy);
const { scope, group, sandbox } = proxies.get(item);
const traceable = scope.isTraceable(value);
const traceable = isTraceable(value);

const origin: EmulatorNS.origin = {
action: "set",
Expand Down Expand Up @@ -353,7 +319,7 @@ const createProxy = (

const itemId = ++data.itemCount;
const dummy = function () {};
const traceable = scope.isTraceable(target);
const traceable = isTraceable(target);
const { proxy, revoke } = Proxy.revocable(dummy, traps);
const group = bindings[namespace];

Expand Down Expand Up @@ -392,3 +358,20 @@ const createProxy = (

return proxy;
};

const exists = (value: any): boolean => {
if (proxies.has(value)) return true;
if (targets.has(value)) return true;
return false;
};

const isTraceable = (value: any): boolean => {
const isObject = typeof value === "object";
const isFunction = typeof value === "function";

if (!isObject && !isFunction) return false;
if (value === null) return false;
if (exists(value)) return false;

return true;
};
6 changes: 2 additions & 4 deletions src/types/Emulator.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { EventEmitter } from "events";

declare namespace Emulator {
interface EmulatorClass extends EventEmitter {
isTraceable(value: any): boolean;
}
interface EmulatorClass extends EventEmitter {}

interface options {
[x: string]: unknown;
Expand All @@ -15,7 +13,7 @@ declare namespace Emulator {
}

interface bindings {
[id: string]: group;
[namespace: string]: group;
}

interface private {
Expand Down

0 comments on commit 756fef0

Please sign in to comment.