Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

@flexbase/observable-subject

Package Overview
Dependencies
Maintainers
4
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@flexbase/observable-subject - npm Package Compare versions

Comparing version
1.4.2
to
1.4.3-beta.15
+8
dist/core/multicast.dispatcher.d.ts
import { SubscriptionDispatcher } from './subscription.dispatcher.js';
import { SubscriptionContext } from './subscription.context.js';
import { SubscriptionCallback } from './subscription.callback.js';
export declare class MulticastDispatcher<T> implements SubscriptionDispatcher<T> {
dispatch(context: SubscriptionContext<T>, callbacks: SubscriptionCallback<T>[]): Promise<void>;
}
/** Represents a global multicast dispatcher instance */
export declare const multicastDispatcher: MulticastDispatcher<unknown>;
/** Represents a subject that can be observed */
export interface Subject {
/** Gets the key that identifies this subject */
get key(): symbol;
}
import { Logger } from '@flexbase/logger';
import { Subject } from './subject.interface.js';
import { SubscriptionDispatcher } from './subscription.dispatcher.js';
import { Subscription } from './subscription.interface.js';
import { SubscriptionContext } from './subscription.context.js';
import { SubscriptionCallback } from './subscription.callback.js';
/** Represents a type that manages subjects and subscriptions */
export declare class SubjectManager {
private _logger?;
private readonly _subjects;
constructor(_logger?: Logger | undefined);
set logger(logger: Logger);
/**
* Registers a subject
* @param subject The subject to be registered
* @param dispatcher The subscription dispatcher to use with this subject. Defaults to {@link multicastDispatcher}
* @returns true if successfully registered; otherwise false
*/
register(subject: Subject, dispatcher?: SubscriptionDispatcher<unknown>): boolean;
/**
* Checks if a subject is registered with this manager
* @param subject The subject to check
* @returns true if subject is registered; otherwise false
*/
isRegistered(subject: Subject): boolean;
/**
* Attaches a subscription to a subject
* @param subject The subject to add a subscription
* @param callback The callback when a subject event is raised
* @returns A subscription
*/
subscribe<T>(subject: Subject, callback: SubscriptionCallback<T>): Subscription;
private _subscribe;
/**
* Removes a subscription from subject notifications
* @param subject The subject to remove the subscription from
* @param subscription The subscription to remove
*/
unsubscribe(subject: Subject, subscription: Subscription): void;
/**
* Check to see if a subscription is attached to a subject
* @param subject The subject to check for the specified subscription
* @param subscription The subscription to check
* @returns true if subject has the specified subscription; otherwise false
*/
hasSubscription(subject: Subject, subscription: Subscription): boolean;
/**
* Gets the number of subscriptions for a subject
* @param subject The subject to inspect
* @returns The number of subscriptions for the specified subject
*/
subscriptionCount(subject: Subject): number;
/**
* Notify subscribers of a subject of an event
* @param subject The subject used to raise a notification
* @param context The context to send to subscriptions
* @returns A promise
*/
notify(subject: Subject, context: SubscriptionContext<unknown>): Promise<void>;
}
/** Represents a global SubjectManager instance */
export declare const subjectManager: SubjectManager;
import { SubscriptionContext } from './subscription.context.js';
/** Represents a subscription callback method */
export type SubscriptionCallback<T> = (context: SubscriptionContext<T>) => Promise<void>;
/** Represents the context sent to subscriptions on a notification */
export interface SubscriptionContext<T> {
/** The value of the context */
value: Readonly<T>;
}
import { SubscriptionCallback } from './subscription.callback.js';
import { SubscriptionContext } from './subscription.context.js';
/** Represents a subscription dispatcher */
export interface SubscriptionDispatcher<T> {
/**
* Handles dispatching the subscription callbacks
* @param context The context sent to each subscription
* @param callbacks The subscription callbacks that are to be notified
*/
dispatch(context: SubscriptionContext<T>, callbacks: SubscriptionCallback<T>[]): Promise<void>;
}
/** Represents a subscription */
export interface Subscription {
/** The key used to identify this subscription */
get key(): symbol;
/** Unsubscribes from subject notifications */
unsubscribe: () => void;
}
declare class NoopSubscription implements Subscription {
private readonly _key;
get key(): symbol;
unsubscribe(): void;
}
/** Represents a global no-op subscription instance */
export declare const noopSubscription: NoopSubscription;
export {};
export { Subject } from './core/subject.interface.js';
export { subjectManager, SubjectManager } from './core/subject.manager.js';
export { Subscription, noopSubscription } from './core/subscription.interface.js';
export { SubscriptionContext } from './core/subscription.context.js';
export { SubscriptionDispatcher } from './core/subscription.dispatcher.js';
export { multicastDispatcher, MulticastDispatcher } from './core/multicast.dispatcher.js';
export { SubscriptionCallback } from './core/subscription.callback.js';
var a = Object.defineProperty;
var b = (i, e, s) => e in i ? a(i, e, { enumerable: !0, configurable: !0, writable: !0, value: s }) : i[e] = s;
var o = (i, e, s) => (b(i, typeof e != "symbol" ? e + "" : e, s), s);
class g {
constructor() {
o(this, "_key", Symbol());
}
get key() {
return this._key;
}
// eslint-disable-next-line @typescript-eslint/no-empty-function
unsubscribe() {
}
}
const p = new g();
class h {
async dispatch(e, s) {
await Promise.allSettled(s.map((t) => t(e)));
}
}
const l = new h();
class y {
constructor(e, s) {
o(this, "_subscription");
o(this, "_callback");
o(this, "_key");
this._subscription = e, this._callback = s, this._key = e.key;
}
get key() {
return this._key;
}
get subscription() {
return this._subscription;
}
get callback() {
return this._callback;
}
}
class k {
constructor(e) {
o(this, "_logger");
o(this, "_subjects", /* @__PURE__ */ new Map());
this._logger = e;
}
set logger(e) {
this._logger = e;
}
/**
* Registers a subject
* @param subject The subject to be registered
* @param dispatcher The subscription dispatcher to use with this subject. Defaults to {@link multicastDispatcher}
* @returns true if successfully registered; otherwise false
*/
register(e, s = l) {
var t;
return this._subjects.has(e.key) ? ((t = this._logger) == null || t.warn(`Duplicate subject ${e.key.toString()} already registered`), !1) : (this._subjects.set(e.key, { subject: e, dispatcher: s, subscriptions: [] }), !0);
}
/**
* Checks if a subject is registered with this manager
* @param subject The subject to check
* @returns true if subject is registered; otherwise false
*/
isRegistered(e) {
return this._subjects.has(e.key);
}
/**
* Attaches a subscription to a subject
* @param subject The subject to add a subscription
* @param callback The callback when a subject event is raised
* @returns A subscription
*/
subscribe(e, s) {
return this._subscribe(e, s);
}
_subscribe(e, s) {
var n;
const t = this._subjects.get(e.key);
if (!t)
return (n = this._logger) == null || n.warn(`Subject ${e.key.toString()} has not been registered`), p;
const c = {
key: Symbol(),
unsubscribe: () => {
this.unsubscribe(e, c);
}
}, r = new y(c, s);
return t.subscriptions.push(r), r.subscription;
}
/**
* Removes a subscription from subject notifications
* @param subject The subject to remove the subscription from
* @param subscription The subscription to remove
*/
unsubscribe(e, s) {
var r, n;
const t = this._subjects.get(e.key);
if (!t) {
(r = this._logger) == null || r.warn(`Subject ${e.key.toString()} has not been registered yet subscription ${s.toString()} called unsubscribe`);
return;
}
const c = t.subscriptions.findIndex((u) => u.key === s.key);
if (c < 0) {
(n = this._logger) == null || n.warn(`Subject ${e.key.toString()} does not have subscription ${s.toString()} registered`);
return;
}
t.subscriptions.splice(c, 1), s.unsubscribe = () => {
};
}
/**
* Check to see if a subscription is attached to a subject
* @param subject The subject to check for the specified subscription
* @param subscription The subscription to check
* @returns true if subject has the specified subscription; otherwise false
*/
hasSubscription(e, s) {
var r;
const t = this._subjects.get(e.key);
return t ? t.subscriptions.findIndex((n) => n.key === s.key) >= 0 : ((r = this._logger) == null || r.warn(`Subject ${e.key.toString()} has not been registered`), !1);
}
/**
* Gets the number of subscriptions for a subject
* @param subject The subject to inspect
* @returns The number of subscriptions for the specified subject
*/
subscriptionCount(e) {
var t;
const s = this._subjects.get(e.key);
return s ? s.subscriptions.length : ((t = this._logger) == null || t.warn(`Subject ${e.key.toString()} has not been registered`), 0);
}
/**
* Notify subscribers of a subject of an event
* @param subject The subject used to raise a notification
* @param context The context to send to subscriptions
* @returns A promise
*/
async notify(e, s) {
var r;
const t = this._subjects.get(e.key);
if (!t) {
(r = this._logger) == null || r.warn(`Subject ${e.key.toString()} has not been registered`);
return;
}
const c = t.subscriptions.map((n) => n.callback);
await t.dispatcher.dispatch(s, c);
}
}
const S = new k();
export {
h as MulticastDispatcher,
k as SubjectManager,
l as multicastDispatcher,
p as noopSubscription,
S as subjectManager
};
+14
-6
{
"name": "@flexbase/observable-subject",
"version": "1.4.2",
"version": "1.4.3-beta.15",
"description": "Observable subject event bus",
"main": "./index.cjs",
"module": "./index.js",
"exports": { ".": { "import": "./index.js", "require": "./index.cjs" } },
"main": "./dist/index.js",
"exports": "./dist/index.js",
"homepage": "https://github.com/flexbase-eng/observable-subject#readme",

@@ -22,5 +21,14 @@ "repository": {

"license": "MIT",
"dependencies": { "@flexbase/logger": "latest" },
"dependencies": {
"@flexbase/logger": "^1.4.3"
},
"type": "module",
"keywords": ["flexbase", "observable", "subject", "event-bus", "event", "bus"]
"keywords": [
"flexbase",
"observable",
"subject",
"event-bus",
"event",
"bus"
]
}
import { SubscriptionDispatcher } from './subscription.dispatcher';
import { SubscriptionContext } from './subscription.context';
import { SubscriptionCallback } from './subscription.callback';
export declare class MulticastDispatcher<T> implements SubscriptionDispatcher<T> {
dispatch(context: SubscriptionContext<T>, callbacks: SubscriptionCallback<T>[]): Promise<void>;
}
/** Represents a global multicast dispatcher instance */
export declare const multicastDispatcher: MulticastDispatcher<unknown>;
/** Represents a subject that can be observed */
export interface Subject {
/** Gets the key that identifies this subject */
get key(): symbol;
}
import { Logger } from '@flexbase/logger';
import { Subject } from './subject.interface';
import { SubscriptionDispatcher } from './subscription.dispatcher';
import { Subscription } from './subscription.interface';
import { SubscriptionContext } from './subscription.context';
import { SubscriptionCallback } from './subscription.callback';
/** Represents a type that manages subjects and subscriptions */
export declare class SubjectManager {
private _logger?;
private readonly _subjects;
constructor(_logger?: Logger | undefined);
set logger(logger: Logger);
/**
* Registers a subject
* @param subject The subject to be registered
* @param dispatcher The subscription dispatcher to use with this subject. Defaults to {@link multicastDispatcher}
* @returns true if successfully registered; otherwise false
*/
register(subject: Subject, dispatcher?: SubscriptionDispatcher<unknown>): boolean;
/**
* Checks if a subject is registered with this manager
* @param subject The subject to check
* @returns true if subject is registered; otherwise false
*/
isRegistered(subject: Subject): boolean;
/**
* Attaches a subscription to a subject
* @param subject The subject to add a subscription
* @param callback The callback when a subject event is raised
* @returns A subscription
*/
subscribe<T>(subject: Subject, callback: SubscriptionCallback<T>): Subscription;
private _subscribe;
/**
* Removes a subscription from subject notifications
* @param subject The subject to remove the subscription from
* @param subscription The subscription to remove
*/
unsubscribe(subject: Subject, subscription: Subscription): void;
/**
* Check to see if a subscription is attached to a subject
* @param subject The subject to check for the specified subscription
* @param subscription The subscription to check
* @returns true if subject has the specified subscription; otherwise false
*/
hasSubscription(subject: Subject, subscription: Subscription): boolean;
/**
* Gets the number of subscriptions for a subject
* @param subject The subject to inspect
* @returns The number of subscriptions for the specified subject
*/
subscriptionCount(subject: Subject): number;
/**
* Notify subscribers of a subject of an event
* @param subject The subject used to raise a notification
* @param context The context to send to subscriptions
* @returns A promise
*/
notify(subject: Subject, context: SubscriptionContext<unknown>): Promise<void>;
}
/** Represents a global SubjectManager instance */
export declare const subjectManager: SubjectManager;
import { SubscriptionContext } from './subscription.context';
/** Represents a subscription callback method */
export type SubscriptionCallback<T> = (context: SubscriptionContext<T>) => Promise<void>;
/** Represents the context sent to subscriptions on a notification */
export interface SubscriptionContext<T> {
/** The value of the context */
value: Readonly<T>;
}
import { SubscriptionCallback } from './subscription.callback';
import { SubscriptionContext } from './subscription.context';
/** Represents a subscription dispatcher */
export interface SubscriptionDispatcher<T> {
/**
* Handles dispatching the subscription callbacks
* @param context The context sent to each subscription
* @param callbacks The subscription callbacks that are to be notified
*/
dispatch(context: SubscriptionContext<T>, callbacks: SubscriptionCallback<T>[]): Promise<void>;
}
/** Represents a subscription */
export interface Subscription {
/** The key used to identify this subscription */
get key(): symbol;
/** Unsubscribes from subject notifications */
unsubscribe: () => void;
}
declare class NoopSubscription implements Subscription {
private readonly _key;
get key(): symbol;
unsubscribe(): void;
}
/** Represents a global no-op subscription instance */
export declare const noopSubscription: NoopSubscription;
export {};
export { Subject } from './core/subject.interface';
export { subjectManager, SubjectManager } from './core/subject.manager';
export { Subscription, noopSubscription } from './core/subscription.interface';
export { SubscriptionContext } from './core/subscription.context';
export { SubscriptionDispatcher } from './core/subscription.dispatcher';
export { multicastDispatcher, MulticastDispatcher } from './core/multicast.dispatcher';
export { SubscriptionCallback } from './core/subscription.callback';
var a = Object.defineProperty;
var b = (i, e, s) => e in i ? a(i, e, { enumerable: !0, configurable: !0, writable: !0, value: s }) : i[e] = s;
var o = (i, e, s) => (b(i, typeof e != "symbol" ? e + "" : e, s), s);
class g {
constructor() {
o(this, "_key", Symbol());
}
get key() {
return this._key;
}
// eslint-disable-next-line @typescript-eslint/no-empty-function
unsubscribe() {
}
}
const p = new g();
class h {
async dispatch(e, s) {
await Promise.allSettled(s.map((t) => t(e)));
}
}
const l = new h();
class y {
constructor(e, s) {
o(this, "_subscription");
o(this, "_callback");
o(this, "_key");
this._subscription = e, this._callback = s, this._key = e.key;
}
get key() {
return this._key;
}
get subscription() {
return this._subscription;
}
get callback() {
return this._callback;
}
}
class k {
constructor(e) {
o(this, "_logger");
o(this, "_subjects", /* @__PURE__ */ new Map());
this._logger = e;
}
set logger(e) {
this._logger = e;
}
/**
* Registers a subject
* @param subject The subject to be registered
* @param dispatcher The subscription dispatcher to use with this subject. Defaults to {@link multicastDispatcher}
* @returns true if successfully registered; otherwise false
*/
register(e, s = l) {
var t;
return this._subjects.has(e.key) ? ((t = this._logger) == null || t.warn(`Duplicate subject ${e.key.toString()} already registered`), !1) : (this._subjects.set(e.key, { subject: e, dispatcher: s, subscriptions: [] }), !0);
}
/**
* Checks if a subject is registered with this manager
* @param subject The subject to check
* @returns true if subject is registered; otherwise false
*/
isRegistered(e) {
return this._subjects.has(e.key);
}
/**
* Attaches a subscription to a subject
* @param subject The subject to add a subscription
* @param callback The callback when a subject event is raised
* @returns A subscription
*/
subscribe(e, s) {
return this._subscribe(e, s);
}
_subscribe(e, s) {
var n;
const t = this._subjects.get(e.key);
if (!t)
return (n = this._logger) == null || n.warn(`Subject ${e.key.toString()} has not been registered`), p;
const c = {
key: Symbol(),
unsubscribe: () => {
this.unsubscribe(e, c);
}
}, r = new y(c, s);
return t.subscriptions.push(r), r.subscription;
}
/**
* Removes a subscription from subject notifications
* @param subject The subject to remove the subscription from
* @param subscription The subscription to remove
*/
unsubscribe(e, s) {
var r, n;
const t = this._subjects.get(e.key);
if (!t) {
(r = this._logger) == null || r.warn(`Subject ${e.key.toString()} has not been registered yet subscription ${s.toString()} called unsubscribe`);
return;
}
const c = t.subscriptions.findIndex((u) => u.key === s.key);
if (c < 0) {
(n = this._logger) == null || n.warn(`Subject ${e.key.toString()} does not have subscription ${s.toString()} registered`);
return;
}
t.subscriptions.splice(c, 1), s.unsubscribe = () => {
};
}
/**
* Check to see if a subscription is attached to a subject
* @param subject The subject to check for the specified subscription
* @param subscription The subscription to check
* @returns true if subject has the specified subscription; otherwise false
*/
hasSubscription(e, s) {
var r;
const t = this._subjects.get(e.key);
return t ? t.subscriptions.findIndex((n) => n.key === s.key) >= 0 : ((r = this._logger) == null || r.warn(`Subject ${e.key.toString()} has not been registered`), !1);
}
/**
* Gets the number of subscriptions for a subject
* @param subject The subject to inspect
* @returns The number of subscriptions for the specified subject
*/
subscriptionCount(e) {
var t;
const s = this._subjects.get(e.key);
return s ? s.subscriptions.length : ((t = this._logger) == null || t.warn(`Subject ${e.key.toString()} has not been registered`), 0);
}
/**
* Notify subscribers of a subject of an event
* @param subject The subject used to raise a notification
* @param context The context to send to subscriptions
* @returns A promise
*/
async notify(e, s) {
var r;
const t = this._subjects.get(e.key);
if (!t) {
(r = this._logger) == null || r.warn(`Subject ${e.key.toString()} has not been registered`);
return;
}
const c = t.subscriptions.map((n) => n.callback);
await t.dispatcher.dispatch(s, c);
}
}
const S = new k();
export {
h as MulticastDispatcher,
k as SubjectManager,
l as multicastDispatcher,
p as noopSubscription,
S as subjectManager
};
(function(r,i){typeof exports=="object"&&typeof module<"u"?i(exports):typeof define=="function"&&define.amd?define(["exports"],i):(r=typeof globalThis<"u"?globalThis:r||self,i(r["observable-subject"]={}))})(this,function(r){"use strict";var d=Object.defineProperty;var _=(r,i,u)=>i in r?d(r,i,{enumerable:!0,configurable:!0,writable:!0,value:u}):r[i]=u;var a=(r,i,u)=>(_(r,typeof i!="symbol"?i+"":i,u),u);class i{constructor(){a(this,"_key",Symbol())}get key(){return this._key}unsubscribe(){}}const u=new i;class g{async dispatch(e,t){await Promise.allSettled(t.map(s=>s(e)))}}const p=new g;class h{constructor(e,t){a(this,"_subscription");a(this,"_callback");a(this,"_key");this._subscription=e,this._callback=t,this._key=e.key}get key(){return this._key}get subscription(){return this._subscription}get callback(){return this._callback}}class l{constructor(e){a(this,"_logger");a(this,"_subjects",new Map);this._logger=e}set logger(e){this._logger=e}register(e,t=p){var s;return this._subjects.has(e.key)?((s=this._logger)==null||s.warn(`Duplicate subject ${e.key.toString()} already registered`),!1):(this._subjects.set(e.key,{subject:e,dispatcher:t,subscriptions:[]}),!0)}isRegistered(e){return this._subjects.has(e.key)}subscribe(e,t){return this._subscribe(e,t)}_subscribe(e,t){var c;const s=this._subjects.get(e.key);if(!s)return(c=this._logger)==null||c.warn(`Subject ${e.key.toString()} has not been registered`),u;const o={key:Symbol(),unsubscribe:()=>{this.unsubscribe(e,o)}},n=new h(o,t);return s.subscriptions.push(n),n.subscription}unsubscribe(e,t){var n,c;const s=this._subjects.get(e.key);if(!s){(n=this._logger)==null||n.warn(`Subject ${e.key.toString()} has not been registered yet subscription ${t.toString()} called unsubscribe`);return}const o=s.subscriptions.findIndex(k=>k.key===t.key);if(o<0){(c=this._logger)==null||c.warn(`Subject ${e.key.toString()} does not have subscription ${t.toString()} registered`);return}s.subscriptions.splice(o,1),t.unsubscribe=()=>{}}hasSubscription(e,t){var n;const s=this._subjects.get(e.key);return s?s.subscriptions.findIndex(c=>c.key===t.key)>=0:((n=this._logger)==null||n.warn(`Subject ${e.key.toString()} has not been registered`),!1)}subscriptionCount(e){var s;const t=this._subjects.get(e.key);return t?t.subscriptions.length:((s=this._logger)==null||s.warn(`Subject ${e.key.toString()} has not been registered`),0)}async notify(e,t){var n;const s=this._subjects.get(e.key);if(!s){(n=this._logger)==null||n.warn(`Subject ${e.key.toString()} has not been registered`);return}const o=s.subscriptions.map(c=>c.callback);await s.dispatcher.dispatch(t,o)}}const y=new l;r.MulticastDispatcher=g,r.SubjectManager=l,r.multicastDispatcher=p,r.noopSubscription=u,r.subjectManager=y,Object.defineProperty(r,Symbol.toStringTag,{value:"Module"})});