|
1 | 1 | require 'concurrent/atomic'
|
2 | 2 | require 'concurrent/delay'
|
3 | 3 |
|
4 |
| - |
5 | 4 | 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 | + # |
7 | 8 | # @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)> |
16 | 17 | class LazyRegister
|
| 18 | + |
17 | 19 | def initialize
|
18 | 20 | @data = Atomic.new Hash.new
|
19 | 21 | end
|
20 | 22 |
|
| 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 | + # |
21 | 27 | # @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 | + # |
23 | 30 | # @raise Exception when the initialization block fails
|
24 | 31 | def [](key)
|
25 | 32 | delay = @data.get[key]
|
26 |
| - delay.value! if delay |
| 33 | + delay ? delay.value! : nil |
27 | 34 | end
|
28 | 35 |
|
| 36 | + # Returns true if the given key is present. |
| 37 | + # |
29 | 38 | # @param [Object] key
|
30 | 39 | # @return [true, false] if the key is registered
|
31 | 40 | def registered?(key)
|
32 |
| - @data.get.key? key |
| 41 | + @data.get.key?(key) |
33 | 42 | end
|
34 | 43 |
|
35 | 44 | alias_method :key?, :registered?
|
| 45 | + alias_method :has_key?, :registered? |
36 | 46 |
|
| 47 | + # Element assignment. Associates the value given by value with the |
| 48 | + # key given by key. |
| 49 | + # |
37 | 50 | # @param [Object] key
|
38 | 51 | # @yield the object to store under the key
|
39 |
| - # @return self |
| 52 | + # |
| 53 | + # @return [LazyRegister] self |
40 | 54 | 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) } |
43 | 57 | self
|
44 | 58 | end
|
45 | 59 |
|
46 | 60 | alias_method :add, :register
|
| 61 | + alias_method :store, :register |
47 | 62 |
|
48 |
| - # Un-registers the object under key, realized or not |
49 |
| - # @return self |
| 63 | + # Un-registers the object under key, realized or not. |
| 64 | + # |
50 | 65 | # @param [Object] key
|
| 66 | + # |
| 67 | + # @return [LazyRegister] self |
51 | 68 | def unregister(key)
|
52 | 69 | @data.update { |h| h.dup.tap { |j| j.delete(key) } }
|
53 | 70 | self
|
54 | 71 | end
|
55 | 72 |
|
56 | 73 | alias_method :remove, :unregister
|
| 74 | + alias_method :delete, :unregister |
57 | 75 | end
|
58 | 76 | end
|
0 commit comments