0.1.1 • Published 4 months ago
bom-message v0.1.1
bom-message
A TypeScript library for cross-window message communication, designed by Shuo Feng.
Installation
yarn add bom-message
Or
npm install bom-message
Usage
Basic Example
import { Entity } from "bom-message";
// Initialize the Entity system
Entity.init();
// Create entities
const entity1 = new Entity("1");
const entity2 = new Entity("2");
// Subscribe to messages
entity2.subscribeMessage((sender, payload, reply) => {
console.log(`Received: ${payload}`);
reply("Hi"); // Send reply
});
// Send message and handle response
entity1.sendMessage("2", "Hello").then((response) => {
console.log(`Got response: ${response}`); // "Hi"
});
Features
- Bidirectional communication between entities
- Promise-based message handling
- Built-in reply mechanism
- Cross-window communication support
- TypeScript support
API Reference
Methods
Method | Description | Parameters | Return Type |
---|---|---|---|
Entity.init() | Initialize the Entity system. Must be called first. | config?: { sourceEntityIds?: string[], enableAutoCustomEmitter?: boolean, globalEmitterKey?: string, verbose?: boolean } | void |
Entity.getById() | Retrieve an entity instance by its ID. | id: string | Entity \| undefined |
Entity.destroy() | Clean up all entities and event listeners. | none | void |
new Entity() | Create new entity instance. | id: string, options?: { verbose?: boolean, errorOnDuplicate?: boolean } | Entity |
entity.destroy() | Remove this specific entity instance. | none | void |
entity.subscribeMessage() | Subscribe to incoming messages. Returns unsubscribe function. | handler: (sender: { id: string }, payload: unknown, reply: (response?: unknown) => void) => void | () => void |
entity.sendMessage() | Send message to another entity. Returns promise with reply. | targetId: string, message: unknown | Promise<unknown> |
License
This library is MIT licensed.