* NEW watcher pattern (after fix): batch insert + parallel remove/update + running guard. * Matches the updated src/client/store/watch.js.
(action)
| 154 | * Matches the updated src/client/store/watch.js. |
| 155 | */ |
| 156 | function createWatcherNew (action) { |
| 157 | const running = {} |
| 158 | return async function simulateWatcherNew (dbName, opts) { |
| 159 | if (running[dbName]) return |
| 160 | running[dbName] = true |
| 161 | try { |
| 162 | const { added, updated, removed } = opts |
| 163 | await Promise.all([ |
| 164 | ...removed.map(item => action(dbName, 'remove', { _id: item.id })), |
| 165 | ...updated.map(item => action(dbName, 'update', { _id: item.id }, { $set: item }, { upsert: false })), |
| 166 | added.length ? action(dbName, 'insert', added) : Promise.resolve() |
| 167 | ]) |
| 168 | const allItems = await action(dbName, 'find', {}) |
| 169 | const newOrder = allItems.map(d => d._id) |
| 170 | await action('data', 'update', |
| 171 | { _id: `${dbName}:order` }, |
| 172 | { $set: { value: newOrder } }, |
| 173 | { upsert: true } |
| 174 | ) |
| 175 | } finally { |
| 176 | running[dbName] = false |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | // Default for backward compat — instantiated in beforeEach |
| 182 | let simulateWatcher |
no test coverage detected