Git Product home page Git Product logo

Comments (9)

lukasoppermann avatar lukasoppermann commented on June 19, 2024 1

Okay, if this is working, awesome. πŸ‘ Thank you for the help, that solves it for me.

from mitt.

wulfmann avatar wulfmann commented on June 19, 2024

@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.

lukasoppermann avatar lukasoppermann commented on June 19, 2024

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.

vitkarpov avatar vitkarpov commented on June 19, 2024

@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.

lukasoppermann avatar lukasoppermann commented on June 19, 2024

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.

emilio-martinez avatar emilio-martinez commented on June 19, 2024

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.

lukasoppermann avatar lukasoppermann commented on June 19, 2024

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.

vitkarpov avatar vitkarpov commented on June 19, 2024

@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.

zablik avatar zablik commented on June 19, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.