Skip to content

Commit

Permalink
fix (client): don't delay initial snapshot to next tick (#1183)
Browse files Browse the repository at this point in the history
This PR modifies the Satellite process' `start` method in order not to
delay the initial snapshot to the next tick because that could cause the
snapshot to be ran after the process was closed, e.g. if someone starts
the process and immediately closes it. I also added a corresponding unit
test for this.
  • Loading branch information
kevin-dp authored Apr 23, 2024
1 parent b969100 commit 244033f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/spicy-shirts-smoke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"electric-sql": patch
---

Ensure no snapshot is taken after closing the Satellite process.
2 changes: 1 addition & 1 deletion clients/typescript/src/satellite/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ export class SatelliteProcess implements Satellite {
)

// Starting now!
setTimeout(this._throttledSnapshot, 0)
await this._throttledSnapshot()

// Need to reload primary keys after schema migration
this.relations = await this._getLocalRelations()
Expand Down
24 changes: 24 additions & 0 deletions clients/typescript/test/satellite/process.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2262,3 +2262,27 @@ test("don't leave a snapshot running when stopping", async (t) => {

t.pass()
})

test("don't snapshot after closing satellite process", async (t) => {
// open and then immediately close
// check that no snapshot is called after close
const { satellite, authState, token } = t.context
const { connectionPromise } = await startSatellite(
satellite,
authState,
token
)

await connectionPromise
await satellite.stop()

satellite._performSnapshot = () => {
t.fail('Snapshot was called')
return Promise.resolve(new Date())
}

// wait some time to see that mutexSnapshot is not called
await sleepAsync(50)

t.pass()
})

0 comments on commit 244033f

Please sign in to comment.