Code Monkey home page Code Monkey logo

task.js's Introduction

task.js

task.js provides an automatic task scheduler along with a library of first-class, synchronizable events, making it easy to do I/O without callbacks.

With task.js you can write non-blocking I/O in a synchronous style, even with error handling:

spawn(function*() {
    try {
        var [foo, bar] = yield join(read("foo.json"),
                                    read("bar.json")).timeout(1000);
        render(foo);
        render(bar);
    } catch (e) {
        console.log("read failed: " + e);
    }
});

Compared with callbacks:

var foo, bar;
var tid = setTimeout(function() { failure(new Error("timed out")) }, 1000);

var xhr1 = makeXHR("foo.json",
                   function(txt) { foo = txt; success() },
                   function(err) { failure() });
var xhr2 = makeXHR("bar.json",
                   function(txt) { bar = txt; success() },
                   function(e) { failure(e) });

function success() {
    if (typeof foo === "string" && typeof bar === "string") {
        cancelTimeout(tid);
        xhr1 = xhr2 = null;
        render(foo);
        render(bar);
    }
}
    
function failure(e) {
    xhr1 && xhr1.abort();
    xhr1 = null;
    xhr2 && xhr2.abort();
    xhr2 = null;
    console.log("read failed: " + e);
}

...tasks can be a lot simpler and cleaner. And unlike pre-emptive threads, yield always makes it clear where tasks block.

Contributing

Currently the best way to contribute is to hang out on IRC: the channel is #task.js on irc.mozilla.org. Or you can always send me email (my Github nick at mozilla.com). And I'm always happy to accept pull requests!

If you're looking for interesting things to work on, check out the issue tracker.

task.js's People

Contributors

byk avatar samth avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

task.js's Issues

update or make an alternate version for the ES6 generators API

The ES6 generators and iterators API is a little different from the one initially implemented in Firefox, which dates back to an earlier design. Update task.js to work with the new API, which can now be tested in the latest build of V8:

http://wingolog.org/archives/2013/05/08/generators-in-v8

We should hopefully start implementing the new generators API soon in SpiderMonkey as well, so that the new version of task.js can then be used.

replace Wait with standard promise API

The silly Wait abstraction is more or less just a non-standard name for promises. Change the API to use standard promises, ideally making it compatible with both jQuery and Dojo, if possible.

choose a doc generation tool

The current API docs are generated using some stupid hand-hacked scripts I have locally on my machine. We should choose a documentation generation tool and use that.

Wiki changes

FYI: The following changes were made to this repository's wiki:

These were made as the result of a recent automated defacement of publically writeable wikis.

fail-soft alternative to join

The Task.prototype.join primitive is fail-fast with exceptions: if any task throws, all the tasks are killed and the exception propagates. Implement an alternative combinator that is fail-soft: if a task throws, simply detach it and continue waiting for a return value from another task. If all tasks throw, the result should throw a "no answer" error, possibly packaged up with all the exceptions that were caught.

something better than ObjectMap

Object-detect for Map to use in place of ObjectMap? Or maybe something better than a side table... I just don't really want observables to have to mutate observers.

remove (mandatory) dependancy on RSVP

Promises are a DOM (and soon ES) standard, and implemented in Firefox and Chrome (enabled by default in upcoming releases). Depending on RSVP adds an unnecessary overhead for the environments implementing standard Promises. Please only load RSVP when Promises are not provided by the platform!

(BTW: The issue list really needs some cleanup. ;-))

CODE_OF_CONDUCT.md file missing

As of January 1 2019, Mozilla requires that all GitHub projects include this CODE_OF_CONDUCT.md file in the project root. The file has two parts:

  1. Required Text - All text under the headings Community Participation Guidelines and How to Report, are required, and should not be altered.
  2. Optional Text - The Project Specific Etiquette heading provides a space to speak more specifically about ways people can work effectively and inclusively together. Some examples of those can be found on the Firefox Debugger project, and Common Voice. (The optional part is commented out in the raw template file, and will not be visible until you modify and uncomment that part.)

If you have any questions about this file, or Code of Conduct policies and procedures, please reach out to [email protected].

(Message COC001)

complete set of DOM facilities

The task.dom.js file is just a stub. Fill this out with a more complete set of promise-ified DOM facilities.

