Giter Site home page Giter Site logo

Comments (7)

dotcypress avatar dotcypress commented on May 6, 2024

Hey @fczuardi

next function don't have any parameters, if you want to change context state use following snippet:

const app = new Telegraf(process.env.BOT_TOKEN)

app.use((ctx, next) => {
  ctx.state.foo = 'bar'
  return next()
})

app.on('text', (ctx) => {
  return ctx.reply(`foo=${ctx.state.foo}`)
})

This snippet is ok with no-param-reassign rule with disabled props option.
/*eslint no-param-reassign: ["error", { "props": false }]*/

from telegraf.

fczuardi avatar fczuardi commented on May 6, 2024

Yeah, by disabling part of the rule it is possible to stop the warnings… it is an acceptable tradeoff until eslint/eslint#6505 gives developers a way to punch more specific exceptions holes into the rule…

Do you think it would make sense to support a different way of passing context further to the next middlewares though?

Like the supported "value" argument of a javascript generator: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Generator/next#Sending_values_to_the_generator

I mean, middlewares are already functions that have ctx as their first argument, next could be the actual middleware function registered to be called next, if we called the function as the function that it is, with any arguments that it has, we could opt to have middlewares written as pure functions, without side-effects.

I honestly don't know if this would have any real benefits or if it is just bikeshedding / unnecessary immutable radicalism, or even if it would have negative impact on performance, I am mostly just curious about the reasoning and possibilities :)

from telegraf.

dotcypress avatar dotcypress commented on May 6, 2024

Oh yeah, now i got it.
Good point, i'll think about it.

Immutable state and pure functions is definitely awesome, but i'm not sure they will fit current framework design, check this out:

app.use((ctx, next) => {
  ctx.state.foo = 'bar'
  // I need next2 function reference in this place :(
  return next(ctx, next2)
})

from telegraf.

fczuardi avatar fczuardi commented on May 6, 2024

I see your point.

As an API suggestion, what about allowing just a single argument though? Like return next(ctx) would that make sense? Just a way to override what ctx is passed to the next middleware of the pipeline.

from telegraf.

dotcypress avatar dotcypress commented on May 6, 2024

Yeah, i see your point.

I created a new branch for that:
https://github.com/telegraf/telegraf/compare/feature/pure-middleware

Need time to think about it and do some performance testing 🚀

from telegraf.

dotcypress avatar dotcypress commented on May 6, 2024

Will published in v4.x.

See telegraf/next branch.

Thank you 👍

from telegraf.

fczuardi avatar fczuardi commented on May 6, 2024

Cool!

from telegraf.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.