()
| 38 | } |
| 39 | |
| 40 | async init() { |
| 41 | const storageKey = `chibiPages/${api.storage.version()}`; |
| 42 | const data: StorageInterface = this.useCache |
| 43 | ? ((await api.storage.get(storageKey)) as StorageInterface | null) || { |
| 44 | chibiPages: {}, |
| 45 | repoUrls: this.collections, |
| 46 | } |
| 47 | : { chibiPages: {}, repoUrls: this.collections }; |
| 48 | |
| 49 | if ( |
| 50 | this.useCache && |
| 51 | data.chibiPages && |
| 52 | Object.keys(data.chibiPages).length > 0 && |
| 53 | JSON.stringify(data.repoUrls) === JSON.stringify(this.collections) |
| 54 | ) { |
| 55 | this.pages = data.chibiPages; |
| 56 | return this; |
| 57 | } |
| 58 | |
| 59 | const collectionsData: ((PageListJsonInterface | { error: Error }) & { url: string })[] = |
| 60 | await Promise.all( |
| 61 | this.collections.map(c => |
| 62 | this.retrieveCollection(c) |
| 63 | .catch(e => { |
| 64 | return { |
| 65 | error: e, |
| 66 | }; |
| 67 | }) |
| 68 | .then(res => { |
| 69 | return { |
| 70 | ...res, |
| 71 | url: c, |
| 72 | }; |
| 73 | }), |
| 74 | ), |
| 75 | ); |
| 76 | |
| 77 | collectionsData.forEach(col => { |
| 78 | if ('error' in col) return; |
| 79 | Object.keys(col.pages).forEach(key => { |
| 80 | // Skip pages that require a higher version than current |
| 81 | if (col.pages[key].minimumVersion && greaterCurrentVersion(col.pages[key].minimumVersion)) { |
| 82 | return; |
| 83 | } |
| 84 | |
| 85 | const newer = |
| 86 | data.chibiPages[key] && |
| 87 | data.chibiPages[key].version.hash !== col.pages[key].version.hash && |
| 88 | Number(data.chibiPages[key].version.timestamp) < Number(col.pages[key].version.timestamp); |
| 89 | |
| 90 | if (!data.chibiPages[key] || newer) { |
| 91 | data.chibiPages[key] = col.pages[key]; |
| 92 | } |
| 93 | }); |
| 94 | }); |
| 95 | |
| 96 | await api.storage.set(storageKey, data); |
| 97 | this.pages = data.chibiPages; |
no test coverage detected