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

Switched to mediator topic promises #37

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
9 changes: 6 additions & 3 deletions lib/client/data-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,12 @@ function saveEventToMetaData(datamanager, event) {
* returns metadata for the dataset managed by this manager
* @param {function} callback - a callback that will get metadata passed in.
*/
DataManager.prototype.getMetaData = function getMetaData(callback) {
this.$fh.sync.getMetaData(this.datasetId, function(metadata) {
callback(metadata);
DataManager.prototype.getMetaData = function getMetaData() {
var self = this;
return q.Promise(function(resolve) {
self.$fh.sync.getMetaData(self.datasetId, function(metadata) {
resolve(metadata);
});
});
};

Expand Down
59 changes: 4 additions & 55 deletions lib/client/mediator-subscribers/create-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ var chai = require('chai');
var _ = require('lodash');
var CONSTANTS = require('../../constants');
var expect = chai.expect;
var q = require('q');
var shortid = require('shortid');
var MediatorTopicUtility = require('fh-wfm-mediator/lib/topics');

describe("Sync Create Mediator Topic", function() {
Expand Down Expand Up @@ -37,8 +35,6 @@ describe("Sync Create Mediator Topic", function() {

describe("Handling A Successful Creation", function() {

var doneTopic = "done:wfm:sync:mockdatasetid:create";

var mockDatasetManager = {
datasetId: mockDatasetId,
create: sinon.stub().resolves(mockDataToCreate)
Expand All @@ -61,29 +57,8 @@ describe("Sync Create Mediator Topic", function() {
});


it("should handle no unique topic id", function() {
var testDeferred = q.defer();

this.subscribers[doneTopic] = mediator.subscribe(doneTopic, testDeferred.resolve);

mediator.publish(createTopic, {itemToCreate: mockDataToCreate});

return testDeferred.promise.then(checkMocksCalled);
});

it("should handle unique topic id", function() {
var testDeferred = q.defer();

var topicUid = shortid.generate();

this.subscribers[doneTopic] = mediator.subscribe(doneTopic + ":" + topicUid, testDeferred.resolve);

mediator.publish(createTopic, {
itemToCreate: mockDataToCreate,
topicUid: topicUid
});

return testDeferred.promise.then(checkMocksCalled);
it("create", function() {
return mediator.publish(createTopic, {itemToCreate: mockDataToCreate}).then(checkMocksCalled);
});

});
Expand Down Expand Up @@ -111,34 +86,8 @@ describe("Sync Create Mediator Topic", function() {
syncSubscribers.on(CONSTANTS.TOPICS.CREATE, require('./create')(syncSubscribers, mockDatasetManager));
});

it("should handle no unique topic id", function() {
var testDeferred = q.defer();


var errorTopic = "error:wfm:sync:mockdatasetid:create";

this.subscribers[errorTopic] = mediator.subscribe(errorTopic, testDeferred.resolve);

mediator.publish(createTopic, {itemToCreate: mockDataToCreate});

return testDeferred.promise.then(checkErrorMocksCalled);
});

it("should handle a unique topic id", function() {
var testDeferred = q.defer();

var topicUid = shortid.generate();

var errorTopic = "error:wfm:sync:mockdatasetid:create:" + topicUid;

this.subscribers[errorTopic] = mediator.subscribe(errorTopic, testDeferred.resolve);

mediator.publish(createTopic, {
itemToCreate: mockDataToCreate,
topicUid: topicUid
});

return testDeferred.promise.then(checkErrorMocksCalled);
it("create", function() {
return mediator.publish(createTopic, {itemToCreate: mockDataToCreate}).catch(checkErrorMocksCalled);
});

});
Expand Down
19 changes: 1 addition & 18 deletions lib/client/mediator-subscribers/create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
var CONSTANTS = require('../../constants');


/**
*
Expand All @@ -17,31 +15,16 @@ module.exports = function subscribeToCreateTopic(syncDatasetTopics, datasetManag
* Handler for the data sync create topic for this dataset.
*
* @param {object} parameters
* @param {string/number} parameters.topicUid - (Optional) A unique ID to be used to publish completion / error topics.
* @param {object} parameters.itemToCreate - The item to create.
*/
return function handleCreateTopic(parameters) {
var self = this;
parameters = parameters || {};
var datasetItemToCreate = parameters.itemToCreate;

// remove _syncStatus, it can cause sync loop when updated and this data is irrelevant outside client
delete datasetItemToCreate._syncStatus;

//Creating a data item for this dataset.
datasetManager.create(datasetItemToCreate).then(function(createdDatasetItem) {

//Item created successfully, publishing the success topic.
var creatDoneTopic = syncDatasetTopics.getTopic(CONSTANTS.TOPICS.CREATE, CONSTANTS.DONE_PREFIX, parameters.topicUid);

self.mediator.publish(creatDoneTopic, createdDatasetItem);

}).catch(function(error) {

//An error occurred while trying to create a new item, publishing the error topic.
var errorCreateTopic = syncDatasetTopics.getTopic(CONSTANTS.TOPICS.CREATE, CONSTANTS.ERROR_PREFIX, parameters.topicUid);

self.mediator.publish(errorCreateTopic, error);
});
return datasetManager.create(datasetItemToCreate);
};
};
44 changes: 3 additions & 41 deletions lib/client/mediator-subscribers/force-sync-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ require("sinon-as-promised");
var chai = require('chai');
var _ = require('lodash');
var q = require('q');
var shortid = require('shortid');
var expect = chai.expect;
var MediatorTopicUtility = require('fh-wfm-mediator/lib/topics');
var CONSTANTS = require('../../constants');
Expand All @@ -30,8 +29,7 @@ describe("Sync Force Sync Mediator Topic", function() {
});


describe("No Error", function() {
var doneTopic = "done:wfm:sync:mockdatasetid:force_sync";
it("No Error", function() {
var mockDatasetManager = {
datasetId: mockDatasetId,
forceSync: sinon.stub().resolves()
Expand All @@ -47,29 +45,9 @@ describe("Sync Force Sync Mediator Topic", function() {
});

it("should handle no unique topic id", function() {
var testDeferred = q.defer();

this.subscribers[doneTopic] = mediator.subscribe(doneTopic, testDeferred.resolve);

mediator.publish(forceSyncTopic);

return testDeferred.promise.then(checkMocks);
return mediator.publish(forceSyncTopic).then(checkMocks);
});

it("should handle a unique topic id", function() {
var testDeferred = q.defer();

var topicUid = shortid.generate();
var expectedTopic = doneTopic + ":" + topicUid;

this.subscribers[doneTopic] = mediator.subscribe(expectedTopic, testDeferred.resolve);

mediator.publish(forceSyncTopic, {
topicUid: topicUid
});

return testDeferred.promise.then(checkMocks);
});
});

describe("Error Case", function() {
Expand Down Expand Up @@ -98,26 +76,10 @@ describe("Sync Force Sync Mediator Topic", function() {

this.subscribers[errorTopic] = mediator.subscribe(errorTopic, testDeferred.resolve);

mediator.publish(forceSyncTopic);

return testDeferred.promise.then(checkMocks);
return mediator.publish(forceSyncTopic).catch(checkMocks);
});

it("should handle a unique topic id", function() {
var testDeferred = q.defer();

var topicUid = shortid.generate();

var expectedErrorTopic = errorTopic + ":" + topicUid;

this.subscribers[errorTopic] = mediator.subscribe(expectedErrorTopic, testDeferred.resolve);

mediator.publish(forceSyncTopic, {
topicUid: topicUid
});

return testDeferred.promise.then(checkMocks);
});

});
});
17 changes: 2 additions & 15 deletions lib/client/mediator-subscribers/force-sync.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
var CONSTANTS = require('../../constants');


/**
*
Expand All @@ -15,21 +13,10 @@ module.exports = function subscribeToForceSyncTopic(syncDatasetTopics, datasetMa
/**
* Handling the force_sync topic for this dataset.
*
* @param {object} parameters
* @param {string/number} parameters.topicUid - (Optional) A unique ID to be used to publish completion / error topics.
*/
return function handleForceSync(parameters) {
var self = this;
parameters = parameters || {};
return function handleForceSync() {

//Creating the item in the sync store
datasetManager.forceSync().then(function() {
var forceSyncDoneTopic = syncDatasetTopics.getTopic(CONSTANTS.SYNC_TOPICS.FORCE_SYNC, CONSTANTS.DONE_PREFIX, parameters.topicUid);
self.mediator.publish(forceSyncDoneTopic);

}).catch(function(error) {
var forceSyncErrorTopic = syncDatasetTopics.getTopic(CONSTANTS.SYNC_TOPICS.FORCE_SYNC, CONSTANTS.ERROR_PREFIX, parameters.topicUid);
self.mediator.publish(forceSyncErrorTopic, error);
});
return datasetManager.forceSync();
};
};
65 changes: 5 additions & 60 deletions lib/client/mediator-subscribers/list-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ require("sinon-as-promised");
var chai = require('chai');
var expect = chai.expect;
var _ = require('lodash');
var q = require('q');
var shortid = require('shortid');
var MediatorTopicUtility = require('fh-wfm-mediator/lib/topics');
var CONSTANTS = require('../../constants');

Expand Down Expand Up @@ -36,7 +34,6 @@ describe("Sync List Mediator Topic", function() {

describe("No Error", function() {

var doneTopic = "done:wfm:sync:mockdatasetid:list";
var arrayOfDataItems = [mockDataItem];
var mockMetaData = {
syncEvents: {
Expand All @@ -48,7 +45,7 @@ describe("Sync List Mediator Topic", function() {
var mockDatasetManager = {
datasetId: mockDatasetId,
list: sinon.stub().resolves(arrayOfDataItems),
getMetaData: sinon.stub().callsArgWith(0, mockMetaData)
getMetaData: sinon.stub().resolves(mockMetaData)
};

function checkMocks(listResult) {
Expand All @@ -63,48 +60,17 @@ describe("Sync List Mediator Topic", function() {
});

it("should handle no unique topic id", function() {
var testDeferred = q.defer();

this.subscribers[doneTopic] = mediator.subscribe(doneTopic, testDeferred.resolve);

mediator.publish(listTopic);

return testDeferred.promise.then(checkMocks);
});

it("should handle a unique topic id", function() {
var testDeferred = q.defer();

var topicUid = shortid.generate();

var expectedDoneTopic = doneTopic + ":" + topicUid;

this.subscribers[doneTopic] = mediator.subscribe(expectedDoneTopic, testDeferred.resolve);

mediator.publish(listTopic, {
topicUid: topicUid
});

return testDeferred.promise.then(checkMocks);
return mediator.publish(listTopic).then(checkMocks);
});

describe("Sync Status", function() {

it("Should return list with ._syncStatus", function() {
var testDeferred = q.defer();

this.subscribers[doneTopic] = mediator.subscribe(doneTopic, testDeferred.resolve);

mediator.publish(listTopic);


return testDeferred.promise.then(function(data) {
return mediator.publish(listTopic).then(function(data) {
expect(data).to.be.an('array');
expect(data[0].id).to.equal(mockDataItem.id);
expect(data[0]._syncStatus).to.deep.equal(mockMetaData.syncEvents.mockdataitemid);
});


});
});

Expand All @@ -114,7 +80,6 @@ describe("Sync List Mediator Topic", function() {
describe("Error", function() {

var expectedTopicError = new Error("SYNC-Error-Code : Sync Error Message");
var errorTopic = "error:wfm:sync:mockdatasetid:list";

var mockMetaData = {
syncEvents: {
Expand All @@ -125,7 +90,7 @@ describe("Sync List Mediator Topic", function() {
var mockDatasetManager = {
datasetId: mockDatasetId,
list: sinon.stub().rejects(expectedTopicError),
getMetaData: sinon.stub().callsArgOnWith(0, mockMetaData)
getMetaData: sinon.stub().resolves(mockMetaData)
};

beforeEach(function() {
Expand All @@ -140,27 +105,7 @@ describe("Sync List Mediator Topic", function() {
}

it("should handle no unique topic id", function() {
var testDeferred = q.defer();
this.subscribers[errorTopic] = mediator.subscribe(errorTopic, testDeferred.resolve);

mediator.publish(listTopic);

return testDeferred.promise.then(checkMocks);
});

it("should handle a unique topic id", function() {
var testDeferred = q.defer();

var topicUid = shortid;
var expectedErrorTopic = errorTopic + ":" + topicUid;

this.subscribers[errorTopic] = mediator.subscribe(expectedErrorTopic, testDeferred.resolve);

mediator.publish(listTopic, {
topicUid: topicUid
});

return testDeferred.promise.then(checkMocks);
return mediator.publish(listTopic).catch(checkMocks);
});

});
Expand Down
Loading