forked from NuSkooler/enigma-bbs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser_interrupt_queue.js
More file actions
112 lines (99 loc) · 3.25 KB
/
Copy pathuser_interrupt_queue.js
File metadata and controls
112 lines (99 loc) · 3.25 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/* jslint node: true */
'use strict';
// ENiGMA½
const Art = require('./art.js');
const {
getActiveConnections
} = require('./client_connections.js');
const ANSI = require('./ansi_term.js');
const { pipeToAnsi } = require('./color_codes.js');
// deps
const _ = require('lodash');
module.exports = class UserInterruptQueue
{
constructor(client) {
this.client = client;
this.queue = [];
}
static queue(interruptItem, opts) {
opts = opts || {};
if(!opts.clients) {
let omitNodes = [];
if(Array.isArray(opts.omit)) {
omitNodes = opts.omit;
} else if(opts.omit) {
omitNodes = [ opts.omit ];
}
omitNodes = omitNodes.map(n => _.isNumber(n) ? n : n.node);
opts.clients = getActiveConnections(true).filter(ac => !omitNodes.includes(ac.node));
}
if(!Array.isArray(opts.clients)) {
opts.clients = [ opts.clients ];
}
opts.clients.forEach(c => {
c.interruptQueue.queueItem(interruptItem);
});
}
queueItem(interruptItem) {
if(!_.isString(interruptItem.contents) && !_.isString(interruptItem.text)) {
return;
}
// pause defaulted on
interruptItem.pause = _.get(interruptItem, 'pause', true);
try {
this.client.currentMenuModule.attemptInterruptNow(interruptItem, (err, ateIt) => {
if(err) {
// :TODO: Log me
} else if(true !== ateIt) {
this.queue.push(interruptItem);
}
});
} catch(e) {
this.queue.push(interruptItem);
}
}
hasItems() {
return this.queue.length > 0;
}
displayNext(options, cb) {
if(!cb && _.isFunction(options)) {
cb = options;
options = {};
}
const interruptItem = this.queue.pop();
if(!interruptItem) {
return cb(null);
}
Object.assign(interruptItem, options);
return interruptItem ? this.displayWithItem(interruptItem, cb) : cb(null);
}
displayWithItem(interruptItem, cb) {
if(interruptItem.cls) {
this.client.term.rawWrite(ANSI.resetScreen());
} else {
this.client.term.rawWrite('\r\n\r\n');
}
const maybePauseAndFinish = () => {
if(interruptItem.pause) {
this.client.currentMenuModule.pausePrompt( () => {
return cb(null);
});
} else {
return cb(null);
}
};
if(interruptItem.contents) {
Art.display(this.client, interruptItem.contents, err => {
if(err) {
return cb(err);
}
//this.client.term.rawWrite('\r\n\r\n'); // :TODO: Prob optional based on contents vs text
maybePauseAndFinish();
});
} else {
this.client.term.write(pipeToAnsi(`${interruptItem.text}\r\n\r\n`, this.client), true, () => {
maybePauseAndFinish();
});
}
}
};