MCPcopy Create free account
hub / github.com/electric-sql/electric / startWritePath

Function startWritePath

examples/linearlite/src/sync.ts:140–164  ·  view source on GitHub ↗
(pg: PGliteWithExtensions)

Source from the content-addressed store, hash-verified

138const syncMutex = new Mutex()
139
140async 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
167async function doSyncToServer(pg: PGliteWithExtensions) {

Callers 1

startSyncFunction · 0.85

Calls 4

doSyncToServerFunction · 0.85
queryMethod · 0.80
acquireMethod · 0.80
releaseMethod · 0.80

Tested by

no test coverage detected