diff --git a/src/Nexo.ts b/src/Nexo.ts index c1624d3..adeaf38 100755 --- a/src/Nexo.ts +++ b/src/Nexo.ts @@ -1,14 +1,54 @@ 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> = new Map(); + readonly links: Map> = 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 {} @@ -16,4 +56,8 @@ export default class Nexo extends EventEmitter { // method: Nexo.functionLike, // dependencies?: Record, // ): Nexo.Proxy {} + + get totalProxies(): number { + return this.map.size; + } } diff --git a/src/events/ProxyEvent.ts b/src/events/ProxyEvent.ts index a58106f..fdbcc5a 100644 --- a/src/events/ProxyEvent.ts +++ b/src/events/ProxyEvent.ts @@ -2,13 +2,13 @@ import Nexo from "../types/Nexo.js"; import { map } from "../utils/index.js"; class ProxyEvent { - 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`); diff --git a/src/types/Nexo.ts b/src/types/Nexo.ts index af7b292..9d85729 100644 --- a/src/types/Nexo.ts +++ b/src/types/Nexo.ts @@ -8,12 +8,6 @@ declare namespace Nexo { interface options {} - type data = { - options: options; - counter: number; - proxyMap: Map>; - }; - interface Mock extends EventEmitter { (...args: unknown[]): unknown; } diff --git a/src/utils/getProxy.ts b/src/utils/getProxy.ts index ba07bcb..b4a322a 100644 --- a/src/utils/getProxy.ts +++ b/src/utils/getProxy.ts @@ -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); @@ -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, @@ -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; }; diff --git a/src/utils/map.ts b/src/utils/map.ts index e8fa3a9..326dbfe 100644 --- a/src/utils/map.ts +++ b/src/utils/map.ts @@ -1,7 +1,6 @@ import Nexo from "../types/Nexo.js"; -const emulators: WeakMap = new WeakMap(); const tracables: WeakMap = new WeakMap(); const proxies: WeakMap = new WeakMap(); -export default { emulators, tracables, proxies }; +export default { tracables, proxies }; diff --git a/src/utils/proxyIterator.ts b/src/utils/proxyIterator.ts index 33da88d..c5b65c1 100644 --- a/src/utils/proxyIterator.ts +++ b/src/utils/proxyIterator.ts @@ -1,9 +1,7 @@ import Nexo from "../types/Nexo.js"; -import map from "./map.js"; const proxyIterator = function* (scope: Nexo): IterableIterator { - 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; }