Last active
September 13, 2019 21:05
-
-
Save rileyhilliard/4ca0548f3305925727788e9fcd53b55e to your computer and use it in GitHub Desktop.
Debounced Event Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
import { debounce } from '@ember/runloop'; | |
// This isnt totally necessary, but in the real world where an error.message might be really | |
// long it might be better to reduce the stored key to something more consistant | |
function hasher(str) { | |
var hash = 0, i, chr; | |
if (str.length === 0) return hash; | |
for (i = 0; i < str.length; i++) { | |
chr = str.charCodeAt(i); | |
hash = ((hash << 5) - hash) + chr; | |
hash |= 0; | |
} | |
return String(hash); | |
} | |
const dictionary = Object.create(null); | |
const DEBOUNCE_TIME = 100; | |
export default Ember.Controller.extend({ | |
events: [ | |
'shred it', 'shred it', 'shred it', 'twist it', 'shred it', 'shred it', 'pop it', 'shred it', 'shred it', 'twist it', 'shred it', 'shred it', 'twist it', 'shred it', 'shred it', 'pop it', 'shred it', 'shred it', 'twist it', 'shred it', 'shred it', 'pop it', 'shred it', 'shred it', 'twist it', 'shred it', 'shred it', 'twist it', 'shred it', 'shred it', 'pop it', 'shred it', 'shove it', 'surf it', 'shred it', 'shred it', 'shred it', 'twist it', 'shred it', 'shred it', 'pop it', 'shred it', 'shred it', 'twist it', 'shred it', 'shred it', 'twist it', 'shred it', 'shred it', 'pop it', 'shred it', 'shred it', 'twist it', 'shred it', 'shred it', 'pop it', 'shred it', 'shred it', 'twist it', 'shred it', 'shred it', 'twist it', 'shred it', 'shred it', 'pop it', 'shred it', 'shove it', 'surf it' | |
], | |
emittedEvents: [], | |
init() { | |
// run through all events | |
this.get('events').forEach(event => { | |
// generate the events stored hash key | |
const key = hasher(event); | |
// If the event has not been seen before, store it in our dictionary | |
if (!dictionary[key]) { | |
dictionary[key] = () => | |
// when the events function is called, add the event to the | |
// 'emittedEvents' array | |
this.emittedEvents.push(event); | |
} | |
// Grab the events function from the dictionary | |
const fn = dictionary[key]; | |
// debounce the events function | |
debounce(this, fn, DEBOUNCE_TIME, true); | |
}); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"version": "0.15.1", | |
"EmberENV": { | |
"FEATURES": {} | |
}, | |
"options": { | |
"use_pods": false, | |
"enable-testing": false | |
}, | |
"dependencies": { | |
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js", | |
"ember": "3.4.3", | |
"ember-template-compiler": "3.4.3", | |
"ember-testing": "3.4.3" | |
}, | |
"addons": { | |
"ember-data": "3.4.2" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment