-
Notifications
You must be signed in to change notification settings - Fork 75
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
custom datatype onChange function #202
Conversation
dataType = this._dataTypes[def.type]; | ||
|
||
// check type if we have one | ||
if (dataType && dataType.set) { | ||
cast = dataType.set(newVal); | ||
cast = dataType.set.call(this, newVal, options); |
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'm not sure I understand the purpose of this change?
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 this was part of the original patch - the idea being to bind this
to the set function. Probably not strictly necessary to fix the issues this is addressing.
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.
It's in the same vein as the _getCompareForType
which binds this
. Useful if you need to do something with the state in the onChange
function. For example, the current 'state' datatype calls 'stopListening' on this
when swapping out states.
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.
Isn't the purpose of the new onChange function where that should happen? Rather than in the set?
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.
Yeah, good point. I guess there could be a user who assumes this
is always the state, but agree that it is not necessary.
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.
Unbinding the compare would be a breaking change for people with custom datatypes right, it wouldn't affect the built-in types?
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.
AFAIK the only built-in type using this
inside of set
was 'state', but I would have to check to be sure. I think ampersand-view defines some datatypes too, not sure about other modules.
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.
er, inside of compare
I mean, not set :P
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.
So here's at least one module using this in change
: https://github.com/legastero/ampersand-wildemitter-datatype-mixin/blob/master/index.js. It makes me think maybe we shouldn't change that for now?
For one, it's kinda hard to deprecate, it's not easy to throw a warning if the user references this or something in the compare function. And secondly, datatype mixins like the above aren't generally pinned against a specific version of ampersand in anyway.
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.
Yeah, makes sense. I've updated this so that it doesn't bind on set
- same as before, and will leave the bound compare
for now.
Thanks @chesles for updating this. |
Hey, late to chim in. Why can't you hook into
In regards to lifecycle callbacks, if Other than that, performance wise, these changes (all done for a custom dataType which I'm not sure many people use) add an extra (even minor) overhead to each set. Is this the best solution for most users who might not use a custom datatype? (I've never actually used one). Not to be hard on the makers of this patch, I perfectly understand the need.. |
This is actually fixing the built-in My initial thought was to have setting the default value go through As far as performance, the difference is almost definitely negligible - if |
Since the `dataType.set` function is called on every set, the name `dataType.onSet` was a little confusing. Since the function is only called when the value changes, `dataType.onChange` seemed more appropriate.
I just rebased this to keep it up to date/resolve conflicts with master. Any chance of getting it merged? |
Thanks for this and for keeping it up to date @chesles! 👍 from me. |
Rebased this against master and tests pass, +1 |
custom datatype onChange function
published as |
Adds an
onChange
function to custom dataType definitions. When defined for a type,onChange
is called whenever the value of a property with that type changes.The built-in 'state' type now uses
onChange
to set up and tear down listeners on properties of type 'state', instead of doing so within thecompare
function, which as discussed in #112 has several drawbacks.This is based on #145 - I rebased @mmacaula's branch on master, added some additional tests and fixes, and renamed
onSet
toonChange
to avoid confusion withset
.