3.0.0-1 • Published 6 years ago

centaurus v3.0.0-1

Weekly downloads
3
License
CC-BY-4.0
Repository
github
Last release
6 years ago

Centaurus

Centaurus

Web worker interface. Simplified.

// load up
const Centaurus = require('centaurus');

// create worker, pass path to our worker
let C = new Centaurus('https://unpkg.com/centaurus/dist/centaurus.worker.min.js');

// load umd scripts, be it lodash, whatever.
C.loadScripts(
  'https://unpkg.com/pako/dist/pako.min.js',
  'https://unpkg.com/lodash/lodash.min.js'
)
  .then(() => C.registerFunctions({
    saySomething: (resolve, reject, sayWhat) => {
      console.log(sayWhat);
      resolve();
    },
    checkMyImports: (resolve, reject, param1) => {
      console.log(param1, Boolean(pako) && Boolean(_));
      console.log('pako:', pako);
      console.log('lodash:', _);
      resolve();
    }
  }))
  .then(() => C.saySomething('Anyeonghaseyo!'))
  .then(() => C.checkMyImports('is pako and lodash found?'))
  .then(() => C.terminate())
  .catch(console.error);

Notes

How does it work?

  • All main-thread to worker-thread communications use promises.
  • UUIDv4 & Maps are used to identify incoming & outgoing messages.
  • serialize-javascript is used to serialize all messages.

How do I create global variable assignments within the worker?

Yes. Just assign self.whatever inside your functions.

And yes, your other functions can use them also.

C.registerFunctions({
    RegisterSomethinGlobal: (resolve, reject) => {
      self.name = 'xemasiv';
      resolve();
    },
    UseThatGlobal: (resolve, reject) => {
      console.log(name);
      resolve();
    }
  })
  .then(() => C.RegisterSomethinGlobal())
  .then(() => C.UseThatGlobal())
  .catch(console.error);

Can I pass parameters?

Yes, please see the top-most example.

Can I return results?

Oh yes you can, you're unstoppable.

What types can we pass to the Worker?

License

Attribution 4.0 International (CC BY 4.0)

cc by

3.0.0-1

6 years ago

2.2.1

6 years ago

2.2.0

6 years ago

2.1.0

6 years ago

2.0.1

6 years ago

2.0.0-alpha-2

6 years ago

2.0.0-alpha

6 years ago

1.0.0

6 years ago

0.0.0

7 years ago