Socket
Socket
Sign inDemoInstall

@stoplight/command

Package Overview
Dependencies
Maintainers
4
Versions
65
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@stoplight/command - npm Package Compare versions

Comparing version 0.0.11-22 to 0.0.11-23

360

lib/command.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const mobx_1 = require("mobx");
const lib_1 = require("@stoplight/common/lib");
const event_1 = require("@stoplight/common/lib/event");
const lib_2 = require("@stoplight/ioc/lib");
const types_1 = require("./types");
let keyboardJS;
class Command {
constructor(command) {
var tslib_1 = require("tslib");
var mobx_1 = require("mobx");
var lib_1 = require("@stoplight/common/lib");
var event_1 = require("@stoplight/common/lib/event");
var lib_2 = require("@stoplight/ioc/lib");
var types_1 = require("./types");
var keyboardJS;
var Command = /** @class */ (function () {
function Command(command) {
this._enabled = false;

@@ -18,18 +18,34 @@ this._executing = false;

}
get enabled() {
return this._enabled;
}
get executing() {
return this._executing;
}
get visible() {
return this._visible;
}
get onDidUpdate() {
return this._didUpdateEmitter.event;
}
update(change) {
Object.defineProperty(Command.prototype, "enabled", {
get: function () {
return this._enabled;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Command.prototype, "executing", {
get: function () {
return this._executing;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Command.prototype, "visible", {
get: function () {
return this._visible;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Command.prototype, "onDidUpdate", {
get: function () {
return this._didUpdateEmitter.event;
},
enumerable: true,
configurable: true
});
Command.prototype.update = function (change) {
if (!change)
return;
const { enabled, executing, visible } = change;
var enabled = change.enabled, executing = change.executing, visible = change.visible;
if (enabled === this.enabled && executing === this.executing && visible === this.visible)

@@ -44,37 +60,38 @@ return;

this._didUpdateEmitter.fire(this);
}
}
tslib_1.__decorate([
mobx_1.observable,
tslib_1.__metadata("design:type", Boolean)
], Command.prototype, "_enabled", void 0);
tslib_1.__decorate([
mobx_1.observable,
tslib_1.__metadata("design:type", Boolean)
], Command.prototype, "_executing", void 0);
tslib_1.__decorate([
mobx_1.observable,
tslib_1.__metadata("design:type", Boolean)
], Command.prototype, "_visible", void 0);
tslib_1.__decorate([
mobx_1.computed,
tslib_1.__metadata("design:type", Object),
tslib_1.__metadata("design:paramtypes", [])
], Command.prototype, "enabled", null);
tslib_1.__decorate([
mobx_1.computed,
tslib_1.__metadata("design:type", Object),
tslib_1.__metadata("design:paramtypes", [])
], Command.prototype, "executing", null);
tslib_1.__decorate([
mobx_1.computed,
tslib_1.__metadata("design:type", Object),
tslib_1.__metadata("design:paramtypes", [])
], Command.prototype, "visible", null);
tslib_1.__decorate([
mobx_1.action,
tslib_1.__metadata("design:type", Function),
tslib_1.__metadata("design:paramtypes", [Object]),
tslib_1.__metadata("design:returntype", void 0)
], Command.prototype, "update", null);
};
tslib_1.__decorate([
mobx_1.observable,
tslib_1.__metadata("design:type", Boolean)
], Command.prototype, "_enabled", void 0);
tslib_1.__decorate([
mobx_1.observable,
tslib_1.__metadata("design:type", Boolean)
], Command.prototype, "_executing", void 0);
tslib_1.__decorate([
mobx_1.observable,
tslib_1.__metadata("design:type", Boolean)
], Command.prototype, "_visible", void 0);
tslib_1.__decorate([
mobx_1.computed,
tslib_1.__metadata("design:type", Object),
tslib_1.__metadata("design:paramtypes", [])
], Command.prototype, "enabled", null);
tslib_1.__decorate([
mobx_1.computed,
tslib_1.__metadata("design:type", Object),
tslib_1.__metadata("design:paramtypes", [])
], Command.prototype, "executing", null);
tslib_1.__decorate([
mobx_1.computed,
tslib_1.__metadata("design:type", Object),
tslib_1.__metadata("design:paramtypes", [])
], Command.prototype, "visible", null);
tslib_1.__decorate([
mobx_1.action,
tslib_1.__metadata("design:type", Function),
tslib_1.__metadata("design:paramtypes", [Object]),
tslib_1.__metadata("design:returntype", void 0)
], Command.prototype, "update", null);
return Command;
}());
exports.Command = Command;

@@ -84,4 +101,4 @@ /**

*/
let CommandRegistry = class CommandRegistry {
constructor() {
var CommandRegistry = /** @class */ (function () {
function CommandRegistry() {
this._commands = {};

@@ -95,27 +112,36 @@ this._handlers = {};

}
/**
* Get all registered commands.
*/
get commands() {
const commands = [];
for (const id of this.commandIds) {
const cmd = this.getCommand(id);
if (cmd) {
commands.push(cmd);
Object.defineProperty(CommandRegistry.prototype, "commands", {
/**
* Get all registered commands.
*/
get: function () {
var commands = [];
for (var _i = 0, _a = this.commandIds; _i < _a.length; _i++) {
var id = _a[_i];
var cmd = this.getCommand(id);
if (cmd) {
commands.push(cmd);
}
}
}
return commands;
}
return commands;
},
enumerable: true,
configurable: true
});
Object.defineProperty(CommandRegistry.prototype, "commandIds", {
/**
* Get all registered commands identifiers.
*/
get: function () {
return Object.keys(this._commands);
},
enumerable: true,
configurable: true
});
/**
* Get all registered commands identifiers.
*/
get commandIds() {
return Object.keys(this._commands);
}
/**
* Get a command for the given command identifier.
*/
getCommand(id) {
CommandRegistry.prototype.getCommand = function (id) {
return this._commands[id];
}
};
/**

@@ -126,5 +152,5 @@ * Register the given command and handler if present.

*/
registerCommand(command, handler) {
CommandRegistry.prototype.registerCommand = function (command, handler) {
if (handler) {
const toDispose = new lib_1.disposable.Collection();
var toDispose = new lib_1.disposable.Collection();
toDispose.push(this._registerCommand(command));

@@ -135,8 +161,9 @@ toDispose.push(this.registerHandler(command.id, handler));

return this._registerCommand(command);
}
};
/**
* Register the given handler for the given command identifier.
*/
registerHandler(commandId, handler) {
let handlers = this._handlers[commandId];
CommandRegistry.prototype.registerHandler = function (commandId, handler) {
var _this = this;
var handlers = this._handlers[commandId];
if (!handlers) {

@@ -147,33 +174,34 @@ this._handlers[commandId] = handlers = [];

this.refreshCommandState(commandId);
return lib_1.disposable.create(() => {
const idx = handlers.indexOf(handler);
return lib_1.disposable.create(function () {
var idx = handlers.indexOf(handler);
if (idx >= 0) {
handlers.splice(idx, 1);
}
this.refreshCommandState(commandId);
_this.refreshCommandState(commandId);
});
}
};
/**
* Register the given shortcut for the given command identifier.
*/
registerShortcut(shortcut, options) {
CommandRegistry.prototype.registerShortcut = function (shortcut, options) {
var _this = this;
if (!keyboardJS)
return lib_1.disposable.NOOP;
const { commandId, execute } = options;
var commandId = options.commandId, execute = options.execute;
if (commandId) {
if (!this._commands[commandId]) {
throw Error(`Cannot bind '${shortcut}' to unregistered command with ID '${commandId}'.`);
throw Error("Cannot bind '" + shortcut + "' to unregistered command with ID '" + commandId + "'.");
}
}
else if (!execute) {
throw Error(`Cannot bind '${shortcut}' shortcut. commandId or execute function must be provided.`);
throw Error("Cannot bind '" + shortcut + "' shortcut. commandId or execute function must be provided.");
}
if (this._shortcuts[shortcut]) {
throw Error(`The '${shortcut}' has already been registered.`);
throw Error("The '" + shortcut + "' has already been registered.");
}
this._shortcuts[shortcut] = keyboardJS.bind(shortcut, (e) => {
this._shortcuts[shortcut] = keyboardJS.bind(shortcut, function (e) {
e.preventDefault();
if (commandId) {
if (this.isEnabled(commandId)) {
this.executeCommand(commandId);
if (_this.isEnabled(commandId)) {
_this.executeCommand(commandId);
}

@@ -185,28 +213,40 @@ }

});
return lib_1.disposable.create(() => {
const sh = this._shortcuts[shortcut];
return lib_1.disposable.create(function () {
var sh = _this._shortcuts[shortcut];
if (sh) {
keyboardJS.unbind(shortcut, sh);
delete this._shortcuts[shortcut];
delete _this._shortcuts[shortcut];
}
});
}
};
/**
* Test whether there is an active handler for the given command.
*/
isEnabled(commandId, ...args) {
return this.getActiveHandler(commandId, ...args) !== undefined;
}
CommandRegistry.prototype.isEnabled = function (commandId) {
var args = [];
for (var _i = 1; _i < arguments.length; _i++) {
args[_i - 1] = arguments[_i];
}
return this.getActiveHandler.apply(this, [commandId].concat(args)) !== undefined;
};
/**
* Test whether there is a visible handler for the given command.
*/
isVisible(commandId, ...args) {
return this.getVisibleHandler(commandId, ...args) !== undefined;
}
CommandRegistry.prototype.isVisible = function (commandId) {
var args = [];
for (var _i = 1; _i < arguments.length; _i++) {
args[_i - 1] = arguments[_i];
}
return this.getVisibleHandler.apply(this, [commandId].concat(args)) !== undefined;
};
/**
* Test whether there is a handler that is currently executing for the given command.
*/
isExecuting(commandId, ...args) {
return this.getExecutingHandler(commandId, ...args) !== undefined;
}
CommandRegistry.prototype.isExecuting = function (commandId) {
var args = [];
for (var _i = 1; _i < arguments.length; _i++) {
args[_i - 1] = arguments[_i];
}
return this.getExecutingHandler.apply(this, [commandId].concat(args)) !== undefined;
};
/**

@@ -217,18 +257,26 @@ * Execute the active handler for the given command and arguments.

*/
executeCommand(commandId, ...args) {
const handler = this.getActiveHandler(commandId, ...args);
CommandRegistry.prototype.executeCommand = function (commandId) {
var args = [];
for (var _i = 1; _i < arguments.length; _i++) {
args[_i - 1] = arguments[_i];
}
var handler = this.getActiveHandler.apply(this, [commandId].concat(args));
if (handler) {
return Promise.resolve(handler.execute(...args));
return Promise.resolve(handler.execute.apply(handler, args));
}
return Promise.reject(`The command '${commandId}' cannot be executed. There are no active
handlers available for the command.`);
}
return Promise.reject("The command '" + commandId + "' cannot be executed. There are no active\n handlers available for the command.");
};
/**
* Get a visible handler for the given command or `undefined`.
*/
getVisibleHandler(commandId, ...args) {
const handlers = this._handlers[commandId];
CommandRegistry.prototype.getVisibleHandler = function (commandId) {
var args = [];
for (var _i = 1; _i < arguments.length; _i++) {
args[_i - 1] = arguments[_i];
}
var handlers = this._handlers[commandId];
if (handlers) {
for (const handler of handlers) {
if (!handler.isVisible || handler.isVisible(...args)) {
for (var _a = 0, handlers_1 = handlers; _a < handlers_1.length; _a++) {
var handler = handlers_1[_a];
if (!handler.isVisible || handler.isVisible.apply(handler, args)) {
return handler;

@@ -238,11 +286,16 @@ }

}
}
};
/**
* Get a handler that is currently executing for the given command or `undefined`.
*/
getExecutingHandler(commandId, ...args) {
const handlers = this._handlers[commandId];
CommandRegistry.prototype.getExecutingHandler = function (commandId) {
var args = [];
for (var _i = 1; _i < arguments.length; _i++) {
args[_i - 1] = arguments[_i];
}
var handlers = this._handlers[commandId];
if (handlers) {
for (const handler of handlers) {
if (handler.isExecuting && handler.isExecuting(...args)) {
for (var _a = 0, handlers_2 = handlers; _a < handlers_2.length; _a++) {
var handler = handlers_2[_a];
if (handler.isExecuting && handler.isExecuting.apply(handler, args)) {
return handler;

@@ -252,11 +305,16 @@ }

}
}
};
/**
* Get an active handler for the given command or `undefined`.
*/
getActiveHandler(commandId, ...args) {
const handlers = this._handlers[commandId];
CommandRegistry.prototype.getActiveHandler = function (commandId) {
var args = [];
for (var _i = 1; _i < arguments.length; _i++) {
args[_i - 1] = arguments[_i];
}
var handlers = this._handlers[commandId];
if (handlers) {
for (const handler of handlers) {
if (!handler.isEnabled || handler.isEnabled(...args)) {
for (var _a = 0, handlers_3 = handlers; _a < handlers_3.length; _a++) {
var handler = handlers_3[_a];
if (!handler.isEnabled || handler.isEnabled.apply(handler, args)) {
return handler;

@@ -266,38 +324,44 @@ }

}
}
};
/**
* Re-compute and set command props such as enabled.
*/
refreshCommandState(commandId, ...args) {
const command = this.getCommand(commandId);
CommandRegistry.prototype.refreshCommandState = function (commandId) {
var args = [];
for (var _i = 1; _i < arguments.length; _i++) {
args[_i - 1] = arguments[_i];
}
var command = this.getCommand(commandId);
if (!command)
return;
command.update({
enabled: this.isEnabled(commandId, ...args),
executing: this.isExecuting(commandId, ...args),
visible: this.isVisible(commandId, ...args),
enabled: this.isEnabled.apply(this, [commandId].concat(args)),
executing: this.isExecuting.apply(this, [commandId].concat(args)),
visible: this.isVisible.apply(this, [commandId].concat(args)),
});
}
_registerCommand(command) {
};
CommandRegistry.prototype._registerCommand = function (command) {
var _this = this;
if (this._commands[command.id]) {
throw Error(`A command with ID '${command.id}' is already registered.`);
throw Error("A command with ID '" + command.id + "' is already registered.");
}
this._commands[command.id] = new Command(command);
let shortcut;
var shortcut;
if (command.shortcut) {
shortcut = this.registerShortcut(command.shortcut, { commandId: command.id });
}
return lib_1.disposable.create(() => {
return lib_1.disposable.create(function () {
if (shortcut) {
shortcut.dispose();
}
delete this._commands[command.id];
delete _this._commands[command.id];
});
}
};
CommandRegistry = tslib_1.__decorate([
lib_2.provideSingleton(types_1.SYMBOLS.CommandRegistry),
tslib_1.__metadata("design:paramtypes", [])
], CommandRegistry);
};
CommandRegistry = tslib_1.__decorate([
lib_2.provideSingleton(types_1.SYMBOLS.CommandRegistry),
tslib_1.__metadata("design:paramtypes", [])
], CommandRegistry);
return CommandRegistry;
}());
exports.CommandRegistry = CommandRegistry;
//# sourceMappingURL=command.js.map
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
var tslib_1 = require("tslib");
tslib_1.__exportStar(require("./command"), exports);
const types = require("./types");
var types = require("./types");
exports.types = types;
//# sourceMappingURL=index.js.map
{
"name": "@stoplight/command",
"version": "0.0.11-22",
"version": "0.0.11-23",
"description": "Stoplight command registry implementation.",

@@ -24,4 +24,4 @@ "main": "lib/index.js",

"dependencies": {
"@stoplight/common": "^0.0.11-22",
"@stoplight/ioc": "^0.0.11-22",
"@stoplight/common": "^0.0.11-23",
"@stoplight/ioc": "^0.0.11-23",
"keyboardjs": "2.4.x",

@@ -28,0 +28,0 @@ "tslib": "1.x.x"

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc