Skip to content

Commit

Permalink
fix: bug with delayed executor
Browse files Browse the repository at this point in the history
  • Loading branch information
fletcherist committed Jul 16, 2018
1 parent 624a590 commit 850bd7f
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 15 deletions.
22 changes: 13 additions & 9 deletions src/alice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
selectSessionId,
isFunction,
delay,
rejectsIn,
} from './utils'

import {
Expand All @@ -29,12 +30,13 @@ import eventEmitter from './eventEmitter'

import {
EVENT_MESSAGE_RECIEVED,
EVENT_MESSAGE_SENT,
EVENT_MESSAGE_NOT_SENT,
} from './constants'
import { resolve } from 'dns'

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

export default class Alice {
public logger: object
Expand Down Expand Up @@ -68,10 +70,7 @@ export default class Alice {
skillId: this.config.skillId,
})

this.timeoutCallback = async (ctx) => {
await delay(this.config.responseTimeout || DEFAULT_RESPONSE_TIMEOUT)
ctx.reply(DEFAULT_TIMEOUT_CALLBACK_MESSAGE)
}
this.timeoutCallback = async (ctx) => ctx.reply(DEFAULT_TIMEOUT_CALLBACK_MESSAGE)
this._handleEnterScene = this._handleEnterScene.bind(this)
this._handleLeaveScene = this._handleLeaveScene.bind(this)
}
Expand Down Expand Up @@ -256,11 +255,16 @@ export default class Alice {
const executors = [
/* proxy request to dev server, if enabled */
this.config.devServerUrl
? this.handleProxyRequest(req, this.config.devServerUrl, sendResponse)
: this.handleRequestBody(req, sendResponse),
await this.timeoutCallback(new Ctx({ req, sendResponse })),
? await this.handleProxyRequest(req, this.config.devServerUrl, sendResponse)
: await this.handleRequestBody(req, sendResponse),
rejectsIn(this.config.responseTimeout || DEFAULT_RESPONSE_TIMEOUT),
].filter(Boolean)
return await Promise.race(executors)
.then((result) => result)
.catch(async (error) => {
eventEmitter.dispatch(EVENT_MESSAGE_NOT_SENT)
this.timeoutCallback(new Ctx({ req, sendResponse }))
})
}
/*
* Метод создаёт сервер, который слушает указанный порт.
Expand Down
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const ALICE_API_URL = 'https://dialogs.yandex.net/api/v1/skills'

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

module.exports.ALICE_PROTOCOL_VERSION = '1.0'
module.exports.DEFAULT_END_SESSION = false
8 changes: 4 additions & 4 deletions src/eventEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ class EventEmitter implements EventEmitterInterface {
callback,
})
}
public dispatch(eventType: string, dataValue) {
public dispatch(eventType: string, dataValue?) {
for (const event of this.events) {
const eventData = {
const eventData: EventData = {
timestamp: new Date().toString(),
type: eventType,
session: dataValue.session,
data: dataValue.data,
data: dataValue && dataValue.data,
session: dataValue && dataValue.session,
}
if (event.type === eventType) {
event.callback(eventData)
Expand Down
6 changes: 4 additions & 2 deletions src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import eventEmitter from './eventEmitter'
import {
EVENT_MESSAGE_RECIEVED,
EVENT_MESSAGE_SENT,
EVENT_MESSAGE_NOT_SENT,
} from './constants'

colors.setTheme({
Expand All @@ -29,6 +30,7 @@ export default class Logger {
constructor() {
eventEmitter.subscribe(EVENT_MESSAGE_RECIEVED, this.log)
eventEmitter.subscribe(EVENT_MESSAGE_SENT, this.log)
eventEmitter.subscribe(EVENT_MESSAGE_NOT_SENT, this.log)
}

public log(event: EventData) {
Expand All @@ -37,8 +39,8 @@ export default class Logger {
[
colors.info(`[${logTime(event.timestamp)}]:`),
colors.verbose(`(${event.type})`),
colors.warn(event.data),
].join(' '),
event.data ? colors.warn(event.data) : null,
].filter(Boolean).join(' '),
)
}
}
1 change: 1 addition & 0 deletions src/types/eventEmitter.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ export interface EventData {
timestamp: string
type: string
data: any
session: object
}
3 changes: 3 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { resolve } from 'path'

const formatToken = (token) => token
.replace('$', '')
.replace('{', '')
Expand Down Expand Up @@ -48,6 +50,7 @@ export const selectSessionId = (req) => selectSession(req).session_id
export const selectUserId = (req) => selectSession(req).user_id
export const isFunction = (fn: () => void) => fn && typeof fn === 'function'
export const delay = (ms: number): Promise<void> => new Promise((resolve) => setTimeout(resolve, ms))
export const rejectsIn = (ms: number) => new Promise((resolve, reject) => setTimeout(reject, ms))

export default {
getFiguresRegexp,
Expand Down

0 comments on commit 850bd7f

Please sign in to comment.