export default Pubst; export type PubstConfig = { /** * - Logger to send warning messages to. */ logger?: Object | undefined; /** * - If logger isn't provided, switches between ConsoleLogger and SilentLogger. */ showWarnings?: boolean | undefined; /** * - A store implementation for persisting topic values. */ store?: Object | undefined; /** * - An array of topic configurations. */ topics?: TopicConfig[] | undefined; }; export type TopicConfig = { /** * - The name of the topic (REQUIRED). */ name: string; /** * - The default value presented to subscribers when the topic is undefined or null. */ default?: any; /** * - Set to true if this topic will not have payload data. */ eventOnly?: boolean | undefined; /** * - Should new subscribers automatically receive the last published value? */ doPrime?: boolean | undefined; /** * - Alert subscribers of all publish events, even if unchanged. */ allowRepeats?: boolean | undefined; /** * - Store-specific configuration passed to the store's registerTopic method. */ storeConfig?: Object | undefined; }; export type SubscriptionConfig = { /** * - The handler to call when the topic is updated. */ handler: Function; /** * - Default value for this subscription. */ default?: any; /** * - Should the handler be primed with the last value? */ doPrime?: boolean | undefined; /** * - Should the handler be called when the value doesn't change? */ allowRepeats?: boolean | undefined; }; /** * @typedef {Object} PubstConfig * @property {Object} [logger] - Logger to send warning messages to. * @property {boolean} [showWarnings] - If logger isn't provided, switches between ConsoleLogger and SilentLogger. * @property {Object} [store] - A store implementation for persisting topic values. * @property {Array} [topics] - An array of topic configurations. */ /** * @typedef {Object} TopicConfig * @property {string} name - The name of the topic (REQUIRED). * @property {*} [default] - The default value presented to subscribers when the topic is undefined or null. * @property {boolean} [eventOnly=false] - Set to true if this topic will not have payload data. * @property {boolean} [doPrime=true] - Should new subscribers automatically receive the last published value? * @property {boolean} [allowRepeats=false] - Alert subscribers of all publish events, even if unchanged. * @property {Object} [storeConfig={}] - Store-specific configuration passed to the store's registerTopic method. */ /** * @typedef {Object} SubscriptionConfig * @property {Function} handler - The handler to call when the topic is updated. * @property {*} [default] - Default value for this subscription. * @property {boolean} [doPrime=true] - Should the handler be primed with the last value? * @property {boolean} [allowRepeats=false] - Should the handler be called when the value doesn't change? */ /** * @summary A slightly opinionated pub/sub utility for Javascript */ declare class Pubst { /** * @summary Set Pubst configuration. * * @param {PubstConfig} userConfig - Your configuration * * @returns {Promise} * * @description *

* Available options are: *

*

*/ configure(userConfig?: PubstConfig): Promise; /** * @summary Configure a new topic. * * @param {TopicConfig} newTopicConfig - Topic configuration * * @returns {Promise} Resolves with the result of registering * the topic in the store. * * @description *

* Allows you to configure a new topic in pubst. * * Available options are: *

    *
  • `name` (REQUIRED) - A string representing the name of the topic.
  • *
  • * `default` (default: undefined) - The default value presented to subscribers when the topic is undefined or null. * This can be overridden by subscribers. *
  • *
  • * `eventOnly` (default: false) - Set this to true if this topic will not have payload data. *
  • *
  • * `doPrime` (default: true) - Should new subscribers automatically receive the last published value on the topic? * If no valid value is present, new subscribers will be primed with the default value (if configured). * This can be overridden by subscribers. *
  • *
  • * `allowRepeats` (default: false) - Alert subscribers of all publish events, even if the value is equal (by strict comparison) to the last value sent. * This can be overridden by subscribers. *
  • *
  • * `storeConfig` (default: {}) - Store-specific configuration that is passed through to the store's * `registerTopic` method. This allows custom store implementations to receive topic-level * configuration (e.g. persistence keys, TTL settings, etc.). *
  • *
*

*/ addTopic(newTopicConfig: TopicConfig): Promise; /** * @summary Configure new topics. * * @param {Array} topics - Topic configurations * * @returns {Promise} * * @description *

* Allows you to configure new topics. This will call `addTopic` with each item passed. * Topics are registered sequentially. * For available options, see `addTopic`. */ addTopics(topics: Array): Promise; /** * @summary Publish to a topic * * @param {string} topic - The topic to publish to * @param {*} payload The payload to publish * * @returns {Promise} */ publish(topic: string, payload: any): Promise; /** * @summary Subscribe to one or more topics * * @param {string|Function} topic - The topic to receive updates for. * If a string is provided, the handler will be called for all updates * for that topic. If a function is provided, it will be used as a * matcher: it receives a topic name string and should return a truthy * value if the subscriber should receive updates for that topic. * If the matcher function throws an error, the error is logged as a * warning and the match is skipped. * @param {Function|SubscriptionConfig} handler - Either your handler function or * a subscription configuration object * @param {*} [def] - (Optional) Value to send when topic is empty * * @returns {Function} - A function that will remove this * subscription from getting further updates. * * @description *

* The first argument may be a string or a matcher function. * If a string is provided, the handler will be called for all * updates for that topic. If a function is provided, it will be * called with each topic name and should return a truthy value to * indicate that the subscriber wishes to receive updates for that topic. * If the matcher function throws, the error is logged as a warning * and the subscriber will not receive the update for that topic. *

* *

* The second argument may be a function or an object. The object * is necessary if you want to provide configuration options for * this subscription. Available options are: *

    *
  • `default` - (Default: undefined) - Default value for this sub.
  • *
  • `doPrime` - (Default: true) - Should the handler be primed with * the last value?
  • *
  • `allowRepeats` - (Default: false) - Should the handler be called * when the value doesn't change?
  • *
  • `handler` - (Required) - The handler to call.
  • *
*

* *

* The handler will be called on topic updates. It will be passed * the new value of the topic as the first argument, and the name of * the topic as the second argument. *

* *

* Note: Subscribe is synchronous and returns an unsubscribe function * immediately. Priming of subscribers with existing values happens * asynchronously via the store. *

*/ subscribe(topic: string | Function, handler: Function | SubscriptionConfig, def?: any): Function; /** * @summary Get the current value of a topic. * * @param {string} topic - The topic to get the value of. * @param {*} [def] - (Optional) a value to return if the topic is * empty. * @returns {Promise<*>} - Resolves with the current value or the default */ currentVal(topic: string, def?: any): Promise; /** * @summary Clears a given topic. * * @param {string} topic - The topic to clear * * @returns {Promise} * * @description Clears the topic by publishing a `null` to it. */ clear(topic: string): Promise; /** * @summary Clears all known topics. * * @returns {Promise} */ clearAll(): Promise; #private; }