Skip to content

Commit

Permalink
fix: elimina groupos, ajusta proxygenerator, revokeproxy y createproxy
Browse files Browse the repository at this point in the history
  • Loading branch information
drusco committed Aug 6, 2023
1 parent 5ac2654 commit 37dcd38
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 39 deletions.
13 changes: 11 additions & 2 deletions src/Emulator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,20 +227,29 @@ describe("Emulator Class", () => {
describe("(method) entriesAfter", () => {
it("Returns an iterator that contains the active proxies created after a certain proxy", () => {
const proxy = $.use();
const newProxies: Exotic.Proxy[] = [proxy];
const newProxies: Exotic.Proxy[] = [];

newProxies.push(proxy.child, proxy.inner, proxy.inner.child);

expect([...$.entriesAfter(proxy)].length).toBe(newProxies.length);
});
});

describe("(method) entriesBefore", () => {
it("Returns an iterator that contains the active proxies created before a certain proxy", () => {
const activeProxies = $.active;
const proxy = $.use();

expect([...$.entriesBefore(proxy)].length).toBe(activeProxies);
});
});

describe("(method) encode", () => {
// TODO
});

describe("(getter) refs", () => {
it("Returns an Array of all binding keys in use", () => {
it("Returns an array of all reference keys in use", () => {
const reference = Date.now().toString();
$.useRef(reference);
expect($.refs.includes(reference)).toBe(true);
Expand Down
20 changes: 14 additions & 6 deletions src/Emulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export default class Emulator extends EventEmitter implements Exotic.Emulator {
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(this, data);
Expand Down Expand Up @@ -48,12 +50,12 @@ export default class Emulator extends EventEmitter implements Exotic.Emulator {

useRef(key: Exotic.key): Exotic.Proxy {
const { refs }: Exotic.emulator.data = map.emulators.get(this);
const group = refs[key];
const proxyRef = refs[key];

// return the first proxy in the existing group
if (group) return group.root;
// return proxy by reference
if (proxyRef) return proxyRef;

// create the first proxy for a new group
// create a proxy by reference key
return createProxy(this, undefined, key);
}

Expand Down Expand Up @@ -108,14 +110,20 @@ export default class Emulator extends EventEmitter implements Exotic.Emulator {
}

*entriesBefore(value: Exotic.traceable): Iterable<Exotic.Proxy> {
const currentProxy = findProxy(value);
for (const proxy of proxyGenerator(this, value, true)) {
yield proxy;
if (proxy !== currentProxy) {
yield proxy;
}
}
}

*entriesAfter(value: Exotic.traceable): Iterable<Exotic.Proxy> {
const currentProxy = findProxy(value);
for (const proxy of proxyGenerator(this, value, false)) {
yield proxy;
if (proxy !== currentProxy) {
yield proxy;
}
}
}

Expand Down
9 changes: 3 additions & 6 deletions src/types/Exotic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ declare namespace Exotic {

// eslint-disable-next-line @typescript-eslint/no-namespace
namespace proxy {
interface group {
root: Exotic.Proxy;
last: Exotic.Proxy;
}

interface sandbox {
[x: key]: any;
}
Expand Down Expand Up @@ -69,14 +64,16 @@ declare namespace Exotic {
}

interface refs {
[x: key]: proxy.group;
[x: key]: Proxy;
}

interface data {
options: options;
refs: refs;
totalProxies: number;
activeProxies: number;
firstProxy?: Proxy;
lastProxy?: Proxy;
}
}
}
Expand Down
33 changes: 16 additions & 17 deletions src/utils/createProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,33 @@ const createProxy = (
if (currentProxy) return currentProxy;

const data: Exotic.emulator.data = map.emulators.get(scope);
const { refs } = data;
const { refs, firstProxy, lastProxy } = data;

const id = ++data.totalProxies;
const mock = function mock() {} as Exotic.Mock;
let group: Exotic.proxy.group = refs[refKey];
const traceable = isTraceable(target);
let proxyRef: Exotic.Proxy | undefined = refs[refKey];

const { proxy, revoke } = Proxy.revocable<Exotic.Proxy>(
Object.setPrototypeOf(mock, mockPrototype),
traps,
);

if (!firstProxy) {
data.firstProxy = proxy;
}

if (traceable) {
map.targets.set(target, proxy);
}

if (!group) {
// create the new group
group = {
root: proxy,
last: proxy,
};

refs[refKey] = group;
if (!proxyRef) {
// create the new reference
proxyRef = proxy;
refs[refKey] = proxyRef;
scope.emit("bind", refKey);
}

const previousProxy = group.last === proxy ? undefined : group.last;
// set the proxy information
const proxyData: Exotic.proxy.data = {
id,
Expand All @@ -55,16 +54,16 @@ const createProxy = (
scope,
sandbox: Object.create(null),
refKey,
prev: previousProxy,
prev: lastProxy,
next: undefined,
revoked: false,
};

if (previousProxy) {
if (lastProxy) {
// update previous proxy
const prevData = map.proxies.get(previousProxy);
if (prevData) {
prevData.next = proxy;
const lastProxyData = map.proxies.get(lastProxy);
if (lastProxyData) {
lastProxyData.next = proxy;
}
}

Expand All @@ -75,7 +74,7 @@ const createProxy = (
ref: refKey,
});

group.last = proxy;
data.lastProxy = proxy;
data.activeProxies += 1;

map.mocks.set(mock, proxy);
Expand Down
12 changes: 7 additions & 5 deletions src/utils/proxyGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ const proxyGenerator = function* (
reverse: boolean = false,
): Iterable<Exotic.Proxy> {
if (value === undefined) {
for (const ref of scope.refs) {
for (const proxy of proxyGenerator(scope, scope.useRef(ref))) {
if (!scope.revoked(proxy)) {
yield proxy;
}
const { firstProxy } = map.emulators.get(scope);

if (!firstProxy) return;

for (const proxy of proxyGenerator(scope, firstProxy, false)) {
if (!scope.revoked(proxy)) {
yield proxy;
}
}
return;
Expand Down
6 changes: 3 additions & 3 deletions src/utils/revokeProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ const revokeProxy = (

const { id, refKey, mock, revoke, target, origin } = proxyData;
const { refs } = data;
const group = refs[refKey];
const proxyRef = refs[refKey];

if (group.root === proxy) {
// delete group
if (proxyRef === proxy) {
// delete reference
delete refs[refKey];
scope.emit("unbind", refKey);
}
Expand Down

0 comments on commit 37dcd38

Please sign in to comment.