Skip to content

Commit

Permalink
feat: create has handler and proxyhandlerevent
Browse files Browse the repository at this point in the history
  • Loading branch information
drusco committed Mar 13, 2024
1 parent c130434 commit f1ae072
Show file tree
Hide file tree
Showing 13 changed files with 91 additions and 102 deletions.
22 changes: 12 additions & 10 deletions src/events/ProxyEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,27 @@ import Nexo from "../types/Nexo.js";
import { map } from "../utils/index.js";

class ProxyEvent {
name: Nexo.events.name;
name: string;
proxy: Nexo.Proxy;
data: Record<string, unknown> = {};
#defaultPrevented: boolean = false;
#returnValue: unknown;

constructor(
name: Nexo.events.name,
data: { proxy: Nexo.Proxy; [K: string]: unknown },
) {
if (!name) {
throw Error("ProxyEvent: event name cannot be empty");
constructor(name: string, proxy: Nexo.Proxy, data?: Record<string, unknown>) {
if (!name.length) {
throw Error(`ProxyEvent: event name cannot be empty`);
}

if (!map.proxies.has(data.proxy)) {
throw Error("ProxyEvent: invalid proxy");
if (!map.proxies.has(proxy)) {
throw Error(`ProxyEvent ${name} proxy not found`);
}

this.name = name;
Object.assign(this, data);
this.proxy = proxy;

if (data) {
Object.assign(this.data, data);
}
}

preventDefault(): void {
Expand Down
14 changes: 14 additions & 0 deletions src/events/ProxyHandlerEvent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
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>,
) {
super(`handler.${name}`, proxy, data);
}
}

export default ProxyHandlerEvent;
8 changes: 2 additions & 6 deletions src/events/handlers/apply.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import Nexo from "../../types/Nexo.js";
import { getTarget, getProxy, map } from "../../utils/index.js";
import ProxyEvent from "../ProxyEvent.js";
import ProxyHandlerEvent from "../ProxyHandlerEvent.js";

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);
const scope = data.scope.deref();

const event = new ProxyEvent("handler.apply", {
proxy,
that,
args,
});
const event = new ProxyHandlerEvent("apply", proxy, { that, args });

scope.emit(event.name, event);

Expand Down
7 changes: 2 additions & 5 deletions src/events/handlers/construct.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import Nexo from "../../types/Nexo.js";
import { getProxy, getTarget, isTraceable, map } from "../../utils/index.js";
import ProxyEvent from "../ProxyEvent.js";
import ProxyHandlerEvent from "../ProxyHandlerEvent.js";

const construct = (mock: Nexo.Mock, args: unknown[]): object => {
const proxy = map.tracables.get(mock);
const data = map.proxies.get(proxy);
const target = getTarget(data.target);
const scope = data.scope.deref();

const event = new ProxyEvent("handler.construct", {
proxy,
args,
});
const event = new ProxyHandlerEvent("construct", proxy, { args });

scope.emit(event.name, event);

Expand Down
5 changes: 2 additions & 3 deletions src/events/handlers/defineProperty.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 ProxyEvent from "../ProxyEvent.js";
import ProxyHandlerEvent from "../ProxyHandlerEvent.js";

const defineProperty = (
mock: Nexo.Mock,
Expand All @@ -14,8 +14,7 @@ const defineProperty = (
const scope = data.scope.deref();
const value = getTarget(descriptor.value, true);

const event = new ProxyEvent("handler.defineProperty", {
proxy,
const event = new ProxyHandlerEvent("defineProperty", proxy, {
key,
descriptor,
});
Expand Down
7 changes: 2 additions & 5 deletions src/events/handlers/deleteProperty.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import Nexo from "../../types/Nexo.js";
import { map } from "../../utils/index.js";
import ProxyEvent from "../ProxyEvent.js";
import ProxyHandlerEvent from "../ProxyHandlerEvent.js";

const deleteProperty = (mock: Nexo.Mock, key: Nexo.objectKey): boolean => {
const proxy = map.tracables.get(mock);
const data = map.proxies.get(proxy);
const { sandbox } = data;
const scope = data.scope.deref();

const event = new ProxyEvent("handler.deleteProperty", {
proxy,
key,
});
const event = new ProxyHandlerEvent("deleteProperty", proxy, { key });

scope.emit(event.name, event);

Expand Down
7 changes: 2 additions & 5 deletions src/events/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 ProxyEvent from "../ProxyEvent.js";
import ProxyHandlerEvent from "../ProxyHandlerEvent.js";

const get = (mock: Nexo.Mock, key: Nexo.objectKey): unknown => {
const proxy = map.tracables.get(mock);
Expand All @@ -11,10 +11,7 @@ const get = (mock: Nexo.Mock, key: Nexo.objectKey): unknown => {

let value: unknown;

const event = new ProxyEvent("handler.get", {
proxy,
key,
});
const event = new ProxyHandlerEvent("get", proxy, { key });

scope.emit(event.name, event);

Expand Down
5 changes: 2 additions & 3 deletions src/events/handlers/getOwnPropertyDescriptor.ts
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 ProxyEvent from "../ProxyEvent.js";
import ProxyHandlerEvent from "../ProxyHandlerEvent.js";

const getOwnPropertyDescriptor = (
mock: Nexo.Mock,
Expand All @@ -13,8 +13,7 @@ const getOwnPropertyDescriptor = (
const scope = data.scope.deref();
const value = getTarget(sandbox.get(key), true);

const event = new ProxyEvent("handler.getOwnPropertyDescriptor", {
proxy,
const event = new ProxyHandlerEvent("getOwnPropertyDescriptor", proxy, {
key,
});

Expand Down
6 changes: 2 additions & 4 deletions src/events/handlers/getPrototypeOf.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import Nexo from "../../types/Nexo.js";
import { getTarget, isTraceable, map } from "../../utils/index.js";
import ProxyEvent from "../ProxyEvent.js";
import ProxyHandlerEvent from "../ProxyHandlerEvent.js";

const getPrototypeOf = (mock: Nexo.Mock): object => {
const proxy = map.tracables.get(mock);
const data = map.proxies.get(proxy);
const target = getTarget(data.target);
const scope = data.scope.deref();

const event = new ProxyEvent("handler.getPrototypeOf", {
proxy,
});
const event = new ProxyHandlerEvent("getPrototypeOf", proxy);

scope.emit(event.name, event);

Expand Down
23 changes: 23 additions & 0 deletions src/events/handlers/has.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import Nexo from "../../types/Nexo.js";
import { map } from "../../utils/index.js";
import ProxyHandlerEvent from "../ProxyHandlerEvent.js";

const has = (mock: Nexo.Mock, key: Nexo.objectKey): boolean => {
const proxy = map.tracables.get(mock);
const data = map.proxies.get(proxy);

const { sandbox } = data;
const scope = data.scope.deref();

const event = new ProxyHandlerEvent("has", proxy, { key });

scope.emit(event.name, event);

if (event.defaultPrevented) {
return event.returnValue === true;
}

return sandbox.has(key);
};

export default has;
2 changes: 2 additions & 0 deletions src/events/handlers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import getOwnPropertyDescriptor from "./getOwnPropertyDescriptor.js";
import getPrototypeOf from "./getPrototypeOf.js";
import construct from "./construct.js";
import apply from "./apply.js";
import has from "./has.js";

export default {
get,
set,
has,
defineProperty,
deleteProperty,
getOwnPropertyDescriptor,
Expand Down
8 changes: 2 additions & 6 deletions src/events/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 ProxyEvent from "../ProxyEvent.js";
import ProxyHandlerEvent from "../ProxyHandlerEvent.js";

const set = (mock: Nexo.Mock, key: Nexo.objectKey, value: unknown): boolean => {
const proxy = map.tracables.get(mock);
Expand All @@ -10,11 +10,7 @@ const set = (mock: Nexo.Mock, key: Nexo.objectKey, value: unknown): boolean => {

let _value = getTarget(value, true);

const event = new ProxyEvent("handler.set", {
proxy,
key,
value,
});
const event = new ProxyHandlerEvent("set", proxy, { key, value });

scope.emit(event.name, event);

Expand Down
79 changes: 24 additions & 55 deletions src/types/Nexo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ declare namespace Nexo {

interface options {}

interface data {
type data = {
options: options;
counter: number;
proxyMap: Map<string, WeakRef<Proxy>>;
}
};

interface Mock extends functionLike {}

Expand All @@ -23,63 +23,32 @@ declare namespace Nexo {
}

namespace proxy {
interface data {
type data = {
id: string;
target: WeakRef<traceable> | void;
scope: WeakRef<Nexo>;
sandbox: Map<objectKey, unknown>;
}
}

namespace events {
type name =
| "handler.get"
| "handler.set"
| "handler.defineProperty"
| "handler.deleteProperty"
| "handler.getOwnPropertyDescriptor"
| "handler.getPrototypeOf"
| "handler.apply"
| "handler.construct";

interface getHandler extends ProxyEvent {
name: "handler.get";
key: objectKey;
}

interface setHandler extends ProxyEvent {
name: "handler.set";
key: objectKey;
value: unknown;
}

interface definePropertyHandler extends ProxyEvent {
name: "handler.defineProperty";
key: objectKey;
}

interface deletePropertyHandler extends ProxyEvent {
name: "handler.deleteProperty";
key: objectKey;
}

interface getOwnPropertyDescriptorHandler extends ProxyEvent {
name: "handler.getOwnPropertyDescriptor";
key: objectKey;
}

interface getPrototypeOfHandler extends ProxyEvent {}

interface applyHandler extends ProxyEvent {
name: "handler.apply";
args: unknown[];
that: unknown;
}

interface constructHandler extends ProxyEvent {
name: "handler.construct";
args: unknown[];
}
};

type handler =
// getters
| "get"
| "has"
| "deleteProperty"
| "getOwnPropertyDescriptor"
| "get"
| "has"
| "deleteProperty"
| "getOwnPropertyDescriptor"
// setters
| "set"
| "defineProperty"
// function calls
| "apply"
// class instances
| "construct"
// objects
| "getPrototypeOf";
}
}

Expand Down

0 comments on commit f1ae072

Please sign in to comment.