-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
90 lines (70 loc) · 2.09 KB
/
index.js
File metadata and controls
90 lines (70 loc) · 2.09 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
var Readable = require('stream').Readable;
var inherits = require('util').inherits;
var walkitout = require('walkitout');
inherits(WioStream, Readable);
WioStream.prototype = Object.create(
WioStream.prototype, {
_read: {
value: function () {
if (!this._walkInitiated) this._walk();
}
},
_walk: {
value: function () {
this._walkInitiated = true;
this._cancelWalk = walkitout(this._walkStartPath,
function (err, filename, done) {
if (err) this.emit('error', err);
else this.push(filename);
done();
}.bind(this),
function (cancelled) {
if (cancelled) this.emit('cancel');
setImmediate(this.push.bind(this, null));
}.bind(this),
null,
this._descentController
);
}
},
_cancelWalkUnbound: {
value: function () {
if (this._cancelWalkRequested)
return;
this._cancelWalkRequested = true;
this._cancelWalkWhenAble();
}
},
_cancelWalkWhenAbleUnbound: {
value: function () {
if (this._cancelWalk) {
this._cancelWalk();
} else {
this._cancelWalkTimer = setTimeout(
this._cancelWalkWhenAble, 70);
}
}
},
_walkStartPath: { value: '', writable: true },
_walkInitiated: { value: false, writable: true },
_descentController: { value: null, writable: true },
_cancelWalkRequested: { value: false, writable: true },
_cancelWalkTimer: { value: null, writable: true },
_cancelWalk: { value: null, writable: true },
_cancelWalkWhenAble: { value: null, writable: true },
cancelWalk: { value: null, writable: true }
});
module.exports = wiostream;
function wiostream(path, encoding, controller) {
return new WioStream(path, encoding || 'utf8', controller);
}
function WioStream(startPath, encoding, controller) {
this._walkStartPath = startPath;
this._descentController = controller;
this._cancelWalkWhenAble = this
._cancelWalkWhenAbleUnbound.bind(this);
this.cancelWalk = this._cancelWalkUnbound.bind(this);
Readable.call(this, {
'encoding': encoding
});
}