live 0.6.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7df6409f470eb90e1f1f68cd70dabcf23cf56c2cbcbcc5707222bcac7d721338
4
- data.tar.gz: faac890736a9dc375e683405d29c534a55b7628e37abbcd64cfae0922ebdeda8
3
+ metadata.gz: eb8e5793466102d92b796d1e86a24c1ff6406eb888b772c65a22c1857c49ad5e
4
+ data.tar.gz: b19804c4d65fa9798615bb681c5062602e2a0406b8a663ac1e8c4ef8a7f2b3c4
5
5
  SHA512:
6
- metadata.gz: d1f0603af15f67f6db3677126f45f1c766bccecf99d93aaae0fc9018c0da5deaaa5f0156fe187ea9d90cac5bf813540de62e084c840bbe28eedfdc18a69fe4af
7
- data.tar.gz: 5db6880399468684e09034225faa3a63929563177d28b32ae67bbe9022158d15b98fc2ba0a967c6bd88177cb1b9518b85a45caeb3ef07309f446fe2b2b639862
6
+ metadata.gz: 5a9c0e7beec4ba0f3102067b1df4da1329fcd35a90e2eb0de38db4ffa565dea7e56d52c6f4c922efd31aac0ee18d8427c40491f552ff3b5fafdec8d887d38898
7
+ data.tar.gz: 75f1b08b01c5b1f280ff93290082bc1da873ff1e259e0a76f0b7a996516fcde81cbe1ceb0824e6f817318d100ec86af05714fbcdc5550526e8572ac6eed71e59
checksums.yaml.gz.sig CHANGED
Binary file
data/lib/live/element.rb CHANGED
@@ -4,13 +4,18 @@
4
4
  # Copyright, 2021-2024, by Samuel Williams.
5
5
 
6
6
  require 'json'
7
+ require 'securerandom'
7
8
 
8
9
  module Live
9
10
  # Represents a single dynamic content area on the page.
10
11
  class Element
12
+ def self.unique_id
13
+ SecureRandom.uuid
14
+ end
15
+
11
16
  # @parameter id [String] The unique identifier within the page.
12
17
  # @parameter data [Hash] The data associated with the element, typically stored as `data-` attributes.
13
- def initialize(id, **data)
18
+ def initialize(id = Element.unique_id, **data)
14
19
  @id = id
15
20
  @data = data
16
21
  @data[:class] ||= self.class.name
@@ -21,13 +26,24 @@ module Live
21
26
  # The unique id within the bound page.
22
27
  attr :id
23
28
 
29
+ # The data associated with the element.
30
+ attr :data
31
+
24
32
  # Generate a JavaScript string which forwards the specified event to the server.
25
33
  # @parameter details [Hash] The details associated with the forwarded event.
26
- def forward(details = nil)
34
+ def forward_event(details = nil)
35
+ if details
36
+ "live.forwardEvent(#{JSON.dump(@id)}, event, #{JSON.dump(details)})"
37
+ else
38
+ "live.forwardEvent(#{JSON.dump(@id)}, event)"
39
+ end
40
+ end
41
+
42
+ def forward_form_event(details = nil)
27
43
  if details
28
- "live.forward(#{JSON.dump(@id)}, event, #{JSON.dump(details)})"
44
+ "live.forwardFormEvent(#{JSON.dump(@id)}, event, #{JSON.dump(details)})"
29
45
  else
30
- "live.forward(#{JSON.dump(@id)}, event)"
46
+ "live.forwardFormEvent(#{JSON.dump(@id)}, event)"
31
47
  end
32
48
  end
33
49
 
@@ -49,9 +65,9 @@ module Live
49
65
  # Enqueue a remote procedure call to the currently bound page.
50
66
  # @parameter method [Symbol] The name of the remote functio to invoke.
51
67
  # @parameter arguments [Array]
52
- def rpc(method, arguments)
68
+ def rpc(*arguments)
53
69
  # This update might not be sent right away. Therefore, mutable arguments may be serialized to JSON at a later time (or never). This could be a race condition:
54
- @page.updates.enqueue([method, arguments])
70
+ @page.updates.enqueue(arguments)
55
71
  end
56
72
  end
57
73
  end
data/lib/live/page.rb CHANGED
@@ -50,7 +50,7 @@ module Live
50
50
  if element = @elements[id]
51
51
  return element.handle(event)
52
52
  else
53
- Console.logger.warn(self, "Could not handle event:", event, details)
53
+ Console.warn(self, "Could not handle event:", event, details)
54
54
  end
55
55
 
56
56
  return nil
@@ -68,12 +68,12 @@ module Live
68
68
  if element = self.resolve(id, data)
69
69
  self.bind(element)
70
70
  else
71
- Console.logger.warn(self, "Could not resolve element:", message)
71
+ Console.warn(self, "Could not resolve element:", message)
72
72
  end
73
73
  elsif id = message[:id]
74
74
  self.handle(id, message[:event])
75
75
  else
76
- Console.logger.warn(self, "Unhandled message:", message)
76
+ Console.warn(self, "Unhandled message:", message)
77
77
  end
78
78
  end
