Skip to content

Instantly share code, notes, and snippets.

@jesperlandberg
Last active December 12, 2019 17:42
Show Gist options
  • Select an option

  • Save jesperlandberg/89de2c0071bee3cfc443bd59cffc2d59 to your computer and use it in GitHub Desktop.

Select an option

Save jesperlandberg/89de2c0071bee3cfc443bd59cffc2d59 to your computer and use it in GitHub Desktop.
import e from '@unseenco/e' // Event emitter, I use my friend Jake's, since its great, but could also use TinyEmitter for example
const Events = new e()
export default Events
// Make sure u import ur global raf at least once somewhere, I usually do that in my main.js file
import Raf from './Raf'
class App {
// My main class
}
new App()
import gsap from 'gsap'
import Events from './Events'
class Raf {
constructor() {
gsap.ticker.add(this.tick)
}
tick = () => {
Events.emit('tick')
/*
in my personal core I calc and emit scroll and mouse values,
so I can access these in any file that might need them.
Events.emit('tick', {
target: this.target,
current: this.currentRounded,
mouse: this.mouse
})
*/
}
}
export default new Raf()
// Example component where I need the raf
import Events from './Events'
export default class {
constructor() {
this.init()
}
init() {
this.addEvents()
}
run = ({ current, mouse }) /* my scroll and mouse get passed like params */ => {
// Do some component specific raf stuff
}
addEvents() {
// Listen to raf
Events.on('tick', this.run)
}
removeEvents() {
// Stop listening to raf
Events.off('tick', this.run)
}
destroy() {
// Remove events and cleanup class
this.removeEvents()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment