Skip to content

Commit

Permalink
feat: inject ctx.scenes with SceneRouter method
Browse files Browse the repository at this point in the history
BREAKING CHANGE: This removes scenes_control_middleware export.
  • Loading branch information
IlyaSemenov committed Dec 27, 2021
1 parent 7836351 commit 4419397
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 25 deletions.
8 changes: 2 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@ yarn add grammy-scenes

```ts
import { Bot, Context, session, SessionFlavor } from "grammy"
import {
scenes_control_middleware,
SceneSessionFlavor,
ScenesFlavor,
} from "grammy-scenes"
import { SceneSessionFlavor, ScenesFlavor } from "grammy-scenes"

import { scenes } from "./scenes"

Expand All @@ -35,7 +31,7 @@ bot.use(
)

// Inject ctx.scenes
bot.use(scenes_control_middleware(scenes))
bot.use(scenes.manager())

bot.command("start", async (ctx) => {
await ctx.reply(`Welcome here`)
Expand Down
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export * from "./manager"
export * from "./middleware"
export * from "./router"
export * from "./scene"
export * from "./types"
17 changes: 0 additions & 17 deletions src/middleware.ts

This file was deleted.

13 changes: 12 additions & 1 deletion src/router.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Composer, MiddlewareFn } from "grammy"
import { assert, Writable } from "ts-essentials"

// Hack to avoid circular import
import { Scene } from "."
import { Scene, ScenesManager } from "."
import { ScenesFlavoredContext } from "./types"

/** Scene (or subscene) collector that will call scene by its name */
Expand Down Expand Up @@ -44,4 +45,14 @@ export class SceneRouter<C extends ScenesFlavoredContext> extends Composer<C> {
})
return composer.middleware()
}

/** Middleware that injects ctx.scenes */
manager(): MiddlewareFn<C> {
return (ctx, next) => {
assert(!ctx.scenes, "ctx.scenes has already been installed.")
const ctx2 = ctx as Writable<C>
ctx2.scenes = new ScenesManager(ctx, this)
return next()
}
}
}

0 comments on commit 4419397

Please sign in to comment.