Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/charliewilco/cyclops
https://github.com/charliewilco/cyclops
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/charliewilco/cyclops
- Owner: charliewilco
- Created: 2018-01-19T21:09:19.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2021-11-12T06:17:17.000Z (about 3 years ago)
- Last Synced: 2024-04-17T02:07:53.950Z (7 months ago)
- Language: TypeScript
- Size: 131 KB
- Stars: 7
- Watchers: 2
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: Readme.md
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();
```