peplum 0.2.2 → 0.2.4

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: 3cf68ae5865f247eefd32f97a3171b4306fdf421a8e73df30429d4ee91c32232
4
- data.tar.gz: d26d484ab649e0258ab2855ac81e14db8dbbb9a06509cf00a5d4b957b188ce6e
3
+ metadata.gz: 7cbd03ad31260b2a970bf3b4d3955a9b5a3aa9f8b8bbbffe5b49c1b07fdca539
4
+ data.tar.gz: cb85849f3733ba341a32aaf2628a5216df125f0fe686b035765c8584e4ad8d51
5
5
  SHA512:
6
- metadata.gz: 18d05dda4ecd556ba03556dae0754a5c4e085cb90c9f77536abc5156147529b941a9377d3bdf9cfe2b124f0dae4a6cb403d1315190a0a9b0817ab16130c7b383
7
- data.tar.gz: 58438b5a98f06d5353cbb4ffa5b9f65b15c7d51401a40498af6f82c9c09a4483e04b6bc78ddeda8427bab930c7402e720015c757d490494876877767c019d5a6
6
+ metadata.gz: 24cc90e163171f3a1ce7622ffef358015a8e3cea47293be4f9be814589196057d1e44213c7c9a4d7b77e3fed5476cddc9f10d1edac3b2155b9cc339e94527dad
7
+ data.tar.gz: efd88627c08d5b5048dd0940366e54f48c20c170dbe22749719574b09626e326909cda7abf730d85e145cff5173403631e4061f32ba23227498c2fdfecd56ea7
@@ -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 = {}
@@ -4,8 +4,13 @@ module Services
4
4
 
5
5
  class SharedHash
6
6
 
7
- def initialize
7
+ def initialize(*)
8
+ super
9
+
8
10
  @hash = {}
11
+
12
+ @on_set_cb = {}
13
+ @on_delete_cb = {}
9
14
  end
10
15
 
11
16
  def get( k )
@@ -16,10 +21,11 @@ class SharedHash
16
21
  return if @hash[k] == v
17
22
 
18
23
  @hash[k] = v
24
+ call_on_set( k, v )
19
25
 
20
26
  if broadcast
21
27
  each_peer do |peer|
22
- peer.shared_hash.set( k, v, false )
28
+ peer.send( name ).set( k, v, false )
23
29
  end
24
30
  end
25
31
 
@@ -30,22 +36,51 @@ class SharedHash
30
36
  return if !@hash.include? k
31
37
 
32
38
  @hash.delete( k )
39
+ call_on_delete( k )
33
40
 
34
41
  if broadcast
35
42
  each_peer do |_, peer|
36
- peer.shared_hash.delete( k, false )
43
+ peer.send( name ).delete( k, false )
37
44
  end
38
45
  end
39
46
 
40
47
  nil
41
48
  end
42
49
 
50
+ def on_set( k, &block )
51
+ (@on_set_cb[k] ||= []) << block
52
+ end
53
+
54
+ def on_delete( k, &block )
55
+ (@on_delete_cb ||= []) << block
56
+ end
57
+
43
58
  def to_h
44
59
  @hash.dup
45
60
  end
46
61
 
47
62
  private
48
63
 
64
+ def call_on_set( k, v )
65
+ return if !@on_set_cb[k]
66
+
67
+ @on_set_cb[k].each do |cb|
68
+ cb.call v
69
+ end
70
+
71
+ nil
72
+ end
73
+
74
+ def call_on_delete( k )
75
+ return if !@on_delete_cb[k]
76
+
77
+ @on_delete_cb[k].each do |cb|
78
+ cb.call
79
+ end
80
+
81
+ nil
82
+ end
83
+
49
84
  def each_peer( &block )
50
85
  Cuboid::Application.application.peers.each( &block )
51
86
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Peplum
4
- VERSION = "0.2.2"
4
+ VERSION = "0.2.4"
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.2
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tasos Laskos