Giter VIP home page Giter VIP logo

Comments (12)

killagu avatar killagu commented on April 27, 2024 1

搞一个 Symbol 然后挂到全局类似于 global[Symbol.for('egg#ctxStorage')] ?

from egg.

fengmk2 avatar fengmk2 commented on April 27, 2024 1

搞一个 Symbol 然后挂到全局类似于 global[Symbol.for('egg#ctxStorage')] ?

好像可以哦,就算多个 app 实例,共用一个 als 似乎也是可以正常工作的。

from egg.

fengmk2 avatar fengmk2 commented on April 27, 2024 1

egg >= 2.x 都支持从 gals 获取了。

from egg.

atian25 avatar atian25 commented on April 27, 2024

https://github.com/search?q=org%3Aeggjs+AsyncLocalStorage&type=pullrequests

egg/index.d.ts

Line 740 in ad6c06a

get ctxStorage(): AsyncLocalStorage<Context>;

可以参考下

from egg.

ljj0915 avatar ljj0915 commented on April 27, 2024

https://github.com/search?q=org%3Aeggjs+AsyncLocalStorage&type=pullrequests

egg/index.d.ts

Line 740 in ad6c06a

get ctxStorage(): AsyncLocalStorage<Context>;

可以参考下

你好,这个方式应该也需要类继承对应的Application吧,如果是不继承呢,全局任意一个地方可以拿的

from egg.

fengmk2 avatar fengmk2 commented on April 27, 2024

可以覆盖 koa 的 application 基类实现 https://github.com/eggjs/egg-core/blob/798d976017a510d155344b0a03d407ce615f20cf/lib/egg.js#L43

from egg.

laoboxie avatar laoboxie commented on April 27, 2024

我也遇到一样的问题,希望ctxStorage能够在任意地方获取到,场景是一些sdk需要拿到当前的上下文ctx。
目前没想到比较好的方法来解决这个问题。
ctxStorage是不是应该设计为在任意地方都能方便地获取到呢?

from egg.

fengmk2 avatar fengmk2 commented on April 27, 2024

只要拿到 app 就能获取到 app.ctxStorage 了

from egg.

fengmk2 avatar fengmk2 commented on April 27, 2024

在 egg app 初始化之后,对 global 对象做赋值 https://github.com/koajs/koa/blob/v2.x/lib/application.js#L71 ,实现全局获取当前 als 能力。

from egg.

fengmk2 avatar fengmk2 commented on April 27, 2024

@ljj0915 可以通过 global[Symbol.for('egg#ctxStorage')] 获取了。

eggjs/egg-core#268

使用 https://github.com/node-modules/gals 库可以获得,统一封装。

from egg.

ljj0915 avatar ljj0915 commented on April 27, 2024

@ljj0915 可以通过 global[Symbol.for('egg#ctxStorage')] 获取了。

eggjs/egg-core#268

使用 https://github.com/node-modules/gals 库可以获得,统一封装。

后来我自己使用AsyncLocalStorage,全局定义了一个,每次进来,在中间件里面,把next()包进去,后续任意一个地方都可以拿到了。差不多

from egg.

laoboxie avatar laoboxie commented on April 27, 2024

优秀!可以愉快地进行异步上下文追踪了~

from egg.

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.