-
Notifications
You must be signed in to change notification settings - Fork 479
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
{?exists} and {^exists} now supports values from a promise #753
{?exists} and {^exists} now supports values from a promise #753
Conversation
I think this won't quite be right because when you use I think something like this might fail with your change:
For the test runner failure, it's because Node 0.10 doesn't have Promise-- use the "FalsePromise" constructor at the top of the test file. |
You probably want to use It hits this code:
|
@sethkinast thanks for the comments! I got pretty busy yesterday, so I had to switch over to other work. I'm going to take another look. |
Yep no rush! I appreciate you taking the time to work on a patch :) |
…ed {expected} to equal {actual}" rather than vice versa
…errors. Previous implementation had an issue where the context was updated to the resolved promise data which was not intended as sethkinast pointed out.
@sethkinast can you take another look? You are right that using |
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.
Looks good! A little change and then if you'll squash up your commits, I think we'll be ready to go
}); | ||
}); | ||
}; | ||
|
||
/** | ||
* Render an error body if available | ||
* @param thenable {Thenable} the target thenable to await |
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.
wrong param
* @param bodies {Object} may optionally contain an "error" which will be rendered | ||
* @return {Chunk} | ||
*/ | ||
Chunk.prototype.renderError = function(err, context, bodies) { |
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.
Nice, good reusable func
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.
Thanks. Should I move it off of the Chunk object? When I added it originally I wasn't considering that I was updating a public api that's pretty well documented here: http://www.dustjs.com/docs/helper-api/
@@ -861,6 +861,17 @@ | |||
var body = bodies.block, | |||
skip = bodies['else']; | |||
|
|||
if (dust.isThenable(elem)) { |
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.
Could you turn this into a little reusable func that we can use for exists and notexists both? Look above at getWithResolvedData
as an example-- just a little helper func not attached to the Chunk prototype.
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.
by "this" I meant the code inside the if() block that is
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.
The code was definitely redundant between the two methods. I made a mapThenable
method to address it.
…e used when also using a thenable. The mapThenable function helps to resuse code in Chunk#await, Chunk#exists, and Chunk#notexists. It was also updated to capture and log errors similar to how Chunk#map does (seems to be a desired behavior that was missing when using thenables). Also fixed incorrect javadoc param on renderError(...).
@@ -725,6 +725,11 @@ | |||
return this; | |||
}; | |||
|
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.
I added these docs straight from http://www.dustjs.com/docs/helper-api/. Looks like other docs are missing from some chunk methods that are on the helper-api page. I could add these if they aren't considered bloat. This Chunk#map
method is great and the docs really made it's usage clear to me. As I was looking at making the code both smaller and resuable, it seemed apparent that what I really needed was a new map method for thenables, so I created the mapThenable(...)
method. I'd like to put it on the Chunk object, but that would be altering the public API of that and I'm not sure that is desirable. I'm also wondering if I should move the Chunk#renderError
method off of the Chunk prototype for the same reason or if it's simple, generic, and useful enough to keep.
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.
I think you have the right separation. I think mapThenable
is not something that a general public API would necessarily need, but renderError
is nice. We can add it to the docs.
Note that this does slightly alter behavior in that a log is always generated on error rather than the previous logic that would only log an error when an error body was not present.
@@ -861,6 +895,12 @@ | |||
var body = bodies.block, | |||
skip = bodies['else']; | |||
|
|||
if (dust.isThenable(elem)) { | |||
return mapThenable(this, elem, context, bodies, function(chunk, data){ |
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.
Fantastic. Style nit: space between function(chunk, data)
and its brace
Great work! If I can hassle you to fix the brace nit (which I should add to the linter...) we're ready to roll. Also shoutout for writing great function documentation. |
dust.log('Unhandled stream error in `' + context.getTemplateName() + '`', INFO); | ||
} | ||
chunk.renderError(err, context, bodies); | ||
dust.log('Unhandled stream error in `' + context.getTemplateName() + '`', INFO); |
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.
Note that this is a logic difference. Previously dust.log(...)
was only called when there was no render body whereas now it is always logged. I think it's ok.
Thanks again for the PR! 🎆 |
@sethkinast it was my pleasure. It was a lot of fun. Makes me want to contribute more in open source projects. What is the release cycle for these changes? I'd like to start using them as soon as they become available. |
I'm tempted to cut a 2.8, which I didn't plan on before a 3, and spend 3.0 working on an ES6 rewrite. You could use the changes today by putting a commit hash in your |
@sethkinast We don't reference dustjs-linkedin directly or I might consider that. We use hoffman for the expressjs integration it provides. hoffman uses duster and duster uses dustjs-linkedin. The ES6 rewrite sounds like a fun project to work on. Is there a branch for it? |
I have not pushed a public branch for ES6 yet but hopefully soon. Let me see about cutting a 2.8 in the next week or two. |
I logged this as bug #752 - {?exists/} behavior different when using promises.