Skip to content

Commit 350dffe

Browse files
committed
Updated docs for LazyRegister.
1 parent d07ff0b commit 350dffe

File tree

1 file changed

+36
-18
lines changed

1 file changed

+36
-18
lines changed

lib/concurrent/lazy_register.rb

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,76 @@
11
require 'concurrent/atomic'
22
require 'concurrent/delay'
33

4-
54
module Concurrent
6-
# Allows to store lazy evaluated values under keys. Uses `Delay`s.
5+
6+
# Hash-like collection that store lazys evaluated values.
7+
#
78
# @example
8-
# register = Concurrent::LazyRegister.new
9-
# #=> #<Concurrent::LazyRegister:0x007fd7ecd5e230 @data=#<Concurrent::Atomic:0x007fd7ecd5e1e0>>
10-
# register[:key]
11-
# #=> nil
12-
# register.add(:key) { Concurrent::Actor.spawn!(Actor::AdHoc, :ping) { -> message { message } } }
13-
# #=> #<Concurrent::LazyRegister:0x007fd7ecd5e230 @data=#<Concurrent::Atomic:0x007fd7ecd5e1e0>>
14-
# register[:key]
15-
# #=> #<Concurrent::Actor::Reference /ping (Concurrent::Actor::AdHoc)>
9+
# register = Concurrent::LazyRegister.new
10+
# #=> #<Concurrent::LazyRegister:0x007fd7ecd5e230 @data=#<Concurrent::Atomic:0x007fd7ecd5e1e0>>
11+
# register[:key]
12+
# #=> nil
13+
# register.add(:key) { Concurrent::Actor.spawn!(Actor::AdHoc, :ping) { -> message { message } } }
14+
# #=> #<Concurrent::LazyRegister:0x007fd7ecd5e230 @data=#<Concurrent::Atomic:0x007fd7ecd5e1e0>>
15+
# register[:key]
16+
# #=> #<Concurrent::Actor::Reference /ping (Concurrent::Actor::AdHoc)>
1617
class LazyRegister
18+
1719
def initialize
1820
@data = Atomic.new Hash.new
1921
end
2022

23+
# Element reference. Retrieves the value object corresponding to the
24+
# key object. Returns nil if the key is not found. Raises an exception
25+
# if the stored item raised an exception when the block was evaluated.
26+
#
2127
# @param [Object] key
22-
# @return value stored under the key
28+
# @return [Object] value stored for the key or nil if the key is not found
29+
#
2330
# @raise Exception when the initialization block fails
2431
def [](key)
2532
delay = @data.get[key]
26-
delay.value! if delay
33+
delay ? delay.value! : nil
2734
end
2835

36+
# Returns true if the given key is present.
37+
#
2938
# @param [Object] key
3039
# @return [true, false] if the key is registered
3140
def registered?(key)
32-
@data.get.key? key
41+
@data.get.key?(key)
3342
end
3443

3544
alias_method :key?, :registered?
45+
alias_method :has_key?, :registered?
3646

47+
# Element assignment. Associates the value given by value with the
48+
# key given by key.
49+
#
3750
# @param [Object] key
3851
# @yield the object to store under the key
39-
# @return self
52+
#
53+
# @return [LazyRegister] self
4054
def register(key, &block)
41-
delay = Delay.new(&block)
42-
@data.update { |h| h.merge key => delay }
55+
delay = Delay.new(executor: :immediate, &block)
56+
@data.update { |h| h.merge(key => delay) }
4357
self
4458
end
4559

4660
alias_method :add, :register
61+
alias_method :store, :register
4762

48-
# Un-registers the object under key, realized or not
49-
# @return self
63+
# Un-registers the object under key, realized or not.
64+
#
5065
# @param [Object] key
66+
#
67+
# @return [LazyRegister] self
5168
def unregister(key)
5269
@data.update { |h| h.dup.tap { |j| j.delete(key) } }
5370
self
5471
end
5572

5673
alias_method :remove, :unregister
74+
alias_method :delete, :unregister
5775
end
5876
end

0 commit comments

Comments
 (0)