Socket
Socket
Sign inDemoInstall

@stoplight/command

Package Overview
Dependencies
9
Maintainers
5
Versions
65
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.0.43 to 0.0.44-alpha.0

367

lib/command.js

@@ -1,50 +0,32 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var mobx_1 = require("mobx");
var disposable = require("@stoplight/disposable");
var emitter_1 = require("@stoplight/emitter");
var ioc_1 = require("@stoplight/ioc");
var types_1 = require("./types");
var keyboardJS;
var Command = /** @class */ (function () {
function Command(command) {
import * as tslib_1 from "tslib";
import { action, computed, observable } from 'mobx';
import * as disposable from '@stoplight/disposable';
import { Emitter } from '@stoplight/emitter';
import { provideSingleton } from '@stoplight/ioc';
import { SYMBOLS, } from './types';
let keyboardJS;
export class Command {
constructor(command) {
this._enabled = false;
this._executing = false;
this._visible = false;
this._didUpdateEmitter = new emitter_1.Emitter();
this._didUpdateEmitter = new Emitter();
this.id = command.id;
}
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) {
get enabled() {
return this._enabled;
}
get executing() {
return this._executing;
}
get visible() {
return this._visible;
}
get onDidUpdate() {
return this._didUpdateEmitter.event;
}
update(change) {
if (!change)
return;
var enabled = change.enabled, executing = change.executing, visible = change.visible;
const { enabled, executing, visible } = change;
if (enabled === this.enabled && executing === this.executing && visible === this.visible)

@@ -59,44 +41,42 @@ 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);
return Command;
}());
exports.Command = Command;
}
}
tslib_1.__decorate([
observable,
tslib_1.__metadata("design:type", Boolean)
], Command.prototype, "_enabled", void 0);
tslib_1.__decorate([
observable,
tslib_1.__metadata("design:type", Boolean)
], Command.prototype, "_executing", void 0);
tslib_1.__decorate([
observable,
tslib_1.__metadata("design:type", Boolean)
], Command.prototype, "_visible", void 0);
tslib_1.__decorate([
computed,
tslib_1.__metadata("design:type", Object),
tslib_1.__metadata("design:paramtypes", [])
], Command.prototype, "enabled", null);
tslib_1.__decorate([
computed,
tslib_1.__metadata("design:type", Object),
tslib_1.__metadata("design:paramtypes", [])
], Command.prototype, "executing", null);
tslib_1.__decorate([
computed,
tslib_1.__metadata("design:type", Object),
tslib_1.__metadata("design:paramtypes", [])
], Command.prototype, "visible", null);
tslib_1.__decorate([
action,
tslib_1.__metadata("design:type", Function),
tslib_1.__metadata("design:paramtypes", [Object]),
tslib_1.__metadata("design:returntype", void 0)
], Command.prototype, "update", null);
/**
* The command registry manages commands and handlers.
*/
var CommandRegistry = /** @class */ (function () {
function CommandRegistry() {
let CommandRegistry = class CommandRegistry {
constructor() {
this._commands = {};

@@ -110,36 +90,27 @@ this._handlers = {};

}
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);
}
/**
* Get all registered commands.
*/
get commands() {
const commands = [];
for (const id of this.commandIds) {
const cmd = this.getCommand(id);
if (cmd) {
commands.push(cmd);
}
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
});
}
return commands;
}
/**
* Get all registered commands identifiers.
*/
get commandIds() {
return Object.keys(this._commands);
}
/**
* Get a command for the given command identifier.
*/
CommandRegistry.prototype.getCommand = function (id) {
getCommand(id) {
return this._commands[id];
};
}
/**

@@ -150,5 +121,5 @@ * Register the given command and handler if present.

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

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

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

@@ -172,34 +142,33 @@ this._handlers[commandId] = handlers = [];

this.refreshCommandState(commandId);
return disposable.create(function () {
var idx = handlers.indexOf(handler);
return disposable.create(() => {
const 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.
*/
CommandRegistry.prototype.registerShortcut = function (shortcut, options) {
var _this = this;
registerShortcut(shortcut, options) {
if (!keyboardJS)
return disposable.NOOP;
var commandId = options.commandId, execute = options.execute;
const { commandId, execute } = options;
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, function (e) {
this._shortcuts[shortcut] = keyboardJS.bind(shortcut, (e) => {
e.preventDefault();
if (commandId) {
if (_this.isActive(commandId)) {
_this.executeCommand(commandId);
if (this.isActive(commandId)) {
this.executeCommand(commandId);
}

@@ -211,40 +180,28 @@ }

});
return disposable.create(function () {
var sh = _this._shortcuts[shortcut];
return disposable.create(() => {
const 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.
*/
CommandRegistry.prototype.isActive = 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;
};
isActive(commandId, ...args) {
return this.getActiveHandler(commandId, ...args) !== undefined;
}
/**
* Test whether there is a visible handler for the given command.
*/
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;
};
isVisible(commandId, ...args) {
return this.getVisibleHandler(commandId, ...args) !== undefined;
}
/**
* Test whether there is a handler that is currently executing for the given command.
*/
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;
};
isExecuting(commandId, ...args) {
return this.getExecutingHandler(commandId, ...args) !== undefined;
}
/**

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

*/
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));
executeCommand(commandId, ...args) {
const handler = this.getActiveHandler(commandId, ...args);
if (handler) {
return Promise.resolve(handler.execute.apply(handler, args));
return Promise.resolve(handler.execute(...args));
}
return Promise.reject("The command '" + commandId + "' cannot be executed. There are no active\n handlers available for the command.");
};
return Promise.reject(`The command '${commandId}' cannot be executed. There are no active
handlers available for the command.`);
}
/**
* Get a visible handler for the given command or `undefined`.
*/
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];
getVisibleHandler(commandId, ...args) {
const handlers = this._handlers[commandId];
if (handlers) {
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)) {
for (const handler of handlers) {
if (!handler.isVisible || handler.isVisible(...args)) {
return handler;

@@ -285,16 +234,11 @@ }

// return this.getHandlerWithState('isVisible', true, commandId, ...args);
};
}
/**
* Get a handler that is currently executing for the given command or `undefined`.
*/
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];
getExecutingHandler(commandId, ...args) {
const handlers = this._handlers[commandId];
if (handlers) {
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)) {
for (const handler of handlers) {
if (handler.isExecuting && handler.isExecuting(...args)) {
return handler;

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

// return this.getHandlerWithState('isExecuting', false, commandId, ...args);
};
}
/**
* Get an active handler for the given command or `undefined`.
*/
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];
getActiveHandler(commandId, ...args) {
const handlers = this._handlers[commandId];
if (handlers) {
for (var _a = 0, handlers_3 = handlers; _a < handlers_3.length; _a++) {
var handler = handlers_3[_a];
if (!handler.isActive || handler.isActive.apply(handler, args)) {
for (const handler of handlers) {
if (!handler.isActive || handler.isActive(...args)) {
return handler;

@@ -325,44 +264,38 @@ }

// return this.getHandlerWithState('isActive', true, commandId, ...args);
};
}
/**
* Re-compute and set command props such as enabled.
*/
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);
refreshCommandState(commandId, ...args) {
const command = this.getCommand(commandId);
if (!command)
return;
command.update({
enabled: this.isActive.apply(this, [commandId].concat(args)),
executing: this.isExecuting.apply(this, [commandId].concat(args)),
visible: this.isVisible.apply(this, [commandId].concat(args)),
enabled: this.isActive(commandId, ...args),
executing: this.isExecuting(commandId, ...args),
visible: this.isVisible(commandId, ...args),
});
};
CommandRegistry.prototype._registerCommand = function (command) {
var _this = this;
}
_registerCommand(command) {
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);
var shortcut;
let shortcut;
if (command.shortcut) {
shortcut = this.registerShortcut(command.shortcut, { commandId: command.id });
}
return disposable.create(function () {
return disposable.create(() => {
if (shortcut) {
shortcut.dispose();
}
delete _this._commands[command.id];
delete this._commands[command.id];
});
};
CommandRegistry = tslib_1.__decorate([
ioc_1.provideSingleton(types_1.SYMBOLS.CommandRegistry),
tslib_1.__metadata("design:paramtypes", [])
], CommandRegistry);
return CommandRegistry;
}());
exports.CommandRegistry = CommandRegistry;
}
};
CommandRegistry = tslib_1.__decorate([
provideSingleton(SYMBOLS.CommandRegistry),
tslib_1.__metadata("design:paramtypes", [])
], CommandRegistry);
export { CommandRegistry };
//# sourceMappingURL=command.js.map

