<commit_before>import _ from "lodash";
import Subscriber from "../subscriptions/subscriber";
import Publisher from "../publications/publisher";
import AppData from "../app/app-data";
import ExternalMessageReceiver from "./external-message-receiver";
import UpdateTimer from "./update-timer";
import Badge from "./badge";
import SubscriberNotifier from "./subscriber-notifier";
const logger = debug("background");

/**
 * Background controller
 */
export default class BackgroundController {
  constructor() {
    this.externalMessageReceiver = new ExternalMessageReceiver(this);
    this.badge = new Badge();
    this.appData = null;
    this.subscriber = null;
    this.publisher = null;
    this.updateTimer = null;
    this.notifier = null;
    this.initializePromise = null;
  }

  /**
   * Start controller to handle messages and events
   */
  start() {
    logger("Initializing");
    this.externalMessageReceiver.register();

    // Start initialization process by loading AppData.
    this.initializePromise = AppData.load().then(appData => {
      this._setupWithAppData(appData);
      return this;
    }).catch((e) => {
      console.error("Error while initialization in BackgroundController", e);
      throw e;
    });
    return this.initializePromise;
  }

  /**
   * Called when AppData is loaded first time.
   */
  _setupWithAppData(appData) {
    this.appData = appData;
    this.appData.on("update", () => { this._handleAppDataUpdate(); });

    this._setupPublisher();
    this._setupSubscriber();
    this._setupUpdateTimer();
  }

  _setupPublisher() {
    this.publisher = new Publisher(this.appData.siteSettings);
    logger("Initialized Publisher", this.publisher);
  }

  _setupSubscriber() {
    this.subscriber = new Subscriber(this.appData.subscriptionSettings);
    this.subscriber.on("update", () => {
      logger("Updated Subscriber", this.subscriber);
      this.badge.setCount(this.subscriber.getUnreadItemsCount());
      this.appData.subscriptionSettings = this.subscriber.subscriptionSettings;
      this.appData.save();
    });
    this.badge.setCount(this.subscriber.getUnreadItemsCount());
    this.notifier = new SubscriberNotifier(this.subscriber);
    logger("Initialized Subscriber", this.subscriber);
  }

  _setupUpdateTimer() {
    this.updateTimer = new UpdateTimer(
      this.appData.updatePeriodMinutes,
      this.appData.lastUpdatedAt
    );
    this.updateTimer.on("timer", () => {
      this.updateSubscriptions();
    });
    this.updateTimer.on("update", () => {
      logger("Updated UpdateTimer", this.updateTimer);
      this.appData.lastUpdatedAt = this.updateTimer.lastUpdatedAt;
      this.appData.updatePeriodMinutes = this.updateTimer.updatePeriodMinutes;
      this.appData.save();
    });
    this.updateTimer.start();
    logger("Initialized UpdateTimer", this.updateTimer);
  }

  /**
   * Called when AppData is updated by chrome.storage.
   */
  _handleAppDataUpdate(appData, keys) {
    const updated = _.keyBy(keys);
    if (updated.siteSettings) {
      this.publisher.siteSettings = this.appData.siteSettings;
    }
    if (updated.subscriptionSettings) {
      this.subscriber.subscriptionSettings = this.appData.subscriptionSettings;
    }
    if (updated.lastUpdatedAt) {
      this.updateTimer.lastUpdatedAt = this.appData.lastUpdatedAt;
    }
    if (updated.updatePeriodMinutes) {
      this.updateTimer.updatePeriodMinutes = this.appData.updatePeriodMinutes;
    }
  }

  /**
   * @return {Promise} Promise that is resolved when initialization has done.
   */
  initialized() {
    return this.initializePromise || Promise.reject("Not started");
  }

