Giter Site home page Giter Site logo

invoke-parallel's People

Contributors

dead-claudia avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

invoke-parallel's Issues

Fix module proxy to use proxies

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.)

Example implementation
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 }
}

The cancel token is severely lacking in basic functionality

Things it needs:

  1. Synchronous inspection. (If a cancel token has already been invoked, it shouldn't even attempt to run a worker method.)
  2. Synchronous cancellation.
  3. "Throw if cancelled", "resolve on cancellation", and "reject on cancellation".
  4. Wrapping a generator to work like an async function, but bail on cancellation (while also notifying the generator).

Notes for ES6 `import` support

General API sketch:

  • invoke.import(import.meta, mod, opts).then(...) = ES6 import
    • I can't do the magic wrapping I do for CommonJS, since I can't access the cache, much less modify it.
  • Calling methods are just as normal.
  • Requires library to be compiled to two separate targets (main and worker).
  • Worker methods are loaded as modules and exported.
  • Note: import isn't a valid variable binding, but it is a valid export name.
  • Shouldn't require a major amount of work short of just re-implementing Node's ESM lookup algorithm.
  • Optional loader hook that allows you to do import {...} from "worker.js#parallel" using the global pool. (easy)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.