78 lines
8.1 KiB
JavaScript
78 lines
8.1 KiB
JavaScript
|
"use strict";
|
||
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||
|
};
|
||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||
|
const process_1 = __importDefault(require("process"));
|
||
|
const readline_1 = require("readline");
|
||
|
const lodash_1 = require("lodash");
|
||
|
const LOCK_KEY_PRESS_TIMEOUT = 1000;
|
||
|
let instance;
|
||
|
class LiveModeKeyboardEventObserver {
|
||
|
constructor() {
|
||
|
this.controllers = [];
|
||
|
this.lockKeyPress = false;
|
||
|
if (!instance) {
|
||
|
this._listenKeyEvents();
|
||
|
instance = this;
|
||
|
}
|
||
|
return instance;
|
||
|
}
|
||
|
push(controller) {
|
||
|
this.controllers.push(controller);
|
||
|
if (process_1.default.stdin.isTTY)
|
||
|
this.setRawMode(true);
|
||
|
}
|
||
|
remove(controller) {
|
||
|
(0, lodash_1.pull)(this.controllers, controller);
|
||
|
if (!this.controllers.length)
|
||
|
this.setRawMode(false);
|
||
|
}
|
||
|
_listenKeyEvents() {
|
||
|
(0, readline_1.emitKeypressEvents)(process_1.default.stdin);
|
||
|
process_1.default.stdin.on('keypress', this._onKeyPress.bind(this));
|
||
|
}
|
||
|
setRawMode(value) {
|
||
|
if (process_1.default.stdin.setRawMode !== void 0)
|
||
|
process_1.default.stdin.setRawMode(value);
|
||
|
}
|
||
|
_onKeyPress(string, key) {
|
||
|
if (this.lockKeyPress)
|
||
|
return;
|
||
|
this.lockKeyPress = true;
|
||
|
setTimeout(() => {
|
||
|
this.lockKeyPress = false;
|
||
|
}, LOCK_KEY_PRESS_TIMEOUT);
|
||
|
if (key && key.ctrl) {
|
||
|
switch (key.name) {
|
||
|
case 's':
|
||
|
this._stop();
|
||
|
return;
|
||
|
case 'r':
|
||
|
this._restart();
|
||
|
return;
|
||
|
case 'c':
|
||
|
this._exit();
|
||
|
return;
|
||
|
case 'w':
|
||
|
this._toggleWatching();
|
||
|
return;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
_stop() {
|
||
|
this.controllers.forEach(c => c.stop());
|
||
|
}
|
||
|
_restart() {
|
||
|
this.controllers.forEach(c => c.restart());
|
||
|
}
|
||
|
_exit() {
|
||
|
this.controllers.forEach(c => c.exit());
|
||
|
}
|
||
|
_toggleWatching() {
|
||
|
this.controllers.forEach(c => c.toggleWatching());
|
||
|
}
|
||
|
}
|
||
|
exports.default = LiveModeKeyboardEventObserver;
|
||
|
module.exports = exports.default;
|
||
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoia2V5Ym9hcmQtb2JzZXJ2ZXIuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvbGl2ZS9rZXlib2FyZC1vYnNlcnZlci50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7OztBQUFBLHNEQUE4QjtBQUM5Qix1Q0FBbUQ7QUFDbkQsbUNBQThCO0FBRzlCLE1BQU0sc0JBQXNCLEdBQUcsSUFBSSxDQUFDO0FBRXBDLElBQUksUUFBdUMsQ0FBQztBQUU1QyxNQUFxQiw2QkFBNkI7SUFJOUM7UUFIUSxnQkFBVyxHQUF5QixFQUFFLENBQUM7UUFDdkMsaUJBQVksR0FBRyxLQUFLLENBQUM7UUFHekIsSUFBSSxDQUFDLFFBQVEsRUFBRTtZQUNYLElBQUksQ0FBQyxnQkFBZ0IsRUFBRSxDQUFDO1lBRXhCLFFBQVEsR0FBRyxJQUFJLENBQUM7U0FDbkI7UUFFRCxPQUFPLFFBQVEsQ0FBQztJQUNwQixDQUFDO0lBRU0sSUFBSSxDQUFFLFVBQThCO1FBQ3ZDLElBQUksQ0FBQyxXQUFXLENBQUMsSUFBSSxDQUFDLFVBQVUsQ0FBQyxDQUFDO1FBRWxDLElBQUksaUJBQU8sQ0FBQyxLQUFLLENBQUMsS0FBSztZQUNuQixJQUFJLENBQ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