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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 684acaeb0eb2b2cccf49d9959482b56da2a73cf6da28d5c6f34833c015a889be
4
- data.tar.gz: 5fb7a928493b17e94abb647f5779dd79671df3443d6bc1cc5063e1ee54f196e1
3
+ metadata.gz: 39020843c5d8edc0d848f1b08bc88fdc73aed389241a757f3b8f55cd1b5a151e
4
+ data.tar.gz: c3a31f27cdd3e95f840cfd0466d6facdf596f8dc306a1063091f5862b19f2ae6
5
5
  SHA512:
6
- metadata.gz: 12ae57302cc03176c3e760f0d3ffece96216dfdf70bb006c758a63c661f10a57aa71abca25a8560028cfacff3f87c95611eed882d50a63e6867fe8bb8471531d
7
- data.tar.gz: 2066cd3a32f3b4e2ba294c553b406ad32ea54c7bba34a0f38f9cc7c66addf5358e57127aec01eb2c271ff4e51f0f1b2a4259410286a09a77ec414dcd3726a6d8
6
+ metadata.gz: 95a6bdcd3f8ce0b86dba303250304e79c3455bbd7f63dc05e226ff5605c7769ce396b7b70f4a0a53144cc0cc0767a70653a0123deac6d756b9b6ab8823489ceb
7
+ data.tar.gz: '0806c44ba530ff2cd8e5ec6cc0e53acdd4519a00c5c5b0e5d6514be173f522c2e01fa137ae97fe411ad3d9698d0db2901f3775b98f96e8fb7c9843d31f5ac2f6'
@@ -2,6 +2,7 @@ module Peplum
2
2
  class Application
3
3
 
4
4
  class Peers
5
+ include Enumerable
5
6
 
6
7
  def initialize
7
8
  @peers = {}
@@ -5,61 +5,79 @@ module Services
5
5
  class SharedHash
6
6
 
7
7
  def initialize
8
- @hash = {}
9
- @sync_counter = 0
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, &block )
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
- @sync_counter += 1
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
- @sync_counter += 1
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
- def sync
59
- sleep 0.1 while @sync_counter != 0
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
- private
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 )
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Peplum
4
- VERSION = "0.2.1"
4
+ VERSION = "0.2.3"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: peplum
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tasos Laskos