1 |
|
2 | var Type = require('./type');
|
3 | function Dup(){
|
4 | this.cache = {};
|
5 | }
|
6 | Dup.prototype.track = function(id){
|
7 | this.cache[id] = Type.time.is();
|
8 | if (!this.to) {
|
9 | this.gc();
|
10 | }
|
11 | return id;
|
12 | };
|
13 | Dup.prototype.check = function(id){
|
14 |
|
15 | return Type.obj.has(this.cache, id)? this.track(id) : false;
|
16 | }
|
17 | Dup.prototype.gc = function(){
|
18 | var de = this, now = Type.time.is(), oldest = now, maxAge = 5 * 60 * 1000;
|
19 |
|
20 | Type.obj.map(de.cache, function(time, id){
|
21 | oldest = Math.min(now, time);
|
22 | if ((now - time) < maxAge){ return }
|
23 | Type.obj.del(de.cache, id);
|
24 | });
|
25 | var done = Type.obj.empty(de.cache);
|
26 | if(done){
|
27 | de.to = null;
|
28 | return;
|
29 | }
|
30 | var elapsed = now - oldest;
|
31 | var nextGC = maxAge - elapsed;
|
32 | de.to = setTimeout(function(){ de.gc() }, nextGC);
|
33 | }
|
34 | module.exports = Dup;
|
35 | |
\ | No newline at end of file |