• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

CaptainFact / captain-fact-extension / 4652555733

pending completion
4652555733

push

github

GitHub
debt: Migrate to manifest V3 (#189)

27 of 112 branches covered (24.11%)

Branch coverage included in aggregate %.

65 of 65 new or added lines in 8 files covered. (100.0%)

60 of 209 relevant lines covered (28.71%)

0.74 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

43.75
/app/lib/data_cache.js
1
import { has } from 'lodash'
2
import HttpApi from './http_api'
3
import BrowserIconBadgeCounter from './browser_icon_badge_counter'
4
import { BrowserExtension } from './browser-extension'
5

6
export const CACHE_KEY = 'cache'
1✔
7
export const CACHE_VALIDITY = 15 * 60 * 1000 // 15 minutes
1✔
8
export const DEFAULT = {
1✔
9
  version: '0.8.0', // Changing this version will force the cache to do a full update
10
  lastId: 0, // Last fetched id
11
  lastUpdate: null, // Timestamp of the last update
12
  lastUpdateNbAdded: 0, // Number of videos added during the last update
13
  data: { youtube: [] }, // The goods
14
}
15

16
export default class DataCache {
17
  /**
18
   * Load the cache. Returns a promise that get passed the current cache.
19
   * If cache is too old, this also updates it.
20
   */
21
  static load() {
22
    return new Promise((fulfill) => {
5✔
23
      return BrowserExtension.storage.local.get(CACHE_KEY, (obj) => {
5✔
24
        if (has(obj, CACHE_KEY) && DataCache.checkVersion(obj[CACHE_KEY])) {
5✔
25
          return fulfill(obj[CACHE_KEY])
4✔
26
        }
27
        return fulfill(DEFAULT)
1✔
28
      })
29
    })
30
  }
31

32
  /**
33
   * Ensure cache is not outdated
34
   */
35
  static checkVersion(cache) {
36
    return cache && cache.version === DEFAULT.version
7✔
37
  }
38

39
  /**
40
   * Check if video exists in cache. Returns a promise with a boolean as first param
41
   */
42
  static hasVideo(provider, id) {
43
    return DataCache.updatedCache().then(({ data }) => {
3✔
44
      return data[provider] && data[provider].includes(id)
3✔
45
    })
46
  }
47

48
  /**
49
   * Update the cache by fetching a new version online **only if existing cache
50
   * is expired**.
51
   *
52
   * If update fails, returns current version of the cache
53
   */
54
  static updatedCache(force = false) {
3✔
55
    return DataCache.load().then((cache) => {
3✔
56
      // Check if cache is expired
57
      if (!force && !isCacheExpired(cache)) return cache
3!
58

59
      // Fetch new videos
60
      return HttpApi.post({
×
61
        query: `{videos(limit: 100000, filters: {minId: ${cache.lastId}}) { entries { id facebookId youtubeId } }}`,
62
      })
63
        .then((result) => {
64
          const videos = result?.videos?.entries
×
65

66
          // Do not update if there is no new video
67
          if (!videos || videos.length === 0) return cache
×
68

69
          // Add videos to cache
70
          const isFirstUpdate = cache.lastId === 0
×
71
          addVideosToCache(cache, videos)
×
72

73
          // Save new cache
74
          BrowserExtension.storage.local.set({ [CACHE_KEY]: cache }, () => {
×
75
            console.info('[CaptainFact] Cache updated')
×
76
            // Notify BrowserIconBadgeCounter that there are new videos.
77
            // This is bypassed on first run to avoid having the counter
78
            // displayed on installation.
79
            if (!isFirstUpdate) {
×
80
              BrowserIconBadgeCounter.increment(cache.lastUpdateNbAdded)
×
81
            }
82
          })
83
          return cache
×
84
        })
85
        .catch((e) => {
86
          console.error('[CaptainFact] Cache update failed')
×
87
          console.error(JSON.stringify(e))
×
88
          return cache
×
89
        })
90
    })
91
  }
92
}
93

94
/**
95
 * Add given `videos` to `cache` and update corresponding fields (lastId,
96
 * lastUpdate...etc).
97
 *
98
 * @param {Object} cache
99
 * @param {Array<Object>} videos
100
 */
101
function addVideosToCache(cache, videos) {
102
  let maxId = 0
×
103
  let nbAdded = 0
×
104
  for (const video of videos) {
×
105
    // Only support YouTube at the moment. Ignore other providers.
106
    if (video.youtubeId) {
×
107
      cache.data.youtube.push(video.youtubeId)
×
108
      nbAdded += 1
×
109
    }
110
    // Store last id
111
    const videoId = parseInt(video.id)
×
112
    if (videoId > maxId) maxId = videoId
×
113
  }
114
  cache.lastId = maxId
×
115
  cache.lastUpdate = Date.now()
×
116
  cache.lastUpdateNbAdded = nbAdded
×
117
  return cache
×
118
}
119

120
/**
121
 * Check if cache need to be updated
122
 *
123
 * @param {Object} cache
124
 */
125
function isCacheExpired(cache) {
126
  return !cache.lastUpdate || Date.now() - cache.lastUpdate > CACHE_VALIDITY
3✔
127
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc