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
Making callbacks consistent #1
Conversation
… initial load/error events are fired with a delay (to ensure event listeners added after instantiation are still triggered)
@@ -75,16 +75,12 @@ | |||
noCache: true | |||
}).run(stageNode, { | |||
url: 'movies/' + script + '?' + new Date().getTime(), | |||
plugins: ['../../../spareparts/src/text_field.js'], |
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.
Should be removed as it is not part of the repo.
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.
Ok. It's been removed.
I like the branch, good work! A few remarks:
|
I will change the method name to I still need to think a bit about the events-only approach. It's very rare that you'd want to instantiate a Bitmap, for example, without listening for its new Bitmap(url, function(){});
// vs.
new Bitmap(url).on('load', function(){}); The events-only approach seems more generalised. The former approach, with a callback passed to the constructor, is more suited to the purpose of making a new Bitmap and waiting for it to load. It depends how generalised we want to be with our API. For me, I like knowing about and using API shortcuts. And it's always cooler to use an API when there are multiple ways to bend it to your will. Maybe this is an emotional appeal. I just think we should imagine what the user will want to do and then mould the API to best suit those imagined intentions... |
I guess we can put the events-only thing to a vote in the flow? |
Yes, let’s invite everybody here to comment on that. |
To be clear for people arriving here, the two options are a more generalised approach: new Bitmap(url).on('load', function(){...}); Or a "convenience" callback as the second arg: new Bitmap(url, function(){...}); And with the second approach you're still free to do Whatever is decided will also count for |
I would prefer to have both, so I'm +1 on the "convenience" callback. |
|
|
Yes, definitely, the convenience callbacks should be bound with
I actually agree, but I still think it's a good idea to offer convenient args for popular use-cases. |
Is it ensured that this callback always will be called? We need to be careful in this case to avoid memory leaks. |
Like James said: it is registered, you just don’t know. Even if registered with function Thing(callback) {
this
.once('load', function(foo, bar) {
callback.call(this, null, foo, bar);
})
.once('error', function(error) {
callback.call(this, error);
});
} when
I can write that without a second line: new Bitmap(url).on('load', function(){ stage.add(this); });
Evidently, even for us it’s impossible to predict the exact behavior of the callback. Although we know the implementation. It just has side effects we are not completely aware of. |
Didn't know that it is using @davidaurelio you are right about writing it in one line if you don't do error-handling. In the case of an error you would have to maintain two callbacks. What happens if you do the following: new Bitmap(url).on('load', function(){ stage.add(this); }).on('error', function() {}); What happens if the 'load' event is triggered? Does it cleanup the |
@klipstein, in that case you'd have to clean it up yourself. @davidaurelio I see that to do it properly we'd need additional code, so I guess I now agree that we should remove the convenience callbacks. |
@padolsey Reading your hint that the user would need to cleanup an error handler on his own I can see a use case for the callback :) Maybe we should do it properly and put the code in a |
@davidaurelio, Or we let |
I see this load/error callback thing as being unique to these specific DIsplayObjects so I think it'd make sense to encapsulate the logic in an interface or whatever... instead of placing it in tools. |
ok, sounds good. |
@davidaurelio I've added a asset_display_object mixin which takes care of the callback binding/unbinding. Please review. |
Ok, just reviewed it and I LIKE. Could you please add a line to the changelog? Thank you. Will merge afterwards |
Ok, just modified the changelog. Linked to this pull request too -- hope that's appropriate. |
That’s cool, thank you! |
Making callbacks consistent
These commits fix the callback ambiguity we had across the project. All callbacks are now using the single-callback pattern used in node.js:
In addition the argument patterns used in
Movie
,Video
,Bitmap
andFontFamily
have now been aligned. A convenience callback argument can be used (with the above pattern) or you can simply bind to theload
andsuccess
events:Before, this might not have worked if the bitmap loaded immediately from cache. The
load
event would be fired before instantiation completed, meaning that listening for it after that would be pointless.But now, using a new
asyncEmit
method on the EventEmitter, we now trigger these events asynchronously. I'm not sure if we want there to be a method especially for this or if we should inline thesetTimeout
in relevant places. Up for discussion on that.