Skip to content

Commit

Permalink
feat: implement logger
Browse files Browse the repository at this point in the history
  • Loading branch information
fletcherist committed Jul 16, 2018
1 parent 538ab1c commit d0e9c7d
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 17 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
},
"homepage": "https:/fletcherist/yandex-dialogs-sdk#readme",
"dependencies": {
"colors": "^1.3.0",
"express": "^4.16.3",
"fuse.js": "^3.2.0",
"node-fetch": "^2.1.2",
Expand Down
21 changes: 15 additions & 6 deletions src/alice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { Sessions } from './sessions'

import Scene from './scene'
import Ctx from './ctx'
import EventEmitter from './eventEmitter'
import ImagesApi from './imagesApi'
import Logger from './logger'
import fetch from 'node-fetch'

import {
Expand All @@ -25,12 +25,19 @@ import { CommandInterface } from './types/command'
import { WebhookResponse, WebhookRequest } from './types/webhook'
import { EventInterface, EventEmitterInterface } from './types/eventEmitter'
import { CtxInterface } from './types/ctx'
import eventEmitter from './eventEmitter'

import {
EVENT_MESSAGE_RECIEVED,
EVENT_MESSAGE_SENT,
} from './constants'

const DEFAULT_SESSIONS_LIMIT: number = 1000
const DEFAULT_TIMEOUT_CALLBACK_MESSAGE = 'Извините, но я не успела найти ответ за отведенное время.'
const DEFAULT_RESPONSE_TIMEOUT = 1300

export default class Alice {
public logger: object
private anyCallback: (ctx: CtxInterface) => void
private welcomeCallback: (ctx: CtxInterface) => void
private timeoutCallback: (ctx: CtxInterface) => void
Expand All @@ -55,7 +62,7 @@ export default class Alice {
this.currentScene = null
this.sessions = new Sessions()
this.config = config
this.eventEmitter = new EventEmitter()
this.logger = new Logger()
this.imagesApi = new ImagesApi({
oAuthToken: this.config.oAuthToken,
skillId: this.config.skillId,
Expand All @@ -72,7 +79,7 @@ export default class Alice {
/* @TODO: Implement watchers (errors, messages) */
// tslint:disable-next-line:no-empty
public on(event: EventInterface['type'], callback: EventInterface['callback']) {
this.eventEmitter.subscribe(event, callback)
eventEmitter.subscribe(event, callback)
}

/*
Expand Down Expand Up @@ -141,12 +148,14 @@ export default class Alice {
*/
server: this.server || null,
middlewares: this.middlewares,
eventEmitter: this.eventEmitter,
eventEmitter,
}
const ctxInstance = new Ctx(ctxDefaultParams)
const ctxWithMiddlewares = await applyMiddlewares(this.middlewares, ctxInstance)

this.eventEmitter.dispatch('MESSAGE_RECIEVED', ctxWithMiddlewares.message)
eventEmitter.dispatch(EVENT_MESSAGE_RECIEVED, {
data: ctxWithMiddlewares.message, session: ctxWithMiddlewares.session,
})

/* check whether current scene is not defined */
if (!session.getData('currentScene')) {
Expand Down Expand Up @@ -273,7 +282,7 @@ export default class Alice {
res.setHeader('Content-type', 'application/json')

let responseAlreadySent = false
const handleResponseCallback = (response) => {
const handleResponseCallback = (response: WebhookResponse) => {
/* dont answer twice */
if (responseAlreadySent) {
return false
Expand Down
3 changes: 3 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@ export const ALICE_PROTOCOL_VERSION = '1.0'
export const DEFAULT_END_SESSION = false
export const ALICE_API_URL = 'https://dialogs.yandex.net/api/v1/skills'

export const EVENT_MESSAGE_RECIEVED = 'messageRecieved'
export const EVENT_MESSAGE_SENT = 'messageSent'

module.exports.ALICE_PROTOCOL_VERSION = '1.0'
module.exports.DEFAULT_END_SESSION = false
9 changes: 8 additions & 1 deletion src/ctx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { BigImageCard } from './types/card'
import { image, bigImageCard, itemsListCard } from './card'
import reply from './reply'

import { EVENT_MESSAGE_RECIEVED } from './constants'

export default class Ctx implements CtxInterface {
public req: WebhookRequest
public sessionId: string
Expand All @@ -20,7 +22,7 @@ export default class Ctx implements CtxInterface {
public payload: {}
public message: string
public session: Session
public EventEmitter: EventEmitterInterface
public eventEmitter: EventEmitterInterface

public command?: CommandInterface

Expand All @@ -38,6 +40,7 @@ export default class Ctx implements CtxInterface {

enterScene,
leaveScene,
eventEmitter,

command,
} = params
Expand All @@ -53,6 +56,7 @@ export default class Ctx implements CtxInterface {

this.session = session

this.eventEmitter = eventEmitter
this.replyBuilder = new ReplyBuilder(this.req)
this.buttonBuilder = new ButtonBuilder()

Expand Down Expand Up @@ -120,6 +124,9 @@ export default class Ctx implements CtxInterface {
return this.sendResponse(replyMessage)
}

this.eventEmitter.dispatch(EVENT_MESSAGE_RECIEVED, {
data: this.message, session: this.req.session,
})
return replyMessage
}
}
18 changes: 14 additions & 4 deletions src/eventEmitter.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,36 @@
import { merge } from 'ramda'
import {
EventEmitterInterface,
EventType,
EventInterface,
EventData,
} from './types/eventEmitter'

export default class EventEmitter implements EventEmitterInterface {
class EventEmitter implements EventEmitterInterface {
public events: EventInterface[]
constructor() {
this.events = []
}
public subscribe(eventType: EventType, callback: EventInterface['callback']) {
public subscribe(eventType: string, callback: EventInterface['callback']) {
this.events.push({
type: eventType,
callback,
})
}
public dispatch(eventType: EventType, data: EventData) {
public dispatch(eventType: string, dataValue) {
for (const event of this.events) {
const eventData = {
timestamp: new Date().toString(),
type: eventType,
session: dataValue.session,
data: dataValue.data,
}
if (event.type === eventType) {
event.callback(data)
event.callback(eventData)
}
}
}
}

const eventEmitter = new EventEmitter()
export default eventEmitter
34 changes: 34 additions & 0 deletions src/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { EventData } from './types/eventEmitter'
import colors from 'colors'
import eventEmitter from './eventEmitter'

import {
EVENT_MESSAGE_RECIEVED,
EVENT_MESSAGE_SENT,
} from './constants'

colors.setTheme({
silly: 'rainbow',
input: 'grey',
verbose: 'cyan',
prompt: 'grey',
info: 'green',
data: 'grey',
help: 'cyan',
warn: 'yellow',
debug: 'blue',
error: 'red',
})

export default class Logger {
constructor() {
eventEmitter.subscribe(EVENT_MESSAGE_RECIEVED, this.log)
eventEmitter.subscribe(EVENT_MESSAGE_SENT, this.log)
}

public log(event: EventData) {
console.log(
colors.warn(event.data),
)
}
}
2 changes: 1 addition & 1 deletion src/types/ctx.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export interface CtxInterface {
payload: {}
message: string
session: {}
EventEmitter: EventEmitterInterface
eventEmitter: EventEmitterInterface

// command?: Command

Expand Down
11 changes: 6 additions & 5 deletions src/types/eventEmitter.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@

export type EventType = 'MESSAGE_RECIEVED' | 'MESSAGE_CREATED' | 'MESSAGE_SENT'
export interface EventInterface {
type: EventType,
type: string,
callback(event: EventData): void
}
export interface EventEmitterInterface {
events: EventInterface[]
subscribe(eventType: EventType, callback: EventInterface['callback'])
dispatch(eventType: EventType, data: EventData)
subscribe(eventType: string, callback: EventInterface['callback'])
dispatch(eventType: string, data: any)
}

export interface EventData {

timestamp: string
type: string
data: any
}

0 comments on commit d0e9c7d

Please sign in to comment.