Skip to content

Commit

Permalink
refactor: add methods to main class and adjust usage across lib
Browse files Browse the repository at this point in the history
  • Loading branch information
drusco committed Mar 15, 2024
1 parent efb6cd0 commit 6b4989f
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 25 deletions.
50 changes: 47 additions & 3 deletions src/Nexo.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,63 @@
import NexoTS from "./types/Nexo.js";
import EventEmitter from "node:events";
import { getProxy, isTraceable } from "./utils/index.js";

export default class Nexo extends EventEmitter {
protected options: NexoTS.options = {};
readonly map: Map<string, WeakRef<NexoTS.Proxy>> = new Map();
readonly links: Map<string, WeakRef<NexoTS.Proxy>> = new Map();

constructor(options?: NexoTS.options) {
console.log(options);
super();

if (options) {
this.options = { ...this.options, ...options };
}
}

new(target?: NexoTS.traceable): NexoTS.Proxy {
return getProxy(this, target);
}

use(target: string | NexoTS.traceable): NexoTS.Proxy {
if (isTraceable(target)) {
return getProxy(this, target);
}

if (this.links.has(target)) {
return this.links.get(target).deref();
}

return this.link(target);
}

link(name: string, target?: NexoTS.traceable): NexoTS.Proxy {
const proxy = getProxy(this, target);

this.links.set(name, new WeakRef(proxy));

this.emit("link", name, proxy);

return proxy;
}

unlink(name: string): boolean {
this.emit("unlink", name);

return this.links.delete(name);
}
use(): void {}
// link(link: Nexo.key, value?: any): Nexo.Proxy {}

// target(value?: any): any {}

// revoke(value: Nexo.traceable): boolean {}
// encode(value: any): any {}
// decode(value: any): any {}
// exec(
// method: Nexo.functionLike,
// dependencies?: Record<string, Nexo.Proxy>,
// ): Nexo.Proxy {}

get totalProxies(): number {
return this.map.size;
}
}
10 changes: 5 additions & 5 deletions src/events/ProxyEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import Nexo from "../types/Nexo.js";
import { map } from "../utils/index.js";

class ProxyEvent<Data = unknown> {
public readonly name: string;
public readonly proxy: Nexo.Proxy;
public readonly data: Data;
public returnValue: unknown;

readonly name: string;
readonly proxy: Nexo.Proxy;
readonly data: Data;
protected prevented: boolean = false;

returnValue: unknown;

constructor(name: string, proxy: Nexo.Proxy, data?: Data) {
if (!name.length) {
throw Error(`ProxyEvent: event name cannot be empty`);
Expand Down
6 changes: 0 additions & 6 deletions src/types/Nexo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ declare namespace Nexo {

interface options {}

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

interface Mock extends EventEmitter {
(...args: unknown[]): unknown;
}
Expand Down
12 changes: 6 additions & 6 deletions src/utils/getProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import findProxy from "./findProxy.js";
import isTraceable from "./isTraceable.js";
import handlers from "../handlers/index.js";
import EventEmitter from "node:events";
import { randomUUID } from "node:crypto";

const getProxy = (scope: Nexo, target: Nexo.traceable | void): Nexo.Proxy => {
const getProxy = (scope: Nexo, target?: Nexo.traceable): Nexo.Proxy => {
// find proxy by target

const usableProxy = findProxy(target);
Expand All @@ -16,18 +17,17 @@ const getProxy = (scope: Nexo, target: Nexo.traceable | void): Nexo.Proxy => {

// create proxy

const traceable = isTraceable(target);
const data: Nexo.data = map.emulators.get(scope);
const { proxyMap } = data;
const mock: Nexo.Mock = Object.setPrototypeOf(
function () {},
EventEmitter.prototype,
);

const proxy = new Proxy(mock, handlers) as Nexo.Proxy;
const traceable = isTraceable(target);

// set information about this proxy

const proxyId = `${++data.counter}`;
const proxyId = randomUUID();

const proxyData: Nexo.proxy.data = {
id: proxyId,
Expand All @@ -44,7 +44,7 @@ const getProxy = (scope: Nexo, target: Nexo.traceable | void): Nexo.Proxy => {
map.tracables.set(target, proxy);
}

proxyMap.set(proxyId, new WeakRef(proxy));
scope.link(proxyId, proxy);

return proxy;
};
Expand Down
3 changes: 1 addition & 2 deletions src/utils/map.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Nexo from "../types/Nexo.js";

const emulators: WeakMap<Nexo, Nexo.data> = new WeakMap();
const tracables: WeakMap<Nexo.traceable, Nexo.Proxy> = new WeakMap();
const proxies: WeakMap<Nexo.Proxy, Nexo.proxy.data> = new WeakMap();

export default { emulators, tracables, proxies };
export default { tracables, proxies };
4 changes: 1 addition & 3 deletions src/utils/proxyIterator.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import Nexo from "../types/Nexo.js";
import map from "./map.js";

const proxyIterator = function* (scope: Nexo): IterableIterator<Nexo.Proxy> {
const { proxyMap } = map.emulators.get(scope);
for (const [, ref] of proxyMap) {
for (const [, ref] of scope.map) {
const proxy = ref.deref();
if (proxy) yield proxy;
}
Expand Down

0 comments on commit 6b4989f

Please sign in to comment.