Typescript 一次调用处理程序#多次调用exec AsyncCache

Typescript 一次调用处理程序#多次调用exec AsyncCache,typescript,Typescript,我正在学习Typescript,然后我从GitHub看到了这个Typescript代码,我试图理解并解决这个问题。到目前为止,我所理解的被称为处理程序一次#exec被多次串联调用,以此类推。如果我执行this.handler,是否调用了该处理程序?我如何计算#exec中有类型脚本方法吗 index.ts export default class<TInput extends Array<any> = Array<any>, TOutput = any> {

我正在学习Typescript,然后我从GitHub看到了这个Typescript代码,我试图理解并解决这个问题。到目前为止,我所理解的被称为
处理程序
一次
#exec
被多次串联调用,以此类推。如果我执行
this.handler
,是否调用了该处理程序?我如何计算
#exec
中有类型脚本方法吗

index.ts

export default class<TInput extends Array<any> = Array<any>, TOutput = any> {
  constructor(
    private handler: (...args: TInput) => Promise<TOutput>,
    private timeout?: number
  ) {
    // do something
    
  }

  async exec(...args: TInput): Promise<TOutput> {
    // do something
    return {} as TOutput;
  }
}
import delay from "@highoutput/delay";
import sinon from "sinon";
import { expect } from "chai";
import R from "ramda";
import AsyncCache from ".";

describe("AsyncCache", () => {
  before(function () {
    this.spy = sinon.spy(async (first: any, second: any) => {
      await delay(100 + Math.random() * 100);

      let result: Record<string, any> = {
        first,
      };

      if (!R.isNil(second)) {
        result = {
          ...result,
          second,
        };
      }
      return result;
    });
  });

  beforeEach(function () {
    sinon.reset();
  });

  it("should call the handler once, when #exec is called multiple times in series", async function () {
    const cache = new AsyncCache<[string], { first: string }>(this.spy);

    await cache
      .exec("hello")
      .then((result) => expect(result).to.deep.equal({ first: "hello" }));
    await cache.exec("hello").then((result) => {
      expect(result).to.deep.equal({ first: "hello" });
      console.log(result);
    });
    console.log(cache);
    expect(this.spy.calledOnce).to.be.true;
  });

  it("should call the handler once, when #exec is called multiple times in parallel", async function () {
    const cache = new AsyncCache<[string], { first: string }>(this.spy);

    await Promise.all([
      cache
        .exec("hello")
        .then((result) => expect(result).to.deep.equal({ first: "hello" })),
      cache
        .exec("hello")
        .then((result) => expect(result).to.deep.equal({ first: "hello" })),
    ]);
    expect(this.spy.calledOnce).to.be.true;
  });

  it("should call the handler multiple times, when #exec is called multiple times in parallel with different inputs", async function () {
    const cache = new AsyncCache<[string], { first: string }>(this.spy);

    await Promise.all([
      cache
        .exec("hello")
        .then((result) => expect(result).to.deep.equal({ first: "hello" })),
      cache
        .exec("world")
        .then((result) => expect(result).to.deep.equal({ first: "world" })),
      cache
        .exec("everybody")
        .then((result) => expect(result).to.deep.equal({ first: "everybody" })),
    ]);

    expect(this.spy.callCount).to.equal(3);
  });

  it("should call the handler once, when #exec is called multiple times in series with the same complex input", async function () {
    const cache = new AsyncCache<
      [{ message: string }, number],
      { first: { message: string }; second: number }
    >(this.spy);

    await cache
      .exec({ message: "hello" }, 5)
      .then((result) =>
        expect(result).to.deep.equal({ first: { message: "hello" }, second: 5 })
      );
    await cache
      .exec({ message: "hello" }, 5)
      .then((result) =>
        expect(result).to.deep.equal({ first: { message: "hello" }, second: 5 })
      );

    expect(this.spy.calledOnce).to.be.true;
  });

  it("should call the handler multiple times, when #exec is called multiple times in series with different complex inputs", async function () {
    const cache = new AsyncCache<
      [{ message: string }, number],
      { first: { message: string }; second: number }
    >(this.spy);

    await cache.exec({ message: "hello" }, 10).then((result) =>
      expect(result).to.deep.equal({
        first: { message: "hello" },
        second: 10,
      })
    );
    await cache
      .exec({ message: "world" }, 5)
      .then((result) =>
        expect(result).to.deep.equal({ first: { message: "world" }, second: 5 })
      );

    expect(this.spy.callCount).to.equal(2);
  });

  it("should call the handler multiple times, when #exec is called multiple times in series with a delay in between calls", async function () {
    const cache = new AsyncCache<[string], { first: string }>(this.spy, 500);

    await cache
      .exec("hello")
      .then((result) => expect(result).to.deep.equal({ first: "hello" }));
    await delay(600);
    await cache
      .exec("hello")
      .then((result) => expect(result).to.deep.equal({ first: "hello" }));

    expect(this.spy.callCount).to.equal(2);
  });
});
导出默认类{
建造师(
私有处理程序:(…args:TInput)=>承诺,
专用超时?:数字
) {
//做点什么
}
异步执行(…args:TInput):承诺{
//做点什么
返回{}作为TOutput;
}
}
索引规范ts

