-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimize createListenerCollection (#1523)
- Loading branch information
1 parent
3eb5271
commit e649fb6
Showing
2 changed files
with
96 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import Subscription from '../../src/utils/Subscription' | ||
|
||
describe('Subscription', () => { | ||
let notifications | ||
let store | ||
let parent | ||
|
||
beforeEach(() => { | ||
notifications = [] | ||
store = { subscribe: () => jest.fn() } | ||
|
||
parent = new Subscription(store) | ||
parent.onStateChange = () => {} | ||
parent.trySubscribe() | ||
}) | ||
|
||
function subscribeChild(name) { | ||
const child = new Subscription(store, parent) | ||
child.onStateChange = () => notifications.push(name) | ||
child.trySubscribe() | ||
return child | ||
} | ||
|
||
it('listeners are notified in order', () => { | ||
subscribeChild('child1') | ||
subscribeChild('child2') | ||
subscribeChild('child3') | ||
subscribeChild('child4') | ||
|
||
parent.notifyNestedSubs() | ||
|
||
expect(notifications).toEqual(['child1', 'child2', 'child3', 'child4']) | ||
}) | ||
|
||
it('listeners can be unsubscribed', () => { | ||
const child1 = subscribeChild('child1') | ||
const child2 = subscribeChild('child2') | ||
const child3 = subscribeChild('child3') | ||
|
||
child2.tryUnsubscribe() | ||
parent.notifyNestedSubs() | ||
|
||
expect(notifications).toEqual(['child1', 'child3']) | ||
notifications.length = 0 | ||
|
||
child1.tryUnsubscribe() | ||
parent.notifyNestedSubs() | ||
|
||
expect(notifications).toEqual(['child3']) | ||
notifications.length = 0 | ||
|
||
child3.tryUnsubscribe() | ||
parent.notifyNestedSubs() | ||
|
||
expect(notifications).toEqual([]) | ||
}) | ||
}) |