peplum 0.2.1 → 0.2.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/peplum/application/peers.rb +1 -0
- data/lib/peplum/application/services/shared_hash.rb +38 -20
- data/lib/peplum/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 39020843c5d8edc0d848f1b08bc88fdc73aed389241a757f3b8f55cd1b5a151e
|
4
|
+
data.tar.gz: c3a31f27cdd3e95f840cfd0466d6facdf596f8dc306a1063091f5862b19f2ae6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 95a6bdcd3f8ce0b86dba303250304e79c3455bbd7f63dc05e226ff5605c7769ce396b7b70f4a0a53144cc0cc0767a70653a0123deac6d756b9b6ab8823489ceb
|
7
|
+
data.tar.gz: '0806c44ba530ff2cd8e5ec6cc0e53acdd4519a00c5c5b0e5d6514be173f522c2e01fa137ae97fe411ad3d9698d0db2901f3775b98f96e8fb7c9843d31f5ac2f6'
|
@@ -5,61 +5,79 @@ module Services
|
|
5
5
|
class SharedHash
|
6
6
|
|
7
7
|
def initialize
|
8
|
-
@hash
|
9
|
-
|
8
|
+
@hash = {}
|
9
|
+
|
10
|
+
@on_set_cb = {}
|
11
|
+
@on_delete_cb = {}
|
10
12
|
end
|
11
13
|
|
12
14
|
def get( k )
|
13
15
|
@hash[k]
|
14
16
|
end
|
15
17
|
|
16
|
-
def set( k, v, broadcast = true
|
17
|
-
if @hash[k] == v
|
18
|
-
block.call if block_given?
|
19
|
-
return
|
20
|
-
end
|
18
|
+
def set( k, v, broadcast = true )
|
19
|
+
return if @hash[k] == v
|
21
20
|
|
22
21
|
@hash[k] = v
|
22
|
+
call_on_set( k, v )
|
23
23
|
|
24
24
|
if broadcast
|
25
25
|
each_peer do |peer|
|
26
|
-
|
27
|
-
peer.shared_hash.set( k, v, false ) { @sync_counter -= 1 }
|
26
|
+
peer.shared_hash.set( k, v, false )
|
28
27
|
end
|
29
28
|
end
|
30
29
|
|
31
|
-
block.call if block_given?
|
32
30
|
nil
|
33
31
|
end
|
34
32
|
|
35
33
|
def delete( k, broadcast = true )
|
36
|
-
if !@hash.include? k
|
37
|
-
block.call if block_given?
|
38
|
-
return
|
39
|
-
end
|
34
|
+
return if !@hash.include? k
|
40
35
|
|
41
36
|
@hash.delete( k )
|
37
|
+
call_on_delete( k )
|
42
38
|
|
43
39
|
if broadcast
|
44
40
|
each_peer do |_, peer|
|
45
|
-
|
46
|
-
peer.shared_hash.delete( k, false ) { @sync_counter -= 1 }
|
41
|
+
peer.shared_hash.delete( k, false )
|
47
42
|
end
|
48
43
|
end
|
49
44
|
|
50
|
-
block.call if block_given?
|
51
45
|
nil
|
52
46
|
end
|
53
47
|
|
48
|
+
def on_set( k, &block )
|
49
|
+
(@on_set_cb[k] ||= []) << block
|
50
|
+
end
|
51
|
+
|
52
|
+
def on_delete( k, &block )
|
53
|
+
(@on_delete_cb ||= []) << block
|
54
|
+
end
|
55
|
+
|
54
56
|
def to_h
|
55
57
|
@hash.dup
|
56
58
|
end
|
57
59
|
|
58
|
-
|
59
|
-
|
60
|
+
private
|
61
|
+
|
62
|
+
def call_on_set( k, v )
|
63
|
+
return if !@on_set_cb[k]
|
64
|
+
|
65
|
+
@on_set_cb[k].each do |cb|
|
66
|
+
cb.call v
|
67
|
+
end
|
68
|
+
|
69
|
+
nil
|
60
70
|
end
|
61
71
|
|
62
|
-
|
72
|
+
def call_on_delete( k )
|
73
|
+
return if !@on_delete_cb[k]
|
74
|
+
|
75
|
+
@on_delete_cb[k].each do |cb|
|
76
|
+
cb.call
|
77
|
+
end
|
78
|
+
|
79
|
+
nil
|
80
|
+
end
|
63
81
|
|
64
82
|
def each_peer( &block )
|
65
83
|
Cuboid::Application.application.peers.each( &block )
|
data/lib/peplum/version.rb
CHANGED