(pg: PGliteWithExtensions)
| 138 | const syncMutex = new Mutex() |
| 139 | |
| 140 | async function startWritePath(pg: PGliteWithExtensions) { |
| 141 | // Use a live query to watch for changes to the local tables that need to be synced |
| 142 | pg.live.query<{ |
| 143 | issue_count: number |
| 144 | comment_count: number |
| 145 | }>( |
| 146 | ` |
| 147 | SELECT * FROM |
| 148 | (SELECT count(id) as issue_count FROM issue WHERE synced = false), |
| 149 | (SELECT count(id) as comment_count FROM comment WHERE synced = false) |
| 150 | `, |
| 151 | [], |
| 152 | async (results) => { |
| 153 | const { issue_count, comment_count } = results.rows[0] |
| 154 | if (issue_count > 0 || comment_count > 0) { |
| 155 | await syncMutex.acquire() |
| 156 | try { |
| 157 | doSyncToServer(pg) |
| 158 | } finally { |
| 159 | syncMutex.release() |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | ) |
| 164 | } |
| 165 | |
| 166 | // Call wrapped in mutex to prevent multiple syncs from happening at the same time |
| 167 | async function doSyncToServer(pg: PGliteWithExtensions) { |
no test coverage detected