-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add functionality to copy feed and append to it.
Change module export name. Squash commits for appending feed
- Loading branch information
Showing
4 changed files
with
158 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
|
||
module.exports.appendTo = appendableFeed | ||
|
||
function appendableFeed (core, feedId) { | ||
var refFeed = core.get(feedId) | ||
var newFeed = core.add() | ||
var blockNumber = 0 | ||
|
||
var copyOriginal = function (resolve, reject) { | ||
for (var i = 0; i < refFeed.blocks; i++) { | ||
refFeed.get(i, function (err, block) { | ||
if (err) { | ||
console.log(err) | ||
reject() | ||
} | ||
|
||
newFeed.append(block, function () { | ||
blockNumber++ | ||
console.log('Copying block ' + blockNumber + '/' + newFeed.blocks + ' from original feed') | ||
|
||
if (blockNumber === refFeed.blocks) { | ||
resolve() | ||
} | ||
}) | ||
}) | ||
} | ||
} | ||
|
||
newFeed.append_p = function (data) { | ||
return new Promise(function (resolve, reject) { | ||
newFeed.append(data, resolve) | ||
}) | ||
} | ||
|
||
newFeed.finalize_p = function () { | ||
return new Promise(function (resolve, reject) { | ||
newFeed.finalize(resolve) | ||
}) | ||
} | ||
|
||
newFeed.initialize = function () { | ||
return new Promise(function (resolve, reject) { | ||
copyOriginal(resolve, reject) | ||
}) | ||
} | ||
|
||
return newFeed | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
var test = require('tape') | ||
var Jawn = require('../') | ||
var memdb = require('memdb') | ||
var feedOps = require('../lib/feedOperations.js') | ||
|
||
test('appendable feed', function (t) { | ||
var jawn = freshJawn() | ||
var feed = jawn.core.add() | ||
|
||
feed.pappend = function (data) { | ||
return new Promise(function (resolve, reject) { | ||
feed.append(data, resolve) | ||
}) | ||
} | ||
|
||
feed.pfinalize = function () { | ||
return new Promise(function (resolve, reject) { | ||
feed.finalize(resolve) | ||
}) | ||
} | ||
|
||
feed.pappend('hello').then(function () { | ||
console.log('Appended') | ||
return feed | ||
}) | ||
.then(function (feed) { | ||
return feed.pfinalize() | ||
}) | ||
.then(function () { | ||
console.log('Finalized with id ' + feed.id.toString('hex')) | ||
var appfeed = feedOps.appendTo(jawn.core, feed.id) | ||
appfeed.initialize().then(function () { | ||
return appfeed.finalize_p() | ||
}) | ||
.then(function () { | ||
t.same(appfeed.blocks, feed.blocks, 'testing') | ||
t.end() | ||
}) | ||
}) | ||
}) | ||
|
||
test('append to feed', function (t) { | ||
var jawn = freshJawn() | ||
var feed = jawn.core.add() | ||
var expected = ['hello', 'there', 'goodbye'] | ||
|
||
storeDataInFeed(feed, ['hello', 'there']) | ||
.then(function () { | ||
console.log('Finalized with id ' + feed.id.toString('hex')) | ||
|
||
var appfeed = feedOps.appendTo(jawn.core, feed.id) | ||
|
||
appfeed.initialize().then(function () { | ||
return appfeed.append_p('goodbye') | ||
}) | ||
.then(function () { | ||
return appfeed.finalize_p() | ||
}) | ||
.then(function () { | ||
t.same(appfeed.blocks, expected.length, 'Correct number of blocks') | ||
for (var i = 0; i < appfeed.blocks; i++) { | ||
appfeed.get(i, function (err, block) { | ||
if (err) { | ||
console.log(err) | ||
} | ||
t.same(block.toString(), expected.shift(), 'Feed block match') | ||
if (expected.length === 0) { | ||
t.end() | ||
} | ||
}) | ||
} | ||
}) | ||
}) | ||
}) | ||
|
||
function storeDataInFeed (feed, data) { | ||
feed.pappend = function (data) { | ||
return new Promise(function (resolve, reject) { | ||
feed.append(data, resolve) | ||
}) | ||
} | ||
|
||
feed.pfinalize = function () { | ||
return new Promise(function (resolve, reject) { | ||
feed.finalize(resolve) | ||
}) | ||
} | ||
|
||
return new Promise(function (resolve, reject) { | ||
feed.pappend(data).then(function () { | ||
console.log('Appended') | ||
return feed | ||
}) | ||
.then(function (feed) { | ||
return feed.pfinalize() | ||
}) | ||
.then(resolve) | ||
}) | ||
} | ||
|
||
function freshJawn () { | ||
return new Jawn({db: memdb()}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters