@@ -17,7 +17,9 @@ import type { Address, OpenOptions, ProgramClient } from "@peerbit/program";
1717import {
1818 type CanonicalOpenAdapter ,
1919 type CanonicalOpenMode ,
20+ type CanonicalOpenOptions ,
2021 createManagedProxy ,
22+ getProgramVariant ,
2123} from "./auto.js" ;
2224import type { CanonicalChannel } from "./client.js" ;
2325import type { CanonicalClient } from "./index.js" ;
@@ -242,7 +244,7 @@ export class PeerbitCanonicalClient {
242244
243245 async open < S extends Program < any > > (
244246 storeOrAddress : S | Address ,
245- openOptions : OpenOptions < S > = { } ,
247+ openOptions : CanonicalOpenOptions < S > = { } ,
246248 ) : Promise < S > {
247249 const state = this . openState ;
248250 if ( ! state || state . adapters . length === 0 ) {
@@ -269,16 +271,29 @@ export class PeerbitCanonicalClient {
269271 }
270272
271273 const program = storeOrAddress as Program < any > ;
274+ const programVariant = getProgramVariant ( program ) ;
272275 const adapter = state . adapters . find ( ( candidate ) =>
273- candidate . canOpen ( program ) ,
276+ typeof candidate . canOpen === "function"
277+ ? candidate . canOpen ( program )
278+ : ! ! programVariant &&
279+ ( candidate . variants ?? ( candidate . variant ? [ candidate . variant ] : [ ] ) )
280+ . map ( String )
281+ . includes ( programVariant ) ,
274282 ) ;
275283 if ( ! adapter ) {
284+ const knownVariants = state . adapters
285+ . flatMap ( ( candidate ) =>
286+ candidate . variants ?? ( candidate . variant ? [ candidate . variant ] : [ ] ) ,
287+ )
288+ . filter ( ( x ) : x is string => typeof x === "string" && x . length > 0 ) ;
276289 throw new Error (
277- `No canonical adapter registered for ${ program . constructor ?. name ?? "program" } ` ,
290+ `No canonical adapter registered for ${ program . constructor ?. name ?? "program" } ${
291+ programVariant ? ` (variant: '${ programVariant } ')` : ""
292+ } ${ knownVariants . length ? `. Known variants: ${ knownVariants . join ( ", " ) } ` : "" } `,
278293 ) ;
279294 }
280295
281- const key = adapter . getKey ?.( program as any , openOptions ) ;
296+ const key = adapter . getKey ?.( program as any , openOptions as OpenOptions < any > ) ;
282297 if ( adapter . getKey && key === undefined ) {
283298 throw new Error (
284299 `Canonical adapter '${ adapter . name } ' requires a cache key (adapter.getKey returned undefined)` ,
@@ -306,21 +321,21 @@ export class PeerbitCanonicalClient {
306321 if ( openOptions ?. parent ) {
307322 PeerbitCanonicalClient . attachParent (
308323 existingProxy as any ,
309- openOptions . parent ,
324+ openOptions . parent as any ,
310325 ) ;
311326 }
312327 return existingProxy as S ;
313328 }
314329 }
315330
316- const peer = this as any as ProgramClient ;
317- const openPromise = ( async ( ) => {
318- const result = await adapter . open ( {
319- program : program as any ,
320- options : openOptions ,
321- peer,
322- client : this . canonical ,
323- } ) ;
331+ const peer = this as any as ProgramClient ;
332+ const openPromise = ( async ( ) => {
333+ const result = await adapter . open ( {
334+ program : program as any ,
335+ options : openOptions as OpenOptions < any > ,
336+ peer,
337+ client : this . canonical ,
338+ } ) ;
324339
325340 let managed : any ;
326341 managed = createManagedProxy ( result . proxy as any , {
@@ -336,7 +351,7 @@ export class PeerbitCanonicalClient {
336351
337352 this . openState ?. proxies . add ( managed ) ;
338353 if ( openOptions ?. parent ) {
339- PeerbitCanonicalClient . attachParent ( managed , openOptions . parent ) ;
354+ PeerbitCanonicalClient . attachParent ( managed , openOptions . parent as any ) ;
340355 }
341356 return managed as S ;
342357 } ) ( ) ;
0 commit comments