# Cycl

A 1.5kb process manager running on a single requestAnimationFrame loop. Every process has a callback that, when active, fires once per frame.

## Install

### NPM + Browserify (recommended)

First install Cycl in your project root.

```  
$ npm install cycl
```

Then include in your module using require().

```javascript
var cycl = require('cycl');
```

### File include

Download the latest cycl.global.min.js from http://github.com/InventingWithMonster/cycl and include it in your HTML document with a script tag, ie:

```html
<script src="/path/to/cycl.global.min.js"></script>
```

This will load Cycl to the global scope as window.cycl, accessible in any script executed subsequently.

## Use

### Example process

```javascript
var process = cycl.newProcess(function (framestamp) {
    console.log('hello world! ' + framestamp);
});

process.start(); // Will output 'hello world' and the timestamp of the executing frame
```

To run a function with Cycl, we first have to create a new process. Active processes are fired once per frame.

### Create a new process

The method newProcess takes the optional parameter scope, and a callback. If scope is provided, our callback executes in its context (this refers to scope). Otherwise the process itself is used as the context.

```javascript
var process = cycl.newProcess(scope, callback);
```

### Start a process

On creation, a process is not active. To start it, we simply call .start(). 

```javascript
process.start(); // Our process runs
```

### Stopping a process

We can stop this process with .stop(). 

```javascript
process.stop(); // Our process stops
```

### Run a process for a specific amount of time

Processes can be set to stop after a predefined period of time by passing start() a duration argument (in ms).

```javascript
process.start(1000); // Our process runs for 1000ms
```

### Run process every x seconds

Processes can be set to fire at regular intervals, like a framerate-locked setInterval().

```javascript
process.every(100); // Our process runs once every 100ms
```

### Change scope and callback

The scope and callback of a process can be changed with .setCallback() and setScope(). All these process methods can be chained.

```javascript
process
    .setScope(newScope)
    .setCallback(newCallback)
    .start();
```

### Kill a process (for garbage collection)

To remove a process from memory, you first need to run .kill(), which will delete it from cycl's internal tracking. Then delete any references to that object.

```javascript
process.kill(); // Process is removed from Cycl, but still exists
delete process; // Now references to process are removed, garbage collection can take place.

```
