HillsideHoldings / cordova-lock-screen-media-controls

A Cordova plugin that allows you to manage the media controls on the lock screen. Things such as play, pause, fast forward, rewind, title and graphic.
0 stars 0 forks source link

listenActions not reset on livereload #2

Open MichaelApproved opened 8 years ago

MichaelApproved commented 8 years ago

I'm using Ionic's livereload feature to test the app. It allows me to edit the code, press save and restart the app on the device with the new code without needing to recompile.

The problem is that the event passed into listenActions persists between reloads. Everything else in the app restarts but the event stays. So, if I reload the app 5 times, the event that's assigned in listenActions is executed 5 times.

Here's what the code looks like (lockscreenControls is a global)

if (typeof lockscreenControls === 'undefined') {
    $log.info("Creating new lock screen object");
    lockscreenControls = new Lockscreen();
    lockscreenControls.listenActions(onLockscreenEventReceived);
}

The code above executes once when the app is started. However, the app restarts during the livereload and the onLockscreenEventReceived function is not removed from the listenAction. So, when the app restarts, the listenActions executes again and onLockscreenEventReceived added again causing it to execute multiple times.

I hope this made sense because it's a confusing thing to explain.

To put it in different words, the plugin doesn't reset information when the app restarts using livereload. However, if I force close the app and restart it, everything works as expected.

My question: Is there a way for the listenActions to clear when the app restarts due to livereload? If not, is there a way I can clear the functions within the listenActions? If I can clear those functions myself, I can add that to my init() code.

Thanks.

alienslab02 commented 8 years ago

I have not used livereload function of ionic but from the description you posted it seems it is the release() function that you need. This function is already there in the plugin. It will just release all the resources from lock screen and remove all listeners. This is a MUST CALL function when you close your application or when lock screen media controls are not required anymore.

So you can use it when using livereload feature lockscreenControls.release(). Once this function is used while app is running and you need to show controls again on lock screen, you need to start the process again from init().

Before you try this, i think you should change you code with something like:

if ((typeof lockscreenControls === 'undefined') && (lockscreenControls == null)) { $log.info("Creating new lock screen object"); lockscreenControls = new Lockscreen(); lockscreenControls.listenActions(onLockscreenEventReceived); }

see the lockscreen==null check. I think in javascript null and undefined are two separate entities. If this works that's good. Otherwise give a try using release() function.

MichaelApproved commented 8 years ago

Thanks for the update. I should've mentioned that I tried release() already and it didn't make a difference. I tried to release() it as soon as I created a new object but it didn't clear the old events. liverefresh causes the application to restart without my control, so releasing at that point isn't possible, which is why I tried to release as soon as I created the object.

BTW - what happens if I never release()? Is it a memory leak or something like that?

alienslab02 commented 8 years ago

release() is necessary to call whenever your app closes or there is no need for lock screen to show media controls. If you don't call this function, lock screen will keep showing the media controls even app is closed unless android garbage collector notices it and removes this unnecessary hold of memory. Not calling release() will cause memory leaks and worst case, crashing the app.

Now what i think about livereload feature and firing multiple events is that while reloading lockscreen object is again being initialized. Means these lines are running every time ionic reloads app:

$log.info("Creating new lock screen object");
lockscreenControls = new Lockscreen();
lockscreenControls.listenActions(onLockscreenEventReceived);

So you must try to implement a check for null and initialize lockscreenControls variable with null. try using this:

var lockscreenControls = null; // assume this is a global variable

later in any function for first time when app runs:

if(lockscreenControls != null && lockscreenControls != undefined){ lockscreenControls.release(); lockscreenControls = null; } if (lockscreenControls == null ) { $log.info("Creating new lock screen object"); lockscreenControls = new Lockscreen(); lockscreenControls.listenActions(onLockscreenEventReceived); }

I think this should resolve the issue. The problem is only that these two lines are running again when app is reloaded everytime while these lines should only run once.

lockscreenControls = new Lockscreen(); lockscreenControls.listenActions(onLockscreenEventReceived);

Give the above code a try and let me know updates.

MichaelApproved commented 8 years ago

I've tried a variation of that. The thing about livereload is that everything in my app is reset. Nothing persists, so I can't detect if things were executed before (because of livereload) or if it's the first time the app is run. As far as my code in the app is concerned a livereload is the same as the app turning on for the first time. However the plugin retains the event information.

I think we can try two options:

Nick-R commented 8 years ago

@MichaelApproved do we have same issue on iOS? If yes, i can try to release old event listeners while plugin init.

MichaelApproved commented 8 years ago

@Nick-R I haven't tested this in iOS yet. I'm going to work on that this week and let you know.