javascript - Do I have to call return true each time I use onMessage.addListener responseCallback? -
i have onmessage.addlistener function this:
chrome.runtime.onmessage.addlistener(function (r, s, sendresponse) { if (r.action == "read") { readmanga(request, function () { sendresponse({}); }, true); } if (r.action == "write") { readmanga(request, function () { sendresponse({}); }, true); } if (request.action == "update") { $.each(request.list, function (index, val) { resetmanga(val, function () {}, false); }); savelist(); refreshupdate(); sendresponse({}); } if (request.action == "release") { releaseimplentationfromid(request.id, function (mirrorname) { updatemirrors(function () { sendresponse({ mirror : mirrorname }); }); homecoming true }); } } (this excerpt, it's working code , please ignore if's, should have used case since long ago)
each time utilize callback function sendresponse (again, need rename it, lets ignore that) defined in documentation never used unless have return true after it's execution. necessary or found dirty hack , i'm doing wrong?
this expected behavior , stated in documentation:
this function [sendresponse] becomes invalid when event listener returns, unless homecoming true event listener indicate wish send response asynchronously (this maintain message channel open other end until sendresponse called).
your function updatemirrors apparently asynchronous: callback function not executed sent queue. so, you're not returning true "after" execution, hitting return true before sendresponse.
therefore, it's necessary tell chrome "expect reply later".
javascript google-chrome-extension
No comments:
Post a Comment