Skip to content

Commit

Permalink
fix: ajusta emulador para nao perder memoria
Browse files Browse the repository at this point in the history
  • Loading branch information
drusco committed Aug 14, 2023
1 parent c482065 commit 0f3088f
Show file tree
Hide file tree
Showing 27 changed files with 114 additions and 346 deletions.
26 changes: 13 additions & 13 deletions src/Emulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ export default class Emulator implements Exotic.Emulator {
}

getId(value: Exotic.traceable): number {
return lib.methods.getId(this, value);
return lib.methods.getId(value);
}

target(value?: any): any {
return lib.methods.target(this, value);
return lib.methods.target(value);
}

parent(value?: Exotic.traceable): undefined | Exotic.Proxy {
return lib.methods.parent(this, value);
return lib.methods.parent(value);
}

values(value?: Exotic.traceable): Exotic.Proxy[] {
Expand All @@ -51,23 +51,19 @@ export default class Emulator implements Exotic.Emulator {
}

revoke(value: Exotic.traceable): boolean {
return lib.methods.revoke(this, value);
return lib.methods.revoke(value);
}

isRevoked(value: Exotic.traceable): boolean {
return lib.methods.isRevoked(this, value);
return lib.methods.isRevoked(value);
}

entries(): IterableIterator<Exotic.Proxy> {
return lib.methods.entries(this);
}

entriesBefore(value: Exotic.traceable): IterableIterator<Exotic.Proxy> {
return lib.methods.entriesBefore(this, value);
[Symbol.iterator](): IterableIterator<Exotic.Proxy> {
return lib.methods.entries();
}

entriesAfter(value: Exotic.traceable): IterableIterator<Exotic.Proxy> {
return lib.methods.entriesAfter(this, value);
entries(): IterableIterator<Exotic.Proxy> {
return lib.methods.entries();
}

encode(value: any): Exotic.payload {
Expand All @@ -77,4 +73,8 @@ export default class Emulator implements Exotic.Emulator {
get(value?: any): Promise<any> {
return lib.methods.get(this, value);
}

kill(): void {
return lib.methods.kill(this);
}
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ for (let i = 0; i < 1000000; i++) {
p();
p();
p();
$.kill();
}
console.log("the end");
22 changes: 2 additions & 20 deletions src/lib/constructor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,14 @@ describe("(constructor)", () => {
const config: Exotic.emulator.options = {};
const emulator = new Emulator(config);
const data: Record<string, any> = map.emulators.get(emulator) || {};

const dataProps = [
"options",
"refs",
"totalProxies",
"activeProxies",
"firstProxy",
"lastProxy",
];

const {
options,
refs,
totalProxies,
activeProxies,
firstProxy,
lastProxy,
} = data;
const dataProps = ["options", "refs", "totalProxies", "activeProxies"];
const { options, refs, totalProxies, activeProxies } = data;

expect(map.emulators.has(emulator)).toBe(true);
expect(Object.keys(data)).toEqual(dataProps);
expect(options).toBe(config);
expect(Reflect.ownKeys(refs).length).toBe(0);
expect(totalProxies).toBe(0);
expect(activeProxies).toBe(0);
expect(firstProxy).toBeUndefined();
expect(lastProxy).toBeUndefined();
});
});
2 changes: 0 additions & 2 deletions src/lib/constructor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ export default function constructor(
refs: Object.create(null),
totalProxies: 0, // total item count including revoked items, it only increases
activeProxies: 0, // items that are not revoked
firstProxy: undefined,
lastProxy: undefined,
};

map.emulators.set(scope, data);
Expand Down
10 changes: 3 additions & 7 deletions src/lib/methods/entries.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import Exotic from "../../types/Exotic";
import { proxyGenerator } from "../../utils";
import { proxyIterator } from "../../utils";

export default function* entries(
scope: Exotic.Emulator,
): IterableIterator<Exotic.Proxy> {
for (const proxy of proxyGenerator(scope)) {
yield proxy;
}
export default function entries(): IterableIterator<Exotic.Proxy> {
return proxyIterator();
}
13 changes: 0 additions & 13 deletions src/lib/methods/entriesAfter.spec.ts

This file was deleted.

14 changes: 0 additions & 14 deletions src/lib/methods/entriesAfter.ts

This file was deleted.

11 changes: 0 additions & 11 deletions src/lib/methods/entriesBefore.spec.ts

This file was deleted.

14 changes: 0 additions & 14 deletions src/lib/methods/entriesBefore.ts

This file was deleted.

5 changes: 1 addition & 4 deletions src/lib/methods/getId.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import Exotic from "../../types/Exotic";
import { findProxy, map } from "../../utils";

export default function getId(
scope: Exotic.Emulator,
value: Exotic.traceable,
): number {
export default function getId(value: Exotic.traceable): number {
const proxy = findProxy(value);
if (!proxy) {
return NaN;
Expand Down
6 changes: 2 additions & 4 deletions src/lib/methods/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ import ownKeys from "./ownKeys";
import revoke from "./revoke";
import isRevoked from "./isRevoked";
import entries from "./entries";
import entriesBefore from "./entriesBefore";
import entriesAfter from "./entriesAfter";
import getId from "./getId";
import encode from "./encode";
import get from "./get";
import kill from "./kill";

export default {
useRef,
Expand All @@ -23,9 +22,8 @@ export default {
revoke,
isRevoked,
entries,
entriesBefore,
entriesAfter,
getId,
encode,
get,
kill,
};
5 changes: 1 addition & 4 deletions src/lib/methods/isRevoked.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import Exotic from "../../types/Exotic";
import { findProxy, map } from "../../utils";

export default function isRevoked(
scope: Exotic.Emulator,
value: Exotic.traceable,
): boolean {
export default function isRevoked(value: Exotic.traceable): boolean {
const proxy = findProxy(value);
if (!proxy) return false;
const { revoked } = map.proxies.get(proxy);
Expand Down
11 changes: 11 additions & 0 deletions src/lib/methods/kill.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Exotic from "../../types/Exotic";
import { proxyIterator, revokeProxy, map } from "../../utils";

export default function kill(scope: Exotic.Emulator): void {
for (const proxy of proxyIterator()) {
const { scope: proxyScope } = map.proxies.get(proxy);
if (scope === proxyScope) {
revokeProxy(proxy);
}
}
}
1 change: 0 additions & 1 deletion src/lib/methods/parent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import Exotic from "../../types/Exotic";
import { findProxy, map } from "../../utils";

export default function parent(
scope: Exotic.Emulator,
value?: Exotic.traceable,
): undefined | Exotic.Proxy {
const proxy = findProxy(value);
Expand Down
7 changes: 2 additions & 5 deletions src/lib/methods/revoke.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import Exotic from "../../types/Exotic";
import { revokeProxy } from "../../utils";

export default function revoke(
scope: Exotic.Emulator,
value: Exotic.traceable,
): boolean {
return revokeProxy(scope, value);
export default function revoke(value: Exotic.traceable): boolean {
return revokeProxy(value);
}
3 changes: 1 addition & 2 deletions src/lib/methods/target.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Exotic from "../../types/Exotic";
import { findProxy, map } from "../../utils";

export default function target(scope: Exotic.Emulator, value?: any): any {
export default function target(value?: any): any {
const proxy = findProxy(value);
if (!proxy) return value;
const { target } = map.proxies.get(proxy);
Expand Down
12 changes: 4 additions & 8 deletions src/types/Exotic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ declare namespace Exotic {
revoke(value: traceable): boolean;
isRevoked(value: traceable): boolean;
entries(): IterableIterator<Proxy>;
entriesBefore(value: traceable): IterableIterator<Proxy>;
entriesAfter(value: traceable): IterableIterator<Proxy>;
encode(value: any): payload;
get(value?: any): Promise<any>;
kill(): void;
refs: key[];
active: number;
revoked: number;
length: number;
[Symbol.iterator](): IterableIterator<Proxy>;
}

namespace emulator {
Expand All @@ -39,8 +39,6 @@ declare namespace Exotic {
refs: Record<key, Proxy>;
totalProxies: number;
activeProxies: number;
firstProxy?: Proxy;
lastProxy?: Proxy;
}
}

Expand All @@ -56,7 +54,7 @@ declare namespace Exotic {
namespace proxy {
interface origin {
action: "get" | "set" | "construct" | "apply";
proxy: Proxy;
proxy?: Proxy;
key?: key;
value?: any;
that?: any;
Expand All @@ -72,9 +70,7 @@ declare namespace Exotic {
sandbox: Record<key, any>;
target?: any;
origin?: origin;
refKey?: key;
next?: Proxy;
prev?: Proxy;
key?: key;
}
}
}
Expand Down
79 changes: 0 additions & 79 deletions src/utils/IterableWeakMap.ts

This file was deleted.

Loading

0 comments on commit 0f3088f

Please sign in to comment.