Skip to content

Commit

Permalink
feat: implement shared middlewares
Browse files Browse the repository at this point in the history
  • Loading branch information
fletcherist committed Aug 31, 2018
1 parent e193059 commit 58843c5
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/alice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,14 @@ export class Alice implements IAlice {
sessionsStorage: ISessionStorage = new InMemorySessionStorage(),
): void {
this.use(sessionMiddleware(sessionsStorage));
this.use(this._mainStage.middleware);
}

private async _runMiddlewares(
context: IContext,
): Promise<IMiddlewareResult | null> {
const middlewares = Array.from(this._middlewares);
// mainStage middleware should always be the latest one
middlewares.push(this._mainStage.middleware);
if (middlewares.length === 0) {
return null;
}
Expand Down
5 changes: 1 addition & 4 deletions src/session/sessionMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ import { ISessionContext } from './sessionContext';
export function sessionMiddleware(
storage: ISessionStorage,
): Middleware<ISessionContext, IContext> {
return async function(context, next): Promise<IMiddlewareResult | null> {
if (context.session) {
return next ? next(context) : null;
}
return async (context, next): Promise<IMiddlewareResult | null> => {
const id = context.data.session.session_id;
const session = await storage.getOrCreate(id);
const sessionContext: ISessionContext = {
Expand Down
12 changes: 6 additions & 6 deletions src/stage/compere.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { Stage } from './stage';
import debug from '../debug';

export interface IStageCompere {
enter(name: string): void;
leave(): void;
enter(name: string): Promise<void>;
leave(): Promise<void>;
}

export class StageCompere implements IStageCompere {
Expand All @@ -14,13 +14,13 @@ export class StageCompere implements IStageCompere {
this._context = context;
}

public enter(name: string): void {
this._context.session.set(Stage.CURRENT_SCENE_SESSION_KEY, name);
public async enter(name: string): Promise<void> {
await this._context.session.set(Stage.CURRENT_SCENE_SESSION_KEY, name);
debug(`scene changed for: ${name}`);
}

public leave(): void {
this._context.session.set(
public async leave(): Promise<void> {
await this._context.session.set(
Stage.CURRENT_SCENE_SESSION_KEY,
Stage.DEFAULT_SCENE_NAME,
);
Expand Down
2 changes: 1 addition & 1 deletion src/stage/stage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export class Stage implements IStage {
);
}
const sceneName =
context.session.get(Stage.CURRENT_SCENE_SESSION_KEY) ||
(await context.session.get(Stage.CURRENT_SCENE_SESSION_KEY)) ||
Stage.DEFAULT_SCENE_NAME;
const scene = this._scenes.has(sceneName)
? this._scenes.get(sceneName)
Expand Down

0 comments on commit 58843c5

Please sign in to comment.