@@ -115,12 +115,8 @@ function isUpToDateMessage<T extends Row<unknown>>(
115
115
// Check if a message contains txids in its headers
116
116
function hasTxids < T extends Row < unknown > > (
117
117
message : Message < T >
118
- ) : message is Message < T > & { headers : { txids ?: Array < number > } } {
119
- return (
120
- `headers` in message &&
121
- `txids` in message . headers &&
122
- Array . isArray ( message . headers . txids )
123
- )
118
+ ) : message is Message < T > & { headers : { txids ?: Array < string > } } {
119
+ return `txids` in message . headers && Array . isArray ( message . headers . txids )
124
120
}
125
121
126
122
/**
@@ -149,7 +145,7 @@ export function electricCollectionOptions<
149
145
TSchema extends StandardSchemaV1 = never ,
150
146
TFallback extends Row < unknown > = Row < unknown > ,
151
147
> ( config : ElectricCollectionConfig < TExplicit , TSchema , TFallback > ) {
152
- const seenTxids = new Store < Set < string > > ( new Set ( [ ` ${ Math . random ( ) } ` ] ) )
148
+ const seenTxids = new Store < Set < string > > ( new Set ( [ ] ) )
153
149
const sync = createElectricSync < ResolveType < TExplicit , TSchema , TFallback > > (
154
150
config . shapeOptions ,
155
151
{
@@ -165,7 +161,7 @@ export function electricCollectionOptions<
165
161
*/
166
162
const awaitTxId : AwaitTxIdFn = async (
167
163
txId : string ,
168
- timeout = 30000
164
+ timeout : number = 30000
169
165
) : Promise < boolean > => {
170
166
if ( typeof txId !== `string` ) {
171
167
throw new TypeError (
@@ -246,18 +242,14 @@ export function electricCollectionOptions<
246
242
ResolveType < TExplicit , TSchema , TFallback >
247
243
>
248
244
) => {
249
- // Runtime check (that doesn't follow type)
250
- // eslint-disable-next-line
251
- const handlerResult = ( await config . onDelete ! ( params ) ) ?? { }
252
- const txid = ( handlerResult as { txid ?: string } ) . txid
253
-
254
- if ( ! txid ) {
245
+ const handlerResult = await config . onDelete ! ( params )
246
+ if ( ! handlerResult . txid ) {
255
247
throw new Error (
256
248
`Electric collection onDelete handler must return a txid`
257
249
)
258
250
}
259
251
260
- await awaitTxId ( txid )
252
+ await awaitTxId ( handlerResult . txid )
261
253
return handlerResult
262
254
}
263
255
: undefined
@@ -333,43 +325,37 @@ function createElectricSync<T extends Row<unknown>>(
333
325
signal : abortController . signal ,
334
326
} )
335
327
let transactionStarted = false
336
- let newTxids = new Set < string > ( )
328
+ const newTxids = new Set < string > ( )
337
329
338
330
unsubscribeStream = stream . subscribe ( ( messages : Array < Message < T > > ) => {
339
331
let hasUpToDate = false
340
332
341
333
for ( const message of messages ) {
342
334
// Check for txids in the message and add them to our store
343
- if ( hasTxids ( message ) && message . headers . txids ) {
344
- message . headers . txids . forEach ( ( txid ) => newTxids . add ( String ( txid ) ) )
335
+ if ( hasTxids ( message ) ) {
336
+ message . headers . txids ? .forEach ( ( txid ) => newTxids . add ( txid ) )
345
337
}
346
338
347
- // Check if the message contains schema information
348
- if ( isChangeMessage ( message ) && message . headers . schema ) {
349
- // Store the schema for future use if it's a valid string
350
- if ( typeof message . headers . schema === `string` ) {
351
- const schema : string = message . headers . schema
339
+ if ( isChangeMessage ( message ) ) {
340
+ // Check if the message contains schema information
341
+ const schema = message . headers . schema
342
+ if ( schema && typeof schema === `string` ) {
343
+ // Store the schema for future use if it's a valid string
352
344
relationSchema . setState ( ( ) => schema )
353
345
}
354
- }
355
346
356
- if ( isChangeMessage ( message ) ) {
357
347
if ( ! transactionStarted ) {
358
348
begin ( )
359
349
transactionStarted = true
360
350
}
361
351
362
- const value = message . value as unknown as T
363
-
364
- // Include the primary key and relation info in the metadata
365
- const enhancedMetadata = {
366
- ...message . headers ,
367
- }
368
-
369
352
write ( {
370
353
type : message . headers . operation ,
371
- value,
372
- metadata : enhancedMetadata ,
354
+ value : message . value ,
355
+ // Include the primary key and relation info in the metadata
356
+ metadata : {
357
+ ...message . headers ,
358
+ } ,
373
359
} )
374
360
} else if ( isUpToDateMessage ( message ) ) {
375
361
hasUpToDate = true
@@ -390,10 +376,9 @@ function createElectricSync<T extends Row<unknown>>(
390
376
391
377
// Always commit txids when we receive up-to-date, regardless of transaction state
392
378
seenTxids . setState ( ( currentTxids ) => {
393
- const clonedSeen = new Set ( currentTxids )
394
- newTxids . forEach ( ( txid ) => clonedSeen . add ( String ( txid ) ) )
395
-
396
- newTxids = new Set ( )
379
+ const clonedSeen = new Set < string > ( currentTxids )
380
+ newTxids . forEach ( ( txid ) => clonedSeen . add ( txid ) )
381
+ newTxids . clear ( )
397
382
return clonedSeen
398
383
} )
399
384
}
0 commit comments