(Depends on issue #12.)

cycle detection for join

I believe that Task.prototype.join will cause a deadlock if there are cyclic joins. Should we do eager cycle detection? Some sort of garbage collection?

maintain task tree

When a task creates a subtask, it should keep a reference to the child task so that if the parent is cancelled, it can cancel its children.

better choice function

The current logic for choosing the next task is to use Math.random(). Should this just deterministically choose the next in the array? Or should it use a better PRNG?

This should be factored out into different schedulers.

clear out dead tasks

For prompt memory management, when a task dies, all its internals should probably be nulled out.

are there any ideas to make an abstraction on top of task.js?

One of the libraries I recently played with for doing non-blocking I/O is called gevent from python community. The nice thing was you can have a completely readable synchronous code yet the run time was non-blocking and managed cooperative scheduling. The gevent library monkey patched some of the system API level calls like "opening a socket" for example. I was wondering if there is a place for an abstraction like that on top of task.js so that there is no yield statements in users code.

DOM "ready" function

There should be a nice "ready" abstraction in task.dom.js that, similar to jQuery, abstracts the "start running when the page is ready" concept. This can serve as the "main" function of the main thread of a task.js application.

Support for Promises/A+?

The task.js documentation indicates that compatibility with Promises/A, the predecessor of A+, is required. Will task.js be updated to require compatibility with A+?

Breaks `window.Array` for Chrome

I have a fairly rich build environment right now (webpack and babel), so it may be hard discovering where the source of error is specifically, but when I build my project while merely importing task.js, it breaks the browsers' environment and lodash comes back to me saying that Array is not a function. I will provide more data on this as needed.

library of Node.js facilities

Just like task.dom.js there should be a task.node.js file that wraps standard Node libraries with promise abstractions for working with task.js. These won't really be useable until there's a version of task.js that implements generators, but once V8 implements generators, Node's async I/O is another good use case for task.js.

(Depends on issue #12.)

recording mode

Create either a custom scheduler-wrapper (which would depend on issue #1) to record all events, or a global recording mode with an on-off switch. Recording should be to a serializable "script" data structure.

(Depends on issue #1.)

replay scheduler

Create a deterministic replay scheduler, which takes a task-interleaving script and re-enacts it.

(Depends on issues #1 and #9).

lib/task.dom.js missing

In examples/finish.html there is a script referenced at lib/task.dom.js which is missing from the repo. I ran the example in Firefox and it was only able to output the following:

outer task waiting...
inner task working...
inner task finishing...

...However it doesn't seem like anything in the example code is depending on anything other then task.js

node.js examples

Can you please add some node.js examples (I assume this works great with node too)? Along the same lines, on your taskjs.org website, can you make it clear if/how to install via NPM?

npm install taskjs

?

race condition in task.pause() API

Forcing clients to pause a task in a separate turn introduces the possibility that the task might be scheduled and executed before the pausing occurs.

Publish the library on bower or npm

Hi,
I think this library should be published on bower or npm, I can help adding a task runner (gulp or grunt), minify the code and ship it to bower and / or npm.
Let me know if this is something you want to accomplish.
Regards

replace promises implementation with RSVP.js

My promises implementation in task.js is not at all compliant with Promises/A{+}. It's funny, actually, I did this work before I understood why promises implicitly chain — and with task.js it doesn't really need them to chain. But my promises will not interoperate correctly with any popular promise libraries.

And anyway, task.js should do one thing and do it well… ;-) So we should just depend on RSVP instead of rolling our own promises.

don't require task functions to be generators

There's absolutely no reason a task function should have to be a generator function. As long as it produces an iterator object, that's all we should care about. I.e., it should be duck typed.

README.md demo code

Suggestion: Instead of

var xhr1 = makeXHR("foo.json",
                   function(txt) { foo = txt; success() },
                   function(err) { failure() });  // missing argument `err`?
var xhr2 = makeXHR("bar.json",
                   function(txt) { bar = txt; success() },
                   function(e) { failure(e) });

write

var xhr1 = makeXHR("foo.json",
                   function(txt) { foo = txt; success() },
                   failure);
var xhr2 = makeXHR("bar.json",
                   function(txt) { bar = txt; success() },
                   failure);

test suite

Need to choose a testing framework and set up a test suite.

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.