I tried to use @singleton() on server side in next.js and it class run some action by setTimemout.
But this is class dont created as singletone and created every container.resolve() and as result i have many requests.
我试着在next.js的服务器端使用@singleton(),它类通过setTimemout运行一些操作。但这是一个类,并没有创建为singletone,而是创建了每个容器.resolve(),因此我有很多请求。
@singleton() export class PriceProvider { public constructor() {
setInterval(() => void this.load(), INTERVAL);
void this.load(); } }
Also I tried to save in appGlobalThis
我还试图保存在appGlobalThis中
const appGlobalThis = globalThis as unknown as { priceProviderStarted: boolean; };
export class PriceProvider {
public constructor(
private readonly config: Config,
private readonly raceRepository: RaceRepository,
) {
if (!appGlobalThis.priceProviderStarted) {
appGlobalThis.priceProviderStarted = true;
void this.step();
}
}
But priceProvider run step every time
但是priceProvider每次都运行步骤
更多回答
@singletone not working in dev?
@singletone不在dev中工作?
优秀答案推荐
我是一名优秀的程序员,十分优秀!