@@ -7,14 +7,14 @@ import { DurableDeferred } from "@effect/workflow"
77import * as Activity from "@effect/workflow/Activity"
88import * as DurableClock from "@effect/workflow/DurableClock"
99import * as Workflow from "@effect/workflow/Workflow"
10- import { WorkflowEngine , WorkflowInstance } from "@effect/workflow/WorkflowEngine"
10+ import { makeUnsafe , WorkflowEngine , WorkflowInstance } from "@effect/workflow/WorkflowEngine"
1111import * as Arr from "effect/Array"
1212import * as Cause from "effect/Cause"
1313import * as Context from "effect/Context"
1414import * as DateTime from "effect/DateTime"
1515import * as Duration from "effect/Duration"
1616import * as Effect from "effect/Effect"
17- import type * as Exit from "effect/Exit"
17+ import * as Exit from "effect/Exit"
1818import * as Fiber from "effect/Fiber"
1919import * as FiberId from "effect/FiberId"
2020import * as Layer from "effect/Layer"
@@ -35,6 +35,7 @@ import { EntityId } from "./EntityId.js"
3535import { EntityType } from "./EntityType.js"
3636import { MessageStorage } from "./MessageStorage.js"
3737import type { WithExitEncoded } from "./Reply.js"
38+ import * as Reply from "./Reply.js"
3839import * as Sharding from "./Sharding.js"
3940import * as Snowflake from "./Snowflake.js"
4041
@@ -252,11 +253,9 @@ export const make = Effect.gen(function*() {
252253 yield * sharding . reset ( requestId . value )
253254 } , Effect . scoped )
254255
255- return WorkflowEngine . of ( {
256- register ( workflow , execute ) {
257- // eslint-disable-next-line @typescript-eslint/no-this-alias
258- const engine = this
259- return Effect . suspend ( ( ) =>
256+ const engine = makeUnsafe ( {
257+ register : ( workflow , execute ) =>
258+ Effect . suspend ( ( ) =>
260259 sharding . registerEntity (
261260 ensureEntity ( workflow ) ,
262261 Effect . gen ( function * ( ) {
@@ -277,8 +276,8 @@ export const make = Effect.gen(function*() {
277276 return parent ? ensureSuccess ( sendResumeParent ( parent ) ) : Effect . void
278277 }
279278 return engine . deferredResult ( InterruptSignal ) . pipe (
280- Effect . flatMap ( ( maybeResult ) => {
281- if ( Option . isNone ( maybeResult ) ) {
279+ Effect . flatMap ( ( maybeExit ) => {
280+ if ( maybeExit === undefined ) {
282281 return Effect . void
283282 }
284283 instance . suspended = false
@@ -354,10 +353,9 @@ export const make = Effect.gen(function*() {
354353 }
355354 } )
356355 ) as Effect . Effect < void , never , Scope . Scope >
357- )
358- } ,
356+ ) ,
359357
360- execute : ( { discard, executionId, parent, payload, workflow } ) => {
358+ execute : ( workflow , { discard, executionId, parent, payload } ) => {
361359 ensureEntity ( workflow )
362360 return RcMap . get ( clients , workflow . name ) . pipe (
363361 Effect . flatMap ( ( make ) =>
@@ -376,7 +374,7 @@ export const make = Effect.gen(function*() {
376374 )
377375 } ,
378376
379- poll : Effect . fnUntraced ( function * ( { executionId , workflow } ) {
377+ poll : Effect . fnUntraced ( function * ( workflow , executionId ) {
380378 const entity = ensureEntity ( workflow )
381379 const exitSchema = Rpc . exitSchema ( entity . protocol . requests . get ( "run" ) ! )
382380 const oreply = yield * requestReply ( {
@@ -395,7 +393,7 @@ export const make = Effect.gen(function*() {
395393 } , Effect . orDie ) ,
396394
397395 interrupt : Effect . fnUntraced (
398- function * ( this : WorkflowEngine [ "Type" ] , workflow , executionId ) {
396+ function * ( workflow , executionId ) {
399397 ensureEntity ( workflow )
400398 const oreply = yield * requestReply ( {
401399 workflow,
@@ -411,11 +409,11 @@ export const make = Effect.gen(function*() {
411409 return
412410 }
413411
414- yield * this . deferredDone ( {
412+ yield * engine . deferredDone ( InterruptSignal , {
415413 workflowName : workflow . name ,
416414 executionId,
417415 deferredName : InterruptSignal . name ,
418- exit : { _tag : "Success" , value : void 0 }
416+ exit : Exit . void
419417 } )
420418 } ,
421419 Effect . retry ( {
@@ -429,7 +427,7 @@ export const make = Effect.gen(function*() {
429427 resume : ( workflow , executionId ) => ensureSuccess ( resume ( workflow , executionId ) ) ,
430428
431429 activityExecute : Effect . fnUntraced (
432- function * ( { activity, attempt } ) {
430+ function * ( activity , attempt ) {
433431 const runtime = yield * Effect . runtime < WorkflowInstance > ( )
434432 const context = runtime . context
435433 const instance = Context . get ( context , WorkflowInstance )
@@ -466,7 +464,6 @@ export const make = Effect.gen(function*() {
466464
467465 deferredResult : ( deferred ) =>
468466 WorkflowInstance . pipe (
469- Effect . tap ( ( instance ) => Effect . annotateCurrentSpan ( "executionId" , instance . executionId ) ) ,
470467 Effect . flatMap ( ( instance ) =>
471468 requestReply ( {
472469 workflow : instance . workflow ,
@@ -476,11 +473,16 @@ export const make = Effect.gen(function*() {
476473 id : deferred . name
477474 } )
478475 ) ,
479- Effect . map ( Option . map ( ( reply ) =>
480- reply . exit . _tag === "Success"
481- ? reply . exit . value as any as Schema . ExitEncoded < unknown , unknown , unknown >
482- : reply . exit
483- ) ) ,
476+ Effect . map ( ( oreply ) => {
477+ if ( Option . isNone ( oreply ) ) {
478+ return undefined
479+ }
480+ const reply = oreply . value
481+ const decoded = decodeDeferredWithExit ( reply as any )
482+ return decoded . exit . _tag === "Success"
483+ ? decoded . exit . value
484+ : decoded . exit
485+ } ) ,
484486 Effect . retry ( {
485487 while : ( e ) => e . _tag === "PersistenceError" ,
486488 times : 3 ,
@@ -502,20 +504,22 @@ export const make = Effect.gen(function*() {
502504 Effect . scoped
503505 ) ,
504506
505- scheduleClock ( options ) {
507+ scheduleClock ( workflow , options ) {
506508 const client = clockClient ( options . executionId )
507509 return DateTime . now . pipe (
508510 Effect . flatMap ( ( now ) =>
509511 client . run ( {
510512 name : options . clock . name ,
511- workflowName : options . workflow . name ,
513+ workflowName : workflow . name ,
512514 wakeUp : DateTime . addDuration ( now , options . clock . duration )
513515 } , { discard : true } )
514516 ) ,
515517 Effect . orDie
516518 )
517519 }
518520 } )
521+
522+ return engine
519523} )
520524
521525const retryPolicy = Schedule . exponential ( 200 , 1.5 ) . pipe (
@@ -569,11 +573,11 @@ const makeWorkflowEntity = (workflow: Workflow.Any) =>
569573 ActivityRpc
570574 ] ) . annotateContext ( workflow . annotations )
571575
572- const ExitUnknown = Schema . encodedSchema ( Schema . Exit ( {
576+ const ExitUnknown = Schema . Exit ( {
573577 success : Schema . Unknown ,
574578 failure : Schema . Unknown ,
575579 defect : Schema . Defect
576- } ) )
580+ } )
577581
578582const DeferredRpc = Rpc . make ( "deferred" , {
579583 payload : {
@@ -586,6 +590,8 @@ const DeferredRpc = Rpc.make("deferred", {
586590 . annotate ( ClusterSchema . Persisted , true )
587591 . annotate ( ClusterSchema . Uninterruptible , true )
588592
593+ const decodeDeferredWithExit = Schema . decodeSync ( Reply . WithExit . schema ( DeferredRpc ) )
594+
589595const ResumeRpc = Rpc . make ( "resume" , {
590596 payload : { } ,
591597 primaryKey : ( ) => ""
@@ -628,11 +634,11 @@ const ClockEntityLayer = ClockEntity.toLayer(Effect.gen(function*() {
628634 return {
629635 run ( request ) {
630636 const deferred = DurableClock . make ( { name : request . payload . name , duration : Duration . zero } ) . deferred
631- return ensureSuccess ( engine . deferredDone ( {
637+ return ensureSuccess ( engine . deferredDone ( deferred , {
632638 workflowName : request . payload . workflowName ,
633639 executionId,
634640 deferredName : deferred . name ,
635- exit : { _tag : "Success" , value : void 0 }
641+ exit : Exit . void
636642 } ) )
637643 }
638644 }
0 commit comments