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

"delay" handler for definitions with async calls #1078

Closed
wants to merge 2 commits into from
Closed

"delay" handler for definitions with async calls #1078

wants to merge 2 commits into from

Conversation

demurgos
Copy link

@demurgos demurgos commented Apr 1, 2014

Some modules do need to perform asynchronous calls to be fully initialized.
In example :
IndexDB calls https://developer.mozilla.org/en/docs/IndexedDB
Ajax requests
Blob builders
Custom methods with asynchronous tasks

To get sure that the asynchronous calls are performed before the defined events are triggered, it is possible to use a plugin that will manually handle those special cases. The problem is that to delay the "define" event, the plugin has to used for each require which creates redundancy across secondary modules depending on the first module requiring async calls. It would be more logical if the async nature of a module was in its definition.
The idea would be to not only load modules asynchronously but also to get their value asynchronously.
The best idea I found was to create a new special handler ( like "require", "exports" or "module" ) called "delay" and being a callback function for the return value.
For example it would look this way :

define(["delay"], function ( delay ) { // depends on the "delay" handler : the value will be returned asynchronously
var Module = ... ;
setTimeout(function(){ //Some asynchronous actions
delay(Module) ; //This does actually returns the Module's value and triggers the "defined" event.
//It's transparent, just as if the loading time over the network was a bit longer.
},1000);
return Module ; //This does not do anything because delay is active
});

This was achieved with only a few lines of code (check the diff) and could offer a useful feature to get a better structured module when its initialization does not simple rely on the load of the file.

PS : I do have a CLA with Dojo

Demurgos added 2 commits April 1, 2014 17:38
"delay" handler registered at line 590
check function edited to support "delayed" definitions at line 836
@jrburke
Copy link
Member

jrburke commented Apr 1, 2014

See notes on #1075

@jrburke jrburke closed this Apr 1, 2014
@ghost
Copy link

ghost commented Apr 4, 2014

@demurgos. As I see I'm not not only one who think about async/delayed exports in AMD.
With your pull request we have already a second way to implement it, see also my implementation #1075. So we should open an issue on the amdjs-api repo to discuss it.

@demurgos
Copy link
Author

demurgos commented Apr 5, 2014

Thanks, I have seen your interesting implementation too.
This was my first pull request I made so sorry, I see it was not complete : I just wanted to propose a way to deal with some rare but existing cases because what you have done is just great.

I'll try to improve my solution because it's still has its issue that it requires a handler name - personnally that's already the reason why I did not named that "async". Could it be usefull to provide some concrete examples or situations when this async definition is helpfull?

An other thing that bugged me : should the Module offer a synchronous AND an asynchronous export ? I feel like its getting to over-complicate it, could this be usefull ?

@ghost
Copy link

ghost commented Apr 6, 2014

I opened an issue on the amdjs-api repo, I quoted your comment there as well. So let's move the discussion to the issue, to get an async/delayed AMD standard implementation soon.

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

Successfully merging this pull request may close these issues.

2 participants