Skip to content

Commit bbfaf4e

Browse files
committed
introducing a new "Processor interface" that i like much better than any other
attempt. it should work for Step/Task, but also for I/O and for the taskWrap itself. ``` def self.compute_callable(ctx, flow_options, circuit_options, signal, lib_ctx, method_name:, **) ``` the idea is to have an extended signature of the circuit interface, where two positional args signal and lib_ctx are passed around, the lib_ctx is automatically passed as kwargs. this used to be the `wrap_ctx` in the original taskWrap version. you `return ctx, flow_options, signal, lib_ctx`, and signal can be anything.
1 parent 0cd0ead commit bbfaf4e

File tree

2 files changed

+20
-30
lines changed

2 files changed

+20
-30
lines changed

lib/trailblazer/activity/circuit/step.rb

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,55 +8,43 @@ def self.Task(filter_with_circuit_interface)
88
end
99

1010
class Processor #< Pipeline
11-
def self.call(pipeline, ctx, flow_options, circuit_options, value = nil, **kws)
11+
def self.call(pipeline, ctx, flow_options, circuit_options, signal = nil, lib_ctx)
1212
pipeline.to_a.each do |(_id, task)|
1313
puts "@@@@@ #{task.inspect}"
14-
# raise "should we pass around value in kws?"
15-
ctx, flow_options, value = task.(ctx, flow_options, circuit_options, value, **kws)
14+
ctx, flow_options, signal, lib_ctx = task.(ctx, flow_options, circuit_options, signal, lib_ctx, **lib_ctx)
1615
end
1716

18-
return ctx, flow_options, value # FIXME: experimenting here.
17+
return ctx, flow_options, signal, lib_ctx # FIXME: experimenting here.
1918
end
2019
end
2120

2221
class Task___Activity# < Activity::Railway # TODO: naming.
23-
# DISCUSS: make the third argument pass-through?
24-
25-
2622
module Generic # DISCUSS: rename back to {Callable}?
2723
# TODO: this is generic, applies to Task and Step!!!
2824
class InstanceMethod
2925
# This is one "step" for the Task/Step adapter, specific to instance methods,
3026
# and it allows calling the returned callable as if it was a MyHandler.
3127
# RUNTIME, THIS IS EXECUTED BY THE TASK/STEP instance.
32-
def self.compute_callable(ctx, flow_options, circuit_options, value, method_name:, **)
28+
def self.compute_callable(ctx, flow_options, circuit_options, signal, lib_ctx, method_name:, **)
3329
exec_context = circuit_options.fetch(:exec_context)
3430
# That was my first idea, but it doesn't play if devs would use dispatching based on {#method_missing}.
3531
# callable = exec_context.method(method_name) # this is the actual change from Option thinking.
3632

37-
# method_name = ctx.fetch(:method_name)
38-
3933
callable = ->(*args, **kws) { exec_context.send(method_name, *args, **kws) } # this should be generic, so we can use it with Task and Step interfaces (and ext-ci)
4034

4135
# tHE IDEA here is that the only difference to a raw filter is that we extract the "callable" before we do the rest
4236
# (invoking with whatever interface, interpreting the result etc)
43-
# ctx[:callable] = callable
4437

45-
return ctx, flow_options, callable
38+
return ctx, flow_options, callable, lib_ctx
4639
end
4740
end
4841

49-
def self.invoke_callable(ctx, flow_options, circuit_options, callable, callable_keyword_arguments: {}, **kwargs)
50-
# callable = ctx[:callable]
51-
# application_ctx = ctx[:application_ctx]
52-
42+
def self.invoke_callable(ctx, flow_options, circuit_options, callable, lib_ctx, callable_keyword_arguments: {}, **)
5343
# DISCUSS: we currently need {callable_keyword_arguments} only in one spot (if I remember correctly, that's the Rescue handler and :exception)
5444

5545
ctx, flow_options, signal = callable.(ctx, flow_options, circuit_options, **callable_keyword_arguments) # This is how any Task is invoked!
5646

