Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

fh-wfm-sync

Package Overview
Dependencies
Maintainers
5
Versions
53
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fh-wfm-sync - npm Package Compare versions

Comparing version 0.1.3-alpha.504.5 to 0.2.0-pre.1

42

lib/client/data-manager-spec.js

@@ -247,44 +247,2 @@ var DataManager = require('./data-manager');

it("should add the sync status for a data object", function() {
var mockOriginalMetaData = {};
var mockEventData = {
uid: "mockEventUID",
code: "mockEventCode",
message: {
type: "mockEventType",
msg: "mockEventMsg",
action: "mockEventAction"
}
};
var mockNewMetaData = {
syncEvents: {
mockEventUID : {
entityId : mockEventData.uid,
code: mockEventData.code,
action: mockEventData.message.action,
message: mockEventData.message.msg,
type: mockEventData.message.type,
ts: sinon.match.number
}
}
};
var mock$fh = {
sync: {
getMetaData: sinon.stub().callsArgWith(1, mockOriginalMetaData),
setMetaData: sinon.stub().callsArgWith(2)
}
};
dataManager = new DataManager(mockDataSetId, mock$fh, this.mockDataSetNotificationObservableStream, mediator);
return dataManager.addEvent(mockEventData).then(function() {
assert(mock$fh.sync.getMetaData.calledOnce);
assert(mock$fh.sync.getMetaData.calledWith(sinon.match(mockDataSetId), sinon.match.func));
assert(mock$fh.sync.setMetaData.calledOnce);
assert(mock$fh.sync.setMetaData.calledWith(sinon.match(mockDataSetId), sinon.match(mockNewMetaData), sinon.match.func, sinon.match.func));
});
});
});

47

lib/client/data-manager.js

@@ -1,6 +0,6 @@

var _ = require('lodash');
var q = require('q');
var _ = require('lodash')
, q = require('q');
var mediatorManager = require('./mediator-subscribers');
var debug = require('../utils/logger')(__filename);
/**

@@ -77,43 +77,2 @@ *

/**
* Sets sync status for an object.
* @param {object} event - object passed in by sync event handler, should contain data used for logItem object.
*/
DataManager.prototype.addEvent = function addEvent(event) {
debug('addEvent', event);
var self = this;
var deferred = q.defer();
this.$fh.sync.getMetaData(this.datasetId, function(metadata) {
metadata = metadata || {};
metadata.syncEvents = metadata.syncEvents || {};
metadata.syncEvents[event.uid] = {
entityId: event.uid,
code: event.code,
action: event.message.action,
message: event.message.msg,
type: event.message.type,
ts: Date.now()
};
self.$fh.sync.setMetaData(self.datasetId, metadata, deferred.resolve, deferred.reject);
}, deferred.reject);
return deferred.promise;
};
/**
* 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);
});
};
/**
* Creating all of the mediator subscribers for this sync data set.

@@ -120,0 +79,0 @@ */

3

lib/client/index.js

@@ -8,4 +8,4 @@ 'use strict';

, DataManager = require('./data-manager');
var debug = require('../utils/logger')(__filename);
var $fh,

@@ -44,3 +44,2 @@ initialized = false,

