@ -8,11 +8,12 @@ type SubCallback = (
type Subscribe = {
type Subscribe = {
cb : SubCallback ;
cb : SubCallback ;
filter : Filter ;
filter : Filter ;
unsub? : boolean ;
} ;
} ;
const relayList : Array < Relay > = [ ] ;
const subList : Array < Sub > = [ ] ;
const subList : Array < Sub > = [ ] ;
const currentSubList : Array < Subscribe > = [ ] ;
const currentSubList : Array < Subscribe > = [ ] ;
const relayMap = new Map < string , Relay > ( ) ;
export const addRelay = async ( url : string ) = > {
export const addRelay = async ( url : string ) = > {
const relay = relayInit ( url ) ;
const relay = relayInit ( url ) ;
@ -25,13 +26,13 @@ export const addRelay = async (url: string) => {
try {
try {
await relay . connect ( ) ;
await relay . connect ( ) ;
currentSubList . forEach ( ( { cb , filter } ) = > subscribe ( cb , filter , relay ) ) ;
currentSubList . forEach ( ( { cb , filter } ) = > subscribe ( cb , filter , relay ) ) ;
relay List. push ( relay ) ;
relay Map. set ( url , relay ) ;
} catch {
} catch {
console . warn ( ` could not connect to ${ url } ` ) ;
console . warn ( ` could not connect to ${ url } ` ) ;
}
}
} ;
} ;
const unsubscribe = ( sub : Sub ) = > {
export const unsubscribe = ( sub : Sub ) = > {
sub . unsub ( ) ;
sub . unsub ( ) ;
subList . splice ( subList . indexOf ( sub ) , 1 ) ;
subList . splice ( subList . indexOf ( sub ) , 1 ) ;
} ;
} ;
@ -40,28 +41,38 @@ const subscribe = (
cb : SubCallback ,
cb : SubCallback ,
filter : Filter ,
filter : Filter ,
relay : Relay ,
relay : Relay ,
unsub? : boolean
) = > {
) = > {
const sub = relay . sub ( [ filter ] ) ;
const sub = relay . sub ( [ filter ] ) ;
subList . push ( sub ) ;
subList . push ( sub ) ;
sub . on ( 'event' , ( event : Event ) = > {
sub . on ( 'event' , ( event : Event ) = > {
cb ( event , relay . url ) ;
cb ( event , relay . url ) ;
} ) ;
} ) ;
if ( unsub ) {
sub . on ( 'eose' , ( ) = > {
sub . on ( 'eose' , ( ) = > {
// console.log('eose', relay.url);
console . log ( 'eose' , relay . url ) ;
// unsubscribe(sub);
unsubscribe ( sub ) ;
} ) ;
} ) ;
} ;
}
return sub ;
const subscribeAll = (
cb : SubCallback ,
filter : Filter ,
) = > {
relayList . forEach ( relay = > subscribe ( cb , filter , relay ) ) ;
} ;
} ;
export const sub = ( obj : Subscribe ) = > {
export const sub = ( obj : Subscribe ) = > {
currentSubList . push ( obj ) ;
currentSubList . push ( obj ) ;
subscribeAll ( obj . cb , obj . filter ) ;
relayMap . forEach ( ( relay ) = > subscribe ( obj . cb , obj . filter , relay , obj . unsub ) ) ;
} ;
export const subOnce = (
obj : Subscribe & { relay : string }
) = > {
const relay = relayMap . get ( obj . relay ) ;
if ( relay ) {
const sub = subscribe ( obj . cb , obj . filter , relay ) ;
sub . on ( 'eose' , ( ) = > {
console . log ( 'eose' , obj . relay ) ;
unsubscribe ( sub ) ;
} ) ;
}
} ;
} ;
export const unsubAll = ( ) = > {
export const unsubAll = ( ) = > {
@ -78,7 +89,7 @@ export const publish = (
event : Event ,
event : Event ,
cb : PublishCallback ,
cb : PublishCallback ,
) = > {
) = > {
relay List. forEach ( relay = > {
relay Map. forEach ( ( relay , url ) = > {
const pub = relay . publish ( event ) ;
const pub = relay . publish ( event ) ;
pub . on ( 'ok' , ( ) = > {
pub . on ( 'ok' , ( ) = > {
console . info ( ` ${ relay . url } has accepted our event ` ) ;
console . info ( ` ${ relay . url } has accepted our event ` ) ;
@ -91,9 +102,10 @@ export const publish = (
} ) ;
} ) ;
} ;
} ;
addRelay ( 'wss://relay.snort.social' ) ; // good one
addRelay ( 'wss://relay.snort.social' ) ;
addRelay ( 'wss://nostr.bitcoiner.social' ) ;
addRelay ( 'wss://nostr.bitcoiner.social' ) ;
addRelay ( 'wss://nostr.mom' ) ;
addRelay ( 'wss://nostr.mom' ) ;
addRelay ( 'wss://relay.nostr.bg' ) ;
addRelay ( 'wss://relay.nostr.bg' ) ;
addRelay ( 'wss://nos.lol' ) ;
addRelay ( 'wss://nos.lol' ) ;
addRelay ( 'wss://relay.nostr.ch' ) ;
// addRelay('wss://relay.nostr.ch');