Skip to content

Commit

Permalink
Finally moved fully into the proxy-target/array logic and everything …
Browse files Browse the repository at this point in the history
…works awesomely!
  • Loading branch information
WebReflection committed Nov 21, 2023
1 parent eb62704 commit 0f881d8
Show file tree
Hide file tree
Showing 13 changed files with 26 additions and 53 deletions.
2 changes: 1 addition & 1 deletion esm/channel.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// ⚠️ AUTOMATICALLY GENERATED - DO NOT CHANGE
export const CHANNEL = '1e57565f-8f58-46e9-bc61-6dba08309169';
export const CHANNEL = '8e239207-5d0c-4e5b-be5f-e352858459c8';

export const MAIN = 'M' + CHANNEL;
export const THREAD = 'T' + CHANNEL;
2 changes: 1 addition & 1 deletion esm/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*! (c) Andrea Giammarchi - ISC */

import {FUNCTION} from 'proxy-target';
import {FUNCTION} from 'proxy-target/types';

import {CHANNEL} from './channel.js';
import {GET, HAS, SET} from './shared/traps.js';
Expand Down
5 changes: 4 additions & 1 deletion esm/shared/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ import {
STRING,
SYMBOL,
UNDEFINED,
} from 'proxy-target/types';

import {
pair,
unwrap
} from 'proxy-target';
} from 'proxy-target/array';

import {
TypedArray,
Expand Down
27 changes: 5 additions & 22 deletions esm/shared/thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ import {
NUMBER,
STRING,
SYMBOL,
} from 'proxy-target/types';

import {
pair, unwrap,
bound, unbound,
} from 'proxy-target';
} from 'proxy-target/array';

import {
TypedArray,
augment,
defineProperty,
asEntry,
symbol,
transform,
isArray,
getOwnPropertyDescriptor
} from './utils.js';

import {
Expand All @@ -39,20 +39,13 @@ import {
DELETE
} from './traps.js';

const LENGTH = 'length';
const length = getOwnPropertyDescriptor([], LENGTH);

export default name => {
let id = 0;
const ids = new Map;
const values = new Map;

const __proxy__ = Symbol();

const isProxy = value => typeof value === OBJECT && !!value && __proxy__ in value;

const localArray = Array[isArray];

return function (main, MAIN, THREAD) {
const $ = this?.transform || transform;
const { [MAIN]: __main__ } = main;
Expand Down Expand Up @@ -93,8 +86,6 @@ export default name => {

const register = (entry, type, value) => {
if (!proxies.has(value)) {
if (type === ARRAY && !(LENGTH in entry))
defineProperty(entry, LENGTH, length);
const target = type === FUNCTION ? bound(entry) : entry;
const proxy = new Proxy(target, proxyHandler);
proxies.set(value, new WeakRef(proxy));
Expand Down Expand Up @@ -161,17 +152,9 @@ export default name => {

const global = new Proxy(pair(OBJECT, null), proxyHandler);

// this is needed to avoid confusion when Proxy of {type, value}
// passes through `isArray` check, as that would return always true
// by specs and there's no Proxy trap to avoid it.
const remoteArray = global.Array[isArray];
defineProperty(Array, isArray, {
value: ref => isProxy(ref) ? remoteArray(ref) : localArray(ref)
});

return {
[name.toLowerCase()]: global,
[`is${name}Proxy`]: isProxy,
[`is${name}Proxy`]: value => typeof value === OBJECT && !!value && __proxy__ in value,
proxy: main
};
};
Expand Down
9 changes: 3 additions & 6 deletions esm/shared/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import {
ARRAY,
OBJECT,
Expand All @@ -10,9 +9,9 @@ import {
BIGINT,
SYMBOL,
NULL,
pair,
wrap,
} from 'proxy-target';
} from 'proxy-target/types';

import { pair, wrap } from 'proxy-target/array';

const {
defineProperty,
Expand All @@ -30,8 +29,6 @@ const {assign, create} = Object;

export const TypedArray = getPrototypeOf(Int8Array);

export const isArray = 'isArray';

export {
assign,
create,
Expand Down
2 changes: 1 addition & 1 deletion esm/shared/worker.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import {FUNCTION} from 'proxy-target';
import {FUNCTION} from 'proxy-target/types';
export default typeof Worker === FUNCTION ? Worker : class {};
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
"@ungap/structured-clone": "^1.2.0",
"@ungap/with-resolvers": "^0.1.0",
"gc-hook": "^0.2.5",
"proxy-target": "^2.0.1"
"proxy-target": "^2.0.4"
},
"optionalDependencies": {
"ws": "^8.14.2"
Expand Down
2 changes: 1 addition & 1 deletion test/ping-pong.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import coincident from '../esm/index.js';
import coincident from '../es.js';

const wrap = coincident(self);

Expand Down
8 changes: 4 additions & 4 deletions test/structured.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import coincident from '../structured.js';

const until = coincident(self);

console.log('asking for an input');
// pauses in a non blocking way the worker until the answer has been received
console.log('input', until.input('what is 1 + 3 ?'));
console.log('input received');
const result = until.input('what is 1 + 3 ?');
console.log('result', result);

console.assert(typeof result.bigInt === 'bigint');
2 changes: 1 addition & 1 deletion types/channel.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export const CHANNEL: "1e57565f-8f58-46e9-bc61-6dba08309169";
export const CHANNEL: "8e239207-5d0c-4e5b-be5f-e352858459c8";
export const MAIN: string;
export const THREAD: string;
9 changes: 0 additions & 9 deletions types/shared/types.d.ts

This file was deleted.

1 change: 0 additions & 1 deletion types/shared/utils.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export const TypedArray: object | null;
export const isArray: "isArray";
export function augment(descriptor: any, how: any): any;
export function asEntry(transform: any): (value: any) => any;
export function symbol(value: any): any;
Expand Down

0 comments on commit 0f881d8

Please sign in to comment.