Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Base for new watcher #314

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 0 additions & 129 deletions core/cb.watch/init.js

This file was deleted.

8 changes: 4 additions & 4 deletions core/cb.watch/main.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
// Requires
var _ = require('lodash');
var init = require('./init').init;
var Watcher= require('./watcher');


function setup(options, imports, register) {
// Import
var events = imports.events;
var logger = imports.logger.namespace("watch");

var watcher = new Watcher(logger, events);

register(null, {
"watch": {
init: _.partial(init, logger, events)
}
"watch": watcher
});
}

Expand Down
132 changes: 132 additions & 0 deletions core/cb.watch/watcher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
// Requires
var Q = require('q');
var _ = require('lodash');
var path = require('path');
var fs = require('fs');
var harmonycollections = require("harmony-collections");

// Hack for pathwatcher and not be dependant on node option --harmony
global.WeakMap = harmonycollections.WeakMap;

var PathWatcher = require("pathwatcher");

var batch = require('../utils').batch;

var Watcher = function(logger, events) {
this.logger = logger;
this.events = events;
this.rootPath = "/";
this.watchers = {};
}

// Init te watcher with a root path
Watcher.prototype.init = function(rootPath) {
this.logger.log("init", rootPath);
this.rootPath = rootPath;
this.start("./");
this.start("./hello.py");
}

// Normalize a path to the root path
Watcher.prototype.normalize = function(fullPath) {
return path.normalize(
'/' + path.relative(this.rootPath, fullPath)
);
}

// Start watching a path
Watcher.prototype.start = function(basePath) {
var that = this;

// already watching this file
if (this.watchers[basePath]) return;

this.logger.log("start", basePath);

this.watchers[basePath] = PathWatcher.watch(path.join(this.rootPath, basePath), function() {
console.log(basePath, arguments);
});
this.watchers[basePath].handleWatcher.on("change", function() {
console.log(basePath, "2", arguments);
});

/*this.watchers[basePath] = fs.watch(
path.join(this.rootPath, basePath),
{

},
batch(function(changeType, filePath) {
console.log(changeType, filePath);
// Simply queue the data for our batch processor
return {
change: changeType,
path: that.normalize(filePath),
stats: {
current: {},
old: {}
}
};
}, function process(eventList) {
// Aggregate events by folder
var folderEvents = _(eventList).reduce(function(context, e) {
// Aggregate by parent folder of changed path
var key = path.dirname(e.path);

// Set list if empty
if(context[key] === undefined) {
context[key] = [];
}

// Add event to folder's event list
context[key].push(e);

return context;
}, {});

// Refresh each of those changed folders
_.each(folderEvents, function(eventList, folder) {
// Many events, so group by folder
if(eventList.length >= 3) {
// Send out aggregated event
return that.events.emit('watch.change.folder', {
change: 'folder',
path: folder
});
}

// Few events so send them out individually
_.each(eventList, function(e) {
events.emit(
// e.change can be any of
// ['updated', 'created', 'deleted']
'watch.change.'+e.change,

// Actual event data
e
);

});
});
})
)*/
}

// Stop watching a path
Watcher.prototype.stop = function (basePath) {

this.logger.log("stop", rootPath);

if (!this.watchers[basePath]) {
this.logger.error("Trying to stop inexistant watcher");
} else {
this.watchers[basePath].close();
delete this.watchers[basePath];

this.events.emit("watch.stop", {
path: this.normalize(basePath)
});
}
}

// Exports
module.exports = Watcher;
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"vfs-http-adapter": "git+https://github.com/AaronO/vfs-http-adapter.git#6c9934e4f2da7886310397170b6ebaf745833936",
"vfs-local": "git+https://github.com/FriendCode/vfs-local.git#af6bafede0ccb9fb362a0c280e708648f7363f33",
"watchr": "2.4.11",
"pathwatcher": "0.19.0",
"gittle": "0.2.2",
"uuid": "1.4.1",
"glob": "3.2.6",
Expand Down