Skip to content

Commit a978d31

Browse files
committed
Add solutions for 3rd module exercises
0 parents  commit a978d31

File tree

5 files changed

+191
-0
lines changed

5 files changed

+191
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { EventEmitter } from 'events'
2+
import { readFile } from 'fs'
3+
4+
class FindRegex extends EventEmitter {
5+
constructor (regex) {
6+
super()
7+
this.regex = regex
8+
this.files = []
9+
}
10+
11+
addFile (file) {
12+
this.files.push(file)
13+
return this
14+
}
15+
16+
find () {
17+
// +++ 3.1 added line
18+
process.nextTick(() => this.emit('start', this.files));
19+
for (const file of this.files) {
20+
readFile(file, 'utf8', (err, content) => {
21+
if (err) {
22+
return this.emit('error', err)
23+
}
24+
25+
this.emit('fileread', file)
26+
27+
const match = content.match(this.regex)
28+
if (match) {
29+
match.forEach(elem => this.emit('found', file, elem))
30+
}
31+
})
32+
}
33+
return this
34+
}
35+
}
36+
37+
const findRegexInstance = new FindRegex(/hello \w+/)
38+
findRegexInstance
39+
.addFile('fileA.txt')
40+
.addFile('fileB.json')
41+
.find()
42+
.on('found', (file, match) => console.log(`Matched "${match}" in file ${file}`))
43+
.on('start', (files) => console.log('Find process started', files)) // +++ added line
44+
.on('error', err => console.error(`Error emitted ${err.message}`))

03-callbacks-and-events/3.2-ticker.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { EventEmitter } from 'events';
2+
3+
// 1 main ticker function
4+
function ticker(timer, callback) {
5+
const eventEmitter = new EventEmitter();
6+
let tick = 0;
7+
const TICK_INTERVAL = 50;
8+
9+
const tickStart = new Date();
10+
11+
const emitTick = () => {
12+
emitter.emit('tick', tick);
13+
tick++;
14+
}
15+
16+
setTimeout(function recursiveTimeout() {
17+
// if timer has passed, terminate and return result
18+
if (new Date() - tickStart >= timer) {
19+
return callback(null, tick);
20+
}
21+
22+
emitTick();
23+
24+
// call recursively for next emit
25+
setTimeout(recursiveTimeout, TICK_INTERVAL);
26+
}, TICK_INTERVAL);
27+
28+
return eventEmitter;
29+
}
30+
31+
// 2 initialize ticker event emitter
32+
const emitter = ticker(1200, (err, tick) => {
33+
if (err) {
34+
return console.log('Callback error', err);
35+
}
36+
console.log(`Totally ticked ${tick} times`);
37+
});
38+
39+
// 3 add listeners
40+
emitter
41+
.on('error', err => console.log('Emitted error', err))
42+
.on('tick', (result) => console.log('Tick #', result));
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { EventEmitter } from 'events';
2+
3+
// 1 main ticker function
4+
function ticker(timer, callback) {
5+
const eventEmitter = new EventEmitter();
6+
let tick = 0;
7+
const TICK_INTERVAL = 50;
8+
9+
const tickStart = new Date();
10+
11+
const emitTick = () => {
12+
emitter.emit('tick', tick);
13+
tick++;
14+
}
15+
16+
// +++ 3.3 added line
17+
process.nextTick(emitTick);
18+
19+
setTimeout(function recursiveTimeout() {
20+
// if timer has passed, terminate and return result
21+
if (new Date() - tickStart >= timer) {
22+
return callback(null, tick);
23+
}
24+
25+
emitTick();
26+
27+
// call recursively for next emit
28+
setTimeout(recursiveTimeout, TICK_INTERVAL);
29+
}, TICK_INTERVAL);
30+
31+
return eventEmitter;
32+
}
33+
34+
// 2 initialize ticker event emitter
35+
const emitter = ticker(1200, (err, tick) => {
36+
if (err) {
37+
return console.log('Callback error', err);
38+
}
39+
console.log(`Totally ticked ${tick} times`);
40+
});
41+
42+
// 3 add listeners
43+
emitter
44+
.on('error', err => console.log('Emitted error', err))
45+
.on('tick', (result) => console.log('Tick #', result));
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { EventEmitter } from 'events';
2+
3+
// 1 main ticker function
4+
function ticker(timer, callback) {
5+
const eventEmitter = new EventEmitter();
6+
let tick = 0;
7+
const TICK_INTERVAL = 50;
8+
9+
const tickStart = new Date();
10+
11+
const emitTick = () => {
12+
// +++ 3.4 added code
13+
if (Date.now() % 5 === 0) {
14+
const err = new Error('Divisible by 5');
15+
callback(err);
16+
return emitter.emit('error', err);
17+
}
18+
//
19+
emitter.emit('tick', tick);
20+
tick++;
21+
}
22+
23+
// +++ 3.3 added line
24+
process.nextTick(emitTick);
25+
26+
setTimeout(function recursiveTimeout() {
27+
// if timer has passed, terminate and return result
28+
if (new Date() - tickStart >= timer) {
29+
return callback(null, tick);
30+
}
31+
32+
emitTick();
33+
34+
// call recursively for next emit
35+
setTimeout(recursiveTimeout, TICK_INTERVAL);
36+
}, TICK_INTERVAL);
37+
38+
return eventEmitter;
39+
}
40+
41+
// 2 initialize ticker event emitter
42+
const emitter = ticker(1200, (err, tick) => {
43+
if (err) {
44+
return console.log('Callback error', err);
45+
}
46+
console.log(`Totally ticked ${tick} times`);
47+
});
48+
49+
// 3 add listeners
50+
emitter
51+
.on('error', err => console.log('Emitted error', err))
52+
.on('tick', (result) => console.log('Tick #', result));

package.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "node.js-design-patterns-solutions",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"author": "Baiaman Kasymbaev",
6+
"license": "MIT",
7+
"type": "module"
8+
}

0 commit comments

Comments
 (0)