Skip to content
This repository has been archived by the owner on Sep 30, 2023. It is now read-only.

Commit

Permalink
add refs property to Entry
Browse files Browse the repository at this point in the history
add tests
  • Loading branch information
shamb0t committed Nov 6, 2019
1 parent d2c8c80 commit e4a2bd6
Show file tree
Hide file tree
Showing 18 changed files with 1,084 additions and 716 deletions.
10 changes: 5 additions & 5 deletions dist/ipfslog.min.js

Large diffs are not rendered by default.

47 changes: 34 additions & 13 deletions lib/es5/entry-io.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,20 +134,41 @@ function () {
value: function () {
var _fetchAll = (0, _asyncToGenerator2.default)(
/*#__PURE__*/
_regenerator.default.mark(function _callee3(ipfs, hashes, _ref2) {
var _ref2$length, length, _ref2$exclude, exclude, _ref2$timeout, timeout, onProgressCallback, result, cache, loadingQueue, addToLoadingQueue, addToExcludeCache, shouldFetchMore, fetchEntry;
_regenerator.default.mark(function _callee3(ipfs, hashes) {
var _ref2,
_ref2$length,
length,
_ref2$exclude,
exclude,
_ref2$timeout,
timeout,
onProgressCallback,
_ref2$concurrency,
concurrency,
result,
cache,
loadingQueue,
addToLoadingQueue,
shouldFetchMore,
addToExcludeCache,
fetchEntry,
_args3 = arguments;

return _regenerator.default.wrap(function _callee3$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
_ref2$length = _ref2.length, length = _ref2$length === void 0 ? -1 : _ref2$length, _ref2$exclude = _ref2.exclude, exclude = _ref2$exclude === void 0 ? [] : _ref2$exclude, _ref2$timeout = _ref2.timeout, timeout = _ref2$timeout === void 0 ? null : _ref2$timeout, onProgressCallback = _ref2.onProgressCallback;
_ref2 = _args3.length > 2 && _args3[2] !== undefined ? _args3[2] : {}, _ref2$length = _ref2.length, length = _ref2$length === void 0 ? -1 : _ref2$length, _ref2$exclude = _ref2.exclude, exclude = _ref2$exclude === void 0 ? [] : _ref2$exclude, _ref2$timeout = _ref2.timeout, timeout = _ref2$timeout === void 0 ? null : _ref2$timeout, onProgressCallback = _ref2.onProgressCallback, _ref2$concurrency = _ref2.concurrency, concurrency = _ref2$concurrency === void 0 ? 32 : _ref2$concurrency;
result = [];
cache = {};
loadingQueue = Array.isArray(hashes) ? hashes.slice() : [hashes]; // Add a hash to the loading queue

addToLoadingQueue = function addToLoadingQueue(e) {
return loadingQueue.push(e);
};

shouldFetchMore = function shouldFetchMore() {
return loadingQueue.length > 0 && (result.length < length || length < 0);
}; // Add entries that we don't need to fetch to the "cache"


Expand All @@ -162,14 +183,10 @@ function () {

exclude.forEach(addToExcludeCache);

shouldFetchMore = function shouldFetchMore() {
return loadingQueue.length > 0 && (result.length < length || length < 0);
};

fetchEntry = function fetchEntry() {
var hash = loadingQueue.shift();

if (cache[hash]) {
if (!hash || cache[hash]) {
return Promise.resolve();
}

Expand All @@ -194,14 +211,15 @@ function () {
addToResults = function addToResults(entry) {
if (Entry.isEntry(entry)) {
entry.next.forEach(addToLoadingQueue);
entry.refs.forEach(addToLoadingQueue);
result.push(entry);
cache[hash] = entry;

if (onProgressCallback) {
onProgressCallback(hash, entry, result.length);
onProgressCallback(hash, entry, result.length, result, loadingQueue);
}
}
}; // Load the entry
}; // // Load the entry


_context2.prev = 2;
Expand Down Expand Up @@ -233,7 +251,7 @@ function () {
}, _callee2, null, [[2, 10, 13, 16]]);
}));

return function (_x6, _x7) {
return function (_x5, _x6) {
return _ref3.apply(this, arguments);
};
}());
Expand All @@ -243,17 +261,20 @@ function () {
return pWhilst(shouldFetchMore, fetchEntry);

case 12:
// Free memory to avoid minor GC
cache = {};
loadingQueue = [];
return _context3.abrupt("return", result);

case 13:
case 15:
case "end":
return _context3.stop();
}
}
}, _callee3);
}));

function fetchAll(_x3, _x4, _x5) {
function fetchAll(_x3, _x4) {
return _fetchAll.apply(this, arguments);
}

Expand Down
36 changes: 21 additions & 15 deletions lib/es5/entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ function () {
_regenerator.default.mark(function _callee(ipfs, identity, logId, data) {
var next,
clock,
refs,
toEntry,
nexts,
entry,
Expand All @@ -70,47 +71,48 @@ function () {
case 0:
next = _args.length > 4 && _args[4] !== undefined ? _args[4] : [];
clock = _args.length > 5 ? _args[5] : undefined;
refs = _args.length > 6 && _args[6] !== undefined ? _args[6] : [];

if (isDefined(ipfs)) {
_context.next = 4;
_context.next = 5;
break;
}

throw IpfsNotDefinedError();

case 4:
case 5:
if (isDefined(identity)) {
_context.next = 6;
_context.next = 7;
break;
}

throw new Error('Identity is required, cannot create entry');

case 6:
case 7:
if (isDefined(logId)) {
_context.next = 8;
_context.next = 9;
break;
}

throw new Error('Entry requires an id');

case 8:
case 9:
if (isDefined(data)) {
_context.next = 10;
_context.next = 11;
break;
}

throw new Error('Entry requires data');

case 10:
case 11:
if (!(!isDefined(next) || !Array.isArray(next))) {
_context.next = 12;
_context.next = 13;
break;
}

throw new Error("'next' argument is not an array");

case 12:
case 13:
// Clean the next objects and convert to hashes
toEntry = function toEntry(e) {
return e.hash ? e.hash : e;
Expand All @@ -126,26 +128,27 @@ function () {
// Can be any JSON.stringifyable data
next: nexts,
// Array of hashes
refs: refs,
v: 1,
// To tag the version of this data structure
clock: clock || new Clock(identity.publicKey)
};
_context.next = 17;
_context.next = 18;
return identity.provider.sign(identity, Entry.toBuffer(entry));

case 17:
case 18:
signature = _context.sent;
entry.key = identity.publicKey;
entry.identity = identity.toJSON();
entry.sig = signature;
_context.next = 23;
_context.next = 24;
return Entry.toMultihash(ipfs, entry);

case 23:
case 24:
entry.hash = _context.sent;
return _context.abrupt("return", entry);

case 25:
case 26:
case "end":
return _context.stop();
}
Expand Down Expand Up @@ -215,6 +218,7 @@ function () {
id: entry.id,
payload: entry.payload,
next: entry.next,
refs: entry.refs,
v: entry.v,
clock: entry.clock
};
Expand Down Expand Up @@ -291,6 +295,7 @@ function () {
id: entry.id,
payload: entry.payload,
next: entry.next,
refs: entry.refs,
v: entry.v,
clock: entry.clock
};
Expand Down Expand Up @@ -371,6 +376,7 @@ function () {
id: e.id,
payload: e.payload,
next: e.next,
refs: e.refs,
v: e.v,
clock: new Clock(e.clock.id, e.clock.time)
};
Expand Down
Loading

0 comments on commit e4a2bd6

Please sign in to comment.