1- import { BucketDescription , RequestParameters , SqlSyncRules } from '@powersync/service-sync-rules' ;
1+ import { BucketDescription , isValidPriority , RequestParameters , SqlSyncRules } from '@powersync/service-sync-rules' ;
22
33import * as storage from '../storage/storage-index.js' ;
44import * as util from '../util/util-index.js' ;
@@ -20,6 +20,7 @@ export interface BucketChecksumStateOptions {
2020 bucketStorage : BucketChecksumStateStorage ;
2121 syncRules : SqlSyncRules ;
2222 syncParams : RequestParameters ;
23+ syncRequest : util . StreamingSyncRequest ;
2324 logger ?: Logger ;
2425 initialBucketPositions ?: { name : string ; after : util . InternalOpId } [ ] ;
2526}
@@ -70,6 +71,7 @@ export class BucketChecksumState {
7071 options . bucketStorage ,
7172 options . syncRules ,
7273 options . syncParams ,
74+ options . syncRequest ,
7375 this . logger
7476 ) ;
7577 this . bucketDataPositions = new Map ( ) ;
@@ -182,12 +184,12 @@ export class BucketChecksumState {
182184
183185 const updatedBucketDescriptions = diff . updatedBuckets . map ( ( e ) => ( {
184186 ...e ,
185- priority : bucketDescriptionMap . get ( e . bucket ) ! . priority
187+ ... bucketDescriptionMap . get ( e . bucket ) !
186188 } ) ) ;
187189 bucketsToFetch = [ ...generateBucketsToFetch ] . map ( ( b ) => {
188190 return {
189- bucket : b ,
190- priority : bucketDescriptionMap . get ( b ) ! . priority
191+ ... bucketDescriptionMap . get ( b ) ! ,
192+ bucket : b
191193 } ;
192194 } ) ;
193195
@@ -227,7 +229,7 @@ export class BucketChecksumState {
227229 write_checkpoint : writeCheckpoint ? String ( writeCheckpoint ) : undefined ,
228230 buckets : [ ...checksumMap . values ( ) ] . map ( ( e ) => ( {
229231 ...e ,
230- priority : bucketDescriptionMap . get ( e . bucket ) ! . priority
232+ ... bucketDescriptionMap . get ( e . bucket ) !
231233 } ) )
232234 }
233235 } satisfies util . StreamingSyncCheckpoint ;
@@ -336,6 +338,7 @@ export class BucketParameterState {
336338 public readonly syncParams : RequestParameters ;
337339 private readonly querier : BucketParameterQuerier ;
338340 private readonly staticBuckets : Map < string , BucketDescription > ;
341+ private readonly explicitStreamSubscriptions : Record < string , util . StreamSubscription > ;
339342 private readonly logger : Logger ;
340343 private cachedDynamicBuckets : BucketDescription [ ] | null = null ;
341344 private cachedDynamicBucketSet : Set < string > | null = null ;
@@ -347,6 +350,7 @@ export class BucketParameterState {
347350 bucketStorage : BucketChecksumStateStorage ,
348351 syncRules : SqlSyncRules ,
349352 syncParams : RequestParameters ,
353+ request : util . StreamingSyncRequest ,
350354 logger : Logger
351355 ) {
352356 this . context = context ;
@@ -355,11 +359,48 @@ export class BucketParameterState {
355359 this . syncParams = syncParams ;
356360 this . logger = logger ;
357361
358- this . querier = syncRules . getBucketParameterQuerier ( this . syncParams ) ;
362+ const explicitStreamSubscriptions : Record < string , util . StreamSubscription > = { } ;
363+ const subscriptions = request . subscriptions ;
364+ if ( subscriptions ) {
365+ for ( const subscription of subscriptions . subscriptions ) {
366+ explicitStreamSubscriptions [ subscription . stream ] = subscription ;
367+ }
368+ }
369+ this . explicitStreamSubscriptions = explicitStreamSubscriptions ;
370+
371+ this . querier = syncRules . getBucketParameterQuerier ( {
372+ globalParameters : this . syncParams ,
373+ hasDefaultSubscriptions : subscriptions ?. include_defaults ?? true ,
374+ resolveSubscription ( name ) {
375+ const subscription = explicitStreamSubscriptions [ name ] ;
376+ if ( subscription ) {
377+ return subscription . parameters ?? { } ;
378+ } else {
379+ return null ;
380+ }
381+ }
382+ } ) ;
359383 this . staticBuckets = new Map < string , BucketDescription > ( this . querier . staticBuckets . map ( ( b ) => [ b . bucket , b ] ) ) ;
360384 this . lookups = new Set < string > ( this . querier . parameterQueryLookups . map ( ( l ) => JSONBig . stringify ( l . values ) ) ) ;
361385 }
362386
387+ /**
388+ * Overrides the `description` based on subscriptions from the client.
389+ *
390+ * In partiuclar, this can override the priority assigned to a bucket.
391+ */
392+ overrideBucketDescription ( description : BucketDescription ) : BucketDescription {
393+ const changedPriority = this . explicitStreamSubscriptions [ description . definition ] ?. override_priority ;
394+ if ( changedPriority != null && isValidPriority ( changedPriority ) ) {
395+ return {
396+ ...description ,
397+ priority : changedPriority
398+ } ;
399+ } else {
400+ return description ;
401+ }
402+ }
403+
363404 async getCheckpointUpdate ( checkpoint : storage . StorageCheckpointUpdate ) : Promise < CheckpointUpdate > {
364405 const querier = this . querier ;
365406 let update : CheckpointUpdate ;
0 commit comments