Skip to content

Commit a52da10

Browse files
authored
Merge pull request #1 from HowProgrammingWorks/few-improvements
Update description and improve examples
2 parents 6804c5d + baf1a6b commit a52da10

File tree

4 files changed

+12
-7
lines changed

4 files changed

+12
-7
lines changed

JavaScript/1-prototype.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

3-
// Facade for Map and Map or Timers
3+
// Facade that wraps Map and Node.js Timers to provide a simple interface for a
4+
// collection with values that have expiration timeout.
45

56
const TimeoutCollection = function(timeout) {
67
this.timeout = timeout;
@@ -14,6 +15,7 @@ TimeoutCollection.prototype.set = function(key, value) {
1415
const timeout = setTimeout(() => {
1516
this.delete(key);
1617
}, this.timeout);
18+
timeout.unref();
1719
this.collection.set(key, value);
1820
this.timers.set(key, timeout);
1921
};

JavaScript/2-class.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class TimeoutCollection {
1313
const timeout = setTimeout(() => {
1414
this.delete(key);
1515
}, this.timeout);
16+
timeout.unref();
1617
this.collection.set(key, value);
1718
this.timers.set(key, timeout);
1819
}

JavaScript/3-function.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
'use strict';
22

3-
const timeoutCollection = timeout => {
3+
const timeoutCollection = interval => {
44
const collection = new Map();
55
const timers = new Map();
6+
67
const facade = {};
78

89
facade.set = (key, value) => {
9-
const prevTimer = timers.get(key);
10-
if (prevTimer) clearTimeout(prevTimer);
11-
const timer = setTimeout(() => {
10+
const timer = timers.get(key);
11+
if (timer) clearTimeout(timer);
12+
const timeout = setTimeout(() => {
1213
collection.delete(key);
13-
}, timeout);
14+
}, interval);
15+
timeout.unref();
1416
collection.set(key, value);
1517
timers.set(key, timer);
1618
};

JavaScript/4-scheduler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ class Scheduler extends EventEmitter {
132132
const scheduler = new Scheduler();
133133

134134
scheduler.on('error', (err, task) => {
135-
console.log(`Error in ${task.name}: ${err.stack}`);
135+
console.log(`Error in ${task.name}:\n ${err.stack}`);
136136
//process.exit(1);
137137
});
138138

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy