Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/charliewilco/cyclops


https://github.com/charliewilco/cyclops

Last synced: about 1 month ago
JSON representation

Awesome Lists containing this project

README

        



Cyclops

[![Build Status](https://travis-ci.org/charliewilco/cyclops.svg?branch=master)](https://travis-ci.org/charliewilco/cyclops)

Simple event emitter w. debugging that reminds me of `require('events')`.

## Rationale

There are lots of different implementations of an _event emitter_. The reason this exists is to extend the ability to have custom events.

```typescript
import * as events from "events";

/**
* Represents a polling timeout
* @constructor
*/
class Poller extends events.EventEmitter {
timeout: number;

constructor(timeout: number = 100) {
super();
this.timeout = timeout;
}

poll() {
setTimeout(() => this.emit("poll"), this.timeout);
}

onPoll(cb: () => void) {
this.on("poll", cb);
}
}
```

## Installation

```sh
yarn add @charliewilco/cyclops
```

## Usage Examples

### Basic

```js
const emit = new Cyclops();
let val = 0;

emit.subscribe("mock event", (n: number) => (val = n));
emit.emit("mock event", 18);
```

### Polling

```js
class Poller extends Cyclops {
constructor(timeout = 500) {
this.timeout = timeout;
}
public poll() {
setTimeout(() => this.emit("poll"), this.timeout);
}

public onPoll(cb) {
this.subscribe("poll", cb);
}
}

let count = 0;
const poll = new Poller(1000);

poll.onPoll(() => {
count++;

console.log(count);

poll.poll();
});

poll.poll();
```