-
Notifications
You must be signed in to change notification settings - Fork 426
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
Listen for events that happened before the listener is created. #87
Comments
@lukasoppermann I'm looking at cleaning up a few of these issues, could you describe a little bit further what you're looking to do? Perhaps with some code examples? |
Hey @wulfmann, Maybe this could be done via a parameter on the listener (not included in the example). import mitt from 'mitt'
const emitter = mitt()
const fetchAnimatinLib = new Promise((resolve, reject) => {
const script = document.createElement('script')
document.head.appendChild(script)
script.onload = resolve
script.onerror = reject
script.async = true
script.src = './animationLib.js'
})
fetchAnimatinLib.then(() => {
emitter.emit('AnimationLibraryLoaded')
})
… some more code
… animation library is fully loaded
// this should fire immediatly
emitter.on('AnimationLibraryLoaded', e => {
animationLib.animate(…)
}) |
@lukasoppermann Hey, just out of curiosity, why don't you just put |
Because I can not use the animation library before it is loaded, this will produce an error. This is why I need to know when the library is loaded. Does that make sense? Or am I misunderstanding the question? |
Agree with @vitkarpov. The listener should just be declared at the top—you wouldn't get an error because it wouldn't be executed until the library is loaded anyway. |
Okay, wait, so what you are saying is: import mitt from 'mitt'
const emitter = mitt()
// this should fire immediatly
emitter.on('AnimationLibraryLoaded', e => {
animationLib.animate(…)
})
const fetchAnimatinLib = new Promise((resolve, reject) => {
const script = document.createElement('script')
document.head.appendChild(script)
script.onload = resolve
script.onerror = reject
script.async = true
script.src = './animationLib.js'
})
fetchAnimatinLib.then(() => {
emitter.emit('AnimationLibraryLoaded')
})
… some more code
… animation library is fully loaded And this should work? Now that I put it in code it seems rather logical. 😓 |
@lukasoppermann example you provided above makes perfect sense to me, I believe it should work. Except for one thing - the comment should say "this should NOT fire immediately" now, it will when you emit the event hence when the library is loaded.
That's also understandable, I mean using an event emitter here is perfectly fine. |
Okay, if this is working, awesome. 👍 Thank you for the help, that solves it for me. |
Hi everyone! Getting back to the topic, here is my case: |
Hey @developit,
this is a really neat library. I was wondering if it is possible to have a listener fire immediatly if it is attached to an event that already happened? It did not work for me, so is it no supported, or did I do it wrong?
For example I would like to fire an event once the animation library is loaded, so that I can start all animations afterwards, no matter where they are defined.
Thanks for your help.
The text was updated successfully, but these errors were encountered: