Skip to content

Commit

Permalink
fix: elimina eventemitter y usa eventtarget
Browse files Browse the repository at this point in the history
  • Loading branch information
drusco committed Aug 8, 2023
1 parent 673cee0 commit a1b9119
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 53 deletions.
5 changes: 0 additions & 5 deletions src/Emulator.spec.ts

This file was deleted.

3 changes: 1 addition & 2 deletions src/Emulator.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import lib from "./lib";
import Exotic from "./types/Exotic";
import { EventEmitter } from "events";

export default class Emulator extends EventEmitter implements Exotic.Emulator {
export default class Emulator extends EventTarget implements Exotic.Emulator {
constructor(options: Exotic.emulator.options = {}) {
super();
lib.constructor(this, options);
Expand Down
42 changes: 20 additions & 22 deletions src/types/Exotic.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { EventEmitter } from "events";

// eslint-disable-next-line @typescript-eslint/no-namespace
declare namespace Exotic {
type traceable = object;
type key = string | symbol;

interface payload {
type payload = {
value: any;
encoded: boolean;
}
};

interface Emulator extends EventEmitter {
interface Emulator extends EventTarget {
use(value?: any): Proxy;
useRef(ref: key): Proxy;
getId(value: traceable): number;
Expand All @@ -30,9 +28,24 @@ declare namespace Exotic {
length: number;
}

type FunctionLike = (...args: any[]) => void;
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace emulator {
interface options {
[x: string]: any;
}

interface data {
options: options;
refs: Record<key, Proxy>;
totalProxies: number;
activeProxies: number;
firstProxy?: Proxy;
lastProxy?: Proxy;
}
}

interface Mock extends FunctionLike {
interface Mock {
(...args: any[]): void;
[x: key]: any;
[Symbol.iterator](): Iterator<any>;
}
Expand Down Expand Up @@ -66,21 +79,6 @@ declare namespace Exotic {
prev?: Proxy;
}
}

// eslint-disable-next-line @typescript-eslint/no-namespace
namespace emulator {
type options = Record<string, any>;
type refs = Record<key, Proxy>;

interface data {
options: options;
refs: refs;
totalProxies: number;
activeProxies: number;
firstProxy?: Proxy;
lastProxy?: Proxy;
}
}
}

export default Exotic;
10 changes: 8 additions & 2 deletions src/utils/createProxy.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import Emulator from "../Emulator";
import createProxy from "./createProxy";

const scope = new Emulator();

describe("(function) createProxy", () => {
it("Runs an exotic proxy emulator", () => {
expect(true).toBeTruthy();
it("Requires a scope parameter that represents the emulator instance", () => {
const proxy = createProxy(scope);
expect(typeof proxy).toBe("function");
});
});
11 changes: 3 additions & 8 deletions src/utils/createProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import traps from "./traps";

const createProxy = (
scope: Exotic.Emulator,
target: any,
target?: any,
origin?: Exotic.proxy.origin,
refKey?: Exotic.key,
): Exotic.Proxy => {
Expand Down Expand Up @@ -46,7 +46,7 @@ const createProxy = (
if (validRefKey) {
// create unique reference
refs[reference] = proxy;
scope.emit("bind", reference, proxy);
scope.dispatchEvent(new Event("bind"));
}

// proxy information
Expand All @@ -72,12 +72,7 @@ const createProxy = (
}
}

scope.emit("proxy", {
id,
proxy,
origin,
ref: reference,
});
scope.dispatchEvent(new Event("proxy"));

if (!firstProxy) {
data.firstProxy = proxy;
Expand Down
6 changes: 3 additions & 3 deletions src/utils/revokeProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const revokeProxy = (
}

const proxyData = map.proxies.get(proxy);
const { id, mock, revoke, target, origin, revoked, refKey } = proxyData;
const { mock, revoke, target, origin, revoked, refKey } = proxyData;

if (revoked) {
return true;
Expand All @@ -30,7 +30,7 @@ const revokeProxy = (
// delete refKey from proxy
proxyData.refKey = undefined;
// inform
scope.emit("unbind", refKey);
scope.dispatchEvent(new Event("unbind"));
}

if (origin) {
Expand Down Expand Up @@ -63,7 +63,7 @@ const revokeProxy = (
});
}

scope.emit("revoke", id);
scope.dispatchEvent(new Event("revoke"));
return true;
};

Expand Down
2 changes: 2 additions & 0 deletions src/utils/traps/apply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const apply = (mock: Exotic.Mock, that?: any, args?: any[]): any => {
const proxy = findProxy(mock);
const { scope, target } = map.proxies.get(proxy);

scope.dispatchEvent(new Event("apply"));

const origin: Exotic.proxy.origin = {
action: "apply",
proxy,
Expand Down
2 changes: 2 additions & 0 deletions src/utils/traps/construct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const construct = (mock: Exotic.Mock, args: any[]): object => {
const proxy = findProxy(mock);
const { scope, target } = map.proxies.get(proxy);

scope.dispatchEvent(new Event("construct"));

const origin: Exotic.proxy.origin = {
action: "construct",
proxy,
Expand Down
6 changes: 1 addition & 5 deletions src/utils/traps/deleteProperty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ const deleteProperty = (mock: Exotic.Mock, key: Exotic.key): boolean => {
try {
// delete from original target too
delete target[key];
scope.emit("action", {
action: "delete",
proxy,
key,
});
scope.dispatchEvent(new Event("deleteProperty"));
} catch (error) {
/* empty */
}
Expand Down
7 changes: 1 addition & 6 deletions src/utils/traps/set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,7 @@ const set = (mock: Exotic.Mock, key: Exotic.key, value: any): boolean => {
const newValue = createProxy(scope, value, origin);
origin.value = newValue;

scope.emit("action", {
action: "set",
proxy,
key,
value: newValue,
});
scope.dispatchEvent(new Event("set"));

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

0 comments on commit a1b9119

Please sign in to comment.