0.1.1 • Published 4 months ago

bom-message v0.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

bom-message

Test codecov

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

MethodDescriptionParametersReturn 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: stringEntity \| undefined
Entity.destroy()Clean up all entities and event listeners.nonevoid
new Entity()Create new entity instance.id: string, options?: { verbose?: boolean, errorOnDuplicate?: boolean }Entity
entity.destroy()Remove this specific entity instance.nonevoid
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: unknownPromise<unknown>

License

This library is MIT licensed.

0.1.1

4 months ago

0.1.0

6 months ago

0.0.3

7 months ago

0.0.5

7 months ago

0.0.4

7 months ago

0.0.7

7 months ago

0.0.6

7 months ago

0.0.2

7 months ago

0.0.1

7 months ago