wasm-ripple 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,324 @@
1
+ # Wasm-Ripple
2
+
3
+
4
+ [![Rust](https://img.shields.io/badge/Rust-000000?style=for-the-badge&logo=rust)](https://www.rust-lang.org/)
5
+ [![WebAssembly](https://img.shields.io/badge/WebAssembly-2024%20Stable-blue?style=for-the-badge&logo=webassembly)](https://webassembly.org/)
6
+
7
+ A high-performance, memory-safe message queue library compiled to WebAssembly, designed for modern web applications.
8
+
9
+ ## โœจ Features
10
+
11
+ - **๐Ÿš€ High Performance** - Built with Rust and compiled to WebAssembly for near-native speed
12
+ - **๐Ÿงต Memory Safe** - Rust's ownership model ensures memory safety without garbage collection pauses
13
+ - **๐Ÿ“ก Topic-based Pub/Sub** - Flexible publish/subscribe pattern with topic-based messaging
14
+ - **๐Ÿ”„ Cross-tab Communication** - Seamless message passing between browser tabs via BroadcastChannel API
15
+ - **โšก Synchronous & Async** - Choose between immediate delivery or microtask-based async publishing
16
+ - **๐Ÿ’พ Ring Buffer Support** - Optional message buffering with O(1) operations and overflow handling
17
+ - **๐Ÿ“ฆ Zero-copy Messaging** - Direct JavaScript value passing without serialization overhead
18
+ - **๐ŸŽฏ Lightweight** - ~40KB gzipped WebAssembly module
19
+
20
+ ## ๐Ÿ“ฆ Installation
21
+
22
+ ```bash
23
+ npm install wasm-ripple
24
+ ```
25
+
26
+ Or use directly from the `pkg` directory:
27
+
28
+ ```html
29
+ <script type="module">
30
+ import init, { MessageQueue } from './pkg/wasm_ripple.js';
31
+
32
+ await init();
33
+ const mq = new MessageQueue('my-channel');
34
+ </script>
35
+ ```
36
+
37
+ ## ๐Ÿš€ Quick Start
38
+
39
+ ```javascript
40
+ import init, { MessageQueue } from 'wasm-ripple';
41
+
42
+ // Initialize the WASM module
43
+ await init();
44
+
45
+ // Create a message queue with optional channel name for cross-tab communication
46
+ const mq = new MessageQueue('my-app');
47
+
48
+ // Register a topic and get its ID (REQUIRED for all operations)
49
+ const topicId = mq.register_topic('events');
50
+
51
+ // Subscribe to a topic using ID
52
+ // Callback receives: payload, topic_id, timestamp, message_id
53
+ const subId = mq.subscribe(topicId, (payload, tid, ts, msgId) => {
54
+ console.log('Received:', payload);
55
+ });
56
+
57
+ // Publish a message synchronously using topic ID
58
+ mq.publish(topicId, { text: 'Hello, World!', count: 42 });
59
+
60
+ // Or publish asynchronously (non-blocking)
61
+ await mq.publish_async(topicId, { text: 'Async message', data: [1, 2, 3] });
62
+
63
+ // Batch publish for high throughput
64
+ const messages = new Array(100).fill({ data: 'batch' });
65
+ mq.publish_batch_by_id(topicId, messages);
66
+
67
+ // Unsubscribe when done
68
+ mq.unsubscribe(topicId, subId);
69
+
70
+ // Clean up resources
71
+ mq.close();
72
+ ```
73
+
74
+ ## ๐Ÿ“Š Performance
75
+
76
+ Benchmark results on Chrome (Apple M3 Pro):
77
+
78
+ | Metric | Result | Notes |
79
+ | :--- | :--- | :--- |
80
+ | **Sync Throughput** | **~5.3M ops/sec** | Zero-allocation hot path |
81
+ | **Batch Throughput** | **~7.9M ops/sec** | Optimized batch processing |
82
+ | **Latency** | **~0.3 ยตs** | Ultra-low overhead dispatch |
83
+ | **100k Messages** | **~20 ms** | Full processing time |
84
+
85
+ > **Note**: These results are achieved using the optimized ID-based API and zero-allocation dispatch mechanism.
86
+
87
+ ## ๐Ÿ“š API Reference
88
+
89
+ ### Constructor
90
+
91
+ ```javascript
92
+ const mq = new MessageQueue(channelName?: string)
93
+ ```
94
+
95
+ - `channelName` (optional): Channel name for cross-tab communication via BroadcastChannel
96
+
97
+ ### Topic Management
98
+
99
+ ```javascript
100
+ // Register a topic and get its ID (O(1) lookups)
101
+ const topicId = mq.register_topic('my-topic'); // returns number (u32)
102
+
103
+ // Check if topic exists by ID
104
+ const exists = mq.has_topic(topicId); // returns boolean
105
+
106
+ // Destroy a topic by ID
107
+ const destroyed = mq.destroy_topic(topicId); // returns boolean
108
+
109
+ // Get topic count
110
+ const count = mq.topic_count(); // returns number
111
+ ```
112
+
113
+ ### Subscription
114
+
115
+ ```javascript
116
+ // Subscribe to a topic by ID
117
+ // Callback signature: (payload, topic_id, timestamp, message_id)
118
+ const subId = mq.subscribe(topicId, callback); // returns subscriber ID
119
+
120
+ // Unsubscribe
121
+ const success = mq.unsubscribe(topicId, subId); // returns boolean
122
+
123
+ // Unsubscribe all
124
+ const count = mq.unsubscribe_all(topicId); // returns number of unsubscribed
125
+
126
+ // Get subscriber count
127
+ const count = mq.subscriber_count(topicId); // returns number
128
+ ```
129
+
130
+ ### Publishing
131
+
132
+ ```javascript
133
+ // Synchronous publish (immediate delivery)
134
+ mq.publish(topicId, payload);
135
+
136
+ // Asynchronous publish (delivered in microtask)
137
+ await mq.publish_async(topicId, payload);
138
+
139
+ // Batch publish (highest throughput)
140
+ mq.publish_batch_by_id(topicId, [payload1, payload2, ...]);
141
+ ```
142
+
143
+ ### Ring Buffer Management
144
+
145
+ ```javascript
146
+ // Enable message buffering for a topic
147
+ mq.enable_topic_buffer(topicId, 100); // capacity (default: 100)
148
+
149
+ // Check if buffering is enabled
150
+ const hasBuffer = mq.has_buffer(topicId); // boolean
151
+
152
+ // Get buffer size
153
+ const size = mq.get_buffer_size(topicId); // current message count
154
+
155
+ // Get buffer capacity
156
+ const capacity = mq.get_buffer_capacity(topicId); // maximum capacity
157
+
158
+ // Get buffered messages
159
+ const messages = mq.get_buffered_messages(topicId); // Array of messages
160
+
161
+ // Clear buffer
162
+ const cleared = mq.clear_buffer(topicId); // number of cleared messages
163
+
164
+ // Disable buffering
165
+ mq.disable_topic_buffer(topicId);
166
+ ```
167
+
168
+ ### Utilities
169
+
170
+ ```javascript
171
+ // Get unique client ID
172
+ const clientId = mq.get_client_id(); // string
173
+
174
+ // Close the queue and release resources
175
+ mq.close();
176
+ ```
177
+
178
+ ## ๐Ÿ”„ Ring Buffer
179
+
180
+ The ring buffer provides efficient message caching with O(1) operations:
181
+
182
+ ```javascript
183
+ const logTopic = mq.register_topic('logs');
184
+
185
+ // Enable buffer with capacity of 5 messages
186
+ mq.enable_topic_buffer(logTopic, 5);
187
+
188
+ // Publish 10 messages
189
+ for (let i = 0; i < 10; i++) {
190
+ mq.publish(logTopic, { id: i, message: `Log ${i}` });
191
+ }
192
+
193
+ // Buffer only keeps the last 5 messages (IDs 5-9)
194
+ console.log(mq.get_buffer_size(logTopic)); // 5
195
+ console.log(mq.get_buffer_capacity(logTopic)); // 5
196
+
197
+ // Retrieve buffered messages
198
+ const messages = mq.get_buffered_messages(logTopic);
199
+ console.log(messages[0].payload.id); // 5 (oldest)
200
+ console.log(messages[4].payload.id); // 9 (newest)
201
+ ```
202
+
203
+ **Key Features:**
204
+ - **Fixed size**: Prevents unbounded memory growth
205
+ - **Automatic overflow**: Oldest messages are automatically displaced when full
206
+ - **O(1) operations**: Constant time push and retrieval
207
+ - **Per-topic**: Each topic can have different buffer settings
208
+
209
+ ## ๐ŸŒ Cross-tab Communication
210
+
211
+ Open the same page in multiple tabs to test cross-tab messaging:
212
+
213
+ ```javascript
214
+ // In tab 1
215
+ const mq = new MessageQueue('cross-tab-channel');
216
+ const topicId = mq.register_topic('updates');
217
+ mq.subscribe(topicId, (msg) => console.log('Tab 1 received:', msg));
218
+
219
+ // In tab 2
220
+ const mq = new MessageQueue('cross-tab-channel');
221
+ const topicId = mq.register_topic('updates');
222
+ mq.publish(topicId, { text: 'Hello from tab 2!' });
223
+ // Tab 1 will receive the message!
224
+ ```
225
+
226
+ ## ๐Ÿ†š Comparison
227
+
228
+ | Feature | **wasm-mq** | **Mitt / Tiny-emitter** | **PubSubJS** | **RxJS** |
229
+ | :--- | :--- | :--- | :--- | :--- |
230
+ | **Sync Throughput** | ~5.3M ops/sec | **~26M ops/sec** | ~18M ops/sec | ~41M ops/sec |
231
+ | **Batch Throughput** | ~7.0M ops/sec | **~44M ops/sec** | ~19M ops/sec | ~47M ops/sec |
232
+ | **Memory Jitter** | **Low (ยฑ0.5 MB)** | Medium (ยฑ0.7 MB) | High (ยฑ1.0 MB) | High (ยฑ0.9 MB) |
233
+ | **Cross-tab** | โœ… **Built-in** | โŒ (Manual) | โŒ | โŒ |
234
+ | **Buffering** | โœ… **Ring Buffer** | โŒ | โŒ | โœ… (ReplaySubject) |
235
+ | **Size (Gzipped)** | ~40KB (WASM) | < 200B | ~3KB | > 20KB |
236
+
237
+ ### When to use which?
238
+
239
+ 1. **Use `wasm-mq` if:**
240
+ * You need **Cross-tab Communication** out of the box.
241
+ * You require **stable memory usage** (low jitter) for long-running apps (dashboards, games).
242
+ * You need **Message History (Ring Buffer)** for late subscribers.
243
+ * You are already using Rust/WASM and want zero-overhead communication within WASM.
244
+
245
+ 2. **Use `mitt` if:**
246
+ * You just need simple, ultra-fast component communication within a single page.
247
+ * Bundle size (<200B) is your top priority.
248
+
249
+ 3. **Use `RxJS` if:**
250
+ * You need complex functional reactive programming (FRP) operators (map, filter, throttle, debounce).
251
+
252
+ ### ๐Ÿ”ฌ Running Benchmarks
253
+
254
+ You can verify these results yourself by running the included benchmark suite:
255
+
256
+ ```bash
257
+ # Start local server
258
+ npm run serve
259
+
260
+ # Open in browser
261
+ # http://localhost:8000/benchmark/comparison/index.html
262
+ ```
263
+
264
+ ## ๐Ÿ—๏ธ Building from Source
265
+
266
+ ```bash
267
+ # Install Rust and wasm-pack
268
+ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
269
+ cargo install wasm-pack
270
+
271
+ # Clone and build
272
+ git clone <repo-url>
273
+ cd mq-
274
+ wasm-pack build --dev --target web
275
+
276
+ # The compiled files will be in the `pkg/` directory
277
+ ```
278
+
279
+ ## ๐Ÿ“– Examples
280
+
281
+ Please refer to the code snippets in the "Quick Start" section above for usage examples.
282
+
283
+ ## ๐ŸŽฏ Use Cases
284
+
285
+ - **Real-time dashboards** - Broadcast updates across multiple tabs
286
+ - **State synchronization** - Keep application state in sync
287
+ - **Event logging** - Buffer and replay events with ring buffer
288
+ - **Multi-tab coordination** - Coordinate actions between browser tabs
289
+ - **Message caching** - Temporarily cache messages for new subscribers
290
+
291
+ ## ๐Ÿ”ง Configuration
292
+
293
+ ### Release Build Optimization
294
+
295
+ The library is optimized for size in release mode:
296
+
297
+ ```toml
298
+ [profile.release]
299
+ lto = true # Link-time optimization
300
+ opt-level = "s" # Optimize for size
301
+ strip = true # Remove debug symbols
302
+ codegen-units = 1 # Better optimization at cost of compile time
303
+ ```
304
+
305
+ Result: ~40KB gzipped WebAssembly module
306
+
307
+ ## ๐Ÿงช Testing
308
+
309
+ ```bash
310
+ # Run Rust tests
311
+ cargo test
312
+ ```
313
+
314
+ ## ๐Ÿ“ License
315
+
316
+ MIT OR Apache-2.0
317
+
318
+ ## ๐Ÿค Contributing
319
+
320
+ Contributions are welcome! Please feel free to submit a Pull Request.
321
+
322
+ ## ๐Ÿ“ฎ Support
323
+
324
+ For issues and questions, please use the GitHub issue tracker.
package/package.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "name": "wasm-ripple",
3
+ "type": "module",
4
+ "collaborators": [
5
+ "Wei Wang <weiwangfds@163.com>"
6
+ ],
7
+ "description": "A high-performance, memory-safe message queue library compiled to WebAssembly",
8
+ "version": "0.1.0",
9
+ "license": "MIT",
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "https://github.com/weiwangfds/wasm-ripple"
13
+ },
14
+ "files": [
15
+ "wasm_ripple_bg.wasm",
16
+ "wasm_ripple.js",
17
+ "wasm_ripple.d.ts"
18
+ ],
19
+ "main": "wasm_ripple.js",
20
+ "types": "wasm_ripple.d.ts",
21
+ "sideEffects": [
22
+ "./snippets/*"
23
+ ]
24
+ }
@@ -0,0 +1,206 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+
4
+
5
+ export interface Subscription {
6
+ unsubscribe(): void;
7
+ }
8
+
9
+ export class Stream {
10
+ constructor(subscribeFn: (callback: Function) => Function);
11
+
12
+ subscribe(callback: Function): Subscription;
13
+ map(transform: (value: any) => any): Stream;
14
+ filter(predicate: (value: any) => boolean): Stream;
15
+ tap(effect: (value: any) => void): Stream;
16
+ debounce(ms: number): Stream;
17
+ }
18
+
19
+
20
+ export class Topic {
21
+ constructor(mq: MessageQueue, name: string, id: number);
22
+ readonly id: number;
23
+ readonly name: string;
24
+
25
+ stream(): Stream;
26
+ publish(payload: any): void;
27
+ publishAsync(payload: any): Promise<any>;
28
+ publishBatch(payloads: Array<any>): void;
29
+ subscribe(callback: Function): number;
30
+ unsubscribe(sub_id: number): boolean;
31
+ unsubscribeAll(): number;
32
+
33
+ readonly subscriberCount: number;
34
+ readonly bufferSize: number;
35
+ readonly bufferCapacity: number;
36
+ readonly hasBuffer: boolean;
37
+
38
+ enableBuffer(capacity?: number): void;
39
+ disableBuffer(): void;
40
+ clearBuffer(): number;
41
+ getBufferedMessages(): Array<any>;
42
+ destroy(): boolean;
43
+ }
44
+
45
+ export class MessageQueue {
46
+ free(): void;
47
+ [Symbol.dispose](): void;
48
+ /**
49
+ * Check if a topic has buffering enabled
50
+ * @param topic_id - ID of the topic
51
+ */
52
+ has_buffer(topic_id: number): boolean;
53
+ /**
54
+ * Get the number of topics
55
+ */
56
+ topic_count(): number;
57
+ /**
58
+ * Unsubscribe from a topic using its ID
59
+ */
60
+ unsubscribe(topic_id: number, sub_id: number): boolean;
61
+ /**
62
+ * Clear all buffered messages for a topic
63
+ * @param topic_id - ID of the topic
64
+ * @returns Number of messages cleared
65
+ */
66
+ clear_buffer(topic_id: number): number;
67
+ create_topic(topic_name: string): boolean;
68
+ destroy_topic(topic_id: number): boolean;
69
+ get_client_id(): string;
70
+ /**
71
+ * Publish a message asynchronously using Promise/microtask
72
+ * This returns immediately and delivers the message in the next microtask
73
+ * Useful for non-blocking operations and better browser responsiveness
74
+ */
75
+ publish_async(topic_id: number, payload: any): Promise<any>;
76
+ /**
77
+ * Register a topic and get its ID (handle) for fast publishing
78
+ * Returns the topic ID that can be used with publish_by_id
79
+ */
80
+ register_topic(topic_name: string): number;
81
+ /**
82
+ * Get the current size of the message buffer for a topic
83
+ * @param topic_id - ID of the topic
84
+ * @returns Number of messages currently buffered, or -1 if buffering is not enabled
85
+ */
86
+ get_buffer_size(topic_id: number): number;
87
+ /**
88
+ * Unsubscribe all subscribers from a topic
89
+ */
90
+ unsubscribe_all(topic_id: number): number;
91
+ /**
92
+ * Get the number of subscribers for a specific topic
93
+ */
94
+ subscriber_count(topic_id: number): number;
95
+ /**
96
+ * Enable message buffering for a specific topic
97
+ * Messages will be cached in a ring buffer for later retrieval
98
+ * @param topic_id - ID of the topic
99
+ * @param capacity - Maximum number of messages to buffer (default: 100)
100
+ */
101
+ enable_topic_buffer(topic_id: number, capacity?: number | null): void;
102
+ /**
103
+ * Get the buffer capacity for a topic
104
+ * @param topic_id - ID of the topic
105
+ * @returns Maximum buffer capacity, or 0 if buffering is not enabled
106
+ */
107
+ get_buffer_capacity(topic_id: number): number;
108
+ /**
109
+ * Publish multiple messages by ID (handle) efficiently
110
+ * This is the fastest way to publish multiple messages
111
+ */
112
+ publish_batch_by_id(topic_id: number, payloads: Array<any>): void;
113
+ /**
114
+ * Disable message buffering for a specific topic
115
+ * Clears all buffered messages
116
+ * @param topic_id - ID of the topic
117
+ */
118
+ disable_topic_buffer(topic_id: number): void;
119
+ /**
120
+ * Get buffered messages for a topic as a JavaScript array
121
+ * @param topic_id - ID of the topic
122
+ * @returns Array of buffered messages (oldest first), or empty array if no buffer
123
+ */
124
+ get_buffered_messages(topic_id: number): Array<any>;
125
+ constructor(channel_name?: string | null);
126
+ close(): void;
127
+ /**
128
+ * Publish using a topic ID (handle)
129
+ * This is O(1) and avoids string hashing/copying - significantly faster for high frequency
130
+ */
131
+ publish(topic_id: number, payload: any): void;
132
+ /**
133
+ * Check if a topic exists
134
+ */
135
+ has_topic(topic_id: number): boolean;
136
+ /**
137
+ * Subscribe to a topic using its ID
138
+ * Callback signature: (payload, topic_id, timestamp, message_id)
139
+ */
140
+ subscribe(topic_id: number, callback: Function): number;
141
+
142
+ /**
143
+ * Get or create a topic wrapper
144
+ */
145
+ topic(name: string): Topic;
146
+
147
+ }
148
+
149
+ export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
150
+
151
+ export interface InitOutput {
152
+ readonly memory: WebAssembly.Memory;
153
+ readonly __wbg_messagequeue_free: (a: number, b: number) => void;
154
+ readonly messagequeue_clear_buffer: (a: number, b: number) => number;
155
+ readonly messagequeue_close: (a: number, b: number) => void;
156
+ readonly messagequeue_create_topic: (a: number, b: number, c: number) => number;
157
+ readonly messagequeue_destroy_topic: (a: number, b: number) => number;
158
+ readonly messagequeue_disable_topic_buffer: (a: number, b: number, c: number) => void;
159
+ readonly messagequeue_enable_topic_buffer: (a: number, b: number, c: number, d: number) => void;
160
+ readonly messagequeue_get_buffer_capacity: (a: number, b: number) => number;
161
+ readonly messagequeue_get_buffer_size: (a: number, b: number) => number;
162
+ readonly messagequeue_get_buffered_messages: (a: number, b: number, c: number) => void;
163
+ readonly messagequeue_get_client_id: (a: number, b: number) => void;
164
+ readonly messagequeue_has_buffer: (a: number, b: number) => number;
165
+ readonly messagequeue_has_topic: (a: number, b: number) => number;
166
+ readonly messagequeue_new: (a: number, b: number, c: number) => void;
167
+ readonly messagequeue_publish: (a: number, b: number, c: number, d: number) => void;
168
+ readonly messagequeue_publish_async: (a: number, b: number, c: number, d: number) => void;
169
+ readonly messagequeue_publish_batch_by_id: (a: number, b: number, c: number, d: number) => void;
170
+ readonly messagequeue_register_topic: (a: number, b: number, c: number) => number;
171
+ readonly messagequeue_subscribe: (a: number, b: number, c: number, d: number) => void;
172
+ readonly messagequeue_subscriber_count: (a: number, b: number) => number;
173
+ readonly messagequeue_topic_count: (a: number) => number;
174
+ readonly messagequeue_unsubscribe: (a: number, b: number, c: number) => number;
175
+ readonly messagequeue_unsubscribe_all: (a: number, b: number) => number;
176
+ readonly __wasm_bindgen_func_elem_144: (a: number, b: number, c: number) => void;
177
+ readonly __wasm_bindgen_func_elem_43: (a: number, b: number) => void;
178
+ readonly __wasm_bindgen_func_elem_528: (a: number, b: number, c: number, d: number) => void;
179
+ readonly __wbindgen_export: (a: number, b: number) => number;
180
+ readonly __wbindgen_export2: (a: number, b: number, c: number, d: number) => number;
181
+ readonly __wbindgen_export3: (a: number) => void;
182
+ readonly __wbindgen_add_to_stack_pointer: (a: number) => number;
183
+ readonly __wbindgen_export4: (a: number, b: number, c: number) => void;
184
+ }
185
+
186
+ export type SyncInitInput = BufferSource | WebAssembly.Module;
187
+
188
+ /**
189
+ * Instantiates the given `module`, which can either be bytes or
190
+ * a precompiled `WebAssembly.Module`.
191
+ *
192
+ * @param {{ module: SyncInitInput }} module - Passing `SyncInitInput` directly is deprecated.
193
+ *
194
+ * @returns {InitOutput}
195
+ */
196
+ export function initSync(module: { module: SyncInitInput } | SyncInitInput): InitOutput;
197
+
198
+ /**
199
+ * If `module_or_path` is {RequestInfo} or {URL}, makes a request and
200
+ * for everything else, calls `WebAssembly.instantiate` directly.
201
+ *
202
+ * @param {{ module_or_path: InitInput | Promise<InitInput> }} module_or_path - Passing `InitInput` directly is deprecated.
203
+ *
204
+ * @returns {Promise<InitOutput>}
205
+ */
206
+ export default function __wbg_init (module_or_path?: { module_or_path: InitInput | Promise<InitInput> } | InitInput | Promise<InitInput>): Promise<InitOutput>;