Skip to content

Commit

Permalink
feat(Disposer): API 重构
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed May 11, 2019
1 parent 6ed3346 commit 453e6ae
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 81 deletions.
62 changes: 16 additions & 46 deletions src/Disposer.ts
Original file line number Diff line number Diff line change
@@ -1,62 +1,32 @@
/* eslint-disable lines-between-class-members */
/* eslint-disable no-dupe-class-members */
import { randomString } from './randomString'
import { isFunction } from './isFunction'

export type Dispose = () => void

const anonymousKey: string = `__anonymous_${randomString()}__`
export type DisposerItem = () => void

/**
* 处置器
* 资源释放器
*/
export class Disposer<N extends string | number = string | number> {
export class Disposer {
/**
* 待处置项目存放容器。
*/
private jar: { [name in N]: Dispose[] } = Object.create(null)

/**
* 将待处置项目加入容器。
*
* @param name 待处置项目名称
* @param dispose 处置行为
* 待释放项目存储容器。
*/
public add(name: N, dispose: Dispose | Dispose[]): void
/**
* 将匿名待处置项目加入容器。
*
* @param dispose 处置行为
*/
public add(dispose: Dispose | Dispose[]): void
public add(name: any, dispose?: any): void {
if (dispose == null) {
dispose = name
name = anonymousKey
}
dispose = Array.isArray(dispose) ? dispose : [dispose]
;(this.jar as any)[name] = [
...((this.jar as any)[name] || []),
...dispose,
]
}
private jar: DisposerItem[] = []

/**
* 处置项目
* 新增待释放项目
*
* @param name 欲处置项目名称,不设置表示匿名项目
* @param items 待释放项目的序列
*/
public dispose(name?: N): void {
name = name != null ? name : anonymousKey as any
(this.jar[name] || /* istanbul ignore next */ []).forEach(dispose => dispose())
delete this.jar[name]
public add(...items: DisposerItem[]): void {
this.jar.push(...items)
}

/**
* 处置所有未处置项目
* 释放所有项目
*/
public disposeAll(): void {
for (const key in this.jar) {
this.dispose(key)
}
public dispose() {
this.jar.forEach(
item => isFunction(item) && item(),
)
this.jar = []
}
}
41 changes: 6 additions & 35 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,46 +177,17 @@ describe('base64', () => {

describe('Disposer', () => {
const disposer = new vtils.Disposer()
const helloDispose1 = jest.fn()
const helloDispose2 = jest.fn()
const helloDispose3 = jest.fn()
test('add', () => {
disposer.add('hello', helloDispose1)
expect((disposer as any).jar.hello).toEqual([helloDispose1])
disposer.add('hello', [helloDispose2, helloDispose3])
expect((disposer as any).jar.hello).toEqual([helloDispose1, helloDispose2, helloDispose3])
})
test('dispose', () => {
disposer.dispose('hello')
expect(helloDispose1).toBeCalledTimes(1)
expect(helloDispose2).toBeCalledTimes(1)
expect(helloDispose3).toBeCalledTimes(1)
expect((disposer as any).jar.hello).toBeUndefined()
})
test('disposeAll', () => {
const dispose1 = jest.fn()
const dispose2 = jest.fn()
const dispose3 = jest.fn()
disposer.add('1', dispose1)
disposer.add('2', dispose2)
disposer.add('3', dispose3)
disposer.disposeAll()
expect(helloDispose1).toBeCalledTimes(1)
expect(helloDispose2).toBeCalledTimes(1)
expect(helloDispose3).toBeCalledTimes(1)
expect((disposer as any).jar).toEqual({})
})
test('匿名项目', () => {

test('ok', () => {
const dispose1 = jest.fn()
const dispose2 = jest.fn()
const dispose3 = jest.fn()
disposer.add(dispose1)
disposer.add([dispose2, dispose3])
disposer.add(dispose2, dispose3)
disposer.dispose()
expect(helloDispose1).toBeCalledTimes(1)
expect(helloDispose2).toBeCalledTimes(1)
expect(helloDispose3).toBeCalledTimes(1)
expect((disposer as any).jar).toEqual({})
expect(dispose1).toBeCalledTimes(1)
expect(dispose2).toBeCalledTimes(1)
expect(dispose3).toBeCalledTimes(1)
})
})

Expand Down

0 comments on commit 453e6ae

Please sign in to comment.