Comments (9)
Okay, if this is working, awesome. π Thank you for the help, that solves it for me.
from mitt.
@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?
from mitt.
Hey @wulfmann,
sure, so as I said, what I would like is to emit an event and when I add a listener to this event afterwards, it should fire immediatly if the event was already fired.
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(β¦)
})
from mitt.
@lukasoppermann Hey, just out of curiosity, why don't you just put emitter.on('AnimationLibraryLoaded', ...)
at the top, so it's going to be called before fetchAnimatinLib
? Is there some real use case in the app?
from mitt.
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?
from mitt.
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.
from mitt.
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. π
from mitt.
@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.
Because I can not use the animation library before it is loaded
That's also understandable, I mean using an event emitter here is perfectly fine.
from mitt.
Hi everyone!
Getting back to the topic, here is my case:
Using Vue.js I run an authentication script that works synchronically.
In the Vue component, I need to start some actions when a user is authenticated, but I can not know for sure what will happen first: authentication completion or a rendering of a component.
So, currently, I use setInterval() and check for a state.
But I wonder if a could check something like "Was this auth-event already emitted?".
Not even to "check", but to run "on" callback immediately. As my "on" logic resides inside a Vue component I don't see the way to put "on" before "emit"
from mitt.
Related Issues (20)
- Nuxt3 issues ? HOT 2
- Persist events that not yet have a handler HOT 1
- Is it possible to use mitt across two different tabs? Or via an iframe? HOT 10
- [TypeScript] define events interface HOT 3
- Mitt - some problems HOT 1
- Typescript issue with `moduleResolution: NodeNext` HOT 1
- `dist/mitt.mjs` must not have a sourcemap HOT 5
- mitt extension
- Unpkg CDN Version is down HOT 1
- Using Ag-grid cell renderer with Mitt HOT 1
- Bug: type issue in [email protected]+ with esm HOT 2
- Could we have a minor update? HOT 1
- Doesn't work with typescript 5 moduleResolution set to bundler HOT 11
- vue3 options methods use mitt Invalid HOT 1
- why use sllice & map HOT 2
- Can't pass multiple arguments with emit HOT 2
- How to declare type of event parameter when listen for event HOT 1
- error TS2349: This expression is not callable when using "module": "NodeNext". HOT 5
- Uncaught TypeError: (0 , mitt__WEBPACK_IMPORTED_MODULE_5__.default)(...) is not a function
- Emit issue on Vue 3. HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mitt.