Skip to content

Commit

Permalink
Add TypeScript definition and examples
Browse files Browse the repository at this point in the history
Fixes #13. Closes #17.
  • Loading branch information
dinoboff authored and novemberborn committed Jan 4, 2018
1 parent f4a2c30 commit a413aee
Show file tree
Hide file tree
Showing 20 changed files with 584 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ yarn.lock
.nyc_output
coverage
/legacy.js
/legacy.d.ts
1 change: 1 addition & 0 deletions examples/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/clocktyped.js
79 changes: 79 additions & 0 deletions examples/clock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#!/usr/bin/env node

'use strict';

const Emittery = require('../');

class Clock extends Emittery {

constructor(tick = 1000) {
super();

this._startedAt = 0;
this._tick = tick > 0 ? tick : 1000;
this._timer = null;
}

async tick() {
if (this._timer === null) {
await this.emit('error', new Error('not started'));
this.stop();
return;
}

const now = Date.now();
const duration = now - this._startedAt;

return this.emit('tick', {now, duration});
}

start() {
this._startedAt = Date.now();
this._timer = setInterval(this.tick.bind(this), this._tick);
this.emit('started', null);
}

stop() {
if (this._timer !== null) {
clearInterval(this._timer);
}

this._timer = null;
this._startedAt = 0;
this.emit('stopped', null);
}
}

const timer = new Clock();
const offTick = timer.on('tick', onTick);
const offError = timer.on('error', onError);

timer.start();

function onTick({duration}) {
console.log(Math.floor(duration / 1000));

if (duration > 5999) {
stop();
}
}

function onError(err) {
stop();
console.error(err);
process.exit(1);
}

function stop() {
offTick();
offError();
timer.stop();
}

// Prints:
// 1
// 2
// 3
// 4
// 5
// 6
99 changes: 99 additions & 0 deletions examples/clocktyped.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#!/usr/bin/env ts-node
import _Emittery = require('../');

// Alias Emittery class to use mapped event types
const Emittery = _Emittery as _Emittery.MappedCtor;

interface TickData {
now: number;
duration: number;
}

// Map Clock's events emitting data to the type of their data.
type EventDataMap = {
tick: TickData,
error: Error
};

// List of event which do not required data
type EmptyEvents = 'started' | 'stopped';

class Clock extends Emittery<EventDataMap, EmptyEvents> {

private _tick: number;
private _timer: NodeJS.Timer | null;
private _startedAt = 0;

constructor(tick = 1000) {
super();

this._tick = tick > 0 ? tick : 1000;
this._timer = null;
}

async tick(): Promise<void> {
if (this._timer == null) {
await this.emit('error', new Error('not started'));
this.stop();
return;
}

const now = Date.now();
const duration = now - this._startedAt;

return this.emit('tick', {now, duration});
}

start() {
this._startedAt = Date.now();
this._timer = setInterval(this.tick.bind(this), this._tick);

this.emit('started');
}

stop() {
if (this._timer != null) {
clearInterval(this._timer);
}

this._timer = null;
this._startedAt = 0;

this.emit('stopped');
}

}

const timer = new Clock();
const offTick = timer.on('tick', onTick);
const offError = timer.on('error', onError);

timer.start();

function onTick({duration}: TickData) {
console.log(Math.floor(duration/1000));

if (duration > 5999) {
stop();
}
}

function onError(err: Error) {
stop();
console.error(err);
process.exit(1);
}

function stop() {
offTick();
offError();
timer.stop();
}

// Prints:
// 1
// 2
// 3
// 4
// 5
// 6
18 changes: 18 additions & 0 deletions examples/emit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env node

'use strict';

const Emittery = require('../');

const myEmitter = new Emittery();

// Emit event in next tick
myEmitter.emit('event');

// Register listener
myEmitter.on('event', () => console.log('an event occurred!'));
myEmitter.onAny(eventName => console.log('"%s" event occurred!', eventName));

// Prints:
// an event occurred!
// "event" event occurred!
20 changes: 20 additions & 0 deletions examples/emitonce.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env node

'use strict';

const Emittery = require('../');

class MyEmitter extends Emittery {}

const myEmitter = new MyEmitter();

// Emit events in next tick
myEmitter.emit('event', 1);
myEmitter.emit('event', 2);

// Register listener for only the one event
myEmitter.once('event')
.then(count => console.log('an event occurred (#%d).', count));

// Prints:
// an event occurred (#1).
20 changes: 20 additions & 0 deletions examples/eventdata.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env node

'use strict';

const Emittery = require('../');

class MyEmitter extends Emittery {}

const myEmitter = new MyEmitter();

// Only accept one event data parameter
myEmitter.emit('event', {a: true, b: true}, 'not', 'supported');

// Does not provide a context either.
myEmitter.on('event', function ({a, b}, ...args) {
console.log(a, b, args, this);
});

// Prints:
// true true [] undefined
Loading

0 comments on commit a413aee

Please sign in to comment.