support subscription label, not only an absolute id.
This commit is contained in:
parent
3c4019a154
commit
05eb62da5b
|
@ -20,6 +20,7 @@ export type SubscribeManyParams = Omit<SubscriptionParams, 'onclose'> & {
|
||||||
maxWait?: number
|
maxWait?: number
|
||||||
onclose?: (reasons: string[]) => void
|
onclose?: (reasons: string[]) => void
|
||||||
id?: string
|
id?: string
|
||||||
|
label?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export class AbstractSimplePool {
|
export class AbstractSimplePool {
|
||||||
|
@ -156,7 +157,7 @@ export class AbstractSimplePool {
|
||||||
subscribeManyEose(
|
subscribeManyEose(
|
||||||
relays: string[],
|
relays: string[],
|
||||||
filters: Filter[],
|
filters: Filter[],
|
||||||
params: Pick<SubscribeManyParams, 'id' | 'onevent' | 'onclose' | 'maxWait'>,
|
params: Pick<SubscribeManyParams, 'label' | 'id' | 'onevent' | 'onclose' | 'maxWait'>,
|
||||||
): SubCloser {
|
): SubCloser {
|
||||||
const subcloser = this.subscribeMany(relays, filters, {
|
const subcloser = this.subscribeMany(relays, filters, {
|
||||||
...params,
|
...params,
|
||||||
|
@ -170,7 +171,7 @@ export class AbstractSimplePool {
|
||||||
async querySync(
|
async querySync(
|
||||||
relays: string[],
|
relays: string[],
|
||||||
filter: Filter,
|
filter: Filter,
|
||||||
params?: Pick<SubscribeManyParams, 'id' | 'maxWait'>,
|
params?: Pick<SubscribeManyParams, 'label' | 'id' | 'maxWait'>,
|
||||||
): Promise<Event[]> {
|
): Promise<Event[]> {
|
||||||
return new Promise(async resolve => {
|
return new Promise(async resolve => {
|
||||||
const events: Event[] = []
|
const events: Event[] = []
|
||||||
|
@ -189,7 +190,7 @@ export class AbstractSimplePool {
|
||||||
async get(
|
async get(
|
||||||
relays: string[],
|
relays: string[],
|
||||||
filter: Filter,
|
filter: Filter,
|
||||||
params?: Pick<SubscribeManyParams, 'id' | 'maxWait'>,
|
params?: Pick<SubscribeManyParams, 'label' | 'id' | 'maxWait'>,
|
||||||
): Promise<Event | null> {
|
): Promise<Event | null> {
|
||||||
filter.limit = 1
|
filter.limit = 1
|
||||||
const events = await this.querySync(relays, filter, params)
|
const events = await this.querySync(relays, filter, params)
|
||||||
|
|
|
@ -279,15 +279,21 @@ export class AbstractRelay {
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
public subscribe(filters: Filter[], params: Partial<SubscriptionParams> & { id?: string }): Subscription {
|
public subscribe(
|
||||||
|
filters: Filter[],
|
||||||
|
params: Partial<SubscriptionParams> & { label?: string; id?: string },
|
||||||
|
): Subscription {
|
||||||
const subscription = this.prepareSubscription(filters, params)
|
const subscription = this.prepareSubscription(filters, params)
|
||||||
subscription.fire()
|
subscription.fire()
|
||||||
return subscription
|
return subscription
|
||||||
}
|
}
|
||||||
|
|
||||||
public prepareSubscription(filters: Filter[], params: Partial<SubscriptionParams> & { id?: string }): Subscription {
|
public prepareSubscription(
|
||||||
|
filters: Filter[],
|
||||||
|
params: Partial<SubscriptionParams> & { label?: string; id?: string },
|
||||||
|
): Subscription {
|
||||||
this.serial++
|
this.serial++
|
||||||
const id = params.id || 'sub:' + this.serial
|
const id = params.id || (params.label ? params.label + ':' : 'sub:') + this.serial
|
||||||
const subscription = new Subscription(this, id, filters, params)
|
const subscription = new Subscription(this, id, filters, params)
|
||||||
this.openSubs.set(id, subscription)
|
this.openSubs.set(id, subscription)
|
||||||
return subscription
|
return subscription
|
||||||
|
|
Loading…
Reference in New Issue