shake.js icon indicating copy to clipboard operation
shake.js copied to clipboard

Multiple events triggered if instantiated multiple times

Open iJungleboy opened this issue 9 years ago • 3 comments

There are cases where multiple things on the screen may init a new Shake(). For example, if a CMS has various components which the editor adds, and more than one of them want to enable shake detection.

At the moment, this causes the shake-event to trigger for each initialized Shake(), so code outside "my" scope could cause the shake-event to occur multiple times.

I believe Shake() should somehow either

  1. check if it's already running, and return the running instance
  2. OR manage a list of registered Shakes and remove one
  3. OR change the concept (or offer the alternative) to use a callback - there seems to be a fork which does this; shake could just offer both: https://github.com/JonWallsten/shake.js/commit/3989b49ef7859f88dcff87dc779a06042b8871d9

iJungleboy avatar Nov 07 '16 07:11 iJungleboy

How many pieces of code do you have running on one page that all need to respond to a shake event in different ways? What are they all doing? I'm curious if this is a common need.

When I get round to it, changing this plugin to use a callback is something I plan on doing if that helps.

alexgibson avatar Nov 07 '16 07:11 alexgibson

So the scenario I'm talking about is a web-platform scenario which may have components from multiple vendors - let's say a CMS component which uses shake to show buttons (http://2sxc.org/en/blog/post/introducing-shake-mobile-content-editing-just-turned-sexy) and another one which maybe will use shake do something with an image gallery. Now both vendors don't know about each other, but when the CMS-owner adds both components, both would start a Shake() and this would double-trigger the event.

iJungleboy avatar Nov 07 '16 13:11 iJungleboy

This sounds like a larger architecture issue with your CMS, but I'm happy to move to a callback API.

alexgibson avatar Nov 07 '16 13:11 alexgibson