peplum 0.2.2 → 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: 3cf68ae5865f247eefd32f97a3171b4306fdf421a8e73df30429d4ee91c32232
4
- data.tar.gz: d26d484ab649e0258ab2855ac81e14db8dbbb9a06509cf00a5d4b957b188ce6e
3
+ metadata.gz: 39020843c5d8edc0d848f1b08bc88fdc73aed389241a757f3b8f55cd1b5a151e
4
+ data.tar.gz: c3a31f27cdd3e95f840cfd0466d6facdf596f8dc306a1063091f5862b19f2ae6
5
5
  SHA512:
6
- metadata.gz: 18d05dda4ecd556ba03556dae0754a5c4e085cb90c9f77536abc5156147529b941a9377d3bdf9cfe2b124f0dae4a6cb403d1315190a0a9b0817ab16130c7b383
7
- data.tar.gz: 58438b5a98f06d5353cbb4ffa5b9f65b15c7d51401a40498af6f82c9c09a4483e04b6bc78ddeda8427bab930c7402e720015c757d490494876877767c019d5a6
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 = {}
@@ -6,6 +6,9 @@ class SharedHash
6
6
 
7
7
  def initialize
8
8
  @hash = {}
9
+
10
+ @on_set_cb = {}
11
+ @on_delete_cb = {}
9
12
  end
10
13
 
11
14
  def get( k )
@@ -16,6 +19,7 @@ class SharedHash
16
19
  return if @hash[k] == v
17
20
 
18
21
  @hash[k] = v
22
+ call_on_set( k, v )
19
23
 
20
24
  if broadcast
21
25
  each_peer do |peer|
@@ -30,6 +34,7 @@ class SharedHash
30
34
  return if !@hash.include? k
31
35
 
32
36
  @hash.delete( k )
37
+ call_on_delete( k )
33
38
 
34
39
  if broadcast
35
40
  each_peer do |_, peer|
@@ -40,12 +45,40 @@ class SharedHash
40
45
  nil
41
46
  end
42
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
+
43
56
  def to_h
44
57
  @hash.dup
45
58
  end
46
59
 
47
60
  private
48
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
70
+ end
71
+
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
81
+
49
82
  def each_peer( &block )
50
83
  Cuboid::Application.application.peers.each( &block )
51
84
  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.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.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tasos Laskos