/** * Event object */ export type Event = { [Key in EventName]: Payload; }; /** * Event handler function */ export type Handler = undefined extends News ? (news?: News | undefined) => void : (news: News) => void; /** * Event subscription object */ export interface Subscription { event: Key; handler: Handler; } /** * Function to subscribe to an event */ export type Subscribe = ReturnType>; /** * Function to unsubscribe to an event */ export type Unsubscribe = ReturnType>; /** * Options for customizing the subscriber factory */ export interface CreateSubscribeOptions { /** * If `true` the event names are case insenseitive */ caseInsensitive: boolean; } /** * Options for customizing the unsubscriber factory */ export interface CreateUnsubscribeOptions { /** * If `true` the event names are case insenseitive */ caseInsensitive: boolean; } /** * 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 = (...args: undefined extends T[Key] ? [key: Key, news?: T[Key], options?: Partial] : [key: Key, news: T[Key], options?: Partial]) => void; /** * Options for customizing the publisher factory */ export interface 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; } /** * Event listener object that stores the event hander and notification times */ export interface Listener { handler: Handler; times: number; } /** * Event stack object that stores all event listeners */ export type Stack = Partial<{ [Key in keyof T & string]: Listener[]; }>; /** * Remove all event listeners */ export type Clear = ReturnType; /** * Options for customizing the pub-sub instance */ export interface PubSubOptions { async: boolean; caseInsensitive: boolean; stack: Stack; } /** * The pub-sub instance */ export interface PubSub { publish: Publish; subscribe: Subscribe; unsubscribe: Unsubscribe; clear: Clear; stack: Stack; } /** * Setup subscriber */ declare const createSubscribe: >(stack: Stack, options?: Partial) => (event: Key, handler: Handler, times?: number) => { event: Key; handler: Handler; }; /** * Factory function for creating `unsubscribe` */ declare function createUnsubscribe(stack: Stack, options?: Partial): { (subscription: Subscription): void; (event: Key_1, handler: Handler): void; }; /** * Factory function for creating `clear()` */ declare const createClear: >(stack: Stack) => () => void; /** * Create a new pub-sub instance */ declare const createPubSub: = Event>(options?: Partial>) => PubSub; /** * Global pub-sub instance */ declare const globalPubSub: PubSub; export { globalPubSub, createPubSub }; export default createPubSub;