2

Say I have a method which implements a do:[] every:40s block. There is in the block a value data that should be returned at at every delay. How can I retun this to the method in pharo as follow:

Class>>updateMethod

"This is a method"
| scheduler data |
scheduler := TaskScheduler new.
scheduler start.
"refresh every 40 seconds"
scheduler
   do: [a get: 'https://MyServer/json'.
        Transcript show: 'Refreshing......'; cr.
        data := NeoJSONReader fromString: a contents; cr.
   every: 60 seconds
eMBee
  • 793
  • 6
  • 16
ludo
  • 543
  • 3
  • 14

1 Answers1

3

If I understand your question, the issue here would be that you cannot use an expression such as ^data to return the received data because the return operator ^ would exit the block.

So, to achieve the desired behavior you need to send the received data in a message. Something on the lines of:

| scheduler data |
scheduler := TaskScheduler new.
scheduler start.
scheduler
  do: [a get: 'https://MyServer/json'.
    Transcript show: 'Refreshing......'; cr.
    data := NeoJSONReader fromString: a contents; cr.
    self processData: data].
  every: 40 seconds.

In this way, at every evaluation of the block, your code will have the chance to receive new data and process it properly.

Addendum: The selector processData: is nothing but a hint or placeholder for the actual message that will do something with the data just read. In other words, you should create such a method and put there whatever processing is required in your application for the data. Something like

processData: data
  self
    validateData: data;
    doSomethingWith: data;
    "...blah..."
Leandro Caniglia
  • 14,495
  • 4
  • 29
  • 51