https://github.com/icholy/tap.js
Transport Agnostic Object Proxy.
https://github.com/icholy/tap.js
Last synced: 3 months ago
JSON representation
Transport Agnostic Object Proxy.
- Host: GitHub
- URL: https://github.com/icholy/tap.js
- Owner: icholy
- Created: 2013-09-09T20:57:27.000Z (almost 12 years ago)
- Default Branch: master
- Last Pushed: 2013-10-29T20:35:21.000Z (over 11 years ago)
- Last Synced: 2025-01-30T03:13:14.052Z (5 months ago)
- Language: JavaScript
- Size: 223 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# TAP [](https://travis-ci.org/icholy/Tap.js)
> Transport Agnostic Object Proxy
**worker.js**
``` javascript
// Some object with methods (could be a class)
var obj = {
x: 0,
foo: function () {
this.x++;
},
bar: function (callback) {
callback(this.x);
},
baz: function () {
return this.x;
}
};// create the tap class
var Remote = Tap.remote();// create a Remote instance wrapping the obj
var remote = new Remote(obj);// events can be sent in either direction
var console = {
log: function () {
var msg = Array.prototype.join.call(arguments, " ");
remote.send("console", msg);
}
};
```**main.js**
``` javascript
// create the worker
var worker = new Worker("worker.js");// create deferred function (optional)
var makeDeferred = Q.defer.bind(Q);// create Tap class
var Local = Tap.local(worker, makeDeferred);// create Tap instance
var local = new Local();// register methods available on remote object
local.methods("foo", "bar", "baz");// invoke methods
local.foo();// callbacks are handled automagically
// Note: they only work for 1 invocation
local.bar(function (x) {
console.log("x:", x);
});// invcations return promises which resolve to the invoked function's return value
// this will only work if a makeDeferred function was provided to the factory
local.baz().then(function (x) {
console.log("x:", x);
});// receive events
local.on(
"console",
console.log.bind(console, "worker:")
);
```