Clumpy.js

Reference

This page documents Clumpy, method by method. See the usage guide for more information on how to use these methods together.

Constructor

new Clumpy(options)

Creates a new Clumpy instance.

Parameter Default Description
options.between null A function to call between clumps.
options.delay 0 The number of milliseconds to rest between clumps.
options.duration 100 The maximum number of milliseconds each clump is allowed to run.

Example

// Defaults:
var clumpy = new Clumpy();

// With Options Specified:
var clumpy = new Clumpy({
  between: function () {
    console.log('Moving right along...');
  },
  // Devote equal time to processing and resting:
  delay: 50,
  duration: 50
});

Looping Actions

The following methods model looping statements.

for_loop(init, test, inc, statements)

Enqueue an action that models a for loop.

Parameter Description
init A function that initializes the loop.
test A function that returns the result of the condition for the loop.
inc A function that increments the loop.
statements A function that performs one iteration of the loop’s body.

All parameters are required.

Example

// Real Loop
for (i = 0; i < 10; i += 1) {
  console.log(i);
}

// Clumpy Equivalent
clumpy.for_loop(
  function () { i = 0; },
  function () { return i < 10; },
  function () { i += 1; },
  function () {
    console.log(i);
  }
);

for_in_loop(getObject, statements)

Enqueue an action that models a for...in loop.

Parameter Description
getObject A function that returns the object over which to iterate.
statements A function that performs one iteration of the loop’s body. It should accept an argument representing the key for the current loop iteration.

Both parameters are required.

Example

// Real Loop
for (key in object) {
  console.log(key + ': ' + object[key]);
}

// Clumpy Equivalent
clumpy.for_in_loop(
  function () { return object; },
  function (k) {
    key = k;
    console.log(key + ': ' + object[key]);
  }
);

The scratch variable k in the example above is for mimicking the scope key has in the real loop. If you don’t need to access the variable from outside the statements function, you can use its argument directly:

clumpy.for_in_loop(
  function () { return object; },
  function (key) {
    console.log(key + ': ' + object[key]);
  }
);

while_loop(test, statements)

Enqueue an action that models a while loop.

Parameter Description
test A function that returns the result of the condition for the loop.
statements A function that performs one iteration of the loop’s body.

Example

// Real Loop
while (i < 10) {
  console.log(i);
  i += 1;
}

// Clumpy Equivalent
clumpy.while_loop(
  function () { return i < 10; },
  function () {
    console.log(i);
    i += 1;
  }
);

do_while_loop(statements, test)

Enqueue an action that models a do...while loop.

Parameter Description
statements A function that performs one iteration of the loop’s body.
test A function that returns the result of the condition for the loop.

Both parameters are required.

Example

// Real Loop
do {
  console.log(i);
  i += 1;
} while (i < 10);

// Clumpy Equivalent
clumpy.do_while_loop(
  function () {
    console.log(i);
    i += 1;
  },
  function () { return i < 10; }
);

Loop Control

The following methods emulate break, continue, and label statements, for modifying the flow of loops.

break_loop(label)

Break the closest loop with the specified label, or the current loop if no label is given.

Parameter Default Description
label undefined The label of the loop to break.

continue_loop(label)

Continue the closest loop with the specified label, or the current loop if no label is given.

Parameter Default Description
label undefined The label of the loop to continue.

label(name)

Assign a label to the next action enqueued, if the action is a loop. If the next action is not a loop, the label is discarded.

Parameter Description
name (required) The string to assign as the label.

Single Actions

The following methods enqueue actions that aren’t loops.

interrupt()

Enqueue an end to the clump that’s running at the time the interrupt is dequeued.

set(options)

Enqueue the act of setting one or more options for this Clumpy instance. Contrast this with setNow below, which sets the options immediately at the time the method is called.

The options object can have the same properties as the Clumpy constructor.

sleep(delay)

Enqueue a sleep of delay milliseconds before deueueing the next action.

Parameter Description
delay (required) The number of milliseconds to sleep.

then(statements)

Enqueue the statements function to be called a single time.

Parameter Description
statements (required) The function to invoke.

wait(statements)

Enqueue an outside asynchronous operation and wait until that operation completes before dequeueing the next action.

Parameter Description
statements (required) The function to call. It should accept a function as an argument (done in the example below), and invoke that function as a callback when the task is complete.

Example

(clumpy
  .while_loop(
    // snip
  )
  .wait(function (done) {
    jQuery.get('/example/async/', function (data) {
      console.log('Got Data: ' + data);
      done();
    });
  })
  .for_loop(
    // snip
  )
);

External Control

The following methods act immediately instead of enqueueing actions. They are for external control over a Clumpy instance, and should generally not be used inside enqueued actions.

clear()

Stop this Clumpy instance immediately and discard all of its enqueued actions.

pause()

Stops execution immediately until resume is called.

resume()

Resumes execution after pause().

setNow(options)

Set options for this Clumpy instance immediately. Contrast this with set above, which doesn’t set the options immediately, but enqueues the act of setting the options.

The options object can have the same properties as the Clumpy constructor.