Skip to content

Commit

Permalink
setup fetch-all once per jsonrpc
Browse files Browse the repository at this point in the history
  • Loading branch information
lipp committed Jun 12, 2015
1 parent 4a18186 commit c881586
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions lib/jet/peer/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,24 +83,25 @@ var FakeFetcher = function (jsonrpc, fetchParams, fetchCb) {
});
};

if (FakeFetcher.elements === undefined) {
FakeFetcher.elements = new Elements();
FakeFetcher.peer = new FakePeer();
if (jsonrpc.fakeContext === undefined) {
var context = jsonrpc.fakeContext = {};
context.elements = new Elements();
context.peer = new FakePeer();

var fetchSimpleDispatcher = function (message) {
var params = message.params;
var event = params.event;

if (event === 'remove') {
fetchCommon.removeCore(FakeFetcher.peer, FakeFetcher.elements, params);
fetchCommon.removeCore(context.peer, context.elements, params);
} else if (event === 'add') {
fetchCommon.addCore(FakeFetcher.peer, FakeFetcher.peer.eachFetcher, FakeFetcher.elements, params);
fetchCommon.addCore(context.peer, context.peer.eachFetcher, context.elements, params);
} else {
fetchCommon.changeCore(FakeFetcher.peer, FakeFetcher.elements, params);
fetchCommon.changeCore(context.peer, context.elements, params);
}
};

FakeFetcher.fetchAllPromise = new Bluebird(function (resolve, reject) {
context.fetchAllPromise = new Bluebird(function (resolve, reject) {
jsonrpc.service('fetch', {}, function (ok, fetchSimpleId) {
jsonrpc.addRequestDispatcher(fetchSimpleId, fetchSimpleDispatcher);
}).then(function () {
Expand All @@ -110,19 +111,22 @@ var FakeFetcher = function (jsonrpc, fetchParams, fetchCb) {
}

this.fetch = function () {
return FakeFetcher.fetchAllPromise.then(function () {
return fetchCommon.fetchCore(FakeFetcher.peer, FakeFetcher.elements, fetchParams, wrappedFetchDispatcher);
var context = jsonrpc.fakeContext;
return context.fetchAllPromise.then(function () {
return fetchCommon.fetchCore(context.peer, context.elements, fetchParams, wrappedFetchDispatcher);
});
};

this.unfetch = function () {
return FakeFetcher.fetchAllPromise.then(function () {
return fetchCommon.unfetchCore(FakeFetcher.peer, FakeFetcher.elements, fetchParams);
var context = jsonrpc.fakeContext;
return context.fetchAllPromise.then(function () {
return fetchCommon.unfetchCore(context.peer, context.elements, fetchParams);
});
};

this.isFetching = function () {
return FakeFetcher.peer.hasFetcher(fetchParams.id);
var context = jsonrpc.fakeContext;
return context.peer.hasFetcher(fetchParams.id);
};

};
Expand Down

0 comments on commit c881586

Please sign in to comment.