57-
# DISCUSS: do we want another return set? Shouldn't that be the Task's responsibility?
58-
# ctx[:result] = result
59-
return ctx, flow_options, signal
47+
return ctx, flow_options, signal, lib_ctx
6048
end
6149
end # Generic
6250

@@ -80,7 +68,6 @@ def call(ctx, flow_options, circuit_options, **kwargs)
8068
callable = task_instance_method_wrap.(ctx, flow_options, circuit_options, **kwargs) #this step is specific to instance methods
8169

8270
callable.(ctx, flow_options, circuit_options, **kwargs) # This is how any Task is invoked!
83-
# return
8471
end
8572
end
8673

@@ -135,11 +122,11 @@ def self.Step(filter_with_step_interface, **options)
135122

136123

137124
class Step___ < Struct.new(:user_filter, :binary?)
138-
def self.invoke_callable_with_step_interface(ctx, flow_options, circuit_options, callable, **kwargs)
125+
def self.invoke_callable_with_step_interface(ctx, flow_options, circuit_options, callable, lib_ctx, **)
139126

140127
result = callable.(ctx, **ctx.to_h) # This is how any Step should be called!
141128

142-
return ctx, flow_options, result
129+
return ctx, flow_options, result, lib_ctx
143130
end
144131

145132
class Binary < Struct.new(:step)
@@ -156,11 +143,11 @@ def self.compute_signal(ctx, flow_options, result)
156143
end
157144

158145

159-
def self.___compute_signal(ctx, flow_options, circuit_options, result, **)
146+
def self.___compute_signal(ctx, flow_options, circuit_options, result, lib_ctx, **)
160147
# we're a step, {result} is always a "boolean".
161148
signal = Binary.binary_signal_for(result, Activity::Right, Activity::Left)
162149

163-
return ctx, flow_options, signal
150+
return ctx, flow_options, signal, lib_ctx
164151
end
165152

166153
# Translates the return value of the user step into a valid signal.

test/circuit_step_test.rb

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,13 @@ def self.call(ctx, outcome:, **)
111111

112112
# Let's invoke a Task :instance_method.
113113
# ctx, _flow_options, signal = Trailblazer::Activity::Circuit::Task___Activity::InstanceMethod.(
114-
ctx, _flow_options, signal = Trailblazer::Activity::Circuit::Processor.(
114+
ctx, _flow_options, signal, _library_ctx = Trailblazer::Activity::Circuit::Processor.(
115115
Trailblazer::Activity::Circuit::Task___Activity::InstanceMethod,
116116
self.ctx,
117117
{},
118118
{exec_context: self},
119-
"value",
120-
**library_ctx = {
119+
nil, # "signal"
120+
library_ctx = {
121121
method_name: :my_output_with_circuit_interface
122122
}
123123
)
@@ -126,15 +126,17 @@ def self.call(ctx, outcome:, **)
126126
# is it worth that?
127127

128128
assert_equal CU.strip(CU.inspect(ctx)), %({:params=>{:id=>1}, :action=>:update})
129+
assert_equal _flow_options, {}
129130
assert_equal signal, {id: 1, exec_context: self} # the handler returns a Hash as a signal?
131+
assert_equal _library_ctx, library_ctx
130132

131-
ctx, _flow_options, signal = Trailblazer::Activity::Circuit::Processor.(
133+
ctx, _flow_options, signal, _library_ctx = Trailblazer::Activity::Circuit::Processor.(
132134
Trailblazer::Activity::Circuit::Step___::Step___Activity___InstanceMethod,
133135
self.ctx,
134136
{},
135137
{exec_context: self},
136-
"value",
137-
**library_ctx = {
138+
nil, # "signal"
139+
library_ctx = {
138140
method_name: :my_handler_with_step_interface,
139141
}
140142
)
@@ -162,6 +164,7 @@ def self.call(ctx, outcome:, **)
162164
{},
163165
{exec_context: self},
164166
MyBinaryStepHandler,
167+
{} # library_ctx
165168
)
166169

167170
assert_equal signal, Trailblazer::Activity::Left

0 commit comments

Comments
 (0)