Chrome onMessage侦听器无法在chrome.storage.local'get'回调中发送响应

时间:2014-08-14 09:41:59

标签: javascript google-chrome google-chrome-extension coffeescript asynccallback

这是我的大部分background.js代码

var DataProcessor;

DataProcessor =(function(){     DataProcessor.prototype.coursesHolder = {};

function DataProcessor() {
  chrome.runtime.onMessage.addListener((function(_this) {
    return function(request, sender, sendResponse) {
      var _base, _name;
      if (request) {
        console.log(request);
        switch (request.type) {
          case "showPageAction":
            (_base = _this.coursesHolder)[_name = request.courseName] || (_base[_name] = {
              courseName: request.courseName,
              courseTitle: request.courseTitle
            });
            chrome.pageAction.show(sender.tab.id);
            break;
          case "getCourse":
            sendResponse(_this.coursesHolder[request.courseName]);
            break;
          case "getAdditional":
            chrome.storage.local.get(request.courseName, function(datum) {
              return sendResponse({
                additional: datum[request.courseName].points.additional
              });
            });
            break;
          case "storeAdditional":
            _this.storeAdditional(request.additional, request.courseName);
            break;
          case "removeAdditional":
            _this.removeAdditional(request.index, request.courseName);
            break;
          case "updateCalculated":
            _this.updateCalculated(request.data, request.pointsType, request.courseName);
            break;
          case "calculatePoints":
            sendResponse(_this.calculatePoints(request.courseName));
            break;
          default:
            return sendResponse({
              error: 'Unidentified Exception'
            });
        }
      }
    };
  })(this));
}

是导致错误的部分。实际上,它是从咖啡脚本代码编译的。 棘手的部分是关于流程的getAdditional分支。如果我在存储访问之前有sendResponse - 它按预期工作。存储本身返回预测的结果。但是在组合中 - sendResponse内部回调到storage.get - 根本没有任何事情发生。没有错误,就像没有调用sendResponse一样!

Coffee Script来源:

class DataProcessor
  coursesHolder: {}

  constructor: ->
    chrome.runtime.onMessage.addListener (request, sender, sendResponse) =>
      if request
        console.log request
        switch request.type
          when "showPageAction"
            @coursesHolder[request.courseName] ||=
              courseName: request.courseName
              courseTitle: request.courseTitle
            chrome.pageAction.show(sender.tab.id)
            break
          when "getCourse"
            sendResponse @coursesHolder[request.courseName]
            break
          when "getAdditional"
            chrome.storage.local.get request.courseName, (datum) =>
              sendResponse
                additional: datum[request.courseName].points.additional
            break
          when "storeAdditional"
            @storeAdditional(request.additional, request.courseName)
            break
          when "removeAdditional"
            @removeAdditional(request.index, request.courseName)
            break
          when "updateCalculated"
            @updateCalculated(request.data, request.pointsType, request.courseName)
            break
          when "calculatePoints"
            sendResponse @calculatePoints(request.courseName)
            break
          else
            sendResponse
              error: 'Unidentified Exception'

邮件发送如下:

    chrome.runtime.sendMessage
      type: "getAdditional"
      courseName: @courseName
    , (response) =>
      console.log response
      if response.additional

如果有人知道如何让这个回调堆按预期工作,请帮忙。

0 个答案:

没有答案