()
| 194 | } |
| 195 | |
| 196 | export function createTransaction() { |
| 197 | const changes: Change[] = []; |
| 198 | |
| 199 | let db: ClientDb | null = null; |
| 200 | |
| 201 | function pushChange(change: Change) { |
| 202 | if (db && change.db !== db) { |
| 203 | throw new Error( |
| 204 | "Single transaction cannot include changes across multiple instances of clientdb" |
| 205 | ); |
| 206 | } |
| 207 | |
| 208 | if (!db) { |
| 209 | db = change.db; |
| 210 | } |
| 211 | |
| 212 | registerEntityChange(change); |
| 213 | changes.push(change); |
| 214 | } |
| 215 | |
| 216 | function getChanges(): Change[] { |
| 217 | return changes; |
| 218 | } |
| 219 | |
| 220 | function commit() { |
| 221 | for (const change of changes) { |
| 222 | removeChange(change); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | function reject() { |
| 227 | const entitiesToRebase = new Set<AnyEntity>(); |
| 228 | for (const change of changes) { |
| 229 | entitiesToRebase.add(change.entity); |
| 230 | change.rollback(); |
| 231 | deregisterEntityChange(change); |
| 232 | } |
| 233 | |
| 234 | for (const entity of entitiesToRebase) { |
| 235 | rebaseEntityChanges(entity); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | function undo() { |
| 240 | const entitiesToRebase = new Set<AnyEntity>(); |
| 241 | for (const change of changes) { |
| 242 | entitiesToRebase.add(change.entity); |
| 243 | undoEntityChange(change); |
| 244 | } |
| 245 | |
| 246 | for (const entity of entitiesToRebase) { |
| 247 | rebaseEntityChanges(entity); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | return { |
| 252 | pushChange, |
| 253 | getChanges, |
no outgoing calls
no test coverage detected