Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: ability to respond with a GIF image from an event

...

The name D is bound to an array of arrays.

Producing an image from an event

The GIF image in this case has been produced from a function, which the sample ruleset shares. What if we wanted to increment a counter and then return an image of the updated counter value? This involves a mutation of the pico state, and so can only be done in the postlude of a rule triggered by an event.

Experimentally, the engine has been modified to intercept a directive named _gif and, if it is one of the directives produced during event evaluation, instead of returning the array of directives in a JSON object, the engine will produce a proper GIF image response.

Note
titleexperimental feature in version 0.45.6

This feature is recent, so you will need to do a git pull after updating to version 0.45.5, or wait until the feature has been included in version 0.45.6

Here is a rule which will do what is desribed above.

Code Block
linenumberstrue
  rule increment_and_display_counter {
    select when counters hit
    pre {
      new_count = ent:counter.defaultsTo(0) + 1;
      image = number(new_count);
    }
    send_directive("_gif",{"content": image})
    fired {
      ent:counter := new_count;
    }
  }

In line 4, we precompute the new counter value, binding it to the name new_count. No worries about a race condition or critical section, because each event arriving at a pico is evaluating in a single thread.

Line 5 calls the function number to get the array of bytes encoding the new counter value as a GIF image.

The special directive is emitted in line 7, and the new counter value stored in the pico in line 9.