|
| 1 | +import { SQLiteCloudConnection } from './connection' |
| 2 | +import SQLiteCloudTlsConnection from './connection-tls' |
| 3 | +import { PubSubCallback } from './types' |
| 4 | + |
| 5 | +export enum PUBSUB_ENTITY_TYPE { |
| 6 | + TABLE = 'TABLE', |
| 7 | + CHANNEL = 'CHANNEL' |
| 8 | +} |
| 9 | + |
| 10 | +/** |
| 11 | + * Pub/Sub class to receive changes on database tables or to send messages to channels. |
| 12 | + */ |
| 13 | +export class PubSub { |
| 14 | + constructor(connection: SQLiteCloudConnection) { |
| 15 | + this.connection = connection |
| 16 | + this.connectionPubSub = new SQLiteCloudTlsConnection(connection.getConfig()) |
| 17 | + } |
| 18 | + |
| 19 | + private connection: SQLiteCloudConnection |
| 20 | + private connectionPubSub: SQLiteCloudConnection |
| 21 | + |
| 22 | + /** |
| 23 | + * Listen for a table or channel and start to receive messages to the provided callback. |
| 24 | + * @param entityType One of TABLE or CHANNEL' |
| 25 | + * @param entityName Name of the table or the channel |
| 26 | + * @param callback Callback to be called when a message is received |
| 27 | + * @param data Extra data to be passed to the callback |
| 28 | + */ |
| 29 | + public async listen(entityType: PUBSUB_ENTITY_TYPE, entityName: string, callback: PubSubCallback, data?: any): Promise<any> { |
| 30 | + const entity = entityType === 'TABLE' ? 'TABLE ' : '' |
| 31 | + |
| 32 | + const authCommand: string = await this.connection.sql(`LISTEN ${entity}${entityName};`) |
| 33 | + |
| 34 | + return new Promise((resolve, reject) => { |
| 35 | + this.connectionPubSub.sendCommands(authCommand, (error, results) => { |
| 36 | + if (error) { |
| 37 | + callback.call(this, error, null, data) |
| 38 | + reject(error) |
| 39 | + } else { |
| 40 | + // skip results from pubSub auth command |
| 41 | + if (results !== 'OK') { |
| 42 | + callback.call(this, null, results, data) |
| 43 | + } |
| 44 | + resolve(results) |
| 45 | + } |
| 46 | + }) |
| 47 | + }) |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Stop receive messages from a table or channel. |
| 52 | + * @param entityType One of TABLE or CHANNEL |
| 53 | + * @param entityName Name of the table or the channel |
| 54 | + */ |
| 55 | + public async unlisten(entityType: string, entityName: string): Promise<any> { |
| 56 | + const subject = entityType === 'TABLE' ? 'TABLE ' : '' |
| 57 | + |
| 58 | + return this.connection.sql(`UNLISTEN ${subject}?;`, entityName) |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Create a channel to send messages to. |
| 63 | + * @param name Channel name |
| 64 | + * @param failIfExists Raise an error if the channel already exists |
| 65 | + */ |
| 66 | + public async createChannel(name: string, failIfExists: boolean = true): Promise<any> { |
| 67 | + let notExistsCommand = '' |
| 68 | + if (!failIfExists) { |
| 69 | + notExistsCommand = 'IF NOT EXISTS;' |
| 70 | + } |
| 71 | + |
| 72 | + return this.connection.sql(`CREATE CHANNEL ? ${notExistsCommand}`, name) |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Send a message to the channel. |
| 77 | + */ |
| 78 | + public notifyChannel(channelName: string, message: string): Promise<any> { |
| 79 | + return this.connection.sql`NOTIFY ${channelName} ${message};` |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Ask the server to close the connection to the database and |
| 84 | + * to keep only open the Pub/Sub connection. |
| 85 | + * Only interaction with Pub/Sub commands will be allowed. |
| 86 | + */ |
| 87 | + public setPubSubOnly(): Promise<any> { |
| 88 | + return new Promise((resolve, reject) => { |
| 89 | + this.connection.sendCommands('PUBSUB ONLY;', (error, results) => { |
| 90 | + if (error) { |
| 91 | + reject(error) |
| 92 | + } else { |
| 93 | + this.connection.close() |
| 94 | + resolve(results) |
| 95 | + } |
| 96 | + }) |
| 97 | + }) |
| 98 | + } |
| 99 | + |
| 100 | + /** True if Pub/Sub connection is open. */ |
| 101 | + public connected(): boolean { |
| 102 | + return this.connectionPubSub.connected |
| 103 | + } |
| 104 | + |
| 105 | + /** Close Pub/Sub connection. */ |
| 106 | + public close(): void { |
| 107 | + this.connectionPubSub.close() |
| 108 | + } |
| 109 | +} |
0 commit comments