public function create()
{
$state = $this->getState();
if ($state === Runtime::STATE_CREATED) {
return Promise::doResolve('Runtime has been already created.');
} else {
if ($state !== Runtime::STATE_DESTROYED) {
return Promise::doReject(new RejectionException("It is not possible to create runtime from state [{$state}]."));
}
}
$promise = new Promise();
$emitter = $this->getEventEmitter();
$emitter->emit('beforeCreate');
$this->getLoop()->onTick(function () use($promise, $emitter) {
$emitter->emit('create');
$emitter->emit('afterCreate');
$promise->resolve($this->start()->then(function () {
return 'Runtime has been created.';
}));
});
$this->setState(Runtime::STATE_CREATED);
$this->setLoopState(self::LOOP_STATE_STARTED);
$this->startLoop();
return $promise;
}