state_machines 0.202.0 → 0.305.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +226 -125
- data/lib/state_machines/branch.rb +56 -22
- data/lib/state_machines/callback.rb +28 -8
- data/lib/state_machines/core.rb +1 -0
- data/lib/state_machines/error.rb +50 -2
- data/lib/state_machines/eval_helpers.rb +31 -18
- data/lib/state_machines/event.rb +187 -4
- data/lib/state_machines/event_collection.rb +6 -1
- data/lib/state_machines/extensions.rb +22 -0
- data/lib/state_machines/helper_module.rb +1 -0
- data/lib/state_machines/machine/action_hooks.rb +13 -10
- data/lib/state_machines/machine/callbacks.rb +15 -0
- data/lib/state_machines/machine/class_methods.rb +34 -3
- data/lib/state_machines/machine/configuration.rb +12 -8
- data/lib/state_machines/machine/event_methods.rb +5 -0
- data/lib/state_machines/machine/helper_generators.rb +26 -11
- data/lib/state_machines/machine/state_methods.rb +4 -3
- data/lib/state_machines/machine/utilities.rb +25 -1
- data/lib/state_machines/machine.rb +62 -3
- data/lib/state_machines/machine_collection.rb +7 -6
- data/lib/state_machines/macro_methods.rb +4 -4
- data/lib/state_machines/node_collection.rb +26 -23
- data/lib/state_machines/path.rb +1 -0
- data/lib/state_machines/path_collection.rb +1 -0
- data/lib/state_machines/state.rb +66 -31
- data/lib/state_machines/state_collection.rb +10 -2
- data/lib/state_machines/state_context.rb +7 -1
- data/lib/state_machines/stdio_renderer.rb +8 -6
- data/lib/state_machines/syntax_validator.rb +3 -3
- data/lib/state_machines/test_helper.rb +309 -259
- data/lib/state_machines/transition.rb +136 -230
- data/lib/state_machines/transition_claim.rb +119 -0
- data/lib/state_machines/transition_collection.rb +147 -31
- data/lib/state_machines/version.rb +1 -1
- metadata +8 -15
- data/lib/state_machines/async_mode/async_event_extensions.rb +0 -49
- data/lib/state_machines/async_mode/async_events.rb +0 -286
- data/lib/state_machines/async_mode/async_machine.rb +0 -60
- data/lib/state_machines/async_mode/async_transition_collection.rb +0 -136
- data/lib/state_machines/async_mode/thread_safe_state.rb +0 -47
- data/lib/state_machines/async_mode.rb +0 -64
- data/lib/state_machines/machine/async_extensions.rb +0 -88
- data/lib/state_machines/machine/validation.rb +0 -39
|
@@ -1,11 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module StateMachines
|
|
4
|
-
# Module to extend Fiber instances for pausable state tracking
|
|
5
|
-
module PausableFiber
|
|
6
|
-
attr_accessor :state_machine_fiber_pausable
|
|
7
|
-
end
|
|
8
|
-
|
|
9
4
|
# A transition represents a state change for a specific attribute.
|
|
10
5
|
#
|
|
11
6
|
# Transitions consist of:
|
|
@@ -35,16 +30,25 @@ module StateMachines
|
|
|
35
30
|
# Whether the transition is only existing temporarily for the object
|
|
36
31
|
attr_writer :transient
|
|
37
32
|
|
|
33
|
+
# Why the most recent perform of this transition was refused.
|
|
34
|
+
#
|
|
35
|
+
# @return [:halted, :action_failed, :contended, nil] the refusal reason, or
|
|
36
|
+
# nil if the transition succeeded or was refused only because another
|
|
37
|
+
# transition in the same collection was
|
|
38
|
+
# @see StateMachines::Event#refusal for the meaning of each reason
|
|
39
|
+
attr_accessor :refusal
|
|
40
|
+
|
|
41
|
+
# The event branch selected for this transition, whose before: / after: /
|
|
42
|
+
# around: callbacks it runs; nil for a transition not built by an event
|
|
43
|
+
attr_reader :branch
|
|
44
|
+
|
|
38
45
|
# Creates a new, specific transition
|
|
39
|
-
def initialize(object, machine, event, from_name, to_name, read_state = true) # :nodoc:
|
|
46
|
+
def initialize(object, machine, event, from_name, to_name, read_state = true, branch: nil) # :nodoc:
|
|
40
47
|
@object = object
|
|
41
48
|
@machine = machine
|
|
49
|
+
@branch = branch
|
|
42
50
|
@args = []
|
|
43
51
|
@transient = false
|
|
44
|
-
@paused_fiber = nil
|
|
45
|
-
@resuming = false
|
|
46
|
-
@continuation_block = nil
|
|
47
|
-
@fiber_thread_storage = nil
|
|
48
52
|
|
|
49
53
|
@event = machine.events.fetch(event)
|
|
50
54
|
@from_state = machine.states.fetch(from_name)
|
|
@@ -161,6 +165,9 @@ module StateMachines
|
|
|
161
165
|
# transition.perform(Time.now) # => Passes in additional arguments and runs the +save+ action
|
|
162
166
|
# transition.perform(Time.now, false) # => Passes in additional arguments and only sets the state attribute
|
|
163
167
|
# transition.perform(Time.now, run_action: false) # => Passes in additional arguments and only sets the state attribute
|
|
168
|
+
#
|
|
169
|
+
# @return [StateMachines::Transition, false] this transition when it
|
|
170
|
+
# succeeds, false when it is refused (see {#refusal})
|
|
164
171
|
def perform(*args)
|
|
165
172
|
run_action = case args.last
|
|
166
173
|
in true | false
|
|
@@ -174,7 +181,7 @@ module StateMachines
|
|
|
174
181
|
self.args = args
|
|
175
182
|
|
|
176
183
|
# Run the transition
|
|
177
|
-
|
|
184
|
+
TransitionCollection.new([self], { use_transactions: machine.use_transactions, actions: run_action }).perform ? self : false
|
|
178
185
|
end
|
|
179
186
|
|
|
180
187
|
# Runs a block within a transaction for the object being transitioned.
|
|
@@ -189,35 +196,35 @@ module StateMachines
|
|
|
189
196
|
#
|
|
190
197
|
# Configuration options:
|
|
191
198
|
# * +before+ - Whether to run before callbacks.
|
|
192
|
-
# * +after+ - Whether to run after callbacks. If false
|
|
193
|
-
#
|
|
199
|
+
# * +after+ - Whether to run after callbacks. If false (and +before+ is
|
|
200
|
+
# enabled), the run is a preview (e.g. validating before a save): every
|
|
201
|
+
# before callback runs, the around callbacks are skipped (not entered,
|
|
202
|
+
# their conditions not evaluated), then the block, and no after
|
|
203
|
+
# callbacks (failure callbacks if it halts or the block fails). The
|
|
204
|
+
# next run with +after+ enabled does not repeat those before callbacks;
|
|
205
|
+
# its around callbacks wrap only its action. So after a preview, a
|
|
206
|
+
# before callback declared after an around callback has run outside
|
|
207
|
+
# that around; without a preview, callbacks interleave as declared.
|
|
194
208
|
# Default is true.
|
|
195
209
|
#
|
|
196
210
|
# This will return true if all before callbacks gets executed. After
|
|
197
211
|
# callbacks will not have an effect on the result.
|
|
198
|
-
def run_callbacks(options = {}, &
|
|
212
|
+
def run_callbacks(options = {}, &)
|
|
199
213
|
options = { before: true, after: true }.merge(options)
|
|
214
|
+
return preview(&) if options[:before] && !options[:after]
|
|
200
215
|
|
|
201
|
-
|
|
202
|
-
# this is an idempotent call on an already-paused transition. Just return true.
|
|
203
|
-
return true if @paused_fiber&.alive? && !options[:after]
|
|
204
|
-
|
|
205
|
-
# Always use fibers for compatibility with existing pause/resume functionality
|
|
206
|
-
# The fiber argument can still be used to explicitly control fiber usage
|
|
207
|
-
pausable_options = options.key?(:fiber) ? { fiber: options[:fiber] } : {}
|
|
208
|
-
|
|
209
|
-
# Check if we're resuming from a pause
|
|
210
|
-
if @paused_fiber&.alive? && options[:after]
|
|
211
|
-
# Resume the paused fiber
|
|
212
|
-
# Don't reset @success when resuming - preserve the state from the pause
|
|
213
|
-
# Store the block for later execution
|
|
214
|
-
@continuation_block = block if block_given?
|
|
215
|
-
halted = pausable(pausable_options) { true }
|
|
216
|
-
else
|
|
216
|
+
if options[:before]
|
|
217
217
|
@success = false
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
218
|
+
begin
|
|
219
|
+
halted = !catch(:halt) do
|
|
220
|
+
before(&)
|
|
221
|
+
true
|
|
222
|
+
end
|
|
223
|
+
ensure
|
|
224
|
+
# However it ends, the full run consumed any preview: the
|
|
225
|
+
# transition is now like one never previewed after the same run
|
|
226
|
+
@previewed = nil
|
|
227
|
+
end
|
|
221
228
|
end
|
|
222
229
|
|
|
223
230
|
# After callbacks are only run if:
|
|
@@ -254,6 +261,22 @@ module StateMachines
|
|
|
254
261
|
@persisted = true
|
|
255
262
|
end
|
|
256
263
|
|
|
264
|
+
# Whether this transition's state change is in place: written by {#persist}
|
|
265
|
+
# and not undone since by {#rollback} (or {#reset}).
|
|
266
|
+
#
|
|
267
|
+
# A transition that is refused never stays persisted: a halting before
|
|
268
|
+
# callback stops it ahead of the write, and a failed or raising action
|
|
269
|
+
# rolls it back. Once the action has succeeded nothing rolls it back, so
|
|
270
|
+
# it stays persisted whatever the after phase does (an after callback
|
|
271
|
+
# halting, an around callback halting or raising after its yield). A
|
|
272
|
+
# successful preview (e.g. ActiveRecord's +valid?+) leaves it persisted
|
|
273
|
+
# for the full run that completes it.
|
|
274
|
+
#
|
|
275
|
+
# @return [Boolean]
|
|
276
|
+
def persisted?
|
|
277
|
+
@persisted == true
|
|
278
|
+
end
|
|
279
|
+
|
|
257
280
|
# Rolls back changes made to the object's state via this transition. This
|
|
258
281
|
# will revert the state back to the +from+ value.
|
|
259
282
|
#
|
|
@@ -287,10 +310,8 @@ module StateMachines
|
|
|
287
310
|
# the state has already been persisted
|
|
288
311
|
def reset
|
|
289
312
|
@before_run = @persisted = @after_run = false
|
|
290
|
-
@
|
|
291
|
-
@
|
|
292
|
-
@continuation_block = nil
|
|
293
|
-
@fiber_thread_storage = nil
|
|
313
|
+
@before_callbacks = nil
|
|
314
|
+
@previewed = nil
|
|
294
315
|
end
|
|
295
316
|
|
|
296
317
|
# Determines equality of transitions by testing whether the object, states,
|
|
@@ -314,45 +335,19 @@ module StateMachines
|
|
|
314
335
|
"#<#{self.class} #{%w[attribute event from from_name to to_name].map { |attr| "#{attr}=#{send(attr).inspect}" } * ' '}>"
|
|
315
336
|
end
|
|
316
337
|
|
|
317
|
-
#
|
|
318
|
-
#
|
|
319
|
-
def
|
|
320
|
-
|
|
321
|
-
end
|
|
322
|
-
|
|
323
|
-
# Checks whether this transition has a paused fiber that can be resumed.
|
|
324
|
-
# Returns true if there is a paused fiber, false otherwise.
|
|
325
|
-
#
|
|
326
|
-
# Note: The actual resuming happens automatically when run_callbacks is called
|
|
327
|
-
# again on a transition with a paused fiber.
|
|
328
|
-
def resumable?
|
|
329
|
-
paused?
|
|
330
|
-
end
|
|
331
|
-
|
|
332
|
-
# Manually resumes the execution of a previously paused callback.
|
|
333
|
-
# Returns true if the transition was successfully resumed and completed,
|
|
334
|
-
# false if there was no paused fiber, and raises an exception if the
|
|
335
|
-
# transition was halted.
|
|
336
|
-
def resume!(&block)
|
|
337
|
-
return false unless paused?
|
|
338
|
-
|
|
339
|
-
# Store continuation block if provided
|
|
340
|
-
@continuation_block = block if block_given?
|
|
341
|
-
|
|
342
|
-
# Run the pausable block which will resume the fiber
|
|
343
|
-
halted = pausable { true }
|
|
344
|
-
|
|
345
|
-
# Return whether the transition completed successfully
|
|
346
|
-
!halted
|
|
338
|
+
# Whether a preview run (run_callbacks with after: false) has run part of
|
|
339
|
+
# this transition's before chain, which the next full run continues
|
|
340
|
+
def previewed?
|
|
341
|
+
!@previewed.nil?
|
|
347
342
|
end
|
|
348
343
|
|
|
349
344
|
# Completes a transition whose before phase has already run by executing
|
|
350
|
-
# its after callbacks,
|
|
345
|
+
# its after callbacks, running any around callbacks its preview left first. Used
|
|
351
346
|
# to finish transitions that were stored for deferred completion (e.g.
|
|
352
347
|
# generated mid-action from an event attribute) once their action has
|
|
353
348
|
# succeeded.
|
|
354
349
|
def complete_deferred_after_callbacks
|
|
355
|
-
if
|
|
350
|
+
if previewed?
|
|
356
351
|
run_callbacks(after: true)
|
|
357
352
|
else
|
|
358
353
|
after
|
|
@@ -361,141 +356,49 @@ module StateMachines
|
|
|
361
356
|
|
|
362
357
|
private
|
|
363
358
|
|
|
364
|
-
#
|
|
365
|
-
#
|
|
366
|
-
#
|
|
367
|
-
#
|
|
368
|
-
#
|
|
369
|
-
#
|
|
359
|
+
# A preview run: every before callback not run by an earlier preview,
|
|
360
|
+
# skipping the around callbacks (not entered, nor their conditions
|
|
361
|
+
# evaluated: they wrap the action of the full run), then the block; on a
|
|
362
|
+
# halt or a failed block, the failure callbacks. Running all the befores
|
|
363
|
+
# lets a validation in the block see what the full run's befores set, so
|
|
364
|
+
# it agrees with the full run. Returns false if it halted, including a
|
|
365
|
+
# halt from the block.
|
|
370
366
|
#
|
|
371
|
-
#
|
|
372
|
-
#
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
# Resume the paused fiber
|
|
385
|
-
@resuming = true
|
|
386
|
-
begin
|
|
387
|
-
result = @paused_fiber.resume
|
|
388
|
-
rescue StandardError => e
|
|
389
|
-
# Clean up on exception
|
|
390
|
-
@resuming = false
|
|
391
|
-
@paused_fiber = nil
|
|
392
|
-
raise e
|
|
393
|
-
end
|
|
394
|
-
@resuming = false
|
|
395
|
-
|
|
396
|
-
result_value = unwrap_fiber_result(result)
|
|
397
|
-
|
|
398
|
-
# Check if fiber is still alive after resume
|
|
399
|
-
if @paused_fiber.alive?
|
|
400
|
-
# Still paused, keep the fiber
|
|
401
|
-
true
|
|
402
|
-
else
|
|
403
|
-
# Fiber completed
|
|
404
|
-
@paused_fiber = nil
|
|
405
|
-
result_value == :halted
|
|
406
|
-
end
|
|
407
|
-
else
|
|
408
|
-
# Capture current fiber's Thread.current storage to preserve object identity
|
|
409
|
-
# This is needed for compatibility but has limitations with dynamic assignments
|
|
410
|
-
parent_fiber_locals = capture_thread_locals
|
|
411
|
-
|
|
412
|
-
# Create a new fiber to run the block
|
|
413
|
-
fiber = Fiber.new do
|
|
414
|
-
# Restore parent's Thread.current storage with exact same object references
|
|
415
|
-
parent_fiber_locals.each do |key, value|
|
|
416
|
-
Thread.current[key] = value
|
|
417
|
-
end
|
|
418
|
-
|
|
419
|
-
# Mark that we're inside a pausable fiber
|
|
420
|
-
Fiber.current.extend(StateMachines::PausableFiber)
|
|
421
|
-
Fiber.current.state_machine_fiber_pausable = true
|
|
422
|
-
begin
|
|
423
|
-
halted = !catch(:halt) do
|
|
424
|
-
yield
|
|
425
|
-
true
|
|
426
|
-
end
|
|
427
|
-
|
|
428
|
-
# Export the final thread storage state along with the result
|
|
429
|
-
[halted ? :halted : :completed, capture_thread_locals]
|
|
430
|
-
rescue StandardError => e
|
|
431
|
-
# Store the exception for re-raising
|
|
432
|
-
[:error, e]
|
|
433
|
-
ensure
|
|
434
|
-
# Clean up the flag
|
|
435
|
-
Fiber.current.state_machine_fiber_pausable = false
|
|
436
|
-
end
|
|
437
|
-
end
|
|
438
|
-
|
|
439
|
-
# Run the fiber
|
|
440
|
-
result = fiber.resume
|
|
441
|
-
|
|
442
|
-
result_value = unwrap_fiber_result(result)
|
|
443
|
-
|
|
444
|
-
# Save if paused
|
|
445
|
-
if fiber.alive?
|
|
446
|
-
@paused_fiber = fiber
|
|
447
|
-
# Return true to indicate paused (treated as halted for flow control)
|
|
448
|
-
true
|
|
449
|
-
else
|
|
450
|
-
# Fiber completed, return whether it was halted
|
|
451
|
-
result_value == :halted
|
|
367
|
+
# Preview bookkeeping (which befores ran) exists only from a successful
|
|
368
|
+
# preview to the end of its full run: it is built aside and kept only if
|
|
369
|
+
# this preview succeeds, so a preview that halts, fails or raises leaves
|
|
370
|
+
# the transition as it found it, minus any earlier preview.
|
|
371
|
+
def preview # rubocop:disable Naming/PredicateMethod -- returns whether the befores ran, as run_callbacks does
|
|
372
|
+
previewed = @previewed ? @previewed.dup : {}.compare_by_identity
|
|
373
|
+
@previewed = nil
|
|
374
|
+
halted = !catch(:halt) do
|
|
375
|
+
before_callbacks.each do |callback|
|
|
376
|
+
next if callback.type == :around || previewed.key?(callback)
|
|
377
|
+
|
|
378
|
+
previewed[callback] = true
|
|
379
|
+
callback.call(object, context, self)
|
|
452
380
|
end
|
|
453
|
-
end
|
|
454
|
-
end
|
|
455
381
|
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
382
|
+
# Inside the catch, as in a full run: the block (e.g. the collection
|
|
383
|
+
# running the next machine's transition) may halt, which fails this
|
|
384
|
+
# transition too
|
|
385
|
+
action = block_given? ? { success: true }.merge(yield) : { success: true }
|
|
386
|
+
@result = action[:result]
|
|
387
|
+
@success = action[:success]
|
|
388
|
+
true
|
|
461
389
|
end
|
|
462
|
-
|
|
390
|
+
@success = false if halted
|
|
463
391
|
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
392
|
+
if @success
|
|
393
|
+
@previewed = previewed
|
|
394
|
+
else
|
|
395
|
+
# A preview that halts or fails is a failed transition: its failure
|
|
396
|
+
# callbacks run, as in a full run, without marking the after phase
|
|
397
|
+
# as done (the full run that may follow runs its own)
|
|
398
|
+
after
|
|
399
|
+
@after_run = false
|
|
472
400
|
end
|
|
473
|
-
|
|
474
|
-
@fiber_thread_storage = result[1] if result.length == 2 && result[1].is_a?(Hash)
|
|
475
|
-
result[0]
|
|
476
|
-
end
|
|
477
|
-
|
|
478
|
-
# Pauses the current callback execution. This should only occur within
|
|
479
|
-
# around callbacks when the remainder of the callback will be executed at
|
|
480
|
-
# a later point in time.
|
|
481
|
-
def pause
|
|
482
|
-
# Don't pause if we're in the middle of resuming
|
|
483
|
-
return if @resuming
|
|
484
|
-
|
|
485
|
-
# Only yield if we're actually inside a fiber created by pausable
|
|
486
|
-
# We use a module extension to track this
|
|
487
|
-
current_fiber = Fiber.current
|
|
488
|
-
return unless current_fiber.respond_to?(:state_machine_fiber_pausable) && current_fiber.state_machine_fiber_pausable
|
|
489
|
-
|
|
490
|
-
Fiber.yield
|
|
491
|
-
|
|
492
|
-
# When we resume from the pause, execute the continuation block if present
|
|
493
|
-
return unless @continuation_block && !@result
|
|
494
|
-
|
|
495
|
-
action = { success: true }.merge(@continuation_block.call)
|
|
496
|
-
@result = action[:result]
|
|
497
|
-
@success = action[:success]
|
|
498
|
-
@continuation_block = nil
|
|
401
|
+
!halted
|
|
499
402
|
end
|
|
500
403
|
|
|
501
404
|
# Runs the machine's +before+ callbacks for this transition. Only
|
|
@@ -504,38 +407,28 @@ module StateMachines
|
|
|
504
407
|
#
|
|
505
408
|
# Once the callbacks are run, they cannot be run again until this transition
|
|
506
409
|
# is reset.
|
|
507
|
-
def before(
|
|
410
|
+
def before(index = 0, &)
|
|
508
411
|
return if @before_run
|
|
509
412
|
|
|
510
|
-
callback =
|
|
413
|
+
callback = before_callbacks[index]
|
|
511
414
|
|
|
512
415
|
if callback
|
|
513
|
-
#
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
#
|
|
521
|
-
callback
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
pause if @success && !complete
|
|
525
|
-
|
|
526
|
-
# If the block failed (success is false), we should halt
|
|
527
|
-
# the around callback from continuing
|
|
528
|
-
throw :halt unless @success
|
|
529
|
-
end
|
|
530
|
-
else
|
|
531
|
-
# Normal before callback
|
|
532
|
-
callback.call(object, context, self)
|
|
533
|
-
# Continue with next callback
|
|
534
|
-
before(complete, index + 1, &block)
|
|
416
|
+
# Callback#call evaluates the callback's conditions (once) and answers
|
|
417
|
+
# whether it applied
|
|
418
|
+
if callback.type == :around
|
|
419
|
+
# The rest of the chain, and the action, run inside the around
|
|
420
|
+
applied = callback.call(object, context, self) do
|
|
421
|
+
before(index + 1, &)
|
|
422
|
+
|
|
423
|
+
# If the block failed (success is false), we should halt
|
|
424
|
+
# the around callback from continuing
|
|
425
|
+
throw :halt unless @success
|
|
535
426
|
end
|
|
427
|
+
before(index + 1, &) unless applied
|
|
536
428
|
else
|
|
537
|
-
#
|
|
538
|
-
|
|
429
|
+
# A preview already ran this before callback
|
|
430
|
+
callback.call(object, context, self) unless @previewed&.key?(callback)
|
|
431
|
+
before(index + 1, &)
|
|
539
432
|
end
|
|
540
433
|
else
|
|
541
434
|
# No more callbacks, execute the action block if at the end
|
|
@@ -568,13 +461,11 @@ module StateMachines
|
|
|
568
461
|
def after
|
|
569
462
|
return if @after_run
|
|
570
463
|
|
|
571
|
-
# Restore the fiber's thread storage to ensure consistency
|
|
572
|
-
# This preserves Thread.current state from before/around callbacks to after callbacks
|
|
573
|
-
@fiber_thread_storage.each { |key, value| Thread.current[key] = value } if @fiber_thread_storage
|
|
574
|
-
|
|
575
464
|
catch(:halt) do
|
|
576
|
-
|
|
577
|
-
|
|
465
|
+
# A branch's afters run before the machine's, closest to the change;
|
|
466
|
+
# they run on success only (a branch has no after_failure)
|
|
467
|
+
callbacks = @success ? branch_callbacks(:after) + machine.callbacks[:after] : machine.callbacks[:failure]
|
|
468
|
+
callbacks.each { |callback| callback.call(object, context, self) }
|
|
578
469
|
end
|
|
579
470
|
|
|
580
471
|
@after_run = true
|
|
@@ -591,5 +482,20 @@ module StateMachines
|
|
|
591
482
|
def context
|
|
592
483
|
@context ||= { on: event, from: from_name, to: to_name }
|
|
593
484
|
end
|
|
485
|
+
|
|
486
|
+
# The before chain: the machine's before / around callbacks, then the
|
|
487
|
+
# selected branch's, so branch callbacks wrap closest to the state change.
|
|
488
|
+
# It is taken once per run: a callback registered while the chain runs
|
|
489
|
+
# (e.g. from inside a before_transition) applies from the next transition,
|
|
490
|
+
# not to the one in progress, whose chain cannot change under it.
|
|
491
|
+
def before_callbacks
|
|
492
|
+
@before_callbacks ||= machine.callbacks[:before] + branch_callbacks(:before, :around)
|
|
493
|
+
end
|
|
494
|
+
|
|
495
|
+
def branch_callbacks(*types)
|
|
496
|
+
return [] unless branch
|
|
497
|
+
|
|
498
|
+
branch.callbacks.select { |callback| types.include?(callback.type) }
|
|
499
|
+
end
|
|
594
500
|
end
|
|
595
501
|
end
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module StateMachines
|
|
4
|
+
# A hold on one of an object's machine attributes, granted for the duration
|
|
5
|
+
# of a single TransitionCollection#perform
|
|
6
|
+
class TransitionClaim
|
|
7
|
+
# The authority a perform holds over the attributes it claimed. The fibers
|
|
8
|
+
# a perform spawns inherit its authority, which is retired once that
|
|
9
|
+
# perform ends.
|
|
10
|
+
class Owner
|
|
11
|
+
def initialize
|
|
12
|
+
@active = true
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def active?
|
|
16
|
+
@active
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def retire
|
|
20
|
+
@active = false
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
CLAIMS_VARIABLE = :@_state_machine_transition_claims
|
|
25
|
+
OWNER_KEY = :state_machines_transition_owner
|
|
26
|
+
LOCK = Thread::Mutex.new
|
|
27
|
+
|
|
28
|
+
class << self
|
|
29
|
+
# Claims an attribute for every given transition whose machine claims
|
|
30
|
+
# transitions (Machine#claims_transitions?), ordered by attribute so that
|
|
31
|
+
# collections over the same attributes claim them in the same order. A
|
|
32
|
+
# collection without such a transition is granted an empty set.
|
|
33
|
+
#
|
|
34
|
+
# Returns the granted claims, or nil when an attribute is held by another
|
|
35
|
+
# active owner or has left the transition's source state. Every exit
|
|
36
|
+
# that does not grant the whole set - a refusal, an exception, a throw -
|
|
37
|
+
# releases the claims it took.
|
|
38
|
+
#
|
|
39
|
+
# @param transitions [Array<StateMachines::Transition>] the transitions
|
|
40
|
+
# to claim attributes for
|
|
41
|
+
# @yieldparam transition [StateMachines::Transition] the transition whose
|
|
42
|
+
# attribute could not be claimed, when the claim is refused
|
|
43
|
+
# @return [Array<StateMachines::TransitionClaim>, nil]
|
|
44
|
+
def claim(transitions)
|
|
45
|
+
claimable = transitions.select { |transition| transition.machine.claims_transitions? }
|
|
46
|
+
granted = []
|
|
47
|
+
admitted = false
|
|
48
|
+
|
|
49
|
+
begin
|
|
50
|
+
admitted = claimable.sort_by { |transition| transition.attribute.to_s }.all? do |transition|
|
|
51
|
+
next true if hold(transition, granted) && at_source?(transition)
|
|
52
|
+
|
|
53
|
+
yield(transition) if block_given?
|
|
54
|
+
false
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
granted if admitted
|
|
58
|
+
ensure
|
|
59
|
+
release(granted) unless admitted
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# Releases the claims granted by .claim
|
|
64
|
+
def release(claims)
|
|
65
|
+
claims.reverse_each(&:release)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
private
|
|
69
|
+
|
|
70
|
+
# Takes the transition's attribute, appending the claim to +granted+ as it
|
|
71
|
+
# is taken. Returns nil when another active owner holds the attribute.
|
|
72
|
+
def hold(transition, granted)
|
|
73
|
+
previous_owner = Fiber[OWNER_KEY]
|
|
74
|
+
owner = previous_owner&.active? ? previous_owner : Owner.new
|
|
75
|
+
object = transition.object
|
|
76
|
+
attribute = transition.attribute
|
|
77
|
+
|
|
78
|
+
LOCK.synchronize do
|
|
79
|
+
claims = claims_for(object)
|
|
80
|
+
holder = claims[attribute]
|
|
81
|
+
next unless holder.nil? || !holder.active? || holder.equal?(owner)
|
|
82
|
+
|
|
83
|
+
claim = new(claims, attribute, owner, previous_owner, !holder.equal?(owner))
|
|
84
|
+
granted << claim
|
|
85
|
+
claims[attribute] = owner
|
|
86
|
+
Fiber[OWNER_KEY] = owner
|
|
87
|
+
claim
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def at_source?(transition)
|
|
92
|
+
transition.machine.states.match!(transition.object).name == transition.from_name
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def claims_for(object)
|
|
96
|
+
object.instance_variable_get(CLAIMS_VARIABLE) || object.instance_variable_set(CLAIMS_VARIABLE, {})
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def initialize(claims, attribute, owner, previous_owner, held) # :nodoc:
|
|
101
|
+
@claims = claims
|
|
102
|
+
@attribute = attribute
|
|
103
|
+
@owner = owner
|
|
104
|
+
@previous_owner = previous_owner
|
|
105
|
+
@held = held
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
# Gives up this claim, retiring its owner's authority when the claim
|
|
109
|
+
# created it
|
|
110
|
+
def release
|
|
111
|
+
Fiber[OWNER_KEY] = @previous_owner
|
|
112
|
+
|
|
113
|
+
LOCK.synchronize do
|
|
114
|
+
@owner.retire unless @owner.equal?(@previous_owner)
|
|
115
|
+
@claims.delete(@attribute) if @held
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
end
|