function init(_$fh, _syncOptions, _mediator) {
debug('Client init');
if (initialized) {

@@ -47,0 +46,0 @@ return;

@@ -28,5 +28,2 @@ var CONSTANTS = require('../../constants');

// 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.

@@ -33,0 +30,0 @@ datasetManager.create(datasetItemToCreate).then(function(createdDatasetItem) {

@@ -5,3 +5,2 @@ var _ = require('lodash');

var MediatorTopicUtility = require('fh-wfm-mediator/lib/topics');
var syncEventHandler = require('./sync-events');

@@ -48,6 +47,2 @@ var topicHandlers = {

_.each(CONSTANTS.SYNC_TOPICS, function(syncTopicName) {
datasetSyncSubscribers.on(syncTopicName, syncEventHandler(datasetManager));
});
//For each remote error, publish a sync error topic

@@ -54,0 +49,0 @@ //This can be subscribed to by other modules. (Generally the module that created the manager)

@@ -5,6 +5,6 @@ var mediator = require("fh-wfm-mediator/lib/mediator");

var chai = require('chai');
var expect = chai.expect;
var _ = require('lodash');
var q = require('q');
var shortid = require('shortid');
var expect = chai.expect;
var MediatorTopicUtility = require('fh-wfm-mediator/lib/topics');

@@ -42,13 +42,6 @@ var CONSTANTS = require('../../constants');

var arrayOfDataItems = [mockDataItem];
var mockMetaData = {
syncEvents: {
mockdataitemid: {
message: 'mock message'
}
}
};
var mockDatasetManager = {
datasetId: mockDatasetId,
list: sinon.stub().resolves(arrayOfDataItems),
getMetaData: sinon.stub().callsArgWith(0, mockMetaData)
list: sinon.stub().resolves(arrayOfDataItems)
};

@@ -93,22 +86,2 @@

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) {
expect(data).to.be.an('array');
expect(data[0].id).to.equal(mockDataItem.id);
expect(data[0]._syncStatus).to.deep.equal(mockMetaData.syncEvents.mockdataitemid);
});
});
});
});

@@ -122,12 +95,5 @@

var mockMetaData = {
syncEvents: {
mockSyncEv1: {}
}
};
var mockDatasetManager = {
datasetId: mockDatasetId,
list: sinon.stub().rejects(expectedTopicError),
getMetaData: sinon.stub().callsArgOnWith(0, mockMetaData)
list: sinon.stub().rejects(expectedTopicError)
};

@@ -171,3 +137,2 @@

});
});

@@ -24,22 +24,16 @@ var CONSTANTS = require('../../constants');

//Creating the item in the sync store
datasetManager.list().then(function(arrayOfDatasetItems) {
var listDoneTopic = syncDatasetTopics.getTopic(CONSTANTS.TOPICS.LIST, CONSTANTS.DONE_PREFIX, parameters.topicUid);
datasetManager.getMetaData(function(metadata) {
datasetManager.list().then(function(arrayOfDatasetItems) {
var listDoneTopic = syncDatasetTopics.getTopic(CONSTANTS.TOPICS.LIST, CONSTANTS.DONE_PREFIX, parameters.topicUid);
self.mediator.publish(listDoneTopic, arrayOfDatasetItems);
if (metadata.syncEvents) {
arrayOfDatasetItems.forEach(function(item) {
item._syncStatus = metadata.syncEvents[item.id];
});
}
self.mediator.publish(listDoneTopic, arrayOfDatasetItems);
}).catch(function(error) {
var listErrorTopic = syncDatasetTopics.getTopic(CONSTANTS.TOPICS.LIST, CONSTANTS.ERROR_PREFIX, parameters.topicUid);
}).catch(function(error) {
self.mediator.publish(listErrorTopic, error);
});
var listErrorTopic = syncDatasetTopics.getTopic(CONSTANTS.TOPICS.LIST, CONSTANTS.ERROR_PREFIX, parameters.topicUid);
self.mediator.publish(listErrorTopic, error);
});
};
};

@@ -40,7 +40,6 @@ var mediator = require("fh-wfm-mediator/lib/mediator");

var doneTopic = "done:wfm:sync:mockdatasetid:read";
var mockMetaData = {};
var mockDatasetManager = {
datasetId: "mockdatasetid",
read: sinon.stub().resolves(mockDataItem),
getMetaData: sinon.stub().callsArgWith(0, mockMetaData)
read: sinon.stub().resolves(mockDataItem)
};

@@ -95,3 +94,2 @@

var errorTopic = "error:wfm:sync:mockdatasetid:read";
var mockMetaData = {};

@@ -107,4 +105,3 @@ function checkMocks(topicError) {

datasetId: "mockdatasetid",
read: sinon.stub().rejects(expectedTopicError),
getMetaData: sinon.stub().callsArgWith(0, mockMetaData)
read: sinon.stub().rejects(expectedTopicError)
};

