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

Use promises instead of callbacks #16

Merged
merged 6 commits into from
Jan 3, 2018
Merged

Use promises instead of callbacks #16

merged 6 commits into from
Jan 3, 2018

Conversation

LaurensRietveld
Copy link
Collaborator

What changed:

  • Callbacks are converted to (or wrapped as) Promises
  • Often multiple arguments were given in the callback function (e.g. triples, totalCount, hasExactCount). As promises should return only one value, I wrapped these in an object.
  • Changed some var keywords to const if applicable
  • Modified eslint config to target es6, and to recognize promises as globals
  • Removed the self reference that were bound to the callbacks. Don't believe there is much need for them anymore with arrow functions, promises, and (on more recent versions of nodejs) async/await.

I planned on transforming the prototype functions to a regular es6 class, but that won't do us any good as we shouldnt create our own class, but simply extend the native class by assiging extra functions to its prototype.

@LaurensRietveld LaurensRietveld changed the base branch from master to develop December 22, 2017 07:40
Copy link
Member

@RubenVerborgh RubenVerborgh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, only formatting nitpicks (and one suspected bug in fromFile).

bin/hdt Outdated
});

},
(error) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No parentheses needed (consistency)

@@ -54,7 +54,7 @@ const Nan::Persistent<Function>& HdtDocument::GetConstructor() {
Nan::SetPrototypeMethod(constructorTemplate, "_searchTriples", SearchTriples);
Nan::SetPrototypeMethod(constructorTemplate, "_searchLiterals", SearchLiterals);
Nan::SetPrototypeMethod(constructorTemplate, "_searchTerms", SearchTerms);
Nan::SetPrototypeMethod(constructorTemplate, "close", Close);
Nan::SetPrototypeMethod(constructorTemplate, "_close", Close);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alignment broken

lib/hdt.js Outdated
triples,
totalCount,
hasExactCount,
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to return; I'd write the above as

if (err)
   reject(err);
else
   resolve({ triples, totalCount, hasExactCount })

lib/hdt.js Outdated
}, self);
HdtDocumentPrototype.countTriples = function (subject, predicate, object) {
return this.search(subject, predicate, object, { offset: 0, limit: 0 })
.then(results => ({ totalCount: results.totalCount, hasExactCount: results.hasExactCount }));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe remove the then part? Doesn't matter that there's an extra triples field (plus, fewer object shapes for V8).

lib/hdt.js Outdated
resolve({
literals,
totalCount,
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Idem as above, without return and resolve on a single line.

lib/hdt.js Outdated
};
HdtDocumentPrototype.close = function () {
return new Promise((resolve, reject) => {
this._close(function (e) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

err for consistency.

lib/hdt.js Outdated
case 'Non-HDT Section':
return reject(new Error('The file "' + filename + '" is not a valid HDT file'));
default:
reject(error);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return missing?
And if so, how did the unit tests miss this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A reject handler from a Promise constructor does not need to be returned, it only needs to be called. Added the return here for consistency

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not about returning a value, but rather about terminating the function right there. Otherwise, execution continues, and that's not what you want. It's not observable in this particular case though, but we reject because we don't want execution to continue.

test/hdt-test.js Outdated
hdt.fromFile(null, function (error) {
this.should.equal(self);
it('should throw an error', function () {
return hdt.fromFile(null).then(() => Promise.reject(new Error('Expected an error')), (error) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Throughout: no parens around error for consistency.

test/hdt-test.js Outdated
});
it('should throw an error', function () {
return document.searchTriples(null, null, null).then(
() => Promise.reject(new Error('Expected an error')),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

() => on line above for consistency.

test/hdt-test.js Outdated
});
it('should throw an error', function () {
return document.searchLiterals('abc').then(
() => Promise.reject(new Error('Expected an error')),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Idem.

@LaurensRietveld
Copy link
Collaborator Author

fixed the style issues per your latest comments

@RubenVerborgh
Copy link
Member

Thanks for fixing!

Given that this will require a new semver major, any other breaking interface changes we might want to consider?

Also, README will still need to be updated.

Copy link
Member

@RubenVerborgh RubenVerborgh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code is perfect. Should still update README to match the code.

@LaurensRietveld
Copy link
Collaborator Author

Updated the readme. Can't think of any breaking changes in the near future. Things like #17 or #9 probably won't change the interface.

RubenVerborgh
RubenVerborgh previously approved these changes Dec 29, 2017
@RubenVerborgh
Copy link
Member

Discussed with @LaurensRietveld that he will add a TypeScript definition for the new interface to this PR. After that, I'll merge and release.

@RubenVerborgh
Copy link
Member

@LaurensRietveld Perfect, thanks!

@LaurensRietveld
Copy link
Collaborator Author

@RubenVerborgh : would you mind if we merge this to develop? Then we can create a new PR for #17 without having to rebase after.

@RubenVerborgh
Copy link
Member

RubenVerborgh commented Jan 3, 2018 via email

@RubenVerborgh
Copy link
Member

I just shortened some commit messages to stay below 50 characters, as long commit titles might lead to tooling problems later on.

@RubenVerborgh
Copy link
Member

Released as v2.0.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants