Skip to content

Commit bd0a47c

Browse files
committed
Update Storage interface and docs for async writeMessages
Clarifies documentation for the Storage interface, explaining the rationale for synchronous APIs except for writeMessages, which is now explicitly async to support SQLite WASM requirements. Updates the writeMessages method signature to return Promise<boolean> and expands comments to detail the async design and its safety.
1 parent b518a22 commit bd0a47c

1 file changed

Lines changed: 21 additions & 3 deletions

File tree

packages/common/src/Evolu/Storage.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,14 @@ import {
3333
*
3434
* The protocol using Storage is agnostic to storage implementation details—any
3535
* storage can be plugged in, as long as it implements this interface.
36-
* Implementations must handle their own errors; return values only indicates
36+
* Implementations must handle their own errors; return values only indicate
3737
* overall success or failure.
38+
*
39+
* The Storage API is designed to be synchronous because SQLite's synchronous
40+
* API is the recommended and most performant way to use SQLite. Asynchronous
41+
* SQLite wrappers add unnecessary overhead and complexity. Only
42+
* {@link Storage#writeMessages} is async because of SQLite WASM requirements
43+
* (see its documentation for details).
3844
*/
3945
export interface Storage {
4046
readonly getSize: (ownerId: BinaryOwnerId) => NonNegativeInt | null;
@@ -81,11 +87,23 @@ export interface Storage {
8187
/** Sets the {@link WriteKey} for the given {@link Owner}. */
8288
readonly setWriteKey: (ownerId: BinaryOwnerId, writeKey: WriteKey) => boolean;
8389

84-
/** Write encrypted {@link CrdtMessage}s to storage. */
90+
/**
91+
* Write encrypted {@link CrdtMessage}s to storage.
92+
*
93+
* Returns `Promise<boolean>` because SQLite WASM must run in a web worker
94+
* while message validation must occur on the main thread. This requires async
95+
* communication between worker and main thread.
96+
*
97+
* The async nature means other operations can interleave between
98+
* `writeMessages` and subsequent sync operations during protocol message
99+
* application. This is safe because storage is append-only and the only
100+
* possible deletion is whole owner deletion. In that case, the sync operation
101+
* must fail anyway (to prevent current and future sync operations).
102+
*/
85103
readonly writeMessages: (
86104
ownerId: BinaryOwnerId,
87105
messages: NonEmptyReadonlyArray<EncryptedCrdtMessage>,
88-
) => boolean;
106+
) => Promise<boolean>;
89107

90108
/** Read encrypted {@link DbChange}s from storage. */
91109
readonly readDbChange: (

0 commit comments

Comments
 (0)