function handleChangeEvent()

in src/amo/addonManager.js [175:230]


  function handleChangeEvent(e: AddonChangeEvent) {
    const { id: guid, type, needsRestart } = e;

    // eslint-disable-next-line amo/only-log-strings
    _log.info('Event received: %o', { type, id: guid, needsRestart });

    if (type === ON_OPERATION_CANCELLED_EVENT) {
      // We need to retrieve the correct status for this add-on.
      return getAddon(guid, { _mozAddonManager: mozAddonManager })
        .then((addon) => {
          const status = getAddonStatus({ addon });

          return callback({
            guid,
            status,
            needsRestart,
            canUninstall: addon.canUninstall,
          });
        })
        .catch((error) => {
          // eslint-disable-next-line amo/only-log-strings
          _log.error(
            'Unexpected error after having received onOperationCancelled event: %o',
            error,
          );
        });
    }

    // We cannot retrieve an add-on when it is not yet installed or already
    // uninstalled.
    if ([ON_INSTALLING_EVENT, ON_UNINSTALLED_EVENT].includes(type)) {
      return callback({
        guid,
        status: GLOBAL_EVENT_STATUS_MAP[type],
        needsRestart,
        // We assume that an add-on can be uninstalled by default.
        canUninstall: true,
      });
    }

    // eslint-disable-next-line no-prototype-builtins
    if (GLOBAL_EVENT_STATUS_MAP.hasOwnProperty(type)) {
      return getAddon(guid, { _mozAddonManager: mozAddonManager }).then(
        (addon) => {
          return callback({
            guid,
            status: GLOBAL_EVENT_STATUS_MAP[type],
            needsRestart,
            canUninstall: addon.canUninstall,
          });
        },
      );
    }

    throw new Error(`Unknown global event: ${type}`);
  }