Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…mozilla#246 and refactor tests
  • Loading branch information
gideonthomas committed May 27, 2015
1 parent 4c36b77 commit c28dc53
Show file tree
Hide file tree
Showing 61 changed files with 4,699 additions and 4,005 deletions.
282 changes: 119 additions & 163 deletions client/src/index.js

Large diffs are not rendered by default.

581 changes: 412 additions & 169 deletions client/src/message-handler.js

Large diffs are not rendered by default.

394 changes: 333 additions & 61 deletions client/src/sync-filesystem.js

Large diffs are not rendered by default.

118 changes: 50 additions & 68 deletions client/src/sync-manager.js
Original file line number Diff line number Diff line change
@@ -1,64 +1,22 @@
var SyncMessage = require( '../../lib/syncmessage' ),
messageHandler = require('./message-handler'),
states = require('./sync-states'),
steps = require('./sync-steps'),
WS = require('ws'),
fsUtils = require('../../lib/fs-utils'),
async = require('../../lib/async-lite.js'),
request = require('request'),
url = require('url');
url = require('url'),
log = require('./logger.js');

function SyncManager(sync, fs) {
function SyncManager(sync, fs, _fs) {
var manager = this;

manager.sync = sync;
manager.fs = fs;
manager.session = {
state: states.CLOSED,
step: steps.SYNCED,
path: '/',

is: Object.create(Object.prototype, {
// States
syncing: {
get: function() { return manager.session.state === states.SYNCING; }
},
ready: {
get: function() { return manager.session.state === states.READY; }
},
error: {
get: function() { return manager.session.state === states.ERROR; }
},
closed: {
get: function() { return manager.session.state === states.CLOSED; }
},

// Steps
init: {
get: function() { return manager.session.step === steps.INIT; }
},
chksum: {
get: function() { return manager.session.step === steps.CHKSUM; }
},
diffs: {
get: function() { return manager.session.step === steps.DIFFS; }
},
patch: {
get: function() { return manager.session.step === steps.PATCH; }
},
synced: {
get: function() { return manager.session.step === steps.SYNCED; }
},
failed: {
get: function() { return manager.session.step === steps.FAILED; }
}
})
};
manager.rawFs = _fs;
manager.downstreams = [];
manager.needsUpstream = [];
}

SyncManager.prototype.init = function(wsUrl, token, options, callback) {
var manager = this;
var session = manager.session;
var sync = manager.sync;
var reconnectCounter = 0;
var socket;
Expand All @@ -74,9 +32,6 @@ SyncManager.prototype.init = function(wsUrl, token, options, callback) {
}

if(data.is.response && data.is.authz) {
session.state = states.READY;
session.step = steps.SYNCED;

socket.onmessage = function(event) {
var data = event.data || event;
messageHandler(manager, data);
Expand Down Expand Up @@ -195,39 +150,66 @@ SyncManager.prototype.init = function(wsUrl, token, options, callback) {
connect();
};

SyncManager.prototype.syncPath = function(path) {
SyncManager.prototype.syncUpstream = function() {
var manager = this;
var fs = manager.fs;
var sync = manager.sync;
var syncRequest;
var syncInfo;

if(!manager.socket) {
throw new Error('sync called before init');
}

syncRequest = SyncMessage.request.sync;
syncRequest.content = {path: path};
manager.send(syncRequest.stringify());
};
if(manager.currentSync) {
sync.onError(new Error('Sync currently underway'));
return;
}

// Remove the unsynced attribute for a list of paths
SyncManager.prototype.resetUnsynced = function(paths, callback) {
var fs = this.fs;
fs.getPathsToSync(function(err, pathsToSync) {
if(err) {
sync.onError(err);
return;
}

if(!pathsToSync || !pathsToSync.length) {
log.warn('Nothing to sync');
sync.onIdle('No changes made to the filesystem');
return;
}

function removeUnsyncedAttr(path, callback) {
fsUtils.removeUnsynced(fs, path, function(err) {
if(err && err.code !== 'ENOENT') {
return callback(err);
syncInfo = pathsToSync[0];

fs.setSyncing(function(err) {
if(err) {
sync.onError(err);
return;
}

callback();
manager.currentSync = syncInfo;
syncRequest = SyncMessage.request.sync;
syncRequest.content = {path: syncInfo.path, type: syncInfo.type};
if(syncInfo.oldPath) {
syncRequest.content.oldPath = syncInfo.oldPath;
}
manager.send(syncRequest.stringify());
});
}
});
};

SyncManager.prototype.syncNext = function(syncedPath) {
var manager = this;
var fs = manager.fs;
var sync = manager.sync;

async.eachSeries(paths, removeUnsyncedAttr, function(err) {
fs.dequeueSync(function(err, syncsLeft, dequeuedSync) {
if(err) {
return callback(err);
log.error('Failed to dequeue sync for ' + syncedPath + ' in SyncManager.syncNext()');
}

callback();
sync.onCompleted(dequeuedSync || syncedPath);
manager.currentSync = false;
manager.syncUpstream();
});
};

Expand Down
6 changes: 0 additions & 6 deletions client/src/sync-states.js

This file was deleted.

2 changes: 1 addition & 1 deletion client/src/sync-steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ module.exports = {
PATCH: "PATCH",
SYNCED: "SYNCED",
FAILED: "FAILED"
};
};
15 changes: 13 additions & 2 deletions lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,23 @@ module.exports = {
rsyncDefaults: {
size: 5,
time: true,
recursive: true
recursive: false,
superficial: true
},

attributes: {
unsynced: 'makedrive-unsynced',
conflict: 'makedrive-conflict',
checksum: 'makedrive-checksum'
checksum: 'makedrive-checksum',
partial: 'makedrive-partial',
pathsToSync: 'makedrive-pathsToSync'
},

// Sync Type constants
syncTypes: {
CREATE: 'create',
RENAME: 'rename',
DELETE: 'delete'
},

server: {
Expand All @@ -23,6 +33,7 @@ module.exports = {
LISTENING: 'LISTENING',
INIT: 'INIT',
OUT_OF_DATE: 'OUT_OF_DATE',
SYNCING: 'SYNCING',
CHKSUM: 'CHKSUM',
PATCH: 'PATCH',
ERROR: 'ERROR'
Expand Down
Loading

0 comments on commit c28dc53

Please sign in to comment.