-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathroee.js
More file actions
30 lines (27 loc) · 766 Bytes
/
roee.js
File metadata and controls
30 lines (27 loc) · 766 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// ========== roee.js ===============
/*
* A read-only event emitter
* */
const EventEmitter = require('events');
module.exports = class Roee extends EventEmitter {
constructor(executor) {
super();
const emit = this.emit.bind(this);
this.emit = undefined;
executor(emit);
}
};
// ========= ticker.js ==============
/*
* A time tick based on roee.js
* */
const Roee = require('./roee');
const ticker = new Roee((emit) => {
let tickCount = 0;
setInterval(() => emit('tick', tickCount++), 1000);
});
module.exports = ticker;
// ========= ticker usage =============
const ticker = require('./ticker');
ticker.on('tick', (tickCount) => console.log(tickCount, 'TICK'));
// ticker.emit('something', {}); <-- This will fail