79
79
 
@@ -82,7 +82,7 @@ module Live
82
82
  def run(connection)
83
83
  queue_task = Async do
84
84
  while update = @updates.dequeue
85
- Console.logger.debug(self, "Sending update:", update)
85
+ Console.debug(self, "Sending update:", update)
86
86
 
87
87
  connection.write(::Protocol::WebSocket::JSONMessage.generate(update))
88
88
  connection.flush if @updates.empty?
@@ -90,12 +90,12 @@ module Live
90
90
  end
91
91
 
92
92
  while message = connection.read
93
- Console.logger.debug(self, "Reading message:", message)
93
+ Console.debug(self, "Reading message:", message)
94
94
 
95
95
  if json_message = ::Protocol::WebSocket::JSONMessage.wrap(message)
96
96
  process_message(json_message.parse)
97
97
  else
98
- Console.logger.warn(self, "Unhandled message:", message)
98
+ Console.warn(self, "Unhandled message:", message)
99
99
  end
100
100
  end
101
101
  ensure
data/lib/live/version.rb CHANGED
@@ -4,5 +4,5 @@
4
4
  # Copyright, 2021-2024, by Samuel Williams.
5
5
 
6
6
  module Live
7
- VERSION = "0.6.0"
7
+ VERSION = "0.7.0"
8
8
  end
data/lib/live/view.rb CHANGED
@@ -9,21 +9,40 @@ require 'xrb/builder'
9
9
  module Live
10
10
  # Represents a single division of content on the page an provides helpers for rendering the content.
11
11
  class View < Element
12
+ # Update the content of the client-side element by rendering this view.
13
+ def update!(**options)
14
+ rpc(:update, @id, self.to_html, options)
15
+ end
16
+
12
17
  # Replace the content of the client-side element by rendering this view.
13
- def replace!
14
- rpc(:replace, [@id, self.to_html])
18
+ # @parameter selector [String] The CSS selector to replace.
19
+ # @parameter node [String] The HTML to replace.
20
+ def replace(selector, node, **options)
21
+ rpc(:replace, selector, node.to_s, options)
22
+ end
23
+
24
+ # Prepend to the content of the client-side element by appending the specified element.
25
+ # @parameter selector [String] The CSS selector to prepend to.
26
+ # @parameter node [String] The HTML to prepend.
27
+ def prepend(selector, node, **options)
28
+ rpc(:prepend, selector, node.to_s, options)
15
29
  end
16
30
 
17
31
  # Append to the content of the client-side element by appending the specified element.
18
- # @parameter node [Live::Element] The element to append.
19
- def append!(element)
20
- rpc(:append, [@id, element.to_html])
32
+ # @parameter selector [String] The CSS selector to append to.
33
+ # @parameter node [String] The HTML to prepend.
34
+ def append(selector, node, **options)
35
+ rpc(:append, selector, node.to_s, options)
21
36
  end
22
37
 
23
- # Prepend to the content of the client-side element by appending the specified element.
24
- # @parameter node [Live::Element] The element to prepend.
25
- def prepend!(element)
26
- rpc(:prepend, [@id, element.to_html])
38
+ # Remove the specified element from the client-side element.
39
+ # @parameter selector [String] The CSS selector to remove.
40
+ def remove(selector, **options)
41
+ rpc(:remove, selector, options)
42
+ end
43
+
44
+ def dispatch_event(selector, type, **options)
45
+ rpc(:dispatch_event, selector, event, options)
27
46
  end
28
47
 
29
48
  # Render the element.
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: live
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
@@ -38,7 +38,7 @@ cert_chain:
38
38
  Q2K9NVun/S785AP05vKkXZEFYxqG6EW012U4oLcFl5MySFajYXRYbuUpH6AY+HP8
39
39
  voD0MPg1DssDLKwXyt1eKD/+Fq0bFWhwVM/1XiAXL7lyYUyOq24KHgQ2Csg=
40
40
  -----END CERTIFICATE-----
41
- date: 2024-04-28 00:00:00.000000000 Z
41
+ date: 2024-05-04 00:00:00.000000000 Z
42
42
  dependencies:
43
43
  - !ruby/object:Gem::Dependency
44
44
  name: async-websocket
metadata.gz.sig CHANGED
@@ -1 +1,3 @@
1
- F6���I��Q�,[z �����&���!zý[qaI�� �ُة�Ö�숔�9h��uXj)R�D�
1
+ [O%��쐕^Q����)���'e�������5����hZz,z�����9М�7��ޫ�<u*[�xGk��I!u2��MW��!t�^
2
+ A����z�������5[B�1�k���2�z���|����E�g��K���(�H�0�:N�]$�(�G�i�~A�U�՗��N$6Qšw�S�}�O�J{���n�`D��ƁaB�bTߚ|��+)~�E�T9��6�5V��Q����S�ԏ��A��Xz��|�\St(��ɛ��G�z�gG�Pٟq4n� ���k�p��도)�����`A���xƬ0�Tb����A桺�K��b�1aƓ+�\!�r����h>V=�
3
+ ���a�Kt؛��.�nf�M� ڊ��nB3b��