Skip to content

Commit 2756f8a

Browse files
committed
feat(shared): 支持Container override
1 parent 863cd1a commit 2756f8a

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed

packages/shared/src/container.ts

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,28 @@
1-
type Listener<T> = (instance: T) => void
1+
type Listener<T> = (value: T) => void
22

33
export class Container<K, T> {
44
private pool = new Map<K, T>()
55
private listeners = new Map<K, Set<Listener<T>>>()
66

7-
public bind(key: K, instance: T): boolean {
8-
if (this.pool.has(key)) {
9-
return false
10-
}
11-
this.pool.set(key, instance)
7+
public bind(key: K, value: T): boolean {
8+
this.pool.set(key, value)
129
this.trigger(key)
1310
return true
1411
}
1512

1613
public unbind(key: K): boolean | T {
17-
const instance = this.pool.get(key)
18-
if (instance) {
14+
const value = this.pool.get(key)
15+
if (value) {
1916
this.pool.delete(key)
20-
return instance
17+
return value
2118
}
2219
return true
2320
}
2421

2522
public use(key: K): boolean | T {
26-
const instance = this.pool.get(key)
27-
if (instance) {
28-
return instance
23+
const value = this.pool.get(key)
24+
if (value) {
25+
return value
2926
}
3027
return false
3128
}
@@ -61,11 +58,11 @@ export class Container<K, T> {
6158
}
6259

6360
public trigger(key: K) {
64-
const instance = this.pool.get(key)
61+
const value = this.pool.get(key)
6562
const listeners = this.listeners.get(key)
6663
if (listeners) {
6764
listeners.forEach((listener) => {
68-
listener(instance as T)
65+
listener(value as T)
6966
})
7067
listeners.clear()
7168
}

0 commit comments

Comments
 (0)