Coder Social home page Coder Social logo

digideskio / clooney Goto Github PK

View Code? Open in Web Editor NEW

This project forked from googlechromelabs/clooney

0.0 1.0 0.0 595 KB

Clooney is an actor library for the web. Use workers without thinking about workers.

License: Apache License 2.0

Dockerfile 4.00% JavaScript 54.51% TypeScript 41.49%

clooney's Introduction

Clooney

Clooney is an actor (ayooo) library for the web. Classes given to Clooney will be instantiated and run in a worker, keeping the main thread responsive.

⚠️ Caveat: The class cannot rely on its surrounding scope, since it is executed in an isolated context. This might change once workers support ES6 modules.

Quickstart

An example says more than 1000 words:

<script src="/clooney.bundle.js"></script>
<script>
  (async function() {
    class MyRemoteClass {
      doExpensiveCalculation(a, b) {
        return a + b;
      }
    }

    const instance = await Clooney.spawn(MyRemoteClass);
    console.log(await instance.doExpensiveCalculation(5, 23));
  })();
</script>

I’m collecting more examples of Clooney in action in this Glitch.

Events and Functions

Functions and events are not transferable (i.e. can’t be sent from to a worker), but Clooney has special handling for them:

class MyRemoteClass {
  onClick(remoteEvent) {
    // … react to click …
  }
}

const instance = await Clooney.spawn(MyRemoteClass);
const button = document.querySelector('button');
button.addEventListener('click', instance.onClick.bind(instance));

The remoteEvent object is a mangled version of the original event to make it transferable:

const remoteEvent = {
  targetId, // = event.target.id
  targetClassList, // = [...event.target.classList]
  detail, // = event.detail
  data // = event.data
};

Promises and async methods

Clooney handles promises (and therefore, async methods) automatically:

class Actor {
  timeoutThing() {
    return new Promise(resolve => setTimeout(_ => resolve('ohai'), 1000));
  }
}

const instance = await strategy.spawn(Actor);
alert(await instance.timeoutThing()); // Will alert() after 1 second

API

Clooney’s job is to take actors (class definitions) and spawn those actors in containers (Web Workers). You can use that instance as if it was a local instance (this is magic provided by Comlink).

Clooney.spawn(class, constructorArgs)

This call is equivalent to Clooney.defaultStrategy.spawn(class, constructorArgs). Clooney creates an instance of RoundRobinStrategy as the default strategy.

Strategies

Strategies decide how many containers are spun up and where a new instance is created.

export interface Strategy {
  /**
   * `spawn` instantiates the given actor in an actor container of the strategy’s choice.
   * @returns The return type is the type as T, but every method is implicitly async.
   */
  spawn<T>(actor: new () => T, constructorArgs: any[], opts: Object): Promise<T>;
  /**
   * `terminate` calls `terminate()` on all existing containers of the strategy.
   */
  terminate(): Promise<void>;
}

Clooney.RoundRobinStrategy(opts)

RoundRobinStrategy creates up to n containers and cycles through the containers with every spawn call. RoundRobinStrategy is the default strategy.

Strategy Options

  • maxNumContainers: Maximum number of containers to create (default: 1)
  • newWorkerFunc: Asynchronous function that creates a new container (default: new Worker(Clooney.defaultWorkerSrc))

Clooney.asRemoteValue(obj)

asRemoteValue marks a value. If a marked value is used as an parameter or return value, it will not be transferred but instead proxied.

CDN

If you want to use Clooney from a CDN, you need to work around the same-origin restrictions that workers have:

<script src="https://cdn.jsdelivr.net/npm/[email protected]/clooney.bundle.min.js"></script>
<script>
  async function newWorkerFunc() {
    const blob = await fetch(Clooney.defaultWorkerSrc).then(resp => resp.blob())
    return new Worker(URL.createObjectURL(blob));
  }

  const strategy = new Clooney.RoundRobinStrategy({newWorkerFunc});
  // Business as usual using strategy.spawn() ...
</script>

License Apache-2.0

clooney's People

Contributors

andarist avatar arthur-melo avatar felixfbecker avatar jsbrn avatar renovate-bot avatar renovate[bot] avatar surma avatar

Watchers

 avatar

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.