Rytm supports multiply JavaScript environment including:
require('path/to/rytm', callback);var Rytm = require('path/to/rytm');Rytm will be a global variable if its factory was ran
without AMD loaderCreate Rytm instance and also loads the tasks
var r = new Rytm(task1, task2, task3 ... )
instance.beatvar r = Rytm()go(), which wraps _go(), make sure the context of go() is always current
instanceA private method which not supposed to be used externally, it creates a node which can be used in the task sequence.
{
// callback - the actual 'task function' in current task
callback: actuallTaskFunc,
// next - the reference to next node in sequence
next: nextNode,
// prev - the reference to prev node
prev: prevNode
// lastCalls - An array that stores callback which returned
// by `all()`
lastCalls: [],
// ticked - true means execution of current task is done
// and it is safe to advance to next task
ticked: false
// went - true to means the cursor advancing is already done
// and do not do it again.
}
Get current task node
Add a callback to the execution sequence
An alias of beat for backward compatibility.
When called, a built-in step will be added to the sequence for pausing a specified millisecond, and pass the arguments to next task.
A wrapper of _go() with bounded context. You can feel free to
call go or pass go to any async function as a callback method without
worrying the changing of context.
function asyncCall(callback){
setTimeout(function(){
callback.call({});
}, 0)
}
(new Rytm(function(){
// no worry, simply pass it to anything you want
asyncCall(this.go);
})).go();
When called, execute the next step immediately (context-binding-free).
go() can force this points to the instance of Rytm,
so in most case we don't have to check this._instance.next() calling context
by visiting _inNextCallContext
if we are, we will have to skip same step because they might already
be called. and then advance to next
beatExecute the next task in the sequence, but delay a bit, same as appending a wait(0) between current task and next task. this is useful when you want to leave the main thread idle so runtime can pick some more important task to process.
Return a new callback, once any of the returned callbacks is called, go to next step and ignore the other returned callbacks.
If we already went to following task and then 1 callback is called we will ignore it. In order to make sure below code can safely advance to next task: var cb = this.once(); this.go(); cb();
When once() is used with all() together, if the callback of once() is called, we advance the cursor and ignore the callbacks of all(), vise versa
Return a new callback each time all() is called, will go to next step
once all returned callbacks are called
It is suggested to call all() in the same synchronous context, if all()
is called in a delayed asynchronous context, the 'next task' may be executed
ealier then you expected.
var r = Rytm(function(){
doAnimate(this.all());
// bbaaaaaaaad! DO NOT invoke all() in a delayed call
// animation may be finished before someElement gets clicked
// at that time, the only one callback was returned by 'all()'
// thus, next task will be executed immedately.
someElement.bind('click', function(){
r.all();
});
}, function(){
blahblah();
});
all() was called,
it will return an noopIf the 'returned callback' is executed within the generating synchronous context, now Rytm will mondatorily delay the actual execution of next task in a asynchronous context. This is to prevent a usage pitfall in old version of Rytm:
s.step(function(){
// this will cause problem if we don't delay the execution.
// Because the first 'returned callback' will be executed
// immediately after it is produced and cause we advanced to next step
// before 2nd all().
executeTheCallbackImmediately(s.all());
executeTheCallbackImmediately(s.all());
})
Here we tick the cur in an asychronous context to tell if the calling is
happened async or sync
A reference to the next task.
The difference between next and go is:
once go gets called, we will immediately advance the internal cursor to next step
so if the go gets called multiple time, your tasks in sequence will be
consumed faster then expected.
next() will make sure that the 'cursor advancing' actually is done in the go()
call of the next task.
next() will do nothing once go() gets called._inNextCallContext indicates how many level of nested next() is called
correspondingly, later call to go() will have to skip the number of _inNextCallContext
tasks.next makes sure the behavior is the same as go when the task has return value.
Return anything other than undefine will cause advancing to next task immediatelythis in task callback actually points to an instance of TaskContext,
TaskContext will make sure calls are fixed to current task.
For example, next() will always call to callback of next task even
when Rytm.cursor is advanced.
Create and return an instance of TaskContext, set current instance.proto to current instance of Rytm, so we can visit the data and method under current instance of Rytm.
If we are at the end, reverse the sequence and go
Return prev beat
To hold the parameters to next beat
Release and destroy current instance (useful when trying to ignore any futher step )
go() cannot be called multiple times or used with the methods such as next()
APIs and Annotation