Skip to content

Commit

Permalink
refactor: improves proxy event handling
Browse files Browse the repository at this point in the history
  • Loading branch information
drusco committed Mar 13, 2024
1 parent 1a1b3e1 commit 8d25b11
Show file tree
Hide file tree
Showing 15 changed files with 51 additions and 62 deletions.
28 changes: 17 additions & 11 deletions src/events/ProxyEvent.ts
Original file line number Diff line number Diff line change
@@ -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<string, unknown> = {};
class ProxyEvent<Data = unknown> {
#name: string;
#proxy: Nexo.Proxy;
#defaultPrevented: boolean = false;
#returnValue: unknown;

constructor(name: string, proxy: Nexo.Proxy, data?: Record<string, unknown>) {
data: Data;

constructor(name: string, proxy: Nexo.Proxy, data?: Data) {
if (!name.length) {
throw Error(`ProxyEvent: event name cannot be empty`);
}
Expand All @@ -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 {
Expand All @@ -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;
}
Expand Down
10 changes: 4 additions & 6 deletions src/events/ProxyHandlerEvent.ts
Original file line number Diff line number Diff line change
@@ -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<string, unknown>,
) {
class ProxyHandlerEvent<Data = unknown> extends ProxyEvent {
declare data: Data;

constructor(name: Nexo.proxy.handlerName, proxy: Nexo.Proxy, data?: Data) {
super(`handler.${name}`, proxy, data);
}
}
Expand Down
4 changes: 0 additions & 4 deletions src/events/index.ts

This file was deleted.

8 changes: 4 additions & 4 deletions src/events/handlers/apply.ts → src/handlers/apply.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/events/handlers/get.ts → src/handlers/get.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/events/handlers/has.ts → src/handlers/has.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
File renamed without changes.
6 changes: 3 additions & 3 deletions src/events/handlers/set.ts → src/handlers/set.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
13 changes: 1 addition & 12 deletions src/types/Nexo.ts
Original file line number Diff line number Diff line change
@@ -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 {}

Expand All @@ -30,24 +28,15 @@ declare namespace Nexo {
sandbox: Map<objectKey, unknown>;
};

type handler =
// getters
type handlerName =
| "get"
| "has"
| "deleteProperty"
| "getOwnPropertyDescriptor"
| "get"
| "has"
| "deleteProperty"
| "getOwnPropertyDescriptor"
// setters
| "set"
| "defineProperty"
// function calls
| "apply"
// class instances
| "construct"
// objects
| "getPrototypeOf";
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils/getProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8d25b11

Please sign in to comment.