@vgerbot/channel is a message-passing abstraction layer implemented in TypeScript. It simplifies communication between different contexts, allowing developers to create and call classes and methods across different environments (e.g., main thread and Web Workers) in a clean, idiomatic way.
- Encapsulation: Abstracts communication details and provides a consistent high-level API.
- Isolation: Data is securely isolated using
channel-idto ensure safe communication. - Simple API: Remote calls are as easy as regular asynchronous function calls, with support for callbacks and consistent error handling.
- Parallel Execution: Supports task decomposition into multiple target contexts for parallel execution, enhancing performance.
- Extensibility: Allows for custom communicators, making it easy to extend and add new features.
Install the library using npm:
npm install @vgerbot/channelHere’s a simple example demonstrating communication between the main thread and a Web Worker.
export interface SpellChecker {
saveToDictionary(word: string): void;
setCaseSensitive(caseSensitive: boolean): void;
check(sentence: string): boolean;
}import { channel } from '@vgerbot/channel';
import { SpellChecker } from './api';
// Create a channel named 'worker-channel' and connect it to the main thread
const chnl = channel('worker-channel')
.connectToMainThread()
.create();
// Define a CPU-intensive method
chnl.def_method(function performCPUIntensiveCalculation() {
return 'Result!';
});
// Implement the SpellChecker interface
chnl.def_class(class DefaultSpellCheckerImpl implements SpellChecker {
saveToDictionary(word: string) {
console.log(`Saving ${word} to dictionary`);
}
setCaseSensitive(caseSensitive: boolean) {
console.log(`Set case sensitive: ${caseSensitive}`);
}
check(sentence: string) {
return sentence === sentence.toLowerCase();
}
});import { channel } from '@vgerbot/channel';
import { SpellChecker } from './api';
// Create a channel named 'worker-channel' and connect it to the Worker
const chnl = channel('worker-channel')
.connectToWorker('./task.js')
.create();
// Call the CPU-intensive method defined in the Worker
const performCPUIntensiveCalculation = chnl.get_method<() => string>('performCPUIntensiveCalculation');
performCPUIntensiveCalculation().then(console.log); // Output: "Result!"
// Retrieve the SpellChecker class from the Worker
const DefaultSpellCheckerImpl = chnl.get_class<SpellChecker>('DefaultSpellCheckerImpl');
// Create an instance of SpellChecker
const spellChecker = new DefaultSpellCheckerImpl();
spellChecker.saveToDictionary('halo');
spellChecker.setCaseSensitive(false);
spellChecker.check('Halo world!').then(console.log); // Output: false
// Manually destroy the remote instance to free up resources
spellChecker.__destroy__();Since JavaScript’s garbage collection cannot automatically clean up remote instances, it is recommended to call __destroy__() when the instance is no longer needed to avoid memory leaks.
For more examples, please refer to examples and unit tests.
@vgerbot/channel supports all data types that can be cloned using the Structured Clone Algorithm. For more details, refer to the MDN documentation.
Additionally, it supports remote objects and callback functions as parameters, but these types cannot be nested within other objects.
@vgerbot/channel is open-source and released under the terms of the MIT License.