finalize cb api

This commit is contained in:
monica
2022-12-07 22:26:51 -06:00
parent b955ba2a09
commit 100c77d2aa
3 changed files with 117 additions and 90 deletions

View File

@@ -14,41 +14,59 @@ pool.setPrivateKey('<hex>') // optional
pool.addRelay('ws://some.relay.com', {read: true, write: true})
pool.addRelay('ws://other.relay.cool', {read: true, write: true})
// example callback function for a subscription
function onEvent(event, relay) {
console.log(`got an event from ${relay.url} which is already validated.`, event)
// example callback functions for listeners
// callback functions take an object argument with following keys:
// - relay: relay url
// - type: type of listener
// - id: sub id for sub specific listeners ('EVENT' or 'EOSE')
// - event: event object, only for 'event' listener
// - notice: notice message, only for 'notice' listener
function onEvent({event, relay, type, id}) {
console.log(`got an event from ${relay} which is already validated.`, event)
}
function onEose({relay, type, id}) { /* callback function here */}
function onNotice({relay, type, notice}) { /* callback function here */}
function onConnection({relay, type}) { /* callback function here */}
// listen for messages for pool
pool.on('event', onEvent)
pool.on('connection', onConnection)
pool.on('notice', onNotice)
// subscribing to a single user
// author is the user's public key
pool.sub({cb: onEvent, filter: {author: '<hex>'}})
pool.sub({filter: {author: '<hex>'}})
// or bulk follow
pool.sub({cb:(event, relay) => {...}, filter: {authors: ['<hex1>', '<hex2>', ..., '<hexn>']}})
pool.sub({filter: {authors: ['<hex1>', '<hex2>', ..., '<hexn>']}})
// reuse a subscription channel
const mySubscription = pool.sub({cb: ..., filter: ....})
const mySubscription = pool.sub({filter: ...., skipVerification: false, beforeSend: ....})
mySubscription.sub({filter: ....})
mySubscription.sub({cb: ...})
mySubscription.sub({skipVerification: true})
// listen for messages for subscription
mySubscription.on('event', onEvent)
mySubscription.on('eose', onEose)
// close subscription
mySubscription.unsub()
// get specific event
const specificChannel = pool.sub({
cb: (event, relay) => {
const specificChannel = pool.sub({ filter: {id: '<hex>'}})
.on('event', ({event, relay}) => {
console.log('got specific event from relay', event, relay)
specificChannel.unsub()
},
filter: {id: '<hex>'}
})
})
// or get a specific event plus all the events that reference it in the 'e' tag
pool.sub({ cb: (event, relay) => { ... }, filter: [{id: '<hex>'}, {'#e': '<hex>'}] })
pool.sub({ filter: [{id: '<hex>'}, {'#e': '<hex>'}] })
// get all events
pool.sub({cb: (event, relay) => {...}, filter: {}})
pool.sub({ filter: {} })
// get recent events
pool.sub({cb: (event, relay) => {...}, filter: {since: timestamp}})
pool.sub({ filter: {since: timestamp} })
// publishing events(inside an async function):
const ev = await pool.publish(eventObject, (status, url) => {