Skip to content

Commit

Permalink
fix(EventBus): 解决 off 问题
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Oct 18, 2021
1 parent 0fdf552 commit 277149e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/utils/EventBus.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { EventBus } from './EventBus'
import { wait } from './wait'

type Events = {
enter: () => number
Expand Down Expand Up @@ -81,6 +82,18 @@ describe('EventBus', () => {
expect(successCallback2).toBeCalled().toBeCalledTimes(1)
})

test('可只订阅一次: bug', async () => {
const bus = new EventBus<Events>()
const successCallback = jest.fn()
const successCallback2 = jest.fn()
bus.once('success', successCallback)
bus.once('success', successCallback2)
await wait(10)
bus.emit('success')
expect(successCallback).toBeCalled().toBeCalledTimes(1)
expect(successCallback2).toBeCalled().toBeCalledTimes(1)
})

test('可获取订阅回调结果', () => {
const bus = new EventBus<Events>()
const enterCallback = jest.fn().mockImplementation(() => 1)
Expand Down
2 changes: 1 addition & 1 deletion src/utils/EventBus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ export class EventBus<TListeners extends EventBusListeners> {
: {
name: eventName,
}
let callbacks = this.callbacks[name] || []
let callbacks = (this.callbacks[name] || []).slice()
if (tag != null) {
callbacks = callbacks.filter(
callback =>
Expand Down

0 comments on commit 277149e

Please sign in to comment.