Skip to content

Commit

Permalink
Add callback invocation capabilities.
Browse files Browse the repository at this point in the history
  • Loading branch information
stlutz committed Dec 26, 2017
1 parent 40db98e commit 8388f22
Show file tree
Hide file tree
Showing 21 changed files with 111 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
instance creation
monitoring: aBlock

^ self new setMonitoredBlock: aBlock
^ self basicNew
setMonitoredBlock: aBlock;
initialize
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
accessing
currentValue

^ monitoredBlock value
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
initialize-release
initialize

super initialize.
self setLastValue: self currentValue
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
callbacks
invokeCallback: aBlock

aBlock value
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
callbacks
invokeCallback: aBlock lastValue: old newValue: new

aBlock argumentCount = 0 ifTrue: [^ aBlock value].
aBlock argumentCount = 1 ifTrue: [^ aBlock valueWithArguments: { new }].
aBlock argumentCount = 2 ifTrue: [^ aBlock valueWithArguments: { old . new }].
self error: 'Callback requires too many arguments'
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
accessing
lastValue

^ lastValue
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
private
setLastValue: anObject

lastValue := anObject
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
change
update

| newValue oldValue |
oldValue := self lastValue.
newValue := self currentValue.
oldValue ~~ newValue ifTrue: [
self setLastValue: newValue.
self callbacks do: [:each | self invokeCallback: each lastValue: oldValue newValue: newValue]]
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
{
"class" : {
"monitoring:" : "sl 12/26/2017 09:55" },
"monitoring:" : "sl 12/26/2017 18:55" },
"instance" : {
"callbacks" : "sl 12/26/2017 10:15",
"currentValue" : "sl 12/26/2017 10:49",
"initialize" : "sl 12/26/2017 10:48",
"invokeCallback:" : "sl 12/26/2017 10:36",
"invokeCallback:lastValue:newValue:" : "sl 12/26/2017 10:46",
"lastValue" : "sl 12/26/2017 18:49",
"monitoredBlock" : "sl 12/23/2017 10:14",
"onChangeDo:" : "sl 12/26/2017 10:11",
"printOn:" : "sl 12/26/2017 10:04",
"registerCallback:" : "sl 12/26/2017 10:13",
"setMonitoredBlock:" : "sl 12/26/2017 09:53" } }
"setLastValue:" : "sl 12/26/2017 18:43",
"setMonitoredBlock:" : "sl 12/26/2017 09:53",
"update" : "sl 12/26/2017 18:49" } }
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"commentStamp" : "",
"instvars" : [
"monitoredBlock",
"callbacks" ],
"callbacks",
"lastValue" ],
"name" : "ActiveExpression",
"pools" : [
],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
running
setUp

value := 42.
callbackCount := 0.
activeExpression := ActiveExpression monitoring: [value].
activeExpression onChangeDo: [callbackCount := callbackCount + 1]
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
tests - callbacks
testCallbackInvocationWhenValueIdentityHasChanged

value := 2.
activeExpression update.
self assert: 1 equals: callbackCount
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
tests - callbacks
testCallbackInvocationWith1Argument

activeExpression onChangeDo: [:new |
self assert: 1 equals: new].
value := 1.
activeExpression update
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
tests - callbacks
testCallbackInvocationWith2Arguments

activeExpression onChangeDo: [:old :new |
self assert: 42 equals: old.
self assert: 1 equals: new].
value := 1.
activeExpression update
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
tests - callbacks
testCallbackInvocationWithMoreThan3ArgumentsFails

activeExpression onChangeDo: [:one :two :three | 42].
value := 1.
self should: [activeExpression update] raise: Error
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
tests
testInitializesToCurrentValue

self assert: 42 equals: activeExpression lastValue
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
tests - callbacks
testNoCallbackInvocationWhenValueIdentityHasNotChanged

activeExpression update.
self assert: 0 equals: callbackCount
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
tests
testRemembersOldValue

value := 1.
activeExpression update.
self assert: 1 equals: activeExpression lastValue

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,11 @@
"class" : {
},
"instance" : {
"testTruth" : "sl 12/23/2017 10:18" } }
"setUp" : "sl 12/26/2017 18:51",
"testCallbackInvocationWhenValueIdentityHasChanged" : "sl 12/26/2017 10:35",
"testCallbackInvocationWith1Argument" : "sl 12/26/2017 19:05",
"testCallbackInvocationWith2Arguments" : "sl 12/26/2017 19:05",
"testCallbackInvocationWithMoreThan3ArgumentsFails" : "sl 12/26/2017 19:01",
"testInitializesToCurrentValue" : "sl 12/26/2017 18:51",
"testNoCallbackInvocationWhenValueIdentityHasNotChanged" : "sl 12/26/2017 18:57",
"testRemembersOldValue" : "sl 12/26/2017 19:09" } }
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
],
"commentStamp" : "",
"instvars" : [
],
"value",
"activeExpression",
"callbackCount" ],
"name" : "ActiveExpressionTests",
"pools" : [
],
Expand Down

0 comments on commit 8388f22

Please sign in to comment.