3535use Bottledcode \DurablePhp \Events \WithActivity ;
3636use Bottledcode \DurablePhp \Events \WithDelay ;
3737use Bottledcode \DurablePhp \Events \WithEntity ;
38+ use Bottledcode \DurablePhp \Events \WithFrom ;
3839use Bottledcode \DurablePhp \Events \WithLock ;
3940use Bottledcode \DurablePhp \Events \WithOrchestration ;
4041use Bottledcode \DurablePhp \Exceptions \Unwind ;
@@ -77,9 +78,11 @@ public function callActivity(string $name, ?string $returnType = null, ?RetryOpt
7778
7879 return $ this ->createFuture (
7980 fn () => $ this ->taskController ->fire (
80- AwaitResult::forEvent (
81- StateId::fromInstance ($ this ->id ),
82- WithActivity::forEvent ($ identity , ScheduleTask::forName ($ name , $ args )),
81+ $ this ->addFrom (
82+ AwaitResult::forEvent (
83+ StateId::fromInstance ($ this ->id ),
84+ WithActivity::forEvent ($ identity , ScheduleTask::forName ($ name , $ args )),
85+ ),
8386 ),
8487 ),
8588 function (Event $ event , string $ eventIdentity ) use ($ identity ): array {
@@ -136,6 +139,14 @@ private function createFuture(
136139 return $ future ;
137140 }
138141
142+ private function addFrom (Event $ event ): Event
143+ {
144+ static $ from = null ;
145+ $ from ??= StateId::fromInstance ($ this ->id );
146+
147+ return WithFrom::forEvent ($ from , $ event );
148+ }
149+
139150 public function callActivityInline (Closure $ activity ): DurableFuture
140151 {
141152 $ identity = $ this ->newGuid ();
@@ -144,22 +155,26 @@ public function callActivityInline(Closure $activity): DurableFuture
144155 try {
145156 $ result = $ activity ();
146157 $ this ->taskController ->fire (
147- WithOrchestration::forInstance (
148- StateId::fromInstance ($ this ->id ),
149- TaskCompleted::forId ($ identity ->toString (), $ result ),
158+ $ this ->addFrom (
159+ WithOrchestration::forInstance (
160+ StateId::fromInstance ($ this ->id ),
161+ TaskCompleted::forId ($ identity ->toString (), $ result ),
162+ ),
150163 ),
151164 );
152165
153166 return [$ identity ];
154167 } catch (Throwable $ exception ) {
155168 $ this ->taskController ->fire (
156- WithOrchestration::forInstance (
157- StateId::fromInstance ($ this ->id ),
158- TaskFailed::forTask (
159- $ identity ->toString (),
160- $ exception ->getMessage (),
161- $ exception ->getTraceAsString (),
162- $ exception ::class,
169+ $ this ->addFrom (
170+ WithOrchestration::forInstance (
171+ StateId::fromInstance ($ this ->id ),
172+ TaskFailed::forTask (
173+ $ identity ->toString (),
174+ $ exception ->getMessage (),
175+ $ exception ->getTraceAsString (),
176+ $ exception ::class,
177+ ),
163178 ),
164179 ),
165180 );
@@ -197,9 +212,11 @@ public function continueAsNew(array $args = []): never
197212
198213 $ this ->history ->restartAsNew ($ args );
199214 $ this ->taskController ->fire (
200- WithOrchestration::forInstance (
201- StateId::fromInstance ($ this ->id ),
202- StartOrchestration::forInstance ($ this ->id ),
215+ $ this ->addFrom (
216+ WithOrchestration::forInstance (
217+ StateId::fromInstance ($ this ->id ),
218+ StartOrchestration::forInstance ($ this ->id ),
219+ ),
203220 ),
204221 );
205222 throw new Unwind ();
@@ -216,9 +233,11 @@ public function createTimer(DateInterval|DateTimeImmutable $fireAt): DurableFutu
216233
217234 return $ this ->createFuture (
218235 fn () => $ this ->taskController ->fire (
219- WithOrchestration::forInstance (
220- StateId::fromInstance ($ this ->id ),
221- WithDelay::forEvent ($ fireAt , RaiseEvent::forTimer ($ identity )),
236+ $ this ->addFrom (
237+ WithOrchestration::forInstance (
238+ StateId::fromInstance ($ this ->id ),
239+ WithDelay::forEvent ($ fireAt , RaiseEvent::forTimer ($ identity )),
240+ ),
222241 ),
223242 ),
224243 function (Event $ event ) use ($ identity ): array {
@@ -385,7 +404,7 @@ public function lockEntity(EntityId ...$entityId): EntityLock
385404 );
386405 $ identity = $ this ->newGuid ()->toString ();
387406 $ future = $ this ->createFuture (
388- fn () => $ this ->taskController ->fire (WithLock::onEntity ($ owner , $ event , ...$ entityId )),
407+ fn () => $ this ->taskController ->fire ($ this -> addFrom ( WithLock::onEntity ($ owner , $ event , ...$ entityId) )),
389408 fn (Event $ event , string $ eventIdentity ) => [$ event , $ identity === $ eventIdentity ],
390409 $ identity ,
391410 );
@@ -396,9 +415,11 @@ public function lockEntity(EntityId ...$entityId): EntityLock
396415 return new EntityLock (function () use ($ owner ): void {
397416 foreach ($ this ->history ->locks as $ lock ) {
398417 $ this ->taskController ->fire (
399- WithLock::onEntity (
400- $ owner ,
401- WithEntity::forInstance ($ lock , RaiseEvent::forUnlock ($ owner ->id , null , null )),
418+ $ this ->addFrom (
419+ WithLock::onEntity (
420+ $ owner ,
421+ WithEntity::forInstance ($ lock , RaiseEvent::forUnlock ($ owner ->id , null , null )),
422+ ),
402423 ),
403424 );
404425 }
@@ -624,7 +645,7 @@ public function callEntity(EntityId $entityId, string $operation, array $args =
624645 $ identity = $ this ->newGuid ()->toString ();
625646
626647 return $ this ->createFuture (
627- fn () => $ this ->taskController ->fire ($ event ),
648+ fn () => $ this ->taskController ->fire ($ this -> addFrom ( $ event) ),
628649 fn (Event $ event , string $ eventIdentity ) => [$ event , $ identity === $ eventIdentity ],
629650 $ identity ,
630651 );
@@ -645,7 +666,7 @@ public function signalEntity(EntityId $entityId, string $operation, array $args
645666 $ event = WithLock::onEntity ($ id , $ event );
646667 }
647668
648- $ this ->taskController ->fire ($ event );
669+ $ this ->taskController ->fire ($ this -> addFrom ( $ event) );
649670 }
650671
651672 public function getCurrentUserId (): string
0 commit comments