123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- Smalltalk createPackage: 'Axon'!
- Object subclass: #AxonBase
- instanceVariableNames: 'factory'
- package: 'Axon'!
- !AxonBase commentStamp!
- I represent a pub-sub based on a key (called 'aspect').
- I manage aspect-block subscriptions (called 'interests') as well as run blocks of dirtied interests.
- The interest objects are responsible of decision if the change of an aspect is relevant for them.
- Interest object must be subclasses of `AxonInterestBase`.
- My subclasses must provide implementation for:
- - add:
- - do:
- - clean
- - (optionally) run
- and issue this call before actual use:
- interestFactory: [ :description :block | ... factory that creates appropriate AxonInterest ... ]!
- !AxonBase methodsFor: 'action'!
- changed: anAspect
- | needsToRun |
- needsToRun := false.
- self do: [ :each |
- (each accepts: anAspect) ifTrue: [
- each flag.
- needsToRun := true.
- ]
- ].
- self dirty: needsToRun
- !
- dirty: aBoolean
- aBoolean ifTrue: [[ self run ] fork]
- !
- interestFactory: aBlock
- factory := aBlock
- !
- on: description hook: aBlock
- self add: (factory value: description value: aBlock) flag.
- self dirty: true
- !
- run
- [
- | needsClean |
- needsClean := false.
- self do: [ :each |
- each isFlagged ifTrue: [ each run ].
- each isEnabled ifFalse: [ needsClean := true ]
- ].
- needsClean ifTrue: [ self clean ]
- ] on: Error do: [ self dirty: true ]
- ! !
- AxonBase subclass: #SimpleAxon
- instanceVariableNames: 'queue'
- package: 'Axon'!
- !SimpleAxon methodsFor: 'accessing'!
- add: aSubscription
- queue add: aSubscription.
- ! !
- !SimpleAxon methodsFor: 'bookkeeping'!
- clean
- queue := queue select: [ :each | each isEnabled ]
- ! !
- !SimpleAxon methodsFor: 'enumeration'!
- do: aBlock
- queue do: aBlock
- ! !
- !SimpleAxon methodsFor: 'initialization'!
- initialize
- super initialize.
- queue := OrderedCollection new
- ! !
- Object subclass: #AxonInterestBase
- instanceVariableNames: 'aspect actionBlock flagged'
- package: 'Axon'!
- !AxonInterestBase methodsFor: 'accessing'!
- aspect: anAspect block: aBlock
- aspect := anAspect.
- actionBlock := aBlock
- !
- flag
- flagged := true
- ! !
- !AxonInterestBase methodsFor: 'action'!
- run
- [ flagged := false. actionBlock value ]
- on: AxonOff do: [ actionBlock := nil ]
- ! !
- !AxonInterestBase methodsFor: 'initialization'!
- initialize
- super initialize.
- aspect := nil.
- actionBlock := nil.
- flagged := false.
- ! !
- !AxonInterestBase methodsFor: 'testing'!
- accepts: anAspect
- "Should return true if change for anAspect is relevant for this AxonInterest"
- self subclassResponsibility
- !
- isEnabled
- ^actionBlock notNil
- !
- isFlagged
- ^flagged
- ! !
- AxonInterestBase subclass: #InterestedInEqual
- instanceVariableNames: ''
- package: 'Axon'!
- !InterestedInEqual methodsFor: 'testing'!
- accepts: anAspect
- ^ anAspect = aspect
- ! !
- Error subclass: #AxonOff
- instanceVariableNames: ''
- package: 'Axon'!
- !AxonOff commentStamp!
- SIgnal me from the subscription block to unsubscribe it.!
|