-
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.
+234
−2
Open
Changes from all commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,5 +4,4 @@ node_js: | |
- "4.0" | ||
- "0.12" | ||
- "0.11" | ||
- "0.10" | ||
- "iojs" |
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,80 @@ | ||
module.exports = FeedOperations | ||
|
||
function FeedOperations (core) { | ||
this.core = core | ||
this.replicate = copyOriginal | ||
|
||
function copyOriginal (refFeed, newFeed, resolve, reject) { | ||
loop(null) | ||
|
||
function loop (err) { | ||
if (err) { | ||
console.log(err) | ||
if (reject) { | ||
return reject() | ||
} | ||
} | ||
|
||
refFeed.get(newFeed.blocks, function (err, block) { | ||
if (err) { | ||
console.log(err) | ||
} | ||
|
||
if (!block) { | ||
return resolve() | ||
} | ||
|
||
newFeed.append(block, loop) | ||
}) | ||
} | ||
} | ||
} | ||
|
||
FeedOperations.prototype.appendableFeed = function (feedId) { | ||
var refFeed = this.core.get(feedId) | ||
var newFeed = this.core.add() | ||
|
||
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) | ||
}) | ||
} | ||
|
||
var copyOriginal = this.replicate | ||
|
||
newFeed.initialize = function () { | ||
return new Promise(function (resolve, reject) { | ||
copyOriginal(refFeed, newFeed, resolve, reject) | ||
}) | ||
} | ||
|
||
return newFeed | ||
} | ||
|
||
FeedOperations.prototype.replicateFeed = function (feedId, callback) { | ||
var refFeed = this.core.get(feedId) | ||
var newFeed = this.core.add() | ||
|
||
this.replicate(refFeed, newFeed, callback) | ||
return newFeed | ||
} | ||
|
||
// Copies data from reference feed, appends data to new feed and finalizes it | ||
FeedOperations.prototype.append = function (feedId, data, callback) { | ||
var refFeed = this.core.get(feedId) | ||
var newFeed = this.core.add() | ||
|
||
this.replicate(refFeed, newFeed, function () { | ||
newFeed.append(data, function () { | ||
newFeed.finalize(callback) | ||
}) | ||
}) | ||
|
||
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,147 @@ | ||
var test = require('tape') | ||
var Jawn = require('../') | ||
var memdb = require('memdb') | ||
var FeedOps = require('../lib/feedOperations.js') | ||
|
||
test('Create appendable feed and copy data from reference feed', function (t) { | ||
var jawn = freshJawn() | ||
var feed = jawn.core.add() | ||
|
||
storeDataInFeed(feed, ['hello']) | ||
.then(function () { | ||
// Verify feed was finalized and has an id | ||
console.log('Finalized with id ' + feed.id.toString('hex')) | ||
// Create appendable feed | ||
var feedOperations = new FeedOps(jawn.core) | ||
var appfeed = feedOperations.appendableFeed(feed.id) | ||
// Copy data from reference feed, finalize it, then verify the feed has the same number of blocks as the reference | ||
appfeed.initialize().then(function () { | ||
return appfeed.finalize_p() | ||
}) | ||
.then(function () { | ||
t.same(appfeed.blocks, feed.blocks, 'testing') | ||
t.end() | ||
}) | ||
}) | ||
}) | ||
|
||
test('Create appendable feeed, copy data from reference feed and 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 = new FeedOps(jawn.core).appendableFeed(feed.id) | ||
|
||
// Copy data from original feed with initialize(), then append an additional block and finalize | ||
appfeed.initialize().then(function () { | ||
return appfeed.append_p('goodbye') | ||
}) | ||
.then(function () { | ||
return appfeed.finalize_p() | ||
}) | ||
.then(function () { | ||
// Verify the feed has the correct number of blocks, and the blocks match what was expected | ||
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() | ||
} | ||
}) | ||
} | ||
}) | ||
}) | ||
}) | ||
|
||
test('Create appendable feed without promises, copy data from reference feed', function (t) { | ||
var jawn = freshJawn() | ||
var feed = jawn.core.add() | ||
|
||
storeDataInFeed(feed, ['hello', 'there']) | ||
.then(function () { | ||
var appFeed = new FeedOps(jawn.core).replicateFeed(feed.id, finalizeCallback) | ||
function finalizeCallback () { | ||
appFeed.finalize(function () { | ||
t.same(appFeed.blocks, feed.blocks, 'Correct number of blocks') | ||
t.end() | ||
}) | ||
} | ||
}) | ||
}) | ||
|
||
test('Append data to feed without promises', function (t) { | ||
var jawn = freshJawn() | ||
var feed = jawn.core.add() | ||
var expected = ['hello', 'there', 'goodbye'] | ||
|
||
storeDataInFeed(feed, ['hello', 'there']) | ||
.then(function () { | ||
var appFeed = new FeedOps(jawn.core).append(feed.id, ['goodbye'], testCallback) | ||
function testCallback () { | ||
t.same(appFeed.blocks, expected.length, 'Correct number of blocks in appended feed') | ||
testFeedContents(appFeed, expected, t) | ||
} | ||
}) | ||
}) | ||
|
||
function testFeedContents (feed, expected, t) { | ||
var blockNumber = 0 | ||
feed.get(0, loop) | ||
|
||
function loop (err, block) { | ||
if (err) { | ||
console.log(err) | ||
} | ||
|
||
blockNumber += 1 | ||
|
||
if (block) { | ||
t.same(block.toString(), expected.shift(), 'Feed block matches expected value') | ||
} | ||
|
||
if (expected.length === 0) { | ||
return t.end() | ||
} | ||
|
||
feed.get(blockNumber, loop) | ||
} | ||
} | ||
|
||
// Create a feed, store data in it and finalize it. | ||
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