author | Claus Gittinger <cg@exept.de> |
Sat, 09 Mar 2019 20:57:31 +0100 | |
changeset 4244 | b60f87ce9eda |
parent 4242 | dee79356dad2 |
child 4257 | 2f858dbfd25b |
permissions | -rw-r--r-- |
4242 | 1 |
"{ Encoding: utf8 }" |
2 |
||
145 | 3 |
" |
4 |
COPYRIGHT (c) 1995 by Claus Gittinger |
|
158 | 5 |
All Rights Reserved |
145 | 6 |
|
7 |
This software is furnished under a license and may be used |
|
8 |
only in accordance with the terms of that license and with the |
|
9 |
inclusion of the above copyright notice. This software may not |
|
10 |
be provided or otherwise made available to, or used by, any |
|
11 |
other person. No title to or ownership of the software is |
|
12 |
hereby transferred. |
|
13 |
" |
|
1665 | 14 |
"{ Package: 'stx:libview2' }" |
15 |
||
3474 | 16 |
"{ NameSpace: Smalltalk }" |
17 |
||
145 | 18 |
ValueModel subclass:#BlockValue |
19 |
instanceVariableNames:'cachedValue arguments block' |
|
146
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
20 |
classVariableNames:'NeverComputed' |
145 | 21 |
poolDictionaries:'' |
22 |
category:'Interface-Support-Models' |
|
23 |
! |
|
24 |
||
25 |
!BlockValue class methodsFor:'documentation'! |
|
26 |
||
27 |
copyright |
|
28 |
" |
|
29 |
COPYRIGHT (c) 1995 by Claus Gittinger |
|
158 | 30 |
All Rights Reserved |
145 | 31 |
|
32 |
This software is furnished under a license and may be used |
|
33 |
only in accordance with the terms of that license and with the |
|
34 |
inclusion of the above copyright notice. This software may not |
|
35 |
be provided or otherwise made available to, or used by, any |
|
36 |
other person. No title to or ownership of the software is |
|
37 |
hereby transferred. |
|
38 |
" |
|
39 |
||
40 |
! |
|
41 |
||
42 |
documentation |
|
43 |
" |
|
44 |
BlockValues depend on multiple other objects (typically valueHolders) |
|
45 |
and recompute a value whenever one of them changes. |
|
46 |
If the new value is different, it triggers itself a change to its dependents. |
|
47 |
||
48 |
Example use is to base an enableChannels value on multiple other boolean values. |
|
49 |
(See example for how this is done) |
|
223 | 50 |
|
231 | 51 |
Notice: |
52 |
this class was implemented using protocol information |
|
53 |
from alpha testers - it may not be complete or compatible to |
|
54 |
the corresponding ST-80 class. |
|
55 |
If you encounter any incompatibilities, please forward a note |
|
56 |
describing the incompatibility verbal (i.e. no code) to the ST/X team. |
|
57 |
||
2760 | 58 |
Warning: |
59 |
BlockValues only work ine one direction; changing the blockValue's value |
|
60 |
does not affect the original model's value. |
|
61 |
||
223 | 62 |
[author:] |
63 |
Claus Gittinger |
|
145 | 64 |
" |
65 |
! |
|
66 |
||
67 |
examples |
|
68 |
" |
|
155 | 69 |
checkToggle 3 shows the value of toggle1 AND toggle2 |
231 | 70 |
[exBegin] |
71 |
|val1 val2 both box| |
|
155 | 72 |
|
231 | 73 |
val1 := false asValue. |
74 |
val2 := false asValue. |
|
75 |
both := BlockValue |
|
76 |
with:[:v1 :v2 | |
|
268 | 77 |
Transcript showCR:'evaluating ...'. |
231 | 78 |
v1 value and:[v2 value] |
79 |
] |
|
80 |
arguments:(Array with:val1 with:val2). |
|
155 | 81 |
|
231 | 82 |
box := Dialog new. |
83 |
box addCheckBox:'one' on:val1. |
|
84 |
box addCheckBox:'two' on:val2. |
|
85 |
box addHorizontalLine. |
|
86 |
box addCheckBox:'both' on:both. |
|
911 | 87 |
box addOkButton. |
231 | 88 |
box open |
89 |
[exEnd] |
|
155 | 90 |
|
91 |
the same, using a convenient instance creation message: |
|
231 | 92 |
[exBegin] |
93 |
|val1 val2 both box| |
|
155 | 94 |
|
231 | 95 |
val1 := false asValue. |
96 |
val2 := false asValue. |
|
97 |
both := BlockValue forLogical:val1 and:val2. |
|
145 | 98 |
|
231 | 99 |
box := Dialog new. |
100 |
box addCheckBox:'one' on:val1. |
|
101 |
box addCheckBox:'two' on:val2. |
|
102 |
box addHorizontalLine. |
|
911 | 103 |
(box addCheckBox:'both' on:both) disable. |
104 |
box addOkButton. |
|
231 | 105 |
box open |
106 |
[exEnd] |
|
155 | 107 |
|
108 |
logical or: |
|
231 | 109 |
[exBegin] |
110 |
|val1 val2 both box| |
|
155 | 111 |
|
231 | 112 |
val1 := false asValue. |
113 |
val2 := false asValue. |
|
114 |
both := BlockValue forLogical:val1 or:val2. |
|
145 | 115 |
|
231 | 116 |
box := Dialog new. |
117 |
box addCheckBox:'one' on:val1. |
|
118 |
box addCheckBox:'two' on:val2. |
|
119 |
box addHorizontalLine. |
|
120 |
box addCheckBox:'both' on:both. |
|
911 | 121 |
box addOkButton. |
231 | 122 |
box open |
123 |
[exEnd] |
|
155 | 124 |
|
125 |
example use: enabling an element depending on two others: |
|
231 | 126 |
[exBegin] |
127 |
|val1 val2 enabler val3 box| |
|
155 | 128 |
|
231 | 129 |
val1 := false asValue. |
130 |
val2 := false asValue. |
|
131 |
val3 := false asValue. |
|
132 |
enabler := BlockValue forLogical:val1 and:val2. |
|
155 | 133 |
|
231 | 134 |
box := Dialog new. |
135 |
box addCheckBox:'one' on:val1. |
|
136 |
box addCheckBox:'two' on:val2. |
|
137 |
box addHorizontalLine. |
|
911 | 138 |
(box addCheckBox:'three (both of the above)' on:val3) enableChannel:enabler. |
139 |
box addOkButton. |
|
231 | 140 |
box open |
141 |
[exEnd] |
|
382 | 142 |
|
143 |
||
144 |
like above, using a logical-or block: |
|
145 |
[exBegin] |
|
146 |
|val1 val2 enabler val3 box| |
|
147 |
||
148 |
val1 := false asValue. |
|
149 |
val2 := false asValue. |
|
150 |
val3 := false asValue. |
|
151 |
enabler := BlockValue forLogical:val1 or:val2. |
|
152 |
||
153 |
box := Dialog new. |
|
154 |
box addCheckBox:'one' on:val1. |
|
155 |
box addCheckBox:'two' on:val2. |
|
156 |
box addHorizontalLine. |
|
911 | 157 |
(box addCheckBox:'three (any of the above)' on:val3) enableChannel:enabler. |
158 |
box addOkButton. |
|
382 | 159 |
box open |
160 |
[exEnd] |
|
161 |
||
162 |
||
163 |
like above, using a bunch of toggles: |
|
164 |
[exBegin] |
|
165 |
|values anyValue box| |
|
166 |
||
167 |
values := (1 to:10) collect:[:i | false asValue]. |
|
168 |
anyValue := BlockValue forLogicalOrAll:values. |
|
169 |
anyValue onChangeSend:#value to:[Transcript showCR:'any is true']. |
|
170 |
||
171 |
box := Dialog new. |
|
172 |
values keysAndValuesDo:[:index :aValueHolder | |
|
173 |
box addCheckBox:index printString on:aValueHolder. |
|
174 |
]. |
|
175 |
box addHorizontalLine. |
|
911 | 176 |
(box addOkButton) enableChannel:anyValue. |
382 | 177 |
box open |
178 |
[exEnd] |
|
145 | 179 |
" |
180 |
! ! |
|
181 |
||
146
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
182 |
!BlockValue class methodsFor:'initialization'! |
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
183 |
|
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
184 |
initialize |
158 | 185 |
NeverComputed isNil ifTrue:[ |
186 |
NeverComputed := Object new. |
|
187 |
] |
|
146
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
188 |
! ! |
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
189 |
|
145 | 190 |
!BlockValue class methodsFor:'instance creation'! |
191 |
||
979
cd28a5d673c9
added #block:arguments: for ST80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
192 |
block:aBlock arguments:aCollectionOfArguments |
cd28a5d673c9
added #block:arguments: for ST80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
193 |
"return a new BlockValue computing aBlock. |
cd28a5d673c9
added #block:arguments: for ST80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
194 |
Same as #with:arguments: for ST80 compatibility" |
cd28a5d673c9
added #block:arguments: for ST80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
195 |
|
cd28a5d673c9
added #block:arguments: for ST80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
196 |
^ self with:aBlock arguments:aCollectionOfArguments |
cd28a5d673c9
added #block:arguments: for ST80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
197 |
|
cd28a5d673c9
added #block:arguments: for ST80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
198 |
"Created: / 20.6.1998 / 14:00:16 / cg" |
cd28a5d673c9
added #block:arguments: for ST80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
199 |
! |
cd28a5d673c9
added #block:arguments: for ST80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
200 |
|
155 | 201 |
forLogical:arg1 and:arg2 |
1326 | 202 |
"return a new BlockValue computing the logical AND of its args |
203 |
(which are usually other valueHolders)" |
|
155 | 204 |
|
1815 | 205 |
^ (self new) |
1326 | 206 |
setBlock:[:a :b | a value and:[b value]] |
207 |
arguments:(Array with:arg1 with:arg2) |
|
155 | 208 |
|
1326 | 209 |
"Created: / 16.12.1995 / 19:20:14 / cg" |
210 |
"Modified: / 30.1.2000 / 23:24:50 / cg" |
|
155 | 211 |
! |
212 |
||
2800 | 213 |
forLogical:arg1 and:arg2 and:arg3 |
214 |
"return a new BlockValue computing the logical AND of its args |
|
215 |
(which are usually other valueHolders)" |
|
216 |
||
217 |
^ (self new) |
|
218 |
setBlock:[:a :b :c | a value and:[b value and:[c value]]] |
|
219 |
arguments:(Array with:arg1 with:arg2 with:arg3) |
|
220 |
! |
|
221 |
||
155 | 222 |
forLogical:arg1 or:arg2 |
1326 | 223 |
"return a new BlockValue computing the logical OR of its args |
224 |
(which are usually other valueHolders)" |
|
155 | 225 |
|
1815 | 226 |
^ (self new) |
1326 | 227 |
setBlock:[:a :b | a value or:[b value]] |
228 |
arguments:(Array with:arg1 with:arg2) |
|
155 | 229 |
|
1326 | 230 |
"Created: / 16.12.1995 / 19:20:14 / cg" |
231 |
"Modified: / 30.1.2000 / 23:24:46 / cg" |
|
155 | 232 |
! |
233 |
||
2800 | 234 |
forLogical:arg1 or:arg2 or:arg3 |
235 |
"return a new BlockValue computing the logical OR of its args |
|
236 |
(which are usually other valueHolders)" |
|
237 |
||
238 |
^ (self new) |
|
239 |
setBlock:[:a :b :c | a value or:[b value or:[c value]]] |
|
240 |
arguments:(Array with:arg1 with:arg2 with:arg3) |
|
241 |
! |
|
242 |
||
382 | 243 |
forLogicalAndAll:argArray |
3474 | 244 |
"return a new BlockValue computing the logical AND of all elements |
1326 | 245 |
in the passed argArray (which are usually other valueHolders)" |
382 | 246 |
|
1815 | 247 |
^ (self new) |
382 | 248 |
setBlock:[:a | (argArray findFirst:[:a | a value not]) == 0] |
911 | 249 |
argumentArray:argArray asArray |
382 | 250 |
|
911 | 251 |
"Created: / 22.1.1997 / 19:12:44 / cg" |
252 |
"Modified: / 28.4.1998 / 20:16:38 / ca" |
|
1326 | 253 |
"Modified: / 30.1.2000 / 23:24:40 / cg" |
382 | 254 |
! |
255 |
||
1045 | 256 |
forLogicalNot:arg |
1326 | 257 |
"return a new BlockValue computing the logical NOT of its arg |
258 |
(which is usually another valueHolder)" |
|
1045 | 259 |
|
1815 | 260 |
^ (self new) |
1045 | 261 |
setBlock:[:a | a value not] |
262 |
arguments:(Array with:arg) |
|
263 |
||
3349 | 264 |
" |
265 |
|v1 v2| |
|
266 |
||
267 |
v1 := ValueHolder new. |
|
268 |
v1 value:true. |
|
269 |
||
270 |
v2 := BlockValue forLogicalNot:v1. |
|
271 |
v2 onChangeEvaluate:[ Transcript showCR:v2 value]. |
|
272 |
Transcript showCR:v2 value. 'shows a false already'. |
|
273 |
||
274 |
v1 value:true. 'no change signalled here'. |
|
275 |
v1 value:false. |
|
276 |
v1 value:true. |
|
277 |
" |
|
278 |
||
1045 | 279 |
"Created: / 17.8.1998 / 10:06:41 / cg" |
1326 | 280 |
"Modified: / 30.1.2000 / 23:24:18 / cg" |
1045 | 281 |
! |
282 |
||
382 | 283 |
forLogicalOrAll:argArray |
284 |
"return a new BlockValue computing the logical OR of all elements |
|
1326 | 285 |
in the passed argArray (which are usually other valueHolders)" |
382 | 286 |
|
1815 | 287 |
^ (self new) |
606 | 288 |
setBlock:[:arg | (arg findFirst:[:el | el value]) ~~ 0] |
911 | 289 |
argumentArray:argArray asArray |
382 | 290 |
|
911 | 291 |
"Created: / 22.1.1997 / 19:13:01 / cg" |
292 |
"Modified: / 28.4.1998 / 20:20:09 / ca" |
|
1326 | 293 |
"Modified: / 30.1.2000 / 23:24:34 / cg" |
382 | 294 |
! |
295 |
||
3349 | 296 |
forValue:someValue equalTo:someConstant |
297 |
"return a new BlockValue generating a true, |
|
298 |
if someValue (usually another holder) contains a value equal to someConstant. |
|
299 |
Useful, if the other valueHolder is a radioButton group model, |
|
300 |
and you want to automatically enable/disable other items based on the value |
|
301 |
(i.e. via the enableChannel)" |
|
302 |
||
303 |
^ (self new) |
|
304 |
setBlock:[:a | a = someConstant] |
|
305 |
arguments:(Array with:someValue) |
|
306 |
||
307 |
" |
|
308 |
|v1 v2| |
|
309 |
||
310 |
v1 := ValueHolder new. |
|
311 |
v1 value:'blah'. |
|
312 |
||
313 |
v2 := BlockValue forValue:v1 equalTo:'hello'. |
|
314 |
v2 onChangeEvaluate:[ Transcript showCR:v2 value]. |
|
315 |
Transcript showCR:v2 value. 'shows a false already'. |
|
316 |
||
317 |
v1 value:'blah'. 'no change signalled here'. |
|
318 |
v1 value:'oops'. 'remains false'. |
|
319 |
v1 value:'hello'. 'changes to true'. |
|
320 |
v2 value |
|
321 |
" |
|
322 |
! |
|
323 |
||
145 | 324 |
with:aBlock |
325 |
"return a new BlockValue computing aBlock" |
|
326 |
||
1815 | 327 |
^ (self new) setBlock:aBlock |
145 | 328 |
|
329 |
"Created: 16.12.1995 / 19:16:33 / cg" |
|
330 |
! |
|
331 |
||
146
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
332 |
with:aBlock argument:anArgument |
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
333 |
"return a new BlockValue computing aBlock" |
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
334 |
|
1815 | 335 |
^ (self new) |
1352 | 336 |
setBlock:aBlock |
337 |
arguments:(Array with:anArgument) |
|
146
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
338 |
|
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
339 |
"Created: 16.12.1995 / 19:20:14 / cg" |
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
340 |
! |
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
341 |
|
1255 | 342 |
with:aBlock argument:anArgument1 argument:anArgument2 |
343 |
"return a new BlockValue computing aBlock" |
|
344 |
||
1815 | 345 |
^ (self new) |
1352 | 346 |
setBlock:aBlock |
347 |
arguments:(Array with:anArgument1 with:anArgument2) |
|
1255 | 348 |
|
349 |
"Created: 16.12.1995 / 19:20:14 / cg" |
|
350 |
! |
|
351 |
||
1352 | 352 |
with:aBlock argument:anArgument1 argument:anArgument2 argument:anArgument3 |
353 |
"return a new BlockValue computing aBlock" |
|
354 |
||
1815 | 355 |
^ (self new) |
1352 | 356 |
setBlock:aBlock |
357 |
arguments:(Array with:anArgument1 with:anArgument2 with:anArgument3) |
|
1815 | 358 |
! |
1352 | 359 |
|
1815 | 360 |
with:aBlock argument:anArgument1 argument:anArgument2 argument:anArgument3 argument:anArgument4 |
361 |
"return a new BlockValue computing aBlock" |
|
362 |
||
363 |
^ (self new) |
|
364 |
setBlock:aBlock |
|
365 |
arguments:(Array with:anArgument1 with:anArgument2 with:anArgument3 with:anArgument4) |
|
1352 | 366 |
! |
367 |
||
145 | 368 |
with:aBlock arguments:aCollectionOfArguments |
369 |
"return a new BlockValue computing aBlock" |
|
370 |
||
1815 | 371 |
^ (self new) |
1352 | 372 |
setBlock:aBlock |
373 |
arguments:aCollectionOfArguments |
|
145 | 374 |
|
375 |
"Created: 16.12.1995 / 19:20:14 / cg" |
|
376 |
! ! |
|
377 |
||
378 |
!BlockValue methodsFor:'accessing'! |
|
379 |
||
3945 | 380 |
setArguments:aCollectionOfArguments |
381 |
"set the receiver's arguments to be passed to it. |
|
382 |
A change in any of the arguments will force reevaluation of the action |
|
383 |
block - possibly generating another change from myself" |
|
384 |
||
385 |
arguments notNil ifTrue:[ |
|
3946 | 386 |
arguments do:[:arg | arg removeDependent:self]. |
3945 | 387 |
]. |
388 |
arguments := aCollectionOfArguments. |
|
389 |
arguments do:[:arg | |
|
390 |
"arg maybe some constant..." |
|
391 |
arg notNil ifTrue:[ |
|
392 |
arg addDependent:self |
|
393 |
]. |
|
394 |
]. |
|
395 |
self recomputeValue. |
|
396 |
||
397 |
"Created: / 30-03-2017 / 14:00:27 / stefan" |
|
3946 | 398 |
"Modified: / 31-03-2017 / 16:28:39 / stefan" |
3945 | 399 |
! |
400 |
||
145 | 401 |
setBlock:aBlock |
3613 | 402 |
"set the receiver's action block" |
382 | 403 |
|
145 | 404 |
block := aBlock. |
405 |
arguments notNil ifTrue:[ |
|
382 | 406 |
self release |
145 | 407 |
]. |
408 |
arguments := nil. |
|
146
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
409 |
cachedValue := NeverComputed. |
145 | 410 |
|
411 |
"Created: 16.12.1995 / 19:16:59 / cg" |
|
382 | 412 |
"Modified: 22.1.1997 / 19:05:54 / cg" |
413 |
! |
|
414 |
||
415 |
setBlock:aBlock argumentArray:anArgumentCollection |
|
3613 | 416 |
"set the receiver's action block, and define an arguments collection |
382 | 417 |
to be passed to it. |
418 |
A change in any element of the collection will force reevaluation of the |
|
419 |
action block (passing the collection as a single argument) |
|
420 |
- possibly generating another change from myself" |
|
421 |
||
422 |
block := aBlock. |
|
423 |
arguments notNil ifTrue:[ |
|
424 |
self release |
|
425 |
]. |
|
426 |
arguments := Array with:anArgumentCollection. |
|
427 |
anArgumentCollection do:[:arg | |
|
2530 | 428 |
arg notNil ifTrue:[ |
429 |
arg addDependent:self |
|
430 |
]. |
|
382 | 431 |
]. |
432 |
cachedValue := NeverComputed. |
|
433 |
||
434 |
"Modified: 22.1.1997 / 19:08:51 / cg" |
|
435 |
"Created: 22.1.1997 / 19:22:13 / cg" |
|
145 | 436 |
! |
437 |
||
438 |
setBlock:aBlock arguments:aCollectionOfArguments |
|
3613 | 439 |
"set the receiver's action block, and define arguments to be passed to it. |
382 | 440 |
A change in any of the arguments will force reevaluation of the action |
441 |
block - possibly generating another change from myself" |
|
442 |
||
145 | 443 |
block := aBlock. |
444 |
arguments notNil ifTrue:[ |
|
382 | 445 |
self release |
145 | 446 |
]. |
447 |
arguments := aCollectionOfArguments. |
|
448 |
arguments do:[:arg | |
|
2530 | 449 |
"arg maybe some constant..." |
450 |
arg notNil ifTrue:[ |
|
451 |
arg addDependent:self |
|
452 |
]. |
|
145 | 453 |
]. |
146
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
454 |
cachedValue := NeverComputed. |
145 | 455 |
|
456 |
"Created: 16.12.1995 / 19:21:41 / cg" |
|
382 | 457 |
"Modified: 22.1.1997 / 19:08:51 / cg" |
145 | 458 |
! |
459 |
||
155 | 460 |
setValue:newValue |
461 |
"physically set my value, without change notifications. |
|
462 |
This is a noop here, since my value is computed." |
|
463 |
||
464 |
^ self |
|
465 |
||
466 |
||
467 |
! |
|
468 |
||
145 | 469 |
value |
382 | 470 |
"retrieve my value - this does not always evaluate the action block, |
471 |
since the returned value is cached internally" |
|
472 |
||
146
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
473 |
cachedValue == NeverComputed ifTrue:[ |
491 | 474 |
cachedValue := self computeValue |
146
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
475 |
]. |
145 | 476 |
^ cachedValue |
477 |
||
478 |
"Created: 16.12.1995 / 19:23:26 / cg" |
|
382 | 479 |
"Modified: 22.1.1997 / 19:06:59 / cg" |
145 | 480 |
! ! |
481 |
||
347 | 482 |
!BlockValue methodsFor:'change & update'! |
145 | 483 |
|
1788 | 484 |
recomputeValue |
4242 | 485 |
"force reevaluation of my actionBlock, |
486 |
and possibly send a change notification to my dependents" |
|
382 | 487 |
|
145 | 488 |
|oldValue| |
489 |
||
490 |
oldValue := cachedValue. |
|
491 | 491 |
cachedValue := self computeValue. |
145 | 492 |
oldValue ~~ cachedValue ifTrue:[ |
382 | 493 |
self changed:#value |
145 | 494 |
]. |
4242 | 495 |
|
496 |
"Modified (comment): / 05-03-2019 / 21:40:21 / Claus Gittinger" |
|
1788 | 497 |
! |
498 |
||
499 |
update:something with:aParameter from:someone |
|
500 |
"the one I depend on has changed - reevaluate my actionBlock, |
|
501 |
and possibly send a change notification to my dependents" |
|
502 |
||
503 |
self recomputeValue |
|
145 | 504 |
|
505 |
"Created: 16.12.1995 / 19:22:54 / cg" |
|
382 | 506 |
"Modified: 22.1.1997 / 19:07:39 / cg" |
145 | 507 |
! ! |
508 |
||
2985 | 509 |
!BlockValue methodsFor:'dependents access'! |
510 |
||
511 |
release |
|
512 |
"release any dependencies upon the arguments" |
|
513 |
||
514 |
arguments notNil ifTrue:[ |
|
515 |
arguments do:[:arg | arg removeDependent:self]. |
|
516 |
]. |
|
3291 | 517 |
super release |
2985 | 518 |
|
519 |
"Modified: 22.1.1997 / 19:08:06 / cg" |
|
520 |
! ! |
|
521 |
||
1788 | 522 |
!BlockValue methodsFor:'misc'! |
523 |
||
524 |
computeValue |
|
3613 | 525 |
"evaluate the receiver's action block" |
1788 | 526 |
|
527 |
|sz arg1 arg2 argValues| |
|
528 |
||
529 |
arguments isNil ifTrue:[ |
|
530 |
^ block value |
|
531 |
]. |
|
532 |
sz := arguments size. |
|
533 |
sz == 0 ifTrue:[ |
|
534 |
^ block value |
|
535 |
]. |
|
536 |
arg1 := (arguments at:1) value. |
|
537 |
sz == 1 ifTrue:[ |
|
538 |
^ block value:arg1 |
|
539 |
]. |
|
540 |
arg2 := (arguments at:2) value. |
|
541 |
sz == 2 ifTrue:[ |
|
542 |
^ block value:arg1 value:arg2 |
|
543 |
]. |
|
544 |
"/ do not evaluate arg[1-2] twice |
|
545 |
argValues := Array new:arguments size. |
|
546 |
argValues at:1 put:arg1. "/ do not evaluate twice |
|
547 |
argValues at:2 put:arg2. "/ do not evaluate twice |
|
548 |
3 to:arguments size do:[:i | argValues at:i put:(arguments at:i) value]. |
|
549 |
||
550 |
^ block valueWithArguments:argValues |
|
551 |
||
552 |
"Created: 16.12.1995 / 19:27:40 / cg" |
|
553 |
"Modified: 22.1.1997 / 19:05:57 / cg" |
|
554 |
! |
|
555 |
||
556 |
dependOn:someObject |
|
557 |
"arrange for the blockValue to be reevaluated, whenever someObject |
|
558 |
changes (i.e. sends a change notification)" |
|
559 |
||
560 |
arguments isNil ifTrue:[ |
|
561 |
arguments := Array with:someObject |
|
562 |
] ifFalse:[ |
|
563 |
arguments := arguments copyWith:someObject |
|
564 |
]. |
|
565 |
someObject addDependent:self |
|
566 |
||
567 |
"Modified: 22.1.1997 / 19:05:26 / cg" |
|
568 |
! |
|
569 |
||
570 |
resetValue |
|
3613 | 571 |
"evaluate the receiver's action block" |
1788 | 572 |
|
573 |
cachedValue := self computeValue |
|
574 |
! ! |
|
575 |
||
145 | 576 |
!BlockValue class methodsFor:'documentation'! |
577 |
||
578 |
version |
|
3613 | 579 |
^ '$Header$' |
2760 | 580 |
! |
581 |
||
582 |
version_CVS |
|
3613 | 583 |
^ '$Header$' |
145 | 584 |
! ! |
1665 | 585 |
|
3291 | 586 |
|
146
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
587 |
BlockValue initialize! |