https://github.com/soarez/m2e
Simple Message to Events plug
https://github.com/soarez/m2e
Last synced: 8 days ago
JSON representation
Simple Message to Events plug
- Host: GitHub
- URL: https://github.com/soarez/m2e
- Owner: soarez
- License: mit
- Created: 2013-01-04T19:05:46.000Z (over 13 years ago)
- Default Branch: master
- Last Pushed: 2013-11-18T12:04:11.000Z (over 12 years ago)
- Last Synced: 2025-02-18T03:14:55.381Z (over 1 year ago)
- Language: JavaScript
- Size: 174 KB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# M2E
#### *Simple Message to Events plug.*
Some APIs are message oriented, like the WebWorker's and WebSocket's API.
I prefer to work with events.
## Usage
To create an M2E object, you need to give it a function to send messages,
when events are triggered, and there's a hook you're responsible to call when
a new message arrives.
Example with webworkers:
````js
// WebWorker
importScripts('build/m2e.js);
// ***********
// ** M2E PLUG
var m2e = M2E(self.postMessage.bind(self));
self.onmessage = function(event) { m2e.onMessage(event.data); };
//
m2e.addListener('echo', function(arg1, arg2) {
m2e.emit('customEvt', arg1, arg2);
});
````
GUI thread:
````js
var worker = new Worker('worker.js?fu_cache=' + +new Date());
var m2e = M2E(worker.postMessage.bind(worker));
worker.onmessage = function(event) { m2e.onMessage(event.data); };
var p1 = 'abc', p2 = 'def';
m2e.addListener('customEvt', function(arg1, arg2) {
// p1 == 'abc'
// p2 == 'def'
})
m2e.emit('echo', p1, p2);
````
## License
MIT