* OLD watcher pattern (before fix): sequential one-by-one operations. * This is what caused the app to hang on large imports.
(dbName, action, { added, updated, removed })
| 131 | * This is what caused the app to hang on large imports. |
| 132 | */ |
| 133 | async function simulateWatcherOld (dbName, action, { added, updated, removed }) { |
| 134 | for (const item of removed) { |
| 135 | await action(dbName, 'remove', { _id: item.id }) |
| 136 | } |
| 137 | for (const item of updated) { |
| 138 | await action(dbName, 'update', { _id: item.id }, { $set: item }, { upsert: false }) |
| 139 | } |
| 140 | for (const item of added) { |
| 141 | await action(dbName, 'insert', item) |
| 142 | } |
| 143 | const allItems = await action(dbName, 'find', {}) |
| 144 | const newOrder = allItems.map(d => d._id) |
| 145 | await action('data', 'update', |
| 146 | { _id: `${dbName}:order` }, |
| 147 | { $set: { value: newOrder } }, |
| 148 | { upsert: true } |
| 149 | ) |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * NEW watcher pattern (after fix): batch insert + parallel remove/update + running guard. |
no test coverage detected