var runner =
{
  stopFlag: false,
  postMessage: function (message)
  {
    self.postMessage(message);
  },
  stop: function ()
  {
    this.stopFlag = true;
  },
  error: function (error)
  {
    this.stopFlag = true;
  },
  setup: function (run)
  {
    this.run = run;
    var that = this;
    self.onmessage = function message(event)
    {
      that.execute(JSON.parse(event.data));
    };
  },
  execute: function (state)
  {
    var that = this;
    setTimeout(function runIterator()
    {
      that.state = that.run.apply(that, [that.state]);
      if (that.stopFlag)
      {
        that.postMessage(that.state);
      }
      else
      {
        that.execute();
      }
    }, 16);
  }
};
(function ()
{
  runner.setup(function (state)
  {
    var newstate = state;
    // w tym miejscu jest modyfikowana zmienna newstate
    return newstate;
  }, {
    time: 0
  });
}());