Skip to content

Commit

Permalink
Issue #24 - Isolate core sync functionality in the engine singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
palant committed Feb 20, 2018
1 parent 450bb4c commit edcd6a5
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 29 deletions.
6 changes: 4 additions & 2 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ panelPort.on("connect", () =>
{
masterPassword.suspendAutoLock();

let {syncData} = sync;
Promise.all([
getCurrentHost(),
masterPassword.state,
sync.syncData ? sync.syncData.provider : null
syncData ? syncData.provider : null
]).then(([currentHost, masterPasswordState, syncProvider]) =>
{
if (masterPasswordState != "known")
Expand All @@ -41,8 +42,9 @@ panelPort.on("disconnect", () =>
masterPassword.resumeAutoLock();
});

sync.addModificationListener(syncData =>
sync.addModificationListener(() =>
{
let {syncData} = sync;
panelPort.emit("init", {syncProvider: syncData ? syncData.provider : null});
});

Expand Down
75 changes: 48 additions & 27 deletions lib/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,54 +68,75 @@ let tracker = {
};
tracker.onModified = tracker.onModified.bind(tracker);

let syncData = null;
storage.get(dataKey, null).then(value =>
{
syncData = value;
if (syncData)
tracker.enable(true);
});
let engine = {
storageKey: "syncData",
data: null,

init()
{
storage.get(this.storageKey, null).then(value =>
{
this.data = value;
if (this.data)
tracker.enable(true);
});
},

setup(provider, token)
{
return lock.acquire().then(() =>
{
this.data = {provider, token};
triggerModificationListeners();
return Promise.all([
storage.set(this.storageKey, this.data, null),
tracker.enable()
]);
}).finally(() => lock.release());
},

disable(noLock)
{
if (!this.data)
return Promise.resolve();

return (noLock ? Promise.resolve() : lock.acquire()).then(() =>
{
this.data = null;
triggerModificationListeners();
return Promise.all([
storage.delete(this.storageKey),
tracker.disable()
]);
}).finally(() => noLock || lock.release());
}
};
engine.init();

Object.defineProperty(exports, "syncData", {
enumerable: true,
get: () => syncData
get: () => engine.data
});

exports.authorize = function()
{
let provider = require("./sync-providers/dropbox");
return provider.authorize().then(token =>
{
return lock.acquire().then(() =>
{
syncData = {provider: "dropbox", token};
triggerModificationListeners(syncData);
return Promise.all([
storage.set(dataKey, syncData, null),
tracker.enable()
]);
}).finally(() => lock.release());
return engine.setup("dropbox", token);
});
};

exports.disable = function(noLock)
{
return (noLock ? Promise.resolve() : lock.acquire()).then(() =>
{
syncData = null;
triggerModificationListeners(syncData);
return Promise.all([
storage.delete(dataKey),
tracker.disable()
]);
}).finally(() => noLock || lock.release());
return engine.disable(noLock);
};

let modificationListeners = [];
function triggerModificationListeners()
{
for (let listener of modificationListeners)
listener(syncData);
listener();
}

function addModificationListener(listener)
Expand Down

0 comments on commit edcd6a5

Please sign in to comment.