export default class<TInput extends Array<any> = Array<any>, TOutput = any> {
  constructor(
    private handler: (...args: TInput) => Promise<TOutput>,
    private timeout?: number
  ) {
    // do something
    
  }

  async exec(...args: TInput): Promise<TOutput> {
    // do something
    return {} as TOutput;
  }
}
import delay from "@highoutput/delay";
import sinon from "sinon";
import { expect } from "chai";
import R from "ramda";
import AsyncCache from ".";

describe("AsyncCache", () => {
  before(function () {
    this.spy = sinon.spy(async (first: any, second: any) => {
      await delay(100 + Math.random() * 100);

      let result: Record<string, any> = {
        first,
      };

      if (!R.isNil(second)) {
        result = {
          ...result,
          second,
        };
      }
      return result;
    });
  });

  beforeEach(function () {
    sinon.reset();
  });

  it("should call the handler once, when #exec is called multiple times in series", async function () {
    const cache = new AsyncCache<[string], { first: string }>(this.spy);

    await cache
      .exec("hello")
      .then((result) => expect(result).to.deep.equal({ first: "hello" }));
    await cache.exec("hello").then((result) => {
      expect(result).to.deep.equal({ first: "hello" });
      console.log(result);
    });
    console.log(cache);
    expect(this.spy.calledOnce).to.be.true;
  });

  it("should call the handler once, when #exec is called multiple times in parallel", async function () {
    const cache = new AsyncCache<[string], { first: string }>(this.spy);

    await Promise.all([
      cache
        .exec("hello")
        .then((result) => expect(result).to.deep.equal({ first: "hello" })),
      cache
        .exec("hello")
        .then((result) => expect(result).to.deep.equal({ first: "hello" })),
    ]);
    expect(this.spy.calledOnce).to.be.true;
  });

  it("should call the handler multiple times, when #exec is called multiple times in parallel with different inputs", async function () {
    const cache = new AsyncCache<[string], { first: string }>(this.spy);

    await Promise.all([
      cache
        .exec("hello")
        .then((result) => expect(result).to.deep.equal({ first: "hello" })),
      cache
        .exec("world")
        .then((result) => expect(result).to.deep.equal({ first: "world" })),
      cache
        .exec("everybody")
        .then((result) => expect(result).to.deep.equal({ first: "everybody" })),
    ]);

    expect(this.spy.callCount).to.equal(3);
  });

  it("should call the handler once, when #exec is called multiple times in series with the same complex input", async function () {
    const cache = new AsyncCache<
      [{ message: string }, number],
      { first: { message: string }; second: number }
    >(this.spy);

    await cache
      .exec({ message: "hello" }, 5)
      .then((result) =>
        expect(result).to.deep.equal({ first: { message: "hello" }, second: 5 })
      );
    await cache
      .exec({ message: "hello" }, 5)
      .then((result) =>
        expect(result).to.deep.equal({ first: { message: "hello" }, second: 5 })
      );

    expect(this.spy.calledOnce).to.be.true;
  });

  it("should call the handler multiple times, when #exec is called multiple times in series with different complex inputs", async function () {
    const cache = new AsyncCache<
      [{ message: string }, number],
      { first: { message: string }; second: number }
    >(this.spy);

    await cache.exec({ message: "hello" }, 10).then((result) =>
      expect(result).to.deep.equal({
        first: { message: "hello" },
        second: 10,
      })
    );
    await cache
      .exec({ message: "world" }, 5)
      .then((result) =>
        expect(result).to.deep.equal({ first: { message: "world" }, second: 5 })
      );

    expect(this.spy.callCount).to.equal(2);
  });

  it("should call the handler multiple times, when #exec is called multiple times in series with a delay in between calls", async function () {
    const cache = new AsyncCache<[string], { first: string }>(this.spy, 500);

    await cache
      .exec("hello")
      .then((result) => expect(result).to.deep.equal({ first: "hello" }));
    await delay(600);
    await cache
      .exec("hello")
      .then((result) => expect(result).to.deep.equal({ first: "hello" }));

    expect(this.spy.callCount).to.equal(2);
  });
});
从“@highoutput/delay”导入延迟;
从“sinon”进口sinon;
从“chai”导入{expect};
从“拉姆达”进口R;
从“”导入异步缓存;
描述(“异步缓存”,()=>{
前(函数(){
this.spy=sinon.spy(异步(第一个:任意,第二个:任意)=>{
等待延迟(100+Math.random()*100);
让结果:记录={
第一,
};
如果(!R.isNil(秒)){
结果={
…结果,
第二
};
}
返回结果;
});
});
beforeach(函数(){
sinon.reset();
});
它(“当连续多次调用#exec时,应调用处理程序一次”),异步函数(){
const cache=new AsyncCache(this.spy);
等待缓存
.exec(“你好”)
.then((result)=>expect(result).to.deep.equal({first:“hello”}));
等待cache.exec(“hello”)。然后((结果)=>{
expect(result).to.deep.equal({first:“hello”});
控制台日志(结果);
});
console.log(缓存);
期望(this.spy.calledOnce).to.be.true;
});
它(“当并行多次调用#exec时,应该调用一次处理程序”,异步函数(){
const cache=new AsyncCache(this.spy);
等待承诺([
隐藏物
.exec(“你好”)
.then((result)=>expect(result).to.deep.equal({first:“hello”})),
隐藏物
.exec(“你好”)
.then((result)=>expect(result).to.deep.equal({first:“hello”})),
]);
期望(this.spy.calledOnce).to.be.true;
});
它(“当使用不同的输入并行调用#exec时,应多次调用处理程序”,异步函数(){
const cache=new AsyncCache(this.spy);
等待承诺([
隐藏物
.exec(“你好”)
.then((result)=>expect(result).to.deep.equal({first:“hello”})),
隐藏物
.exec(“世界”)
.then((result)=>expect(result).to.deep.equal({first:“world”})),
隐藏物
.exec(“所有人”)
.then((result)=>expect(result).to.deep.equal({first:“everybody”})),
]);
expect(this.spy.callCount).to.equal(3);
});
它(“当#exec使用相同的复杂输入被多次串行调用时,应该调用一次处理程序”,异步函数(){
常量缓存=新的异步缓存<
[{message:string},number],
{第一个:{消息:字符串};第二个:数字}
>(这是间谍);
等待缓存
.exec({message:“hello”},5)
。然后((结果)=>
expect(result).to.deep.equal({first:{message:“hello”},second:5})
);
等待缓存
.exec({message:“hello”},5)
。然后((结果)=>
expect(result).to.deep.equal({first:{message:“hello”},second:5})
);
期望(this.spy.calledOnce).to.be.true;
});
它(“当#exec用不同的复杂输入串联调用多次时,应多次调用处理程序”,异步函数(){
常量缓存=新的异步缓存<
[{message:string},number],
{第一个:{消息:字符串};第二个:数字}
>(这是间谍);
wait cache.exec({message:“hello”},10)。然后((result)=>
期望(结果)等于({
第一:{留言:“你好”},
第二:10,
})
);
等待缓存
.exec({message:“world”},5)
。然后((结果)=>
expect(result).to.deep.equal({first:{message:“world”},second:5})
);
expect(this.spy.callCount).to.equal(2);
});
它(“当连续多次调用#exec且调用之间有延迟时,应多次调用处理程序”,异步函数(){
const cache=new AsyncCache(this.spy,500);
等待缓存
.exec(“你好”)
.then((result)=>expect(result).to.deep.equal({first:“hello”}));
等待延迟(600);
等待缓存
.exec(“你好”)
.then((result)=>expect(result).to.deep.equal({first:“hello”}));
expect(this.spy.callCount).to.equal(2);
});
});