UNPKG

@iprokit/service

Version:

Powering distributed systems with simplicity and speed.

149 lines 3.61 kB
"use strict"; /** * @iProKit/Service * Copyright (c) 2019-2025 Rutvik Katuri / iProTechs * SPDX-License-Identifier: Apache-2.0 */ Object.defineProperty(exports, "__esModule", { value: true }); /** * Signal is used for multi-stage communication. * It is represented as a case-sensitive string. * * Example: `NEXT%ONE=A&TWO=B` * * A Signal consists of an event name and tags: * - Event: `NEXT` * - Tags: `ONE=A&TWO=B` */ class Signal { /** * Event name of the signal. */ event; /** * Tags of the signal. */ tags; /** * Creates an instance of `Signal`. * * @param event name of the signal. * @param tags optional tags of the signal. */ constructor(event, tags) { this.event = event; this.tags = tags ?? {}; } ////////////////////////////// //////// Gets/Sets ////////////////////////////// /** * Gets a tag value. * * @param key tag key. */ get(key) { return this.tags[key]; } /** * Returns `true` if the tag exists, `false` otherwise. * * @param key tag key. */ has(key) { return key in this.tags; } /** * Sets a tag. * * @param key tag key. * @param value tag value. */ set(key, value) { this.tags[key] = value; return this; } /** * Removes a tag. * * @param key tag key. */ delete(key) { delete this.tags[key]; return this; } /** * Returns an array of tag keys. */ keys() { return Object.keys(this.tags); } /** * Returns an array of tag values. */ values() { return Object.values(this.tags); } /** * Returns an array of key-value pairs of tags. */ entries() { return Object.entries(this.tags); } /** * Returns the number of tags. */ get size() { return this.keys().length; } ////////////////////////////// //////// To/From Helpers ////////////////////////////// /** * Returns the stringified version of the `Signal`. */ stringify() { // Combine tags as a string. const _tags = this.entries() .map(([key, value]) => key + Signal.TAG_DELIMITER + value) .join(Signal.TAGS_DELIMITER); const tags = _tags ? Signal.EVENT_DELIMITER + _tags : ''; // Return the combined event and tags as a string. return this.event + tags; } /** * Returns the objectified version of a `Signal`. * * @param signal stringified version of a `Signal`. */ static objectify(signal) { // Deconstruct event and tags from the string. const [event, _tags] = signal.split(Signal.EVENT_DELIMITER); const tags = _tags ? Object.fromEntries(_tags.split(Signal.TAGS_DELIMITER).map((tag) => tag.split(Signal.TAG_DELIMITER))) : {}; // Return a new Signal as an object. return new Signal(event, tags); } ////////////////////////////// //////// Delimiter Definitions ////////////////////////////// /** * Delimiter for the event, denoted by `%`. * * @example `event%tags` */ static EVENT_DELIMITER = '%'; /** * Delimiter for tags, denoted by `&`. * * @example `tag1&tag2` */ static TAGS_DELIMITER = '&'; /** * Delimiter for a key-value pair, denoted by `=`. * * @example `key=value` */ static TAG_DELIMITER = '='; } exports.default = Signal; //# sourceMappingURL=signal.js.map