Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions src/Molecule-Tests/MolEventsTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,102 @@ MolEventsTest >> tearDown [
super tearDown.
]

{ #category : #tests }
MolEventsTest >> testOneListenerWithSeveralWarnersWithoutSameSubscribers [

| warner1 warner2 listener subscriptions subscribers listener2 subscribers2 subscriptions2 |
"Deployment"
MolMyWarnerComponentImpl deploy.
MolMyListenerComponentImpl deploy.

"Instanciation"
warner1 := MolMyWarnerComponentImpl instantiate: #warner1.
warner2 := MolMyWarnerComponentImpl instantiate: #warner2.
listener := MolMyListenerComponentImpl instantiate: #listener1.
listener2 := MolMyListenerComponentImpl instantiate: #listener2.

"Subscriptions"
subscriptions := Array with: #warner1.
listener forEvents: MolMyStateChangeEvents useAllProducers: subscriptions.

subscriptions2 := Array with: #warner2.
listener2 forEvents: MolMyStateChangeEvents useAllProducers: subscriptions2.

subscribers := listener eventsSubscribers.
self assert: subscribers size equals: 1.
self assert: (subscribers at: MolMyStateChangeEvents) equals: #warner1.

subscribers2 := listener2 eventsSubscribers.
self assert: subscribers2 size equals: 1.
self assert: (subscribers2 at: MolMyStateChangeEvents) equals: #warner2.

"Activation"
MolMyWarnerComponentImpl activate: #warner1.
MolMyWarnerComponentImpl activate: #warner2.
MolMyListenerComponentImpl activate: #listener1.
MolMyListenerComponentImpl activate: #listener2.

"Tests"
warner1 operationalEvent.
self assert: listener currentState equals: #operational.
self assert: listener2 currentState isNil.
warner2 failureEvent. "warner 2 is not active, so he can't send an event"
self assert: listener currentState equals: #operational.
self assert: listener2 currentState equals: #failure
]

{ #category : #tests }
MolEventsTest >> testOneListenerWithSeveralWarnersWithoutSameSubscribers2 [

| warner1 warner2 listener subscriptions subscribers listener2 subscribers2 subscriptions2 |
"Deployment"
MolMyWarnerComponentImpl deploy.
MolMyListenerComponentImpl deploy.

"Instanciation"
warner1 := MolMyWarnerComponentImpl instantiate: #warner1.
warner2 := MolMyWarnerComponentImpl instantiate: #warner2.
listener := MolMyListenerComponentImpl instantiate: #listener1.
listener2 := MolMyListenerComponentImpl instantiate: #listener2.

"Subscriptions"
subscriptions := #warner1.
listener forEvents: MolMyStateChangeEvents useProducer: subscriptions.

subscriptions2 := #warner2.
listener2
forEvents: MolMyStateChangeEvents
useProducer: subscriptions2.

subscribers := listener eventsSubscribers.
self assert: subscribers size equals: 1.
self
assert: (subscribers at: MolMyStateChangeEvents)
equals: #warner1.

subscribers2 := listener2 eventsSubscribers.
self assert: subscribers2 size equals: 1.
self
assert: (subscribers2 at: MolMyStateChangeEvents)
equals: #warner2.

"Activation"

MolMyWarnerComponentImpl activate: #warner1.

MolMyListenerComponentImpl activate: #listener1.
MolMyListenerComponentImpl activate: #listener2.

"Tests"

warner1 operationalEvent.
self assert: listener currentState equals: #operational.
self assert: listener2 currentState isNil.
warner2 failureEvent. "warner 2 is not active, so he can't send an event"
self assert: listener currentState equals: #operational.
self assert: listener2 currentState isNil.
]

{ #category : #tests }
MolEventsTest >> testSeveralListeners [

Expand Down
16 changes: 10 additions & 6 deletions src/Molecule/MolEventSubscriber.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,16 @@ MolEventSubscriber >> connectAllOriginatorsTo: aComponent [
MolEventSubscriber >> connectOriginator: componentName to: aComponent [
"Connect a component to event pipeline"

self events allSelectors do: [ :event | | originator |

originator := self originatorsLinks at: componentName.
originator ifNil: [ ^ self error: 'Component originator is nil' ].
originator when: event send: event to: aComponent.
].
| key connect |
key := aComponent eventsSubscribers at: self events ifAbsent: [ nil ].
connect := key isSymbol ifTrue:[ componentName = key ] ifFalse:[ key includes: componentName ].

connect ifTrue: [
self events allSelectors do: [ :event |
| originator |
originator := self originatorsLinks at: componentName.
originator ifNil: [ ^ self error: 'Component originator is nil' ].
originator when: event send: event to: aComponent ] ]
]

{ #category : #private }
Expand Down
Loading