Skip to content
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

Add callback argument to applyPatches patched option #130

Merged
merged 2 commits into from
Aug 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ or
This method will iterate over the contents of the patch and apply to data provided through callbacks. The general flow for each patch index is:

- `options.loadFile(index, callback)` is called. The caller should then load the contents of the file and then pass that to the `callback(err, data)` callback. Passing an `err` will terminate further patch execution.
- `options.patched(index, content)` is called once the patch has been applied. `content` will be the return value from `applyPatch`.
- `options.patched(index, content, callback)` is called once the patch has been applied. `content` will be the return value from `applyPatch`. When it's ready, the caller should call `callback(err)` callback. Passing an `err` will terminate further patch execution.

Once all patches have been applied or an error occurs, the `options.complete(err)` callback is made.

Expand Down
8 changes: 6 additions & 2 deletions src/patch/apply.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,13 @@ export function applyPatches(uniDiff, options) {
}

let updatedContent = applyPatch(data, index, options);
options.patched(index, updatedContent);
options.patched(index, updatedContent, function(err) {
if (err) {
return options.complete(err);
}

setTimeout(processIndex, 0);
processIndex();
});
});
}
processIndex();
Expand Down
36 changes: 32 additions & 4 deletions test/patch/apply.js
Original file line number Diff line number Diff line change
Expand Up @@ -534,15 +534,37 @@ describe('patch/apply', function() {
+ 'foo5\n'
};

it('should handle errors on complete', function(done) {
const expected = new Error();

applyPatches(patch, {
loadFile(index, callback) {
callback(undefined, contents[index.index]);
},
patched(index, content, callback) {
callback(expected);
},
complete(err) {
expect(err)
.to.equal(expected)
.to.not.be.undefined;

done();
}
});
});

it('should handle multiple files', function(done) {
applyPatches(patch, {
loadFile(index, callback) {
callback(undefined, contents[index.index]);
},
patched(index, content) {
patched(index, content, callback) {
expect(content)
.to.equal(expected[index.index])
.to.not.be.undefined;

callback();
},
complete: done
});
Expand All @@ -552,10 +574,12 @@ describe('patch/apply', function() {
loadFile(index, callback) {
callback(undefined, contents[index.index]);
},
patched(index, content) {
patched(index, content, callback) {
expect(content)
.to.equal(expected[index.index])
.to.not.be.undefined;

callback();
},
complete: done
});
Expand Down Expand Up @@ -594,10 +618,12 @@ describe('patch/apply', function() {
loadFile(index, callback) {
callback(undefined, contents[index.oldFileName]);
},
patched(index, content) {
patched(index, content, callback) {
expect(content)
.to.equal(expected[index.newFileName])
.to.not.be.undefined;

callback();
},
complete: done
});
Expand Down Expand Up @@ -649,10 +675,12 @@ foo3
loadFile(index, callback) {
callback(undefined, contents[index.oldFileName]);
},
patched(index, content) {
patched(index, content, callback) {
expect(content)
.to.equal(expected[index.newFileName])
.to.not.be.undefined;

callback();
},
complete: done
});
Expand Down