From 0f881d860f99a38c06a63c934e40b317446a9381 Mon Sep 17 00:00:00 2001 From: webreflection Date: Tue, 21 Nov 2023 16:23:05 +0100 Subject: [PATCH] Finally moved fully into the proxy-target/array logic and everything works awesomely! --- esm/channel.js | 2 +- esm/index.js | 2 +- esm/shared/main.js | 5 ++++- esm/shared/thread.js | 27 +++++---------------------- esm/shared/utils.js | 9 +++------ esm/shared/worker.js | 2 +- package-lock.json | 8 ++++---- package.json | 2 +- test/ping-pong.js | 2 +- test/structured.js | 8 ++++---- types/channel.d.ts | 2 +- types/shared/types.d.ts | 9 --------- types/shared/utils.d.ts | 1 - 13 files changed, 26 insertions(+), 53 deletions(-) delete mode 100644 types/shared/types.d.ts diff --git a/esm/channel.js b/esm/channel.js index edaad8c..496f167 100644 --- a/esm/channel.js +++ b/esm/channel.js @@ -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; diff --git a/esm/index.js b/esm/index.js index 54f27e8..222fc62 100644 --- a/esm/index.js +++ b/esm/index.js @@ -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'; diff --git a/esm/shared/main.js b/esm/shared/main.js index 966f0ee..2f7a71a 100644 --- a/esm/shared/main.js +++ b/esm/shared/main.js @@ -8,9 +8,12 @@ import { STRING, SYMBOL, UNDEFINED, +} from 'proxy-target/types'; + +import { pair, unwrap -} from 'proxy-target'; +} from 'proxy-target/array'; import { TypedArray, diff --git a/esm/shared/thread.js b/esm/shared/thread.js index ccd3880..5694a38 100644 --- a/esm/shared/thread.js +++ b/esm/shared/thread.js @@ -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 { @@ -39,9 +39,6 @@ import { DELETE } from './traps.js'; -const LENGTH = 'length'; -const length = getOwnPropertyDescriptor([], LENGTH); - export default name => { let id = 0; const ids = new Map; @@ -49,10 +46,6 @@ export default name => { 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; @@ -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)); @@ -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 }; }; diff --git a/esm/shared/utils.js b/esm/shared/utils.js index 765c4fc..bb7bf53 100644 --- a/esm/shared/utils.js +++ b/esm/shared/utils.js @@ -1,4 +1,3 @@ - import { ARRAY, OBJECT, @@ -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, @@ -30,8 +29,6 @@ const {assign, create} = Object; export const TypedArray = getPrototypeOf(Int8Array); -export const isArray = 'isArray'; - export { assign, create, diff --git a/esm/shared/worker.js b/esm/shared/worker.js index 3b72c67..0cd6884 100644 --- a/esm/shared/worker.js +++ b/esm/shared/worker.js @@ -1,2 +1,2 @@ -import {FUNCTION} from 'proxy-target'; +import {FUNCTION} from 'proxy-target/types'; export default typeof Worker === FUNCTION ? Worker : class {}; diff --git a/package-lock.json b/package-lock.json index 1cfd3a1..c95586e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,7 +12,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", "ws": "8.14.2" }, "devDependencies": { @@ -1003,9 +1003,9 @@ } }, "node_modules/proxy-target": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/proxy-target/-/proxy-target-2.0.1.tgz", - "integrity": "sha512-sAilzoWysBVaxmUU17powPm3QUJDBAeWReIXSiXU/QC9T0tmZa79nVcznfQrISxVmKk/5yUj24tWQdlzwrShcg==" + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/proxy-target/-/proxy-target-2.0.4.tgz", + "integrity": "sha512-v2cpWMTx6GH5crqOwQgG/W2QAMpGBVRZmqXop+F3zl+Q6WILlh42jvFoVDn97/LGWlbOz3KH5JcqumbYvHV3gQ==" }, "node_modules/randombytes": { "version": "2.1.0", diff --git a/package.json b/package.json index 26e9669..bbd5cf4 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/test/ping-pong.js b/test/ping-pong.js index 51aa75e..692d3be 100644 --- a/test/ping-pong.js +++ b/test/ping-pong.js @@ -1,4 +1,4 @@ -import coincident from '../esm/index.js'; +import coincident from '../es.js'; const wrap = coincident(self); diff --git a/test/structured.js b/test/structured.js index 4f73fab..cd401f3 100644 --- a/test/structured.js +++ b/test/structured.js @@ -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'); diff --git a/types/channel.d.ts b/types/channel.d.ts index fcf2e9a..639712d 100644 --- a/types/channel.d.ts +++ b/types/channel.d.ts @@ -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; diff --git a/types/shared/types.d.ts b/types/shared/types.d.ts deleted file mode 100644 index c667a63..0000000 --- a/types/shared/types.d.ts +++ /dev/null @@ -1,9 +0,0 @@ -export const OBJECT: "object"; -export const FUNCTION: "function"; -export const BOOLEAN: "boolean"; -export const NUMBER: "number"; -export const STRING: "string"; -export const UNDEFINED: "undefined"; -export const BIGINT: "bigint"; -export const SYMBOL: "symbol"; -export const NULL: "null"; diff --git a/types/shared/utils.d.ts b/types/shared/utils.d.ts index 4f33a5f..627d5f1 100644 --- a/types/shared/utils.d.ts +++ b/types/shared/utils.d.ts @@ -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;