Skip to content

Commit

Permalink
Merge pull request #484 from pharo-graphics/taskitRunner
Browse files Browse the repository at this point in the history
Add convenience objects to use taskit in Bloc
  • Loading branch information
tinchodias committed May 18, 2024
2 parents 1c824d3 + 5a86feb commit ea3997e
Show file tree
Hide file tree
Showing 6 changed files with 203 additions and 0 deletions.
60 changes: 60 additions & 0 deletions src/Bloc-Taskit-Tests/TKTBlocExamples.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
Class {
#name : #TKTBlocExamples,
#superclass : #BlExamplesTest,
#category : #'Bloc-Taskit-Tests'
}

{ #category : #examples }
TKTBlocExamples >> runnerExample [
<sampleInstance>
" This is equivalent to:
space
enqueueTask:
(BlRepeatedTaskAction new
delay: 200 milliSeconds;
action: [ space root background: Color random ];
yourself)."

| space runner |
space := BlSpace new.
space
extent: 100@100;
show.

runner := TKTBlSpaceRunner on: space.

[ 17 timesRepeat: [
runner schedule: [
space root background: Color random ] asTask.
200 milliSeconds wait ] ] fork.

^ space
]

{ #category : #examples }
TKTBlocExamples >> serviceExample [
<sampleInstance>
"This example is equivalent to:
space
enqueueTask:
(BlRepeatedTaskAction new
delay: 100 milliSeconds;
action: [ space root background: Color random ];
yourself)."

| space service |
space := BlSpace new.
space
extent: 100@100;
show.

service := TKTBlSpaceService on: space.
service step: [ space root background: Color random ].
service stepDelay: 100 milliSeconds.
service start.

[ 2 seconds wait.
service stop waitForCompletion: 1 second ] fork.

^ space
]
56 changes: 56 additions & 0 deletions src/Bloc-Taskit-Tests/TKTBlocTest.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
Class {
#name : #TKTBlocTest,
#superclass : #BlParameterizedHostTest,
#category : #'Bloc-Taskit-Tests'
}

{ #category : #running }
TKTBlocTest >> isBeingExecutedInTaskPhase [

^ thisContext stack anySatisfy: [ :each |
each receiver class = BlSpaceFrameTaskPhase ]
]

{ #category : #tests }
TKTBlocTest >> testRunner [

| space runner |
space := self newTestingSpace.
space root background: Color green.

runner := TKTBlSpaceRunner on: space.
runner schedule: [
space root background: Color red.
self assert: self isBeingExecutedInTaskPhase ] asTask.

"Task is not executed immediately"
self
assert: space root background paint color
equals: Color green.
"Check again after pulse"
self waitTestingSpaces.
self
assert: space root background paint color
equals: Color red
]

{ #category : #tests }
TKTBlocTest >> testService [

| space service count |
space := self newTestingSpace.
space show.

service := TKTBlSpaceService on: space.
count := 0.
service step: [
count := count + 1.
self assert: self isBeingExecutedInTaskPhase ].
service stepDelay: 50 milliSeconds.
service start.

0.5 seconds wait.
service stop waitForCompletion: 1 second.

self assert: count > 1
]
1 change: 1 addition & 0 deletions src/Bloc-Taskit-Tests/package.st
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Package { #name : #'Bloc-Taskit-Tests' }
36 changes: 36 additions & 0 deletions src/Bloc-Taskit/TKTBlSpaceRunner.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"
I'm a TaskIt runner associated with a Bloc space.
"
Class {
#name : #TKTBlSpaceRunner,
#superclass : #TKTRunner,
#instVars : [
'space'
],
#category : #'Bloc-Taskit-Objects'
}

{ #category : #'instance creation' }
TKTBlSpaceRunner class >> on: aBlSpace [

^ self basicNew
initializeOn: aBlSpace;
yourself
]

{ #category : #initialization }
TKTBlSpaceRunner >> initializeOn: aBlSpace [

self initialize.

space := aBlSpace
]

{ #category : #schedulling }
TKTBlSpaceRunner >> scheduleTaskExecution: aTaskExecution [

space enqueueTask:
(BlTaskAction new
action: [ self executeTask: aTaskExecution ];
yourself)
]
49 changes: 49 additions & 0 deletions src/Bloc-Taskit/TKTBlSpaceService.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"
I'm a TaskIt service associated with a Bloc space.
"
Class {
#name : #TKTBlSpaceService,
#superclass : #TKTService,
#instVars : [
'space',
'actionBlock'
],
#category : #'Bloc-Taskit-Objects'
}

{ #category : #'instance creation' }
TKTBlSpaceService class >> on: aBlSpace [

^ self basicNew
initializeOn: aBlSpace;
yourself
]

{ #category : #initialization }
TKTBlSpaceService >> initializeOn: aBlSpace [

self initialize.

space := aBlSpace
]

{ #category : #accessing }
TKTBlSpaceService >> name [

^ '{1}/{2}' format: { space. self identityHash }
]

{ #category : #accessing }
TKTBlSpaceService >> step: aBlockClosure [

actionBlock := aBlockClosure
]

{ #category : #stepping }
TKTBlSpaceService >> stepService [

space enqueueTask:
(BlTaskAction new
action: actionBlock;
yourself)
]
1 change: 1 addition & 0 deletions src/Bloc-Taskit/package.st
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Package { #name : #'Bloc-Taskit' }

0 comments on commit ea3997e

Please sign in to comment.