This commit is contained in:
peter
2020-02-11 02:16:14 +01:00
parent dad45d4f56
commit f26121d616
3 changed files with 58 additions and 100 deletions
-98
View File
@@ -1,98 +0,0 @@
import { RPCExporter, SubscriptionResponse, ErrorResponse, SuccessResponse } from "rpclibrary";
import { FrontworkAdmin } from "../Admin/Admin";
import { TableDefinitionExporter } from "../Types/Interfaces";
import { getLogger } from 'frontblock-generic/Types';
import * as uuid from 'uuid/v4'
import { TableDefiniton } from "../Types/Types";
export type NotificationSeverity = 'Info' | 'Important' | 'Error'
export type Notification = {
ID?:number,
severity: NotificationSeverity,
topic: string,
message:string,
time?: number
}
export type EventbusIfc = {
Eventbus: {
getNotificationLog: () => Promise<Notification[]>
pushNotification: (notification:Notification) => Promise<void>
subscribeNotifications: (callback:Function) => Promise<SubscriptionResponse | ErrorResponse>
}
}
const logger = getLogger("Eventbus", 'debug')
export class FrontworkEventBus
implements RPCExporter<EventbusIfc, "Eventbus">, TableDefinitionExporter {
name = "Eventbus" as "Eventbus"
private subscriptions : { [uid in string]:Function } = {}
constructor(private admin: FrontworkAdmin){
}
async subscribeNotifications(callback) : Promise<SubscriptionResponse>{
const uid = uuid()
this.subscriptions[uid] = callback
return { result: 'Success', uuid: uid }
}
async getNotificationLog() : Promise<Notification[]>{
try{
return await this.admin.knex.select('*').from('notifications')
}catch(e){
logger.error(e)
throw e
}
}
async unsubscribeNotifications(uid:string) : Promise<SuccessResponse | ErrorResponse>{
if(!this.subscriptions[uid]) return { result: 'Error', message: "Unknown subscription" }
delete this.subscriptions[uid]
return { result: 'Success' }
}
async pushNotification(notification: Notification){
if(!notification.time) notification.time = Date.now()
logger.debug("inserting into notifications", notification)
try{
await this.admin.knex('notifications').insert(notification)
}catch(e){
logger.error(e)
throw e
}
Object.values(this.subscriptions).forEach(callback => {
callback(notification)
})
}
exportRPCs(){
return [{
name: 'getNotificationLog' as 'getNotificationLog',
call: this.getNotificationLog
},{
name: 'pushNotification' as 'pushNotification',
call: this.pushNotification
},{
name: 'subscribeNotificaitons' as 'subscribeNotifications',
hook: async (callback: Function) => await this.subscribeNotifications(callback)
}]
}
getTableDefinitions(): TableDefiniton[]{
return [{
name: 'notifications',
tableBuilder: (table) => {
table.increments('ID').primary();
table.string('severity');
table.string('topic');
table.string('message');
table.timestamp('time');
}
}]
}
}
+1 -1
View File
@@ -1,7 +1,7 @@
import { SubscriptionResponse } from "rpclibrary"
export class IPubSub<UpdateType>{
publish: (topic: string, p: UpdateType) => void
publish: (topic: string, p: UpdateType) => Promise<void>
subscribe: (topic:string, callback: (p:UpdateType)=>any) => Promise<SubscriptionResponse>
unsubscribe: (uuid: string) => Promise<void>
}
+57 -1
View File
@@ -135,7 +135,63 @@ export const T2:string[] = [
"Band of Dark Dominion",
"Draconic Avenger",
"Interlaced Shadow Jerkin",
"Ringo's Blizzard Boots"
"Ringo's Blizzard Boots",
"Scrolls of Blinding Light",
"Rune of Metamorphosis",
"Natural Alignment Crystal",
"Mind Quickening Gem",
"Lifegiving Gem",
"Arcane Infused Gem",
"The Black Book",
"Venomous Totem",
"Nemesis Spaulders",
"Nemesis Robes",
"Nemesis Bracers",
"Nemesis Gloves",
"Nemesis Belt",
"Nemesis Boots",
"Netherwind Mantle",
"Netherwind Robes",
"Netherwind Bindings",
"Netherwind Gloves",
"Netherwind Belt",
"Netherwind Boots",
"Bloodfang Spaulders",
"Bloodfang Chestpiece",
"Bloodfang Bracers",
"Bloodfang Gloves",
"Bloodfang Belt",
"Bloodfang Boots",
"Stormrage Pauldrons",
"Stormrage Chestguard",
"Stormrage Bracers",
"Stormrage Handguards",
"Stormrage Belt",
"Stormrage Boots",
"Dragonstalker's Spaulders",
"Dragonstalker's Breastplate",
"Dragonstalker's Bracers",
"Dragonstalker's Guantlets",
"Dragonstalker's Belt",
"Dragonstalker's Greaves",
"Judgement Spaulders",
"Judgement Breastplate",
"Judgement Bindings",
"Judgement Guantlets",
"Judgement Belt",
"Judgement Sabatons",
"Pauldrons of Transcendence",
"Robes of Transcendence",
"Bindings of Transcendence",
"Handguards of Transcendence",
"Belt of Transcendence",
"Boots of Transcendence",
"Pauldrons of Wrath",
"Breastplate of Wrath",
"Bracelets of Wrath",
"Guantlets of Wrath",
"Waistband of Wrath",
"Sabatons of Wrath"
]
export type AllItems = {