dead-claudia / invoke-parallel Goto Github PK
View Code? Open in Web Editor NEWSimple worker pools done right. (on hold)
Simple worker pools done right. (on hold)
Why not save a bit of memory by using proxies with caching to lighten the footprint of methods? Also, while we're at it, the length is a bit useless, and could probably be shed. (Most of the time functions depend on lengths, they also have a facility to specify it explicitly.)
const methodData = new WeakMap()
const methodCache = new Map()
const hasOwn = Object.prototype.hasOwnProperty
const proxyTarget = Object.create(null)
Object.defineProperty(proxyTarget, Symbol.toStringTag, {
configurable: false, enumerable: false, writable: false,
value: "Module Proxy",
})
function compileMethod(name) {
let memo = methodCache.get(name)
if (memo == null) {
methodCache.set(name, memo = /** @this */ function (...args) {
const {module, options} = methodData.get(this)
return module.pool.invoke(
{module, options, name},
args.length ? args : undefined
)
})
}
return memo
}
exports.ModuleWrap = class ModuleWrap {
constructor(pool, name, methods) {
this.pool = pool
this.name = name
this.methods = methods
this.proxy = this.apply(proxyTarget, undefined, [])
}
apply(target, thisArg, [options]) {
if (options == null || typeof options !== "object") {
throw new TypeError("`options` must be an object")
}
const proxy = new Proxy(target, this)
methodData.set(proxy, {module: this, options})
return proxy
}
get(target, key, receiver) {
return hasOwn.call(this.methods, key)
? compileMethod(key)
: Reflect.get(target, key, receiver)
}
has(target, key) {
return hasOwn.call(this.methods, key) || Reflect.has(target, key)
}
set() { return false }
defineProperty() { return false }
deleteProperty() { return false }
setPrototypeOf() { return false }
preventExtensions() { return false }
}
Line in question - it should be closer to this:
const p = new Promise(res => {
resolve = () => { if (res != null) { res(); res = undefined } }
})
General API sketch:
invoke.import(import.meta, mod, opts).then(...)
= ES6 import
export
ed.import
isn't a valid variable binding, but it is a valid export name.import {...} from "worker.js#parallel"
using the global pool. (easy)Things it needs:
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.