@@ -105,7 +105,10 @@ class Info
105105 # @return [String, Symbol, nil] Name of the activity, or nil if the activity is dynamic.
106106 attr_reader :name
107107
108- # @return [Proc] Proc for the activity.
108+ # @return [Object, Proc, nil] The pre-created instance or the proc to create/return it.
109+ attr_reader :instance
110+
111+ # @return [Proc] Proc for the activity. Should use {Context#instance} to access the instance.
109112 attr_reader :proc
110113
111114 # @return [Symbol] Name of the executor. Default is `:default`.
@@ -134,18 +137,20 @@ def self.from_activity(activity)
134137 details = activity . _activity_definition_details
135138 new (
136139 name : details [ :activity_name ] ,
140+ instance : proc { activity . new } ,
137141 executor : details [ :activity_executor ] ,
138142 cancel_raise : details [ :activity_cancel_raise ] ,
139143 raw_args : details [ :activity_raw_args ]
140- ) { |*args | activity . new . execute ( *args ) } # Instantiate and call
144+ ) { |*args | Context . current . instance &. execute ( *args ) }
141145 when Definition
142146 details = activity . class . _activity_definition_details
143147 new (
144148 name : details [ :activity_name ] ,
149+ instance : activity ,
145150 executor : details [ :activity_executor ] ,
146151 cancel_raise : details [ :activity_cancel_raise ] ,
147152 raw_args : details [ :activity_raw_args ]
148- ) { |*args | activity . execute ( *args ) } # Just and call
153+ ) { |*args | Context . current . instance &. execute ( *args ) }
149154 when Info
150155 activity
151156 else
@@ -156,12 +161,21 @@ def self.from_activity(activity)
156161 # Manually create activity definition info. Most users will use an instance/class of {Definition}.
157162 #
158163 # @param name [String, Symbol, nil] Name of the activity or nil for dynamic activity.
164+ # @param instance [Object, Proc, nil] The pre-created instance or the proc to create/return it.
159165 # @param executor [Symbol] Name of the executor.
160166 # @param cancel_raise [Boolean] Whether to raise in thread/fiber on cancellation.
161167 # @param raw_args [Boolean] Whether to use {Converters::RawValue}s as arguments.
162168 # @yield Use this block as the activity.
163- def initialize ( name :, executor : :default , cancel_raise : true , raw_args : false , &block )
169+ def initialize (
170+ name :,
171+ instance : nil ,
172+ executor : :default ,
173+ cancel_raise : true ,
174+ raw_args : false ,
175+ &block
176+ )
164177 @name = name
178+ @instance = instance
165179 raise ArgumentError , 'Must give block' unless block_given?
166180
167181 @proc = block
0 commit comments