discourse/discourse — pull request #1501 by velesin (2013-10-07 16:06:47+00:00)
Title: adds unit tests for Discourse.debouncePromise

Second half of the tests for Discourse debouncing mechanism. As discussed here: https://github.com/discourse/discourse/pull/1489 debouncing related code will need further refactoring, but this pull request should help doing it more safely.

As a side effect of writing this test, I've discovered also one issue: when issuing two subsequent debounce calls, Discourse.debounce preserves context and params from the first call, _.debounce from the last call but Discourse.debouncePromise preserves context from the first call but params from the last call - what is clearly a mismatch and a bug. This pull request, apart from the tests, contains also a fix for this bug (I believed that invoking the same debounced function with different params is much more common than with different contexts, so I decided params to be the "leading" one and made the context behave the same way as params - so both of them are now preserved from the last call).

PS: during the process I have also discovered, that Ember itself has a debounce function: http://emberjs.com/api/classes/Ember.run.html#method_debounce that operates inside a run loop etc. - so maybe this is the best way to go? I'll do some more research on how the debounce code is currently used throughout a project, and present some results with possible consequences of using various debounce mechanisms as a topic on meta.