utils#isGuerrillaLive JavaScript Examples

The following examples show how to use utils#isGuerrillaLive. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: index.js    From holo-schedule with MIT License 4 votes vote down vote up
alarm = {
  $defaultIsNtfEnabled: true,
  $store: undefined,
  livesToAlarm: undefined,
  firedAlarms: undefined,
  savedCurrentLives: [],
  savedScheduledLives: [],
  schedule(live) {
    this.livesToAlarm.add(live)
    return this.$store.set({ [LIVES_TO_ALARM]: JSON.parse(JSON.stringify(this.livesToAlarm)) })
  },
  remove(live) {
    this.livesToAlarm.remove(find(this.livesToAlarm, { id: live['id'] }))
    return this.$store.set({ [LIVES_TO_ALARM]: JSON.parse(JSON.stringify(this.livesToAlarm)) })
  },
  isScheduled(live) {
    return find(this.livesToAlarm, { id: live['id'] })
  },
  getIsNtfEnabled() {
    return this.$store.get(IS_NTF_ENABLED) ?? this.$defaultIsNtfEnabled
  },
  fire(live, isGuerrilla = false) {
    const { id, title } = live

    this.remove({ id })

    if ((find(this.firedAlarms, { id }) && isGuerrilla) || !this.getIsNtfEnabled()) return

    const member = workflows.getMember(live)

    this.firedAlarms.add({ id })
    this.$store.set({ [FIRED_ALARMS]: JSON.parse(JSON.stringify(this.firedAlarms)) })

    notification.create(id.toString(), {
      title,
      message: i18n.getMessage(
        `notification.${isGuerrilla ? 'guerrilla' : 'reminder'}`, { name: member['name'] },
      ),
      iconUrl: member['avatar'] ?? browser.runtime.getURL('assets/default_avatar.png'),
      onClick() {
        browser.tabs.create({ url: constructUrl(live) }).then(
          () => console.log('[background/alarm]Successfully created a tab'),
        )
      },
    })
  },
  async init(store) {
    this.$store = store
    this.livesToAlarm = createEnhancedArray(this.$store.get(LIVES_TO_ALARM), 50)
    this.firedAlarms = createEnhancedArray(this.$store.get(FIRED_ALARMS), 30)

    await store.set({ [IS_NTF_ENABLED]: this.getIsNtfEnabled() })

    store.subscribe(CURRENT_LIVES, (lives, prevLives) => {
      // Skip the first run
      if (prevLives === undefined) {
        return
      }

      differenceBy(lives, prevLives, 'id').forEach(live => {
        // Scheduled alarms
        if (this.isScheduled(live)) {
          this.fire(live)
        }
        // Guerrilla lives
        if (isGuerrillaLive(live)) {
          this.fire(live, true)
        }
      })
    })

    store.subscribe(SCHEDULED_LIVES, (lives, prevLives) => {
      // Skip the first run
      if (prevLives === undefined) {
        return
      }

      // Guerrilla lives
      differenceBy(lives, prevLives, 'id').forEach(live => {
        if (isGuerrillaLive(live)) {
          this.fire(live, true)
        }
      })
    })
  },
}