export type Event = { [Key in EventName]: Payload; }; /** * - Event handler function */ export type Handler = Event, Key extends keyof T> = (news: T[Key]) => void; /** * - Event subscription object */ export type Subscription = Event, Key extends keyof T> = { event: Key; handler: Handler; }; /** * - Function to subscribe to an event */ export type Subscribe = Event, Key extends keyof T> = (event: Key, handler: Handler, times?: number) => Subscription; /** * - Options for customizing the subscriber factory */ export type CreateSubscribeOptions = { /** * - If `true` the event names are case insenseitive */ caseInsensitive: boolean; }; /** * - Factory function for `subscribe()` */ export type CreateSubscribe = Event, Key extends keyof T> = (stack: any, options?: Partial) => Subscribe; /** * - Function to unsubscribe from an event */ export type Unsubscribe = Event, Key extends keyof T> = (event: Key | Subscription, handler?: Handler) => void; /** * - Options for customizing the unsubscriber factory */ export type CreateUnsubscribeOptions = { /** * - If `true` the event names are case insenseitive */ caseInsensitive: boolean; }; /** * - Factory function for `unsubscribe()` */ export type CreateUnsubscribe = Event, Key extends keyof T> = (stack: any, options?: Partial) => Unsubscribe; /** * - Inform listeners about some news */ export type Inform = Event, Key extends keyof T> = (listeners: (Handler)[], news: T[Key]) => void; /** * - Options for how to publish an event */ export type PublishOptions = { /** * - If `true` event will *not* be broadcasted gloablly even if `isGlobal` is `true`. */ isNoGlobalBroadcast: boolean; /** * - If `true` event will *not* be broadcasted synchronously even if `async` is `false` globally. */ async: boolean; }; /** * - Function to publish an event */ export type Publish = Event, Key extends keyof T> = (event: Key, news: T[Key], options?: Partial) => void; /** * - Factory function for `publish()` */ export type CreatePublishOptions = { /** * - If `true` event will be published globally. */ isGlobal: boolean; /** * - If `true` the event names are case insenseitive */ caseInsensitive: boolean; /** * - If `true` the pub-sub instance publishes events asynchronously (recommended) */ async: boolean; }; /** * - Factory function for `publish()` */ export type CreatePublish = Event> = (stack: any, options?: Partial) => any; /** * - Remove all event listeners */ export type Clear = () => void; /** * - Factory function for `clear()` */ export type CreateClear = Event> = (stack: any) => Clear; /** * - Event stack object that stores the events handers and notification times */ export type Stack = Event, Key extends keyof T> = { __times__: {}; }; /** * - Options for customizing the pub-sub instance */ export type PubSubOptions = { /** * - If `true` the pub-sub instance publishes events asynchronously (recommended) */ async: boolean; /** * - If `true` the event names are case insenseitive */ caseInsensitive: boolean; /** * A custom event subscriber stack */ stack: any; }; /** * - The pub-sub instance */ export type PubSub = Event> = { publish: Publish; subscribe: Subscribe; unsubscribe: Unsubscribe; clear: Clear; stack: Stack; }; /** * - Create a new empty stack object */ export type CreateStack = Event> = () => Stack; /** * - Create a new pub-sub instance */ export type CreatePubSub = Event> = (options?: Partial) => PubSub; /** * @typedef {(options?: Partial) => PubSub} CreatePubSub - Create a new pub-sub instance * @template {Event} [T=Event] */ /** * Create a new pub-sub instance * @type {CreatePubSub} */ export const createPubSub: CreatePubSub; /** * Global pub-sub instance * @type {PubSub} */ export const globalPubSub: PubSub; export { createPubSub as default };