@taicode/common-base 4.0.0-beta.2 → 4.0.0-beta.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"disposer.d.ts","sourceRoot":"","sources":["../../source/disposer/disposer.ts"],"names":[],"mappings":"AAEA,KAAK,eAAe,GAAG,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAAA;AAEvD,qBAAa,QAAQ;IACnB,OAAO,CAAC,gBAAgB,CAAwB;IAGhD,GAAG,CAAC,SAAS,EAAE,eAAe,GAAG,IAAI;IAK/B,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;CAY/B"}
1
+ {"version":3,"file":"disposer.d.ts","sourceRoot":"","sources":["../../source/disposer/disposer.ts"],"names":[],"mappings":"AAEA,KAAK,eAAe,GAAG,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAAA;AAEvD,qBAAa,QAAQ;IACnB,OAAO,CAAC,gBAAgB,CAAwB;IAGhD,GAAG,CAAC,SAAS,EAAE,eAAe,GAAG,IAAI;IAK/B,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;CAa/B"}
@@ -5,11 +5,12 @@ export class Disposer {
5
5
  add(cleanupFn) {
6
6
  this.cleanupFunctions.push(cleanupFn);
7
7
  }
8
- // 执行所有清理函数
8
+ // 执行所有清理函数(LIFO:后注册的先执行)
9
9
  async dispose() {
10
10
  const cleanupFunctions = this.cleanupFunctions;
11
11
  this.cleanupFunctions = [];
12
- for (const cleanupFn of cleanupFunctions) {
12
+ // 后创建的东西依赖先创建的,必须**后**销毁:逆注册序执行
13
+ for (const cleanupFn of [...cleanupFunctions].reverse()) {
13
14
  try {
14
15
  await cleanupFn();
15
16
  }
@@ -54,15 +54,16 @@ describe('Disposer', () => {
54
54
  expect(cleanupFn2).toHaveBeenCalledOnce();
55
55
  expect(cleanupFn3).toHaveBeenCalledOnce();
56
56
  });
57
- it('应该按添加顺序执行清理函数', async () => {
57
+ it('应该按添加顺序的**逆序**(LIFO)执行清理函数', async () => {
58
58
  const executionOrder = [];
59
59
  disposer.add(() => executionOrder.push(1));
60
60
  disposer.add(() => executionOrder.push(2));
61
61
  disposer.add(() => executionOrder.push(3));
62
62
  await disposer.dispose();
63
- expect(executionOrder).toEqual([1, 2, 3]);
63
+ // 后创建的东西依赖先创建的,必须后销毁
64
+ expect(executionOrder).toEqual([3, 2, 1]);
64
65
  });
65
- it('应该按添加顺序执行异步清理函数', async () => {
66
+ it('应该按添加顺序的**逆序**(LIFO)执行异步清理函数', async () => {
66
67
  const executionOrder = [];
67
68
  disposer.add(async () => {
68
69
  await Promise.resolve();
@@ -77,7 +78,7 @@ describe('Disposer', () => {
77
78
  executionOrder.push(3);
78
79
  });
79
80
  await disposer.dispose();
80
- expect(executionOrder).toEqual([1, 2, 3]);
81
+ expect(executionOrder).toEqual([3, 2, 1]);
81
82
  });
82
83
  it('应该处理清理函数中的错误', async () => {
83
84
  const workingFn1 = vi.fn();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@taicode/common-base",
3
- "version": "4.0.0-beta.2",
3
+ "version": "4.0.0-beta.3",
4
4
  "author": "Alain",
5
5
  "license": "ISC",
6
6
  "description": "",