-
Notifications
You must be signed in to change notification settings - Fork 8
Add functionality to copy feed and append to it. #38
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
Open
benjaminettori
wants to merge
3
commits into
CodeForPhilly:master
Choose a base branch
from
benjaminettori:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this might append blocks in the wrong order as this is async. you probably wanna do this