@@ -10,6 +10,7 @@ import {
1010 SlateDBPersistedReplicationStream ,
1111 SlateDBReplicationStreamRecord
1212} from './SlateDBPersistedSyncConfigContent.js' ;
13+ import { SlateDBSyncBucketStorage } from './SlateDBSyncBucketStorage.js' ;
1314
1415export type SlateDBBucketStorageOptions = {
1516 config : SlateDBStorageConfigDecoded ;
@@ -21,6 +22,8 @@ export class SlateDBBucketStorageFactory extends storage.BucketStorageFactory {
2122
2223 readonly replicationStreamNamePrefix : string ;
2324 private store : Promise < SlateDBKVStore > | undefined ;
25+ private resolvedStore : SlateDBKVStore | undefined ;
26+ private activeStorageCache : SlateDBSyncBucketStorage | undefined ;
2427
2528 constructor ( readonly options : SlateDBBucketStorageOptions ) {
2629 super ( ) ;
@@ -31,14 +34,27 @@ export class SlateDBBucketStorageFactory extends storage.BucketStorageFactory {
3134 this . store ??= SlateDBKVStore . open ( {
3235 path : this . options . config . path
3336 } ) ;
34- return this . store ;
37+ this . resolvedStore ??= await this . store ;
38+ return this . resolvedStore ;
3539 }
3640
3741 getInstance (
38- _replicationStream : storage . PersistedReplicationStream ,
42+ replicationStream : storage . PersistedReplicationStream ,
3943 _options ?: storage . GetIntanceOptions
4044 ) : storage . SyncRulesBucketStorage {
41- throw notImplemented ( 'getInstance' ) ;
45+ if ( ! ( replicationStream instanceof SlateDBPersistedReplicationStream ) ) {
46+ throw new Error ( `Expected SlateDBPersistedReplicationStream` ) ;
47+ }
48+ if ( this . activeStorageCache ?. replicationStreamId == replicationStream . replicationStreamId ) {
49+ return this . activeStorageCache ;
50+ }
51+ const store = this . resolvedStore ;
52+ if ( store == null ) {
53+ throw new Error ( `SlateDB store has not been initialized` ) ;
54+ }
55+ const instance = new SlateDBSyncBucketStorage ( this , store , replicationStream ) ;
56+ this . activeStorageCache = instance ;
57+ return instance ;
4258 }
4359
4460 async updateSyncRules ( _options : storage . UpdateSyncRulesOptions ) : Promise < storage . PersistedReplicationStream > {
@@ -174,6 +190,7 @@ export class SlateDBBucketStorageFactory extends storage.BucketStorageFactory {
174190 const store = await this . store ;
175191 await store [ Symbol . asyncDispose ] ( ) ;
176192 this . store = undefined ;
193+ this . resolvedStore = undefined ;
177194 }
178195 }
179196
0 commit comments