(files)
| 79 | } |
| 80 | |
| 81 | async function sync(files) { |
| 82 | const db = await getDB(); |
| 83 | const existingRecords = await getExistingRecords(db); |
| 84 | let indexed = 0; |
| 85 | let skipped = 0; |
| 86 | const total = files.length; |
| 87 | |
| 88 | postStatus({ |
| 89 | state: total ? "indexing" : "ready", |
| 90 | indexed, |
| 91 | total, |
| 92 | message: total ? "Preparing search cache" : "Search cache ready", |
| 93 | }); |
| 94 | |
| 95 | for (const file of files) { |
| 96 | const currentRecord = existingRecords.get(file.url); |
| 97 | |
| 98 | if ( |
| 99 | currentRecord && |
| 100 | !dirtyUrls.has(file.url) && |
| 101 | isRecordCurrent(file, currentRecord) |
| 102 | ) { |
| 103 | indexed += 1; |
| 104 | if (currentRecord.skipped) skipped += 1; |
| 105 | continue; |
| 106 | } |
| 107 | |
| 108 | try { |
| 109 | if (file.size && file.size > MAX_INDEXED_CHARS) { |
| 110 | const record = buildSkippedRecord(file, "large"); |
| 111 | await writeRecord(db, currentRecord, record); |
| 112 | dirtyUrls.delete(file.url); |
| 113 | indexed += 1; |
| 114 | skipped += 1; |
| 115 | await yieldToEventLoop(INDEX_IDLE_DELAY); |
| 116 | continue; |
| 117 | } |
| 118 | |
| 119 | const content = await getFile(file.url); |
| 120 | const record = buildRecord(file, content); |
| 121 | await writeRecord(db, currentRecord, record); |
| 122 | dirtyUrls.delete(file.url); |
| 123 | indexed += 1; |
| 124 | if (record.skipped) skipped += 1; |
| 125 | await yieldToEventLoop(INDEX_IDLE_DELAY); |
| 126 | } catch { |
| 127 | skipped += 1; |
| 128 | } |
| 129 | |
| 130 | if (indexed % 25 === 0 || indexed === total) { |
| 131 | postStatus({ |
| 132 | state: "indexing", |
| 133 | indexed, |
| 134 | total, |
| 135 | skipped, |
| 136 | message: `Cached ${indexed}/${total} files`, |
| 137 | }); |
| 138 | await yieldToEventLoop(); |
no test coverage detected