Skip to content

Commit

Permalink
Make the state hash fully thread safe
Browse files Browse the repository at this point in the history
Fixes issue described here: ruby-concurrency/concurrent-ruby#970
  • Loading branch information
mensfeld authored and piotrmurach committed Oct 8, 2023
1 parent 8b1e421 commit 9e6344c
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/finite_machine/hooks.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require "concurrent/array"
require "concurrent/map"

require_relative "hook_event"
Expand All @@ -12,13 +13,17 @@ class Hooks
# Initialize a hooks_map of hooks
#
# @example
# Hoosk.new(machine)
# Hooks.new
#
# @api public
def initialize
@hooks_map = Concurrent::Map.new do |events_hash, hook_event|
events_hash[hook_event] = Concurrent::Map.new do |state_hash, name|
state_hash[name] = []
events_hash.compute_if_absent(hook_event) do
Concurrent::Map.new do |state_hash, name|
state_hash.compute_if_absent(name) do
Concurrent::Array.new
end
end
end
end
end
Expand Down

0 comments on commit 9e6344c

Please sign in to comment.