diff --git a/CHANGELOG.md b/CHANGELOG.md index 28a60a4..398b098 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ by Vadim Kononov(@vkononov) * Fix message queue shutdown to raise valid error * Fix any_event and any_state methods in the define method and definition class +* Fix hooks_map initialization to be fully thread-safe + by Maciej Mensfeld(@mensfeld) ## [v0.14.0] - 2020-09-12 diff --git a/lib/finite_machine/hooks.rb b/lib/finite_machine/hooks.rb index 5007671..00502aa 100644 --- a/lib/finite_machine/hooks.rb +++ b/lib/finite_machine/hooks.rb @@ -1,5 +1,6 @@ # frozen_string_literal: true +require "concurrent/array" require "concurrent/map" require_relative "hook_event" @@ -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