-
Notifications
You must be signed in to change notification settings - Fork 45
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
Adds currying of user supplied onBeforeElUpdated. #55
Conversation
@@ -8,29 +8,32 @@ module.exports = bel | |||
module.exports.update = function (fromNode, toNode, opts) { | |||
if (!opts) opts = {} | |||
if (opts.events !== false) { |
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.
@shama @maxogden @yoshuawuyts Is this opts.events
check needed?
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.
You can set it to false
to avoid copying any events
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.
Ah, OK.
Was thinking that we could write this so that a person could return early from the onBeforeElUpdated
and skip the events copying.
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.
Turns out this request requires both.
fixes #54 |
@@ -8,29 +8,32 @@ module.exports = bel | |||
module.exports.update = function (fromNode, toNode, opts) { | |||
if (!opts) opts = {} | |||
if (opts.events !== false) { | |||
if (!opts.onBeforeElUpdated) opts.onBeforeElUpdated = copier | |||
if (!opts.onBeforeElUpdated) opts.onBeforeElUpdated = currier(opts.onBeforeElUpdated, opts) |
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 PR is still using the currier only if onBeforeElUpdated
is -not- provided by the user.
Expected:
opts.onBeforeElUpdated = (opts.onBeforeElUpdated) ?
currier(opts.onBeforeElUpdated, opts) :
copier;
The function currier
is also immediately calling the update
function on first assignment, instead of returning a function that calls update
then copier
(provided the update
function has not returned false).
EDIT: The first t.ok
test would pass (incorrectly, if user onBeforeElUpdated
was provided) due to update
being called on the curry rather than the actual element comparison. However I really must be missing something as I can't understand how the other test would be passing, since line 11 should currently never run either currier
or the hidden copier
due to the user onBeforeElUpdated
being supplied..
But I would have expected:
function currier(update, opts){
return function (f, t){
const userUpdatePassed = update(f, t);
if (userUpdatePassed) return copier(f, t);
return false; // or omit if morphdom doesn't care about undefined/false distinction
}
}
function copier(f, t){}
Thanks!
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.
You are right! Fixing.
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 don't understand this use case, whereby you provide your own onBeforeElUpdated
, to determine whether a node should update - but then regardless of the outcome of this (e.g., "no! I don't want this element to update"), the rest of the content of copier
runs regardless, clobbering the element I have specified to preserve.
If my user onBeforeElUpdated
returns false
, then I've already done what I needed to to that node and wouldn't then want events copied over again as this would contradict/go behind my check's "back" 🗡. If it should update though, then by all means copy across the events
while you're at it, since that is the purpose of the yo-yo
module.
The only way I'd see to supporting both is via a flag (i.e., a) I'm providing my own comparison function, which the copier
function should also respect/chain through to; or b) even if I say I don't want the node updated, the copier
function should run anyway). But I really don't see this second combination (as implemented in this PR) alone as solving the issue at hand, or ever really being desired behavior (but if someone else has this as a valid use case then just LMK).
(As discussed in the issue, the third case is when opts.events
is set to false
, in which case there is no further logic needed as the opts
object is just passed through untouched to morphdom
, where the user's onBeforeElUpdated
will be the sole comparator if present.)
Cheers!
Here is the thinking behind adding a currier: A user only wants to run their own onBeforElUpdated and not have
|
Hey, thanks for your work on this.
if (onBeforeElUpdated(fromEl, toEl) === false) {
return;
} ...it ignores the result of this function and continues updating down the tree, but with events now in place. The issue is therefore that Is that any clearer? I appreciate that given how |
Personally, for my case, I don't especially care if events get re-copied, even if I had returned However, at best re-copying is a waste of cycles (again, I don't care about this though), but at worst I do actually have some event I don't want changed which I would lose control over deciding. (NB this is not the same as setting This point is relatively minor - but the main thing is that in your PR, I still cannot stop |
Makes perfect sense. Just wanted to make sure I understood the intention and that it was spelled out appropriately in the PR discussion. Thanks! |
events: false, | ||
onBeforeElUpdated: function(from, to) { | ||
t.ok('User supplied onBeforeElUpdated called') | ||
return false |
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.
Returning false
from user supplied onBeforeElUpdated
will skip yo-yo
's copying of events.
// that can be set via attributes | ||
return function copier (f, t) { | ||
var copyEvents = userUpdate? userUpdate(f, t): true | ||
if (!copyEvents) { return false } |
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.
morphdom
only stops the update if the onBeforeElUpdated
function explicitly returns false
. This falsy check will incorrectly stop the copier
from running if the user-provided onBeforeElUpdated
function returns undefined
.
(The copier
function itself returns undefined
, yet the node swapping continues downward because false
wasn't returned).
The code needs the explicit check if (copyEvents === false) return false
, same as is present in morphdom
.
Cheers
Ok, that works for me. I still need to manually copy across those attributes I'm interested in that trigger updates in the |
@shama @yoshuawuyts What ya'll think? |
@kristoferjoseph hey, this has been in my inbox for a while now and I've given it a few shots but not sure I follow what's going on. Could you give me the tl;dr of what's going on? thanks! 😁 |
@yoshuawuyts tl;dr This PR supplies a way for a user to provide their own This allows a user to run their own update routine then decide if they want to copy events. In the case they have determined during their update routine that no further updates are needed they can return |
Hmm, wouldn't this be a legit reason to drop down into On Wed, Nov 2, 2016 at 10:52 PM kj [email protected] wrote:
|
@yoshuawuyts your response is similar to my initial thoughts. You may need to read this issue thread to get the bigger picture. |
In the meantime I've done just that and needed to bring this code into my The docs say that yo-yo passes your options through to morphdom. This is It's not about configuring yo-yo or changing its behavior at all - by using On Wed, Nov 2, 2016, 3:25 PM kj [email protected] wrote: @yoshuawuyts https://github.com/yoshuawuyts your response is similar to — |
No description provided.