@@ -116,81 +116,83 @@ type KeyExhangeOptions = {
116116 groupKeys ?: Record < string , GroupKeysSerialized >
117117}
118118
119- export function PublisherKeyExhange ( client : StreamrClient , { groupKeys = { } } : KeyExhangeOptions = { } ) {
120- let enabled = true
121- const getGroupKeyStore = pMemoize ( async ( streamId ) => {
122- const clientId = await client . getAddress ( )
119+ export class PublisherKeyExhange {
120+ enabled = true
121+ next
122+ client
123+ initialGroupKeys
124+ constructor ( client : StreamrClient , { groupKeys = { } } : KeyExhangeOptions = { } ) {
125+ this . client = client
126+ this . initialGroupKeys = groupKeys
127+ this . getGroupKeyStore = pMemoize ( this . getGroupKeyStore . bind ( this ) , {
128+ cacheKey ( [ maybeStreamId ] ) {
129+ const { streamId } = validateOptions ( maybeStreamId )
130+ return streamId
131+ }
132+ } )
133+
134+ let sub : Subscription | undefined
135+ this . next = Scaffold ( [
136+ async ( ) => {
137+ sub = await PublisherKeyExhangeSubscription ( client , this . getGroupKeyStore )
138+ return async ( ) => {
139+ if ( ! sub ) { return }
140+ const cancelTask = sub . cancel ( )
141+ sub = undefined
142+ await cancelTask
143+ }
144+ }
145+ ] , async ( ) => this . enabled )
146+ }
147+
148+ async getGroupKeyStore ( streamId : string ) {
149+ const clientId = await this . client . getAddress ( )
123150 return new GroupKeyStore ( {
124151 clientId,
125152 streamId,
126- groupKeys : [ ...parseGroupKeys ( groupKeys [ streamId ] ) . entries ( ) ]
153+ groupKeys : [ ...parseGroupKeys ( this . initialGroupKeys [ streamId ] ) . entries ( ) ]
127154 } )
128- } , {
129- cacheKey ( [ maybeStreamId ] ) {
130- const { streamId } = validateOptions ( maybeStreamId )
131- return streamId
132- }
133- } )
134-
135- let sub : Subscription | undefined
136- const next = Scaffold ( [
137- async ( ) => {
138- sub = await PublisherKeyExhangeSubscription ( client , getGroupKeyStore )
139- return async ( ) => {
140- if ( ! sub ) { return }
141- const cancelTask = sub . cancel ( )
142- sub = undefined
143- await cancelTask
144- }
145- }
146- ] , async ( ) => enabled )
155+ }
147156
148- async function rotateGroupKey ( streamId : string ) {
149- if ( ! enabled ) { return }
150- const groupKeyStore = await getGroupKeyStore ( streamId )
157+ async rotateGroupKey ( streamId : string ) {
158+ if ( ! this . enabled ) { return }
159+ const groupKeyStore = await this . getGroupKeyStore ( streamId )
151160 await groupKeyStore . rotateGroupKey ( )
152161 }
153162
154- async function setNextGroupKey ( streamId : string , groupKey : GroupKey ) {
155- if ( ! enabled ) { return }
156- const groupKeyStore = await getGroupKeyStore ( streamId )
163+ async setNextGroupKey ( streamId : string , groupKey : GroupKey ) {
164+ if ( ! this . enabled ) { return }
165+ const groupKeyStore = await this . getGroupKeyStore ( streamId )
157166
158167 await groupKeyStore . setNextGroupKey ( groupKey )
159168 }
160169
161- async function useGroupKey ( streamId : string ) {
162- await next ( )
163- if ( ! enabled ) { return [ ] }
164- const groupKeyStore = await getGroupKeyStore ( streamId )
170+ async useGroupKey ( streamId : string ) {
171+ await this . next ( )
172+ if ( ! this . enabled ) { return [ ] }
173+ const groupKeyStore = await this . getGroupKeyStore ( streamId )
165174 return groupKeyStore . useGroupKey ( )
166175 }
167176
168- async function hasAnyGroupKey ( streamId : string ) {
169- const groupKeyStore = await getGroupKeyStore ( streamId )
177+ async hasAnyGroupKey ( streamId : string ) {
178+ const groupKeyStore = await this . getGroupKeyStore ( streamId )
170179 return ! groupKeyStore . isEmpty ( )
171180 }
172181
173- async function rekey ( streamId : string ) {
174- if ( ! enabled ) { return }
175- const groupKeyStore = await getGroupKeyStore ( streamId )
182+ async rekey ( streamId : string ) {
183+ if ( ! this . enabled ) { return }
184+ const groupKeyStore = await this . getGroupKeyStore ( streamId )
176185 await groupKeyStore . rekey ( )
177- await next ( )
186+ await this . next ( )
187+ }
188+ async start ( ) {
189+ this . enabled = true
190+ return this . next ( )
178191 }
179192
180- return {
181- setNextGroupKey,
182- useGroupKey,
183- rekey,
184- rotateGroupKey,
185- hasAnyGroupKey,
186- async start ( ) {
187- enabled = true
188- return next ( )
189- } ,
190- async stop ( ) {
191- pMemoize . clear ( getGroupKeyStore )
192- enabled = false
193- return next ( )
194- }
193+ async stop ( ) {
194+ pMemoize . clear ( this . getGroupKeyStore )
195+ this . enabled = false
196+ return this . next ( )
195197 }
196198}
0 commit comments