  /**
   * @return {Promise.<AppData>}
   */
  getAppData() {
    return this.initialized().then(() => this.appData);
  }
<commit_msg>Fix saved option not set to notifier<commit_after>import _ from "lodash";
import Subscriber from "../subscriptions/subscriber";
import Publisher from "../publications/publisher";
import AppData from "../app/app-data";
import ExternalMessageReceiver from "./external-message-receiver";
import UpdateTimer from "./update-timer";
import Badge from "./badge";
import SubscriberNotifier from "./subscriber-notifier";
const logger = debug("background");

/**
 * Background controller
 */
export default class BackgroundController {
  constructor() {
    this.externalMessageReceiver = new ExternalMessageReceiver(this);
    this.badge = new Badge();
    this.appData = null;
    this.subscriber = null;
    this.publisher = null;
    this.updateTimer = null;
    this.notifier = null;
    this.initializePromise = null;
  }

  /**
   * Start controller to handle messages and events
   */
  start() {
    logger("Initializing");
    this.externalMessageReceiver.register();

    // Start initialization process by loading AppData.
    this.initializePromise = AppData.load().then(appData => {
      this._setupWithAppData(appData);
      return this;
    }).catch((e) => {
      console.error("Error while initialization in BackgroundController", e);
      throw e;
    });
    return this.initializePromise;
  }

  /**
   * Called when AppData is loaded first time.
   */
  _setupWithAppData(appData) {
    this.appData = appData;
    this.appData.on("update", this._handleAppDataUpdate.bind(this));

    this._setupPublisher();
    this._setupSubscriber();
    this._setupUpdateTimer();
  }

  _setupPublisher() {
    this.publisher = new Publisher(this.appData.siteSettings);
    logger("Initialized Publisher", this.publisher);
  }

  _setupSubscriber() {
    this.subscriber = new Subscriber(this.appData.subscriptionSettings);
    this.subscriber.on("update", () => {
      logger("Updated Subscriber", this.subscriber);
      this.badge.setCount(this.subscriber.getUnreadItemsCount());
      this.appData.subscriptionSettings = this.subscriber.subscriptionSettings;
      this.appData.save();
    });
    this.badge.setCount(this.subscriber.getUnreadItemsCount());
    this.notifier = new SubscriberNotifier(this.subscriber);
    logger("Initialized Subscriber", this.subscriber);
  }

  _setupUpdateTimer() {
    this.updateTimer = new UpdateTimer(
      this.appData.updatePeriodMinutes,
      this.appData.lastUpdatedAt
    );
    this.updateTimer.on("timer", () => {
      this.updateSubscriptions();
    });
    this.updateTimer.on("update", () => {
      logger("Updated UpdateTimer", this.updateTimer);
      this.appData.lastUpdatedAt = this.updateTimer.lastUpdatedAt;
      this.appData.updatePeriodMinutes = this.updateTimer.updatePeriodMinutes;
      this.appData.save();
    });
    this.updateTimer.start();
    logger("Initialized UpdateTimer", this.updateTimer);
  }

  /**
   * Called when AppData is updated by chrome.storage.
   */
  _handleAppDataUpdate(appData, keys) {
    const updated = _.keyBy(keys);
    if (updated.siteSettings) {
      this.publisher.siteSettings = this.appData.siteSettings;
    }
    if (updated.subscriptionSettings) {
      this.subscriber.subscriptionSettings = this.appData.subscriptionSettings;
    }
    if (updated.notificationSettings) {
      this.notifier.options = this.appData.notificationSettings;
    }
    if (updated.lastUpdatedAt) {
      this.updateTimer.lastUpdatedAt = this.appData.lastUpdatedAt;
    }
    if (updated.updatePeriodMinutes) {
      this.updateTimer.updatePeriodMinutes = this.appData.updatePeriodMinutes;
    }
  }

  /**
   * @return {Promise} Promise that is resolved when initialization has done.
   */
  initialized() {
    return this.initializePromise || Promise.reject("Not started");
  }

  /**
   * @return {Promise.<AppData>}
   */
  getAppData() {
    return this.initialized().then(() => this.appData);
  }

{'fraction_non_alphanumeric': 0.12786757038581856, 'fraction_numerical': 0.0, 'mean_word_length': 3.5241745283018866, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3940253', 'n_tokens_mistral': 2224, 'n_tokens_neox': 2205, 'n_words': 414}