-
-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add TypeScript definition and examples
- Loading branch information
1 parent
f4a2c30
commit a413aee
Showing
20 changed files
with
584 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ yarn.lock | |
.nyc_output | ||
coverage | ||
/legacy.js | ||
/legacy.d.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/clocktyped.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.