Skip to content

Commit

Permalink
new alternative APIs for load:
Browse files Browse the repository at this point in the history
- loader(): returns a thunk, which means users don't need to specify the exports but have to remember to lazily apply the thunk to guarantee laziness
- proxy(): returns a proxy, which means users don't need to specify the exports and always get the correct lazy behavior no matter what

right now hidden behind ugly `__UNSTABLE_foo` names
  • Loading branch information
David Herman committed Dec 10, 2023
1 parent 6e61a0c commit d1dd027
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
11 changes: 11 additions & 0 deletions pkgs/cargo-messages/lib/load.cjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
// module.exports = require('@neon-rs/load').__UNSTABLE_proxy({
// 'darwin-x64': () => require('@cargo-messages/darwin-x64'),
// 'win32-x64-msvc': () => require('@cargo-messages/win32-x64-msvc'),
// 'aarch64-pc-windows-msvc': () => require('@cargo-messages/win32-arm64-msvc'),
// 'darwin-x64': () => require('@cargo-messages/darwin-x64'),
// 'darwin-arm64': () => require('@cargo-messages/darwin-arm64'),
// 'linux-x64-gnu': () => require('@cargo-messages/linux-x64-gnu'),
// 'linux-arm-gnueabihf': () => require('@cargo-messages/linux-arm-gnueabihf'),
// 'android-arm-eabi': () => require('@cargo-messages/android-arm-eabi')
// });

module.exports = require('@neon-rs/load').lazy({
targets: {
'darwin-x64': () => require('@cargo-messages/darwin-x64'),
Expand Down
70 changes: 70 additions & 0 deletions pkgs/load/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,73 @@ export function lazy(optionsOrLoaders: LazyOptions | Record<string, () => any>,
? lazyV1(optionsOrLoaders as Record<string, () => any>, exports)
: lazyV2(optionsOrLoaders as LazyOptions);
}

export function __UNSTABLE_loader(loaders: Record<string, () => Record<string, any>>): () => Record<string, any> {
const target = currentTarget();
if (!loaders.hasOwnProperty(target)) {
throw new Error(`no precompiled module found for ${target}`);
}
const loader = loaders[target];
let loaded: Record<string, any> | null = null;
return () => {
if (loaded) {
return loaded;
}
loaded = loader();
return loaded;
};
}

export function __UNSTABLE_proxy(loaders: Record<string, () => Record<string, any>>): any {
const target = currentTarget();
if (!loaders.hasOwnProperty(target)) {
throw new Error(`no precompiled module found for ${target}`);
}
const loader = loaders[target];
let loaded: Record<string, any> | null = null;

function load(): Record<string, any> {
if (!loaded) {
loaded = loader();
}
return loaded;
}

const handler = {
has(_target: any, key: string) {
return Reflect.has(load(), key);
},
get(_target: any, key: string) {
return Reflect.get(load(), key);
},
ownKeys(_target: any) {
return Reflect.ownKeys(load());
},
defineProperty(_target: any, _key: string, _descriptor: any) {
throw new Error('attempt to modify read-only Neon module proxy');
},
deleteProperty(_target: any, _key: string) {
throw new Error('attempt to modify read-only Neon module proxy');
},
set(_target: any, _key: string, _val: any) {
throw new Error('attempt to modify read-only Neon module proxy');
},
setPrototypeOf(_target: any, _proto: any) {
throw new Error('attempt to modify read-only Neon module proxy');
},
getPrototypeOf(_target: any) {
return Object.getPrototypeOf(load());
},
isExtensible(_target: any) {
return Reflect.isExtensible(load());
},
preventExtensions(_target: any) {
return Reflect.preventExtensions(load());
},
getOwnPropertyDescriptor(_target: any, key: string) {
return Reflect.getOwnPropertyDescriptor(load(), key);
}
};

return new Proxy({}, handler);
}

0 comments on commit d1dd027

Please sign in to comment.