wifi.removelistener() do not work properly #5668
Replies: 1 comment
-
Posted at 2017-07-22 by Robin Sat 2017.07.22 Welcome @user79576 I'm glad you have found enjoyment with the Espruino interpreter. Although I haven't tried out a fix to your particular issue, I believe the solution is to place the removeListener() function outside your creation of function f() as shown in the online reference: http://www.espruino.com/Reference#l_Object_removeListener As the var f is assigned the object result of the function creation, 'connected1' is output as expected, then function f() is created again, 'connected2' is output as expected then the object f() is removed. My guess is that processing is halting at this step as memory is corrupt, and the rest of the statements are ignored as you are seeing. Also, try:
Other detail Posted at 2017-07-23 by Polypod Thank Robin for your answer,
This give time for the first callback calls round to happen (connected1 to connected4), but then again the call remove f function callback and all following callbacks. The process is not only halted, but callbacks get removed. I can see each callbacks left with
So next "connected" trigger only the first callback. My workaround now is to manage myself my wifi listeners and subscribe only one callback, or else when I can I subscribe listeners in the reverse order I plan to unsubscribe them. But I am left with the feeling that something is wrong and I can't put the finger on it. Sofyen Posted at 2017-07-24 by Wilberforce Hi, Can't you do everything you need to do in the first connection function? Posted at 2017-07-24 by Polypod Hi Wilberforce, Yes, I sure can use only one listener, that's the solution I mention, and I manage the callbacks myself, this way I can still have modules adding their listeners and removing them to free memory. But this is exactly what wifi.removeListener() was supposed to do. Posted at 2017-07-24 by Robin Mon 2017.07.24 @Polypod, have you solved the original removeListener() mystery or is there some doubt in the resolution you have settled with? While It is true that a listener can be connected to most objects as you so astutely described, after reading the online reference, I agree with @wilberforce that it appears the WiFi connected object was meant to have a single listener. Polypod, if you are still wondering if you are at a sutiable solution, please post the entire code module as it now stands. This may provide
To those that are following along, is anyone able to post an example with multiple listeners or reference an online tutorial? Posted at 2017-07-24 by Polypod Hi Robin For example, here is the structure of the callback memory section of wifi module :
In this example, we can see there is an array with "#onconnected" element with 2 callbacks in it. From there I was able to find a hacky solution to directly change the content of this hidden variable :
That's my best solution so far, nearest in behaviour to the original removeListener(e,f). I tried splicing this table, but of course if you do it from within the callback that is part of the table, it has the exact same problem than the original wifi.removeListener(e,f) I posted in the beginning. So my final guess is it was not designed to be changed from within the callback, because to do so, the array must be changed after iterating or a copy of the array is iterated and the original is modified. For those who like less hacky solutions, I explored these 2 options, but that are not as practical as my hacky solutions :
Posted at 2017-07-24 by Robin Mon 2017.07.24 Thank you for posting your snippet. I see what you are doing with multiple connect objects, but I'm struggling as to why the need for several. I struggled with the different modules myself, but was able to use just one instance of the WiFi object. Although this is not a solution to what you are after, please read over this to see if it sparks some ideas. 'STA or AP mode browser unable to connect to ESP8266-12' The overall gist is to create a server/station object using the WiFi class module. I'm still searching for some code snippets to see if we are able to use a different class object rather than Wifi to accomplish what you are after. Posted at 2017-07-24 by Polypod Hi again Robin, If you find anything that may help, it will be welcome Polypod Posted at 2017-07-24 by Robin Thought that the gist ref #16 of just having one WiFi instance and using a server might replace what you had intended. Do any of these spark some ideas? http://www.espruino.com/Pico+Weather+Station http://www.espruino.com/wifi_humidity http://www.espruino.com/WiFi+Enabled+Thermometer from: Why not call each of your modules from inside the 'connected' function once connected? This would remove the need to 'pass' the concept of connected to each individual module, as each is only called once 'inside' the on-connected function. Ref Weather Station example above |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Posted at 2017-07-22 by Polypod
Hi,
I am so happy since I found esp and espruino, but I have a little problem. I have a small code with several listeners waiting wifi connection on boot and removing themselves afterward. It appears that the first to call wifi.removeListener remove not only this listener but also the following ones. Try for yourself :
Result is :
I expected connected3 and connected4, but they never get displayed. But if I remove the wifi.removeListener(), they are displayed. I am removing listeners mostly in order to save memory.
Am I am misunderstanding the situation ? is wifi.removeListener() working properly ?Please help.
Sof
Beta Was this translation helpful? Give feedback.
All reactions