Giter VIP home page Giter VIP logo

webwork's Introduction

webwork.js

Execute Web Workers without external files.

Typically, Javascript code executes in a single thread, with each task lined up to execute after the previous one has completed.

By calling in external files, Web Workers allow browsers to process large tasks in the background, creating new threads to handle more tasks simultaneously. Webwork.js lets you create and execute Web Workers inline without the need for importing external files.

Usage is really simple:

import webwork from '@kolodny/webwork';
callableWebworker = webwork(callback);

callback takes a single data argument and should return the result
callableWebworker is a function that takes the data to send and
a node style callback ie function(err, result) {}

Example

var goodWorker = webwork(function (data) {
    return "!!" + data  + "!!";
});
var badWorker = webwork(function (data) {
    return "!!" + data + imNotDefined + "!!";
});

goodWorker("Test123", function(err, result) {
    if (err) return alert("goodWorker Errored with " + err.message);
    alert("first goodWorker returned with " + result);
});
goodWorker("Test123", function(err, result) {
    if (err) return alert("goodWorker Errored with " + err.message);
    alert("second goodWorker returned with " + result);
});
badWorker("Test456", function(err, result) {
    if (err) return alert("badWorker Errored with " + err.message);
    alert("badWorker returned with " + result);
});
alert("Since webworkers are async this should happen first.\nThe callbacks may fire in any order");

The astute may realize that they can access the event variable inside the callback.
Also since the callback is stringified and injected into the webworker, you don't have access to any vars in scope

What to Use webwork.js for and When to Use It

webwork.js is best used for any project that requires web workers, but needs to minimize or eliminate external web worker files. It also allows for simpler, cleaner inline web worker code. webwork.js should be used when a project:

  • needs web workers, but wants to keep code inline for minification and distribution purposes
  • needs web workers, but wants to keep code inline to avoid excessive external web worker files and page requests
  • needs inline web workers, and wants to avoid repetitive and tedious Blob code

webwork's People

Contributors

kolodny avatar salzbrenner avatar merrimanbt avatar peterkroon avatar

Stargazers

Hatem Hosny avatar Matt Price avatar  avatar Cat  avatar Caldis_Chen avatar  avatar  avatar LiQiang avatar kunkun avatar zun. avatar Mendy Berger avatar Jan avatar _deprecated_n_w avatar  avatar devcorn avatar スィルヴァン avatar Pavel avatar Tomasz Kociołek avatar Ernst Salzmann avatar Rory Duncan avatar Angus H. avatar  avatar Jake Hendley avatar Mladen Krivaćević avatar Eric Kennedy avatar Chris Hart avatar Aswini S avatar albert avatar  avatar Jim Gray avatar Steve Hill avatar Steve Jabour avatar Daniel Ribeiro avatar Javier Sánchez - Marín avatar Petter Aas avatar Jason Gerfen avatar Alex avatar  avatar Gustavo Yauny avatar Haris Pobric avatar Eric Winther avatar Simon Peters avatar John Sun avatar Thomas Gratier avatar Tanmay Patel avatar Eliazer Braun avatar hika, maeng avatar Brice avatar Amanda Shih avatar  avatar  avatar Nick Gault avatar Ax avatar r2fresh avatar Andris Sīlis avatar Julien avatar Mohammad Sadegh Khoeini avatar iMaZiNe avatar JaePil Jung avatar  avatar Chookeun Moon avatar Bob TheBuilder avatar  avatar Steven Olsen avatar Chris A. avatar Oto Dočkal avatar Jeffrey Hyer avatar Joshua Jones avatar Jesse Baird avatar 西红那个柿 avatar James Van Dyke avatar Joel Kallman avatar JP Camara avatar Riad Benguella avatar Romaric Drigon avatar April Johnson avatar Stefan Vatov avatar Aldiantoro Nugroho avatar 唐睿 avatar Octavio Turra avatar Frank avatar Justin Sisley avatar Li Song avatar Harry Pfleger avatar Daniel Ramirez avatar Ishan Anand avatar tim walker avatar 孙茂胤 (Sun, Maoyin) avatar Dirk Gadsden avatar Thomas Cresine avatar  avatar Volodymyr Melnyk avatar Cristián Romo avatar  avatar Nikolay Kolev avatar Steven Schobert avatar Varun Singh avatar Ben Morrall avatar Gal Dolber avatar Adam Renklint avatar

Watchers

Glenn Nagel avatar Václav Oborník avatar  avatar  avatar Kuu Miyazaki avatar Oto Dočkal avatar Aswini S avatar calmlion avatar  avatar

webwork's Issues

Fall back to trying data: URL

At least Presto allows data: URLs in workers (and it's allowed per spec now, too), but didn't support creating workers from Blobs. So if you care about Presto you could try that if the Blob thing failed.

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.