-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathnassh_stream_set.js
79 lines (69 loc) · 1.59 KB
/
nassh_stream_set.js
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
// Copyright 2017 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import {Stream} from './nassh_stream.js';
/**
* A set of open streams for a command instance.
*/
export class StreamSet {
constructor() {
/**
* Collection of currently open stream instances.
*
* @private {!Object<number, !Stream>}
* @const
*/
this.openStreams_ = {};
}
/**
* Open a new stream instance of a given class.
*
* @param {function(new:Stream, number, ?)} streamClass
* @param {number} fd
* @param {!Object} arg
* @param {function(boolean, ?string=)} onOpen
* @return {!Stream}
*/
openStream(streamClass, fd, arg, onOpen) {
if (this.openStreams_[fd]) {
throw Stream.ERR_FD_IN_USE;
}
const stream = new streamClass(fd, arg);
stream.asyncOpen(arg, (success, errorMessage) => {
if (success) {
this.openStreams_[fd] = stream;
stream.open = true;
}
onOpen(success, errorMessage);
});
return stream;
}
/**
* Closes a stream instance.
*
* @param {number} fd
*/
closeStream(fd) {
const stream = this.openStreams_[fd];
stream.close();
stream.open = false;
delete this.openStreams_[fd];
}
/**
* Closes all stream instances.
*/
closeAllStreams() {
for (const fd in this.openStreams_) {
this.closeStream(Number(fd));
}
}
/**
* Returns a stream instance.
*
* @param {number} fd
* @return {!Stream}
*/
getStreamByFd(fd) {
return this.openStreams_[fd];
}
}