Skip to content

Commit

Permalink
Collapse any duplicated events when flusing the event queue.
Browse files Browse the repository at this point in the history
  • Loading branch information
joao-pedro-braz committed Mar 10, 2024
1 parent 5068242 commit e29d4a4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
7 changes: 7 additions & 0 deletions src/Bloc/BlEvent.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,13 @@ BlEvent >> bubblingTarget: aTBlEventTarget [
currentTarget := aTBlEventTarget
]

{ #category : #accessing }
BlEvent >> canBeCollapsed [
"Whether I can be skipped in case there's too many of myself queued."

^ false
]

{ #category : #testing }
BlEvent >> canBePropagated [
<return: #Boolean>
Expand Down
17 changes: 13 additions & 4 deletions src/Bloc/BlHostEventFetcher.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Class {
'eventQueue',
'hostSpace'
],
#category : 'Bloc-Universe - Host'
#category : #'Bloc-Universe - Host'
}

{ #category : #accessing }
Expand All @@ -35,11 +35,20 @@ BlHostEventFetcher >> enqueueEvent: aBlEvent [

{ #category : #'event processing' }
BlHostEventFetcher >> fetchedEventsDo: aBlock [
"Flush event queue and evaluate a given block with every queued event as argument"
| theEvents |
"I flush the event queue and evaluate a given block with every queued event as an argument.
In case there are multiple events of the same kind that can be skipped (such as mouse movements),
I will also collapse them into a single event, by only flushing the newest one."

| theEvents |
theEvents := LinkedList new.
eventQueue flush: [ :anEvent | theEvents add: anEvent ].
eventQueue
flush: [ :anEvent |
| theLastEventLink |
(anEvent canBeCollapsed
and: [ theEvents isNotEmpty
and: [ (theLastEventLink := theEvents lastLink) value class = anEvent class ] ])
ifTrue: [ theLastEventLink value: anEvent ]
ifFalse: [ theEvents add: anEvent ] ].
theEvents do: aBlock
]

Expand Down
5 changes: 5 additions & 0 deletions src/Bloc/BlMouseMoveEvent.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ BlMouseMoveEvent class >> secondary [
^ self button: BlMouseButton secondary
]

{ #category : #accessing }
BlMouseMoveEvent >> canBeCollapsed [
^ true
]

{ #category : #accessing }
BlMouseMoveEvent >> delta [
^ delta
Expand Down

0 comments on commit e29d4a4

Please sign in to comment.