diff --git a/src/events/ProxyEvent.ts b/src/events/ProxyEvent.ts index 5085dec..85e9704 100644 --- a/src/events/ProxyEvent.ts +++ b/src/events/ProxyEvent.ts @@ -1,14 +1,15 @@ import Nexo from "../types/Nexo.js"; import { map } from "../utils/index.js"; -class ProxyEvent { - name: string; - proxy: Nexo.Proxy; - data: Record = {}; +class ProxyEvent { + #name: string; + #proxy: Nexo.Proxy; #defaultPrevented: boolean = false; #returnValue: unknown; - constructor(name: string, proxy: Nexo.Proxy, data?: Record) { + data: Data; + + constructor(name: string, proxy: Nexo.Proxy, data?: Data) { if (!name.length) { throw Error(`ProxyEvent: event name cannot be empty`); } @@ -17,12 +18,9 @@ class ProxyEvent { throw Error(`ProxyEvent ${name} proxy not found`); } - this.name = name; - this.proxy = proxy; - - if (data) { - Object.assign(this.data, data); - } + this.#name = name; + this.#proxy = proxy; + this.data = data; } preventDefault(): void { @@ -33,6 +31,14 @@ class ProxyEvent { this.#returnValue = value; } + get name(): string { + return this.#name; + } + + get proxy(): Nexo.Proxy { + return this.#proxy; + } + get defaultPrevented(): boolean { return this.#defaultPrevented; } diff --git a/src/events/ProxyHandlerEvent.ts b/src/events/ProxyHandlerEvent.ts index a7c86c5..593376a 100644 --- a/src/events/ProxyHandlerEvent.ts +++ b/src/events/ProxyHandlerEvent.ts @@ -1,12 +1,10 @@ import Nexo from "../types/Nexo.js"; import ProxyEvent from "./ProxyEvent.js"; -class ProxyHandlerEvent extends ProxyEvent { - constructor( - name: Nexo.proxy.handler, - proxy: Nexo.Proxy, - data?: Record, - ) { +class ProxyHandlerEvent extends ProxyEvent { + declare data: Data; + + constructor(name: Nexo.proxy.handlerName, proxy: Nexo.Proxy, data?: Data) { super(`handler.${name}`, proxy, data); } } diff --git a/src/events/index.ts b/src/events/index.ts deleted file mode 100644 index 215eb2c..0000000 --- a/src/events/index.ts +++ /dev/null @@ -1,4 +0,0 @@ -import ProxyEvent from "./ProxyEvent.js"; -import ProxyHandlerEvent from "./ProxyHandlerEvent.js"; - -export default { ProxyEvent, ProxyHandlerEvent }; diff --git a/src/events/handlers/apply.ts b/src/handlers/apply.ts similarity index 71% rename from src/events/handlers/apply.ts rename to src/handlers/apply.ts index b228195..dca3ab1 100644 --- a/src/events/handlers/apply.ts +++ b/src/handlers/apply.ts @@ -1,8 +1,8 @@ -import Nexo from "../../types/Nexo.js"; -import { getTarget, getProxy, map } from "../../utils/index.js"; -import ProxyHandlerEvent from "../ProxyHandlerEvent.js"; +import Nexo from "../types/Nexo.js"; +import { getTarget, getProxy, map } from "../utils/index.js"; +import ProxyHandlerEvent from "../events/ProxyHandlerEvent.js"; -const apply = (mock: Nexo.Mock, that?: unknown, args?: unknown[]): unknown => { +const apply = (mock: Nexo.Mock, that: unknown, args: unknown[]): unknown => { const proxy = map.tracables.get(mock); const data = map.proxies.get(proxy); const target = getTarget(data.target); diff --git a/src/events/handlers/construct.ts b/src/handlers/construct.ts similarity index 81% rename from src/events/handlers/construct.ts rename to src/handlers/construct.ts index a963f10..b997352 100644 --- a/src/events/handlers/construct.ts +++ b/src/handlers/construct.ts @@ -1,6 +1,6 @@ -import Nexo from "../../types/Nexo.js"; -import { getProxy, getTarget, isTraceable, map } from "../../utils/index.js"; -import ProxyHandlerEvent from "../ProxyHandlerEvent.js"; +import Nexo from "../types/Nexo.js"; +import { getProxy, getTarget, isTraceable, map } from "../utils/index.js"; +import ProxyHandlerEvent from "../events/ProxyHandlerEvent.js"; const construct = (mock: Nexo.Mock, args: unknown[]): object => { const proxy = map.tracables.get(mock); diff --git a/src/events/handlers/defineProperty.ts b/src/handlers/defineProperty.ts similarity index 78% rename from src/events/handlers/defineProperty.ts rename to src/handlers/defineProperty.ts index 74316b0..814d476 100644 --- a/src/events/handlers/defineProperty.ts +++ b/src/handlers/defineProperty.ts @@ -1,6 +1,6 @@ -import Nexo from "../../types/Nexo.js"; -import { getTarget, isTraceable, map } from "../../utils/index.js"; -import ProxyHandlerEvent from "../ProxyHandlerEvent.js"; +import Nexo from "../types/Nexo.js"; +import { getTarget, isTraceable, map } from "../utils/index.js"; +import ProxyHandlerEvent from "../events/ProxyHandlerEvent.js"; const defineProperty = ( mock: Nexo.Mock, diff --git a/src/events/handlers/deleteProperty.ts b/src/handlers/deleteProperty.ts similarity index 76% rename from src/events/handlers/deleteProperty.ts rename to src/handlers/deleteProperty.ts index 9e44a3b..d92618a 100644 --- a/src/events/handlers/deleteProperty.ts +++ b/src/handlers/deleteProperty.ts @@ -1,6 +1,6 @@ -import Nexo from "../../types/Nexo.js"; -import { map } from "../../utils/index.js"; -import ProxyHandlerEvent from "../ProxyHandlerEvent.js"; +import Nexo from "../types/Nexo.js"; +import { map } from "../utils/index.js"; +import ProxyHandlerEvent from "../events/ProxyHandlerEvent.js"; const deleteProperty = (mock: Nexo.Mock, key: Nexo.objectKey): boolean => { const proxy = map.tracables.get(mock); diff --git a/src/events/handlers/get.ts b/src/handlers/get.ts similarity index 86% rename from src/events/handlers/get.ts rename to src/handlers/get.ts index 68a172f..4d1d1d2 100644 --- a/src/events/handlers/get.ts +++ b/src/handlers/get.ts @@ -1,6 +1,6 @@ -import Nexo from "../../types/Nexo.js"; -import { getProxy, getTarget, isTraceable, map } from "../../utils/index.js"; -import ProxyHandlerEvent from "../ProxyHandlerEvent.js"; +import Nexo from "../types/Nexo.js"; +import { getProxy, getTarget, isTraceable, map } from "../utils/index.js"; +import ProxyHandlerEvent from "../events/ProxyHandlerEvent.js"; const get = (mock: Nexo.Mock, key: Nexo.objectKey): unknown => { const proxy = map.tracables.get(mock); diff --git a/src/events/handlers/getOwnPropertyDescriptor.ts b/src/handlers/getOwnPropertyDescriptor.ts similarity index 78% rename from src/events/handlers/getOwnPropertyDescriptor.ts rename to src/handlers/getOwnPropertyDescriptor.ts index 174fe41..21911b9 100644 --- a/src/events/handlers/getOwnPropertyDescriptor.ts +++ b/src/handlers/getOwnPropertyDescriptor.ts @@ -1,6 +1,6 @@ -import Nexo from "../../types/Nexo.js"; -import { getTarget, map } from "../../utils/index.js"; -import ProxyHandlerEvent from "../ProxyHandlerEvent.js"; +import Nexo from "../types/Nexo.js"; +import { getTarget, map } from "../utils/index.js"; +import ProxyHandlerEvent from "../events/ProxyHandlerEvent.js"; const getOwnPropertyDescriptor = ( mock: Nexo.Mock, diff --git a/src/events/handlers/getPrototypeOf.ts b/src/handlers/getPrototypeOf.ts similarity index 74% rename from src/events/handlers/getPrototypeOf.ts rename to src/handlers/getPrototypeOf.ts index 2a51eaf..fc001f4 100644 --- a/src/events/handlers/getPrototypeOf.ts +++ b/src/handlers/getPrototypeOf.ts @@ -1,6 +1,6 @@ -import Nexo from "../../types/Nexo.js"; -import { getTarget, isTraceable, map } from "../../utils/index.js"; -import ProxyHandlerEvent from "../ProxyHandlerEvent.js"; +import Nexo from "../types/Nexo.js"; +import { getTarget, isTraceable, map } from "../utils/index.js"; +import ProxyHandlerEvent from "../events/ProxyHandlerEvent.js"; const getPrototypeOf = (mock: Nexo.Mock): object => { const proxy = map.tracables.get(mock); diff --git a/src/events/handlers/has.ts b/src/handlers/has.ts similarity index 75% rename from src/events/handlers/has.ts rename to src/handlers/has.ts index cc325e8..4f4cd82 100644 --- a/src/events/handlers/has.ts +++ b/src/handlers/has.ts @@ -1,6 +1,6 @@ -import Nexo from "../../types/Nexo.js"; -import { map } from "../../utils/index.js"; -import ProxyHandlerEvent from "../ProxyHandlerEvent.js"; +import Nexo from "../types/Nexo.js"; +import { map } from "../utils/index.js"; +import ProxyHandlerEvent from "../events/ProxyHandlerEvent.js"; const has = (mock: Nexo.Mock, key: Nexo.objectKey): boolean => { const proxy = map.tracables.get(mock); diff --git a/src/events/handlers/index.ts b/src/handlers/index.ts similarity index 100% rename from src/events/handlers/index.ts rename to src/handlers/index.ts diff --git a/src/events/handlers/set.ts b/src/handlers/set.ts similarity index 78% rename from src/events/handlers/set.ts rename to src/handlers/set.ts index 166166a..b537a57 100644 --- a/src/events/handlers/set.ts +++ b/src/handlers/set.ts @@ -1,6 +1,6 @@ -import Nexo from "../../types/Nexo.js"; -import { getTarget, isTraceable, map } from "../../utils/index.js"; -import ProxyHandlerEvent from "../ProxyHandlerEvent.js"; +import Nexo from "../types/Nexo.js"; +import { getTarget, isTraceable, map } from "../utils/index.js"; +import ProxyHandlerEvent from "../events/ProxyHandlerEvent.js"; const set = (mock: Nexo.Mock, key: Nexo.objectKey, value: unknown): boolean => { const proxy = map.tracables.get(mock); diff --git a/src/types/Nexo.ts b/src/types/Nexo.ts index 9acb912..a56b6ee 100644 --- a/src/types/Nexo.ts +++ b/src/types/Nexo.ts @@ -1,11 +1,9 @@ import Nexo from "../Nexo.js"; -import PxEvent from "../events/ProxyEvent.js"; declare namespace Nexo { type functionLike = (...args: unknown[]) => unknown; type traceable = object | functionLike; type objectKey = string | symbol; - type ProxyEvent = PxEvent; interface options {} @@ -30,24 +28,15 @@ declare namespace Nexo { sandbox: Map; }; - type handler = - // getters + type handlerName = | "get" | "has" | "deleteProperty" | "getOwnPropertyDescriptor" - | "get" - | "has" - | "deleteProperty" - | "getOwnPropertyDescriptor" - // setters | "set" | "defineProperty" - // function calls | "apply" - // class instances | "construct" - // objects | "getPrototypeOf"; } } diff --git a/src/utils/getProxy.ts b/src/utils/getProxy.ts index 1bbbb23..f7837b0 100644 --- a/src/utils/getProxy.ts +++ b/src/utils/getProxy.ts @@ -2,7 +2,7 @@ import Nexo from "../types/Nexo.js"; import map from "./map.js"; import findProxy from "./findProxy.js"; import isTraceable from "./isTraceable.js"; -import handlers from "../events/handlers/index.js"; +import handlers from "../handlers/index.js"; const getProxy = (scope: Nexo, target: Nexo.traceable | void): Nexo.Proxy => { // find proxy by target