@@ -147,93 +144,3 @@

describe("Sync Event", function() {
var doneTopic = "done:wfm:sync:mockdatasetid:read";
var mockMetaData = {
syncEvents: {
mockdataitemid: {
entityId: "mockdataitemid",
code: "mockEventCode",
action: "mockEventAction",
message: "mockEventMessage",
type: "mockEventType",
ts: sinon.match.number
}
}
};
var mockDatasetManager;
it("should add a sync status to the item read if there is any available", function() {
var testReadResultWithEvents = {
id: mockDataItem.id,
name: mockDataItem.name,
_syncStatus: mockMetaData.syncEvents[mockDataItem.id]
};
mockDatasetManager = {
datasetId: "mockdatasetid",
read: sinon.stub().resolves(mockDataItem),
getMetaData: sinon.stub().callsArgWith(0, mockMetaData)
};
mockDatasetManager.read.reset();
syncSubscribers.on(CONSTANTS.TOPICS.READ, require('./read')(syncSubscribers, mockDatasetManager));
var testDeferred = q.defer();
this.subscribers[doneTopic] = mediator.subscribe(doneTopic, testDeferred.resolve);
mediator.publish(readTopic, {
id: mockDataItem.id
});
return testDeferred.promise.then(function(readResult) {
sinon.assert.calledOnce(mockDatasetManager.read);
sinon.assert.calledOnce(mockDatasetManager.getMetaData);
sinon.assert.calledWith(mockDatasetManager.getMetaData, sinon.match.func);
sinon.assert.calledWith(mockDatasetManager.read, sinon.match(mockDataItem.id));
expect(readResult).to.deep.equal(testReadResultWithEvents);
});
});
it("should not add a sync status to the item read if no sync events exist for that item", function() {
var mockDataItemNoEvents = {
id: "mockDataItemNoEvents",
name: "This is a mock data item with no sync events"
};
var testReadResultNoEvents = {
id: mockDataItemNoEvents.id,
name: mockDataItemNoEvents.name,
_syncStatus: mockMetaData.syncEvents[mockDataItemNoEvents.id]
};
mockDatasetManager = {
datasetId: "mockdatasetid",
read: sinon.stub().resolves(mockDataItemNoEvents),
getMetaData: sinon.stub().callsArgWith(0, mockMetaData)
};
mockDatasetManager.read.reset();
syncSubscribers.on(CONSTANTS.TOPICS.READ, require('./read')(syncSubscribers, mockDatasetManager));
var testDeferred = q.defer();
this.subscribers[doneTopic] = mediator.subscribe(doneTopic, testDeferred.resolve);
mediator.publish(readTopic, {
id: mockDataItemNoEvents.id
});
return testDeferred.promise.then(function(readResult) {
sinon.assert.calledOnce(mockDatasetManager.read);
sinon.assert.calledOnce(mockDatasetManager.getMetaData);
sinon.assert.calledWith(mockDatasetManager.getMetaData, sinon.match.func);
sinon.assert.calledWith(mockDatasetManager.read, sinon.match(mockDataItemNoEvents.id));
expect(readResult).to.deep.equal(testReadResultNoEvents);
});
});
});
});

@@ -26,21 +26,15 @@ var CONSTANTS = require('../../constants');

//Creating the item in the sync store
datasetManager.getMetaData(function(metadata) {
datasetManager.read(parameters.id).then(function(itemRead) {
var readDoneTopic = syncDatasetTopics.getTopic(CONSTANTS.TOPICS.READ, CONSTANTS.DONE_PREFIX, parameters.topicUid);
if (metadata.syncEvents) {
itemRead._syncStatus = metadata.syncEvents[itemRead.id];
}
self.mediator.publish(readDoneTopic, itemRead);
datasetManager.read(parameters.id).then(function(itemRead) {
var readDoneTopic = syncDatasetTopics.getTopic(CONSTANTS.TOPICS.READ, CONSTANTS.DONE_PREFIX, parameters.topicUid);
}).catch(function(error) {
self.mediator.publish(readDoneTopic, itemRead);
var readErrorTopic = syncDatasetTopics.getTopic(CONSTANTS.TOPICS.READ, CONSTANTS.ERROR_PREFIX, parameters.topicUid);
}).catch(function(error) {
self.mediator.publish(readErrorTopic, error);
});
var readErrorTopic = syncDatasetTopics.getTopic(CONSTANTS.TOPICS.READ, CONSTANTS.ERROR_PREFIX, parameters.topicUid);
self.mediator.publish(readErrorTopic, error);
});
};
};

