Permalink
Browse files

Add watcher support to vfs-socket

  • Loading branch information...
1 parent 639f4d4 commit 0fb9b18605e8307054e41ee63d4e24946cda1fde @creationix creationix committed Jun 11, 2012
Showing with 48 additions and 2 deletions.
  1. +24 −1 socket/consumer.js
  2. +24 −1 socket/worker.js
View
25 socket/consumer.js
@@ -11,11 +11,13 @@ function Consumer() {
onExit: onExit,
onData: onData,
onEnd: onEnd,
- onClose: onClose
+ onClose: onClose,
+ onChange: onChange,
});
var proxyStreams = {}; // Stream proxies given us by the other side
var proxyProcesses = {}; // Process proxies given us by the other side
+ var proxyWatchers = {}; // Watcher proxies given us by the other side
this.vfs = {
ping: ping, // Send a simple ping request to the worker
@@ -32,6 +34,7 @@ function Consumer() {
rename: route("rename"),
copy: route("copy"),
symlink: route("symlink"),
+ watch: route("watch"),
}
var remote = this.remoteApi;
@@ -83,6 +86,17 @@ function Consumer() {
return process;
}
+ function makeWatcherProxy(token) {
+ var watcher = new EventEmitter();
+ var id = token.id;
+ watcher.id = id;
+ proxyWatchers[id] = watcher;
+ watcher.close = function () {
+ remote.close(id);
+ };
+ return watcher;
+ }
+
function onExit(pid, code, signal) {
var process = proxyProcesses[pid];
process.emit("exit", code, signal);
@@ -107,6 +121,12 @@ function Consumer() {
delete proxyStreams[id];
}
+ function onChange(id, event, filename) {
+ var watcher = proxyWatchers[id];
+ if (!watcher) return;
+ watcher.emit("change", event, filename);
+ }
+
// Return fake endpoints in the initial return till we have the real ones.
function route(name) {
return function (path, options, callback) {
@@ -118,6 +138,9 @@ function Consumer() {
if (meta.process) {
meta.process = makeProcessProxy(meta.process);
}
+ if (meta.watcher) {
+ meta.watcher = makeWatcherProxy(meta.watcher);
+ }
return callback(null, meta);
});
}
View
25 socket/worker.js
@@ -13,6 +13,7 @@ function Worker(vfs) {
end: end,
destroy: destroy,
kill: kill,
+ close: close,
ping: ping,
// Route other calls to the local vfs instance
spawn: route("spawn"),
@@ -27,10 +28,12 @@ function Worker(vfs) {
rmdir: route("rmdir"),
rename: route("rename"),
copy: route("copy"),
- symlink: route("symlink")
+ symlink: route("symlink"),
+ watch: route("watch"),
});
var streams = {};
+ var watchers = {};
var processes = {};
var self = this;
var remote = this.remoteApi;
@@ -87,6 +90,17 @@ function Worker(vfs) {
return token;
}
+ function storeWatcher(watcher) {
+ var id = getID();
+ watchers[id] = watcher;
+ watcher.id = id;
+ watcher.on("change", function (event, filename) {
+ remote.onChange(id, event, filename);
+ });
+ var token = {id: id};
+ return token;
+ }
+
// Remote side writing to our local writable streams
function write(id, chunk) {
// They want to write to our real stream
@@ -116,6 +130,12 @@ function Worker(vfs) {
process.kill(code);
}
+ function close(id) {
+ var watcher = watchers[id];
+ delete watchers[id];
+ watcher.close();
+ }
+
// Can be used for keepalive checks.
function ping(callback) {
callback();
@@ -142,6 +162,9 @@ function Worker(vfs) {
if (meta.process) {
meta.process = storeProcess(meta.process);
}
+ if (meta.watcher) {
+ meta.watcher = storeWatcher(meta.watcher);
+ }
// Call the remote callback with the result
callback(null, meta);
});

0 comments on commit 0fb9b18

Please sign in to comment.