hermes-dispatcherdeprecated

A Event dispatcher for Electron apps.

Usage no npm install needed!

<script type="module">
  import hermesDispatcher from 'https://cdn.skypack.dev/hermes-dispatcher';
</script>

README

Hermes Dispatcher

A Event dispatcher for Electron apps

THIS IS AN ALPHA RELEASE

Please do not use this module in production, because it is not finished and not well documented yet.

API Documentation

enum Scope { Local, Global, GlobalOnly }
namespace Target {
    const Broadcast: Target
    const Local: Target
    const Main: Target
    const Other: (dispatcherId: number) => number
}

interface Sender {
    id: number
    name: string;
    expectsReply: boolean
    reply: (result: R) => void
    send: (eventName: string, ...args) => void
}

/**
 * Initilize current dispatcher and register it with the main process.
 * Should be called before and other dispatcher methode
 * @param name Name of the current process
 */
export declare function init(name: string): void

/**
 * Listen for messages
 * @param eventName Event for which target is listening
 * @param scope Scope:
 *  Local (listen within own process ony),
 *  Global (listen within own process and across others)
 *  GlobalOnly (listen across other processes only)
 * @param listener Function which will be called when message is received:
 *  Fist parameter is the sender object and the rest are arguments.
 *  Warning: Before listener returns it should check (sender.expectsReply) if
 *  the sender is expection a reply. If so call sender.reply(result).
 */
export declare function on(eventName: string, scope: Scope, listener: (sender: Sender, ...args) => void): void

/**
 * Listen for messages and close event once a message is received
 * @param eventName Event for which target is listening
 * @param scope Scope:
 *  Local (listen within own process ony),
 *  Global (listen within own process and across others)
 *  GlobalOnly (listen across other processes only)
 * @param listener Function which will be called when message is received:
 *  Fist parameter is the sender object and the rest are arguments.
 *  Warning: Before listener returns it should check (sender.expectsReply) if
 *  the sender is expection a reply. If so call sender.reply(result).
 */
export declare function once(eventName: string, scope: Scope, listener: (sender: Sender, ...args) => void): void

/**
 * Remove all listeners for a given event from the current dispatcher
 * @param eventName Event name
 */
export declare function removeEvent(eventName: string): void

/**
 * Send data to all that are listening
 * @param eventName Name of the event for which the target is listening
 * @param args Arguments which will be sent to the target
 */
export function emit(eventName: string, ...args): void

/**
 * Send data to a target process
 * @param target Targets process to which the request will be sent
 * @param eventName Name of the event for which the target is listening
 * @param args Arguments which will be sent to the target
 */
export declare function emitTo(target: Target, eventName: string, ...args: any[]): void

/**
 * Send data to a target process and await a response
 * @param target Targets process to which the request will be sent
 * @param eventName Name of the event for which the target is listening
 * @param args Arguments which will be sent to the target
 */
export declare function request<T = any>(target: Target, eventName: string, ...args: any[]): Promise<T>

/**
 * Add a serializer to the dispatcher which allow for custom objects be sent
 * @param serializer Serilizer object
 */
export declare function addSerializer(serializer: wrapper.Serializer): void

/**
 * List all available dispatchers with name and ID
 */
export declare function dispatcherList(): Promise<{
    [name: string]: number
}>;