@@ -26,9 +26,4 @@ var CONSTANTS = require('../../constants');

var datasetItemToUpdate = parameters.itemToUpdate;
// remove _syncStatus, it can cause sync loop when updated and this data is irrelevant outside client
delete datasetItemToUpdate._syncStatus;
//Creating the item in the sync store
datasetManager.update(datasetItemToUpdate).then(function(updatedDataSetItem) {
datasetManager.update(parameters.itemToUpdate).then(function(updatedDataSetItem) {
var creatDoneTopic = syncDatasetTopics.getTopic(CONSTANTS.TOPICS.UPDATE, CONSTANTS.DONE_PREFIX, parameters.topicUid);

@@ -35,0 +30,0 @@

@@ -13,11 +13,5 @@ module.exports = {

START: "start",
STOP: "stop"
},
SYNC_TOPICS:{
COLLISION_DETECTED: "collision_detected",
REMOTE_UPDATE_APPLIED: "remote_update_applied",
REMOTE_UPDATE_FAILED: "remote_update_failed",
SYNC_COMPLETE: "sync_complete",
STOP: "stop",
FORCE_SYNC: "force_sync"
}
};

@@ -6,7 +6,6 @@ 'use strict';

var _ = require('lodash');
var debug = require('./utils/logger')(__filename);
function initSync(mediator, mbaasApi, datasetId, syncOptions) {
syncOptions = syncOptions || defaultConfig.syncOptions;
debug('Sync init');
var dataListHandler = function(datasetId, queryParams, cb) {

@@ -21,3 +20,3 @@ mediator.request('wfm:cloud:' + datasetId + ':list', queryParams, {uid: null, timeout: 5000})

}, function(error) {
debug('Sync error: init:', datasetId, error);
console.log('Sync error: init:', datasetId, error);
cb(error);

@@ -37,3 +36,3 @@ });

}, function(error) {
debug('Sync error: init:', datasetId, error);
console.log('Sync error: init:', datasetId, error);
cb(error);

@@ -48,3 +47,3 @@ });

}, function(error) {
debug('Sync error: init:', datasetId, error);
console.log('Sync error: init:', datasetId, error);
cb(error);

@@ -59,3 +58,3 @@ });

}, function(error) {
debug('Sync error: init:', datasetId, error);
console.log('Sync error: init:', datasetId, error);
cb(error);

@@ -70,3 +69,3 @@ });

}, function(error) {
debug('Sync error: init:', datasetId, error);
console.log('Sync error: init:', datasetId, error);
cb(error);

@@ -82,3 +81,3 @@ });

if (err) {
debug('Sync error: init:', datasetId, err);
console.log('Sync error: init:', datasetId, err);
deferred.reject(err);

@@ -96,2 +95,8 @@ } else {

}
//Set optional custom hash function to deal with detecting model changes.
if (_.isFunction(syncOptions.hashFunction)) {
mbaasApi.sync.handleHash(datasetId, syncOptions.hashFunction);
}
deferred.resolve(datasetId);

@@ -98,0 +103,0 @@ }

{
"name": "fh-wfm-sync",
"version": "0.1.3-alpha.504.5",
"version": "0.2.0-pre.1",
"description": "An sync module for WFM",

@@ -24,3 +24,2 @@ "main": "lib/angular/sync-ng.js",

"dependencies": {
"debug": "^2.6.3",
"fh-wfm-mediator": "0.3.2",

@@ -27,0 +26,0 @@ "lodash": "4.7.0",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc