Well, I think if I make the views dumber than I don't have to worry about the results of messages as much and that fixes a core problem in my application.
But if you have a situation where you message the background page for data that you need right away there's no clean way I think. The best way would be to send the request and then make an interval which polls a status variable and when it's valid sends the result to a function that can use it. It's not too bad I guess. Since the latency isn't too bad for many applications and I can probably poll at 10ms and see the result after 1 or 2 calls. Erek On Mon, Jan 4, 2010 at 11:43 AM, Ernest Delgado <[email protected]> wrote: > The callback function can always set a non local variable so it's > visible by the other functions. Although you might as well go for a > better pattern depending on the needs of your extension. > > Ernest > > On Sun, Jan 3, 2010 at 5:25 PM, Erek Speed <[email protected]> wrote: >> Hi, >> >> So I realized this was working but that my code which checked the >> variable was running before the callback. >> >> This lead to a different problem where I can't ever know the status of >> a message in the same function that sent the message. Or even in a >> few functions later due to the delay. >> >> I tried to get synchronous messages by spinning in a loop checking a >> status variable, but it seems that in JS, while the loop is running >> nothing else happens. Probably because the extension is all in one >> thread. >> >> Right now, I'm planning on refactoring my code to a more chrome >> friendly manner but it seems that it could be good to be able to know >> the that a message succeeded at some specified point of time instead >> of a vague time in the future. >> >> Erek >> >> On Sat, Jan 2, 2010 at 6:48 AM, Erek Speed <[email protected]> wrote: >>> I want to set a variable from inside my callback to a sendRequest >>> function. >>> >>> The mappy extension gives an example of doing this but I can't seem to >>> manage it using global or local variables. >>> >>> Here's mappy's code for reference: >>> <script> >>> // Global accessor that the popup uses. >>> var addresses = {}; >>> var selectedAddress = null; >>> var selectedId = null; >>> >>> function updateAddress(tabId) { >>> chrome.tabs.sendRequest(tabId, {}, function(address) { >>> addresses[tabId] = address; >>> if (!address) { >>> chrome.pageAction.hide(tabId); >>> } else { >>> chrome.pageAction.show(tabId); >>> if (selectedId == tabId) { >>> updateSelected(tabId); >>> } >>> } >>> }); >>> } >>> ... >>> >>> In the response function, address is changed which is convenient. >>> What is it about addresses that let's it retain value even in the >>> mystical space of messages? >>> >>> -- >>> >>> You received this message because you are subscribed to the Google Groups >>> "Chromium-extensions" group. >>> To post to this group, send email to [email protected]. >>> To unsubscribe from this group, send email to >>> [email protected]. >>> For more options, visit this group at >>> http://groups.google.com/group/chromium-extensions?hl=en. >>> >>> >>> >> >> -- >> >> You received this message because you are subscribed to the Google Groups >> "Chromium-extensions" group. >> To post to this group, send email to [email protected]. >> To unsubscribe from this group, send email to >> [email protected]. >> For more options, visit this group at >> http://groups.google.com/group/chromium-extensions?hl=en. >> >> >> > -- You received this message because you are subscribed to the Google Groups "Chromium-extensions" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/chromium-extensions?hl=en.
