Skip to content

Commit b55f4cd

Browse files
committed
coming from the user handler in Wrap(), we no longer map random signals like true
or nil to actual signals. this has never been official, anyways, and is not deprecated.
1 parent f23aff4 commit b55f4cd

File tree

3 files changed

+31
-61
lines changed

3 files changed

+31
-61
lines changed

CHANGES.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,24 @@ Inject(:composer_index) => ->(ctx, index:, **) { index },
99
* Remove deprecation for `Nested(Memo::Operation::Create)`. Without a dynamic decider, use `Subprocess()`.
1010
* Rename `:auto_wire` option in `Nested()` to `:static`.
1111
12+
## Wrap
13+
14+
* Silently deprecate the "feature" to return arbitrary "signals" from the user handler.
15+
Before 2.2, you could do things like
16+
17+
```ruby
18+
class HandleUnsafeProcess
19+
def self.call(ctx, flow_options, _circuit_options, &block)
20+
yield
21+
rescue
22+
true # Or any other object that will be treated as a signal.
23+
end
24+
end
25+
```
26+
27+
As this has never been officially documented, you won't be warned about the deprecation. =========> consistency
28+
29+
1230
# 2.1.16
1331
1432
* Fix a bug in `patch` where `Subprocess()` was missing in `Macro::Strategy`.

lib/trailblazer/macro/wrap.rb

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,13 @@ def self.Wrap(user_wrap, id: Macro.id_for(user_wrap, macro: :Wrap), &block)
88

99
outputs = Hash[outputs.collect { |output| [output.semantic, output] }] # FIXME: redundant to Subprocess().
1010

11-
# Since in the user block, you can return Railway.pass! etc, we need to map
12-
# those to the actual wrapped block_activity's end.
11+
# Since in the user block, you can return Railway.pass! etc, we need to translate
12+
# those to the actual block_activity's termini.
1313
signal_to_output = {
1414
Activity::Right => outputs[:success].signal,
1515
Activity::Left => outputs[:failure].signal,
1616
Activity::FastTrack::PassFast => outputs[:pass_fast].signal,
1717
Activity::FastTrack::FailFast => outputs[:fail_fast].signal,
18-
true => outputs[:success].signal,
19-
false => outputs[:failure].signal,
20-
nil => outputs[:failure].signal,
2118
}
2219

2320
state = Declarative::State(
@@ -45,31 +42,20 @@ def self.Wrap(user_wrap, id: Macro.id_for(user_wrap, macro: :Wrap), &block)
4542
class Wrap < Macro::Strategy
4643
def self.call(ctx, flow_options, circuit_options)
4744
# since yield is called without arguments, we need to pull default params from here. Oh ... tricky.
48-
4945
block_called_from_user_yield = ->() { # DISCUSS: because we allow users to call {yield}, we don't receive any args here.
5046
Activity::Circuit::Runner.(block_activity, ctx, flow_options, circuit_options)
5147
}
5248

53-
# call the user's Wrap {} block in the operation.
54-
# This will invoke block_called_from_user_yield above if the user block yields.
5549
user_handler = @state.get(:user_wrap)
5650

5751
# Invoke the user's handler.
58-
returned = user_handler.(ctx, flow_options, circuit_options, &block_called_from_user_yield)
59-
60-
# {returned} can be
61-
# 1. {circuit interface return} from the begin block, because the wrapped OP passed
62-
# 2. {task interface return} because the user block returns "customized" signals, true of fale
63-
64-
if returned.is_a?(Array) # 1. {circuit interface return}, new style.
65-
ctx, flow_options, signal = returned
66-
else # 2. {task interface return}, only a signal (or true/false)
67-
# TODO: deprecate this?
68-
signal = returned
69-
end
52+
ctx, flow_options, signal = user_handler.(ctx, flow_options, circuit_options, &block_called_from_user_yield)
7053

71-
# If there's no mapping, use the original {signal} .
72-
# This usually means signal is a terminus or a custom signal.
54+
# The user handler is allowed to return signals like Right and Left. We need to translate
55+
# those to termini signals from the block_activity, cause those are the only ones
56+
# we route.
57+
# DISCUSS: i would love to skip this, but that'd mean the user has to return a
58+
# "native" signal from their block.
7359
signal = @state.get(:signal_to_output).fetch(signal, signal)
7460

7561
return ctx, flow_options, signal

test/docs/wrap_test.rb

Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ def self.call(ctx, flow_options, _circuit_options, &block) # FIXME: deprecate th
1212
end
1313
#:my_transaction end
1414

15+
# TODO: add 2.1 version of the above
16+
1517
class WrapSimpleHandlerTest < Minitest::Spec
1618
MyTransaction = ::MyTransaction
1719

@@ -315,7 +317,7 @@ class MyTransaction
315317
def self.call(ctx, flow_options, _circuit_options, &block)
316318
yield # calls the wrapped steps
317319
rescue
318-
MyFailSignal
320+
return ctx, flow_options, MyFailSignal
319321
end
320322
end
321323
#:custom-handler end
@@ -389,7 +391,7 @@ class HandleUnsafeProcess
389391
def self.call(ctx, flow_options, _circuit_options, &block)
390392
yield # calls the wrapped steps
391393
rescue
392-
true
394+
return ctx, flow_options, Trailblazer::Activity::Right
393395
end
394396
end
395397

@@ -425,7 +427,7 @@ class HandleUnsafeProcess
425427
def self.call(ctx, flow_options, _circuit_options, &block)
426428
yield # calls the wrapped steps
427429
rescue
428-
false
430+
return ctx, flow_options, Trailblazer::Activity::Left
429431
end
430432
end
431433

@@ -448,42 +450,6 @@ def self.call(ctx, flow_options, _circuit_options, &block)
448450
end
449451
end
450452

451-
=begin
452-
When success: return the block's returns
453-
When raise: return {nil} and go "failed"
454-
You can return nil in wrap.
455-
=end
456-
class WrapGoesIntoNilFromRescueTest < Minitest::Spec
457-
Memo = Module.new
458-
459-
class Memo::Create < Trailblazer::Operation
460-
class HandleUnsafeProcess
461-
def self.call(ctx, flow_options, _circuit_options, &block)
462-
yield # calls the wrapped steps
463-
rescue
464-
nil
465-
end
466-
end
467-
468-
step :model
469-
step Wrap( HandleUnsafeProcess ) {
470-
step :update
471-
step :rehash
472-
}
473-
step :notify
474-
left :log_error
475-
476-
#~methods
477-
include T.def_steps(:model, :update, :notify, :log_error)
478-
include Rehash
479-
#~methods end
480-
end
481-
482-
it "translates nil returned form a wrap to a signal with a `failure` semantic" do
483-
assert_call Memo::Create, seq: "[:model, :update, :rehash, :log_error]", rehash_raise: RuntimeError, terminus: :failure
484-
end
485-
end
486-
487453
=begin
488454
When success: return the block's returns
489455
When raise: return {Railway.fail!}

0 commit comments

Comments
 (0)