@cosmicmind/patternjs
Version:
A library of Design Patterns in TypeScript.
276 lines (275 loc) • 7.71 kB
JavaScript
var __defProp = Object.defineProperty;
var __typeError = (msg) => {
throw TypeError(msg);
};
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
var _props, _Builder_instances, clear_fn;
import { assign, guard, timeout } from "@cosmicmind/foundationjs";
const _Builder = class _Builder {
constructor(props) {
__privateAdd(this, _Builder_instances);
__privateAdd(this, _props);
__privateSet(this, _props, props);
}
set(prop, value) {
Object.defineProperty(__privateGet(this, _props), prop, {
configurable: true,
enumerable: true,
writable: false,
value
});
return this;
}
map(props) {
for (const [key, value] of Object.entries(props)) {
this.set(key, value);
}
return this;
}
build() {
const instance = __privateGet(this, _props);
__privateMethod(this, _Builder_instances, clear_fn).call(this);
return instance;
}
};
_props = new WeakMap();
_Builder_instances = new WeakSet();
clear_fn = /* @__PURE__ */ __name(function() {
__privateSet(this, _props, {});
}, "#clear");
__name(_Builder, "Builder");
let Builder = _Builder;
const _Prototype = class _Prototype {
/**
* Creates a copy of itself and returns it.
*/
clone() {
return assign(Object.create(Object.getPrototypeOf(this) ?? null), this);
}
};
__name(_Prototype, "Prototype");
let Prototype = _Prototype;
const _CommandHistory = class _CommandHistory {
constructor() {
__publicField(this, "commands");
this.commands = [];
}
/**
* Adds a command to the commands array.
*
* @param {Command} command - The command to be added.
*
* @return {void}
*/
push(command) {
this.commands.push(command);
}
/**
* Removes and returns the last command from the list of commands.
*
* @returns {Optional<Command>} The last command, or undefined if the list is empty.
*/
pop() {
return this.commands.pop();
}
clear() {
this.commands = [];
}
};
__name(_CommandHistory, "CommandHistory");
let CommandHistory = _CommandHistory;
const _Observable = class _Observable {
constructor() {
__publicField(this, "topics");
this.topics = {};
}
subscribe(topic, ...fn) {
if (!this.topics[topic]) {
this.topics[topic] = /* @__PURE__ */ new Set();
}
const topics = this.topics[topic];
if (guard(topics)) {
for (const cb of fn) {
topics == null ? void 0 : topics.add(cb);
}
}
}
once(topic, ...fn) {
const cb = /* @__PURE__ */ __name((message) => {
this.unsubscribe(topic, cb);
for (const cb2 of fn) {
cb2(message);
}
}, "cb");
this.subscribe(topic, cb);
}
unsubscribe(topic, ...fn) {
if (this.topics[topic]) {
const topics = this.topics[topic];
if (guard(topics)) {
for (const cb of fn) {
topics == null ? void 0 : topics.delete(cb);
}
}
}
}
publish(topic, message) {
return timeout(() => {
const topics = this.topics[topic];
if (guard(topics)) {
for (const fn of topics) {
fn(message);
}
}
});
}
publishSync(topic, message) {
const topics = this.topics[topic];
if (guard(topics)) {
for (const fn of topics) {
fn(message);
}
}
}
};
__name(_Observable, "Observable");
let Observable = _Observable;
const _Plugin = class _Plugin {
};
__name(_Plugin, "Plugin");
let Plugin = _Plugin;
const _PluginManager = class _PluginManager {
constructor() {
__publicField(this, "plugins");
this.plugins = [];
}
register(...plugins) {
for (const plugin of plugins) {
const i = this.indexOf(plugin);
if (-1 === i) {
this.plugins.push(plugin);
return true;
}
}
return false;
}
deregister(...plugins) {
for (const plugin of plugins) {
const i = this.indexOf(plugin);
if (-1 < i) {
this.plugins.splice(i, 1);
return true;
}
}
return false;
}
/**
* Executes the `execute` method of all plugins.
*
* @param {...T} args - The arguments to pass to the `execute` method.
* @return {void}
*/
execute(...args) {
for (const p of this.plugins) {
p.execute(...args);
}
}
/**
* Finds the index of the specified plugin in the plugins array.
*
* @param {Plugin<T> | string} plugin - The plugin to search for. Can be either a Plugin object or a string representing the plugin name.
* @return {number} - The index of the plugin in the plugins array. Returns -1 if the plugin is not found.
*/
indexOf(plugin) {
const plugins = this.plugins;
const name = "string" === typeof plugin ? plugin : plugin.name;
for (let i = plugins.length - 1; i >= 0; --i) {
if (name === plugins[i].name) {
return i;
}
}
return -1;
}
};
__name(_PluginManager, "PluginManager");
let PluginManager = _PluginManager;
const _RequestChain = class _RequestChain {
constructor() {
__publicField(this, "_next");
}
/**
* Retrieves the next item in the chain.
*
* @returns {Optional<Chainable<T>>} The next item in the chain, or undefined if there isn't a next item.
*/
get next() {
return this._next;
}
/**
* Appends a chainable object to the current object.
*
* @param {Chainable<T>} chainable - The chainable object to append.
* @return {void} - This method does not return anything.
*/
append(chainable) {
this._next = chainable;
}
/**
* Clears the next reference of the object.
*
* @returns {void}
*/
clear() {
this._next = void 0;
}
/**
* Executes the processor if the given arguments are executable,
* otherwise passes the arguments to the next execute method in the chain.
*
* @template T - Type of the arguments.
*
* @param {...T[]} args - The arguments to be passed to the processor.
*
* @returns {boolean}
*/
execute(...args) {
var _a;
if (this.isProcessable(...args)) {
return this.processor(...args);
} else if ((_a = this.next) == null ? void 0 : _a.execute(...args)) {
return true;
}
return false;
}
};
__name(_RequestChain, "RequestChain");
let RequestChain = _RequestChain;
function generateRequestChain(...chainables) {
if (1 < chainables.length) {
let chain = chainables[0];
for (let i = 1, l = chainables.length; i < l; ++i) {
const item = chainables[i];
chain.append(item);
chain = item;
}
}
return chainables[0];
}
__name(generateRequestChain, "generateRequestChain");
export {
Builder,
CommandHistory,
Observable,
Plugin,
PluginManager,
Prototype,
RequestChain,
generateRequestChain
};