@@ -1,8 +0,5 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
tslib_1.__exportStar(require("./command"), exports);
tslib_1.__exportStar(require("./types"), exports);
var types = require("./types");
exports.types = types;
export * from './command';
export * from './types';
import * as types from './types';
export { types };
//# sourceMappingURL=index.js.map

@@ -1,9 +0,7 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Unique symbol identifiers, used for IOC.
*/
exports.SYMBOLS = {
export const SYMBOLS = {
CommandRegistry: 'CommandRegistry',
};
//# sourceMappingURL=types.js.map
{
"name": "@stoplight/command",
"version": "0.0.43",
"version": "0.0.44-alpha.0",
"description": "Stoplight command registry implementation.",
"main": "lib/index.js",
"module": "lib/index.es.js",
"jsnext:main": "lib/index.es.js",
"sideEffects": false,
"author": "Stoplight <dev@stoplight.io>",

@@ -23,9 +22,9 @@ "homepage": "https://stoplight.io",

"dependencies": {
"@stoplight/disposable": "0.0.43",
"@stoplight/emitter": "0.0.43",
"@stoplight/ioc": "0.0.43",
"@stoplight/disposable": "0.0.44-alpha.0",
"@stoplight/emitter": "0.0.44-alpha.0",
"@stoplight/ioc": "0.0.44-alpha.0",
"keyboardjs": "2.4.x",
"tslib": "1.x.x"
},
"gitHead": "863127087b0dcfaebf40e7c0ed86bedcd2835468"
"gitHead": "ba495e3f8984dd68df510ccfbc2d8401515c4161"
}

Sorry, the diff of this file is not supported yet

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc