live 0.12.0 → 0.13.0

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: b389272cf70f88ade8e987e6df962c0e73761d738d7fc15e29134a6d3677f072
4
- data.tar.gz: adebf021e1a94bed6fd09a4c90364f3176873fcbfb0b691005262a7095768927
3
+ metadata.gz: '08c6f4d6d5b5fc0e7ba635b617256c06ca84c925b528b99e4ab758bf6461af19'
4
+ data.tar.gz: 69266f1a602d85f9f3d14104a2548304498ed38310f34ffa5dfe9e3e9d48aed6
5
5
  SHA512:
6
- metadata.gz: 8f36c200788c094e7662f14dad854db15b5bfe2e04c96e4bf546e2821be91eef549eb574a872476db632e2a6d00cbbe3e3670954756d5b801364d06cfa627809
7
- data.tar.gz: ac325892c144ae36d6417990f76271016782cb196a8cbb7d59645a14a4868804bdd2f61a813de2cff14a8053912115bb1e64961a0ea937023a7ea0ff04d6848e
6
+ metadata.gz: a4e608d98be19c629dd8cba31cb5be8eeac52de4a5fb007ba4822f0baf8b473e16aa4db604585b8ff481d39292c2986408b2d3977667b77552de1cff31b4a85c
7
+ data.tar.gz: 1fa963047b5369012f008c57061a585345d98922082d3c744b032ae15c8599a928f7a9c5b12169ac2242cb1f013b55404cce13f4cd4a2763259eda23b8fe06ac
checksums.yaml.gz.sig CHANGED
Binary file
data/lib/live/element.rb CHANGED
@@ -16,13 +16,18 @@ module Live
16
16
  SecureRandom.uuid
17
17
  end
18
18
 
19
+ def self.mount(parent, id, data = {})
20
+ full_id = parent.id + ":" + id
21
+
22
+ self.new(full_id, data)
23
+ end
24
+
19
25
  # @parameter id [String] The unique identifier within the page.
20
26
  # @parameter data [Hash] The data associated with the element, typically stored as `data-` attributes.
21
- def initialize(id = Element.unique_id, **data)
27
+ def initialize(id = Element.unique_id, data = {})
22
28
  @id = id
23
29
  @data = data
24
30
  @data[:class] ||= self.class.name
25
-
26
31
  @page = nil
27
32
  end
28
33
 
@@ -32,6 +37,9 @@ module Live
32
37
  # The data associated with the element.
33
38
  attr :data
34
39
 
40
+ # @attribute [Page | Nil] The page this elemenet is bound to.
41
+ attr :page
42
+
35
43
  # Generate a JavaScript string which forwards the specified event to the server.
36
44
  # @parameter detail [Hash] The detail associated with the forwarded event.
37
45
  def forward_event(detail = nil)
@@ -77,5 +85,70 @@ module Live
77
85
  raise PageError, "Element is not bound to a page, make sure to implement #close!"
78
86
  end
79
87
  end
88
+
89
+ def script(code, **options)
90
+ rpc(:script, @id, code, options)
91
+ end
92
+
93
+ # Update the content of the client-side element by rendering this view.
94
+ def update!(**options)
95
+ rpc(:update, @id, self.to_html, options)
96
+ end
97
+
98
+ # Replace the content of the client-side element by rendering this view.
99
+ # @parameter selector [String] The CSS selector to replace.
100
+ # @parameter node [String] The HTML to replace.
101
+ def replace(selector, fragment = nil, **options, &block)
102
+ fragment ||= XRB::Builder.fragment(&block)
103
+
104
+ rpc(:replace, selector, fragment.to_s, options)
105
+ end
106
+
107
+ # Prepend to the content of the client-side element by appending the specified element.
108
+ # @parameter selector [String] The CSS selector to prepend to.
109
+ # @parameter node [String] The HTML to prepend.
110
+ def prepend(selector, fragment = nil, **options, &block)
111
+ fragment ||= XRB::Builder.fragment(&block)
112
+
113
+ rpc(:prepend, selector, fragment.to_s, options)
114
+ end
115
+
116
+ # Append to the content of the client-side element by appending the specified element.
117
+ # @parameter selector [String] The CSS selector to append to.
118
+ # @parameter node [String] The HTML to prepend.
119
+ def append(selector, fragment = nil, **options, &block)
120
+ fragment ||= XRB::Builder.fragment(&block)
121
+
122
+ rpc(:append, selector, fragment.to_s, options)
123
+ end
124
+
125
+ # Remove the specified element from the client-side element.
126
+ # @parameter selector [String] The CSS selector to remove.
127
+ def remove(selector, **options)
128
+ rpc(:remove, selector, options)
129
+ end
130
+
131
+ def dispatch_event(selector, type, **options)
132
+ rpc(:dispatch_event, selector, event, options)
133
+ end
134
+
135
+ # Render the element.
136
+ # @parameter builder [XRB::Builder] The HTML builder.
137
+ def render(builder)
138
+ builder.text(self.class.name)
139
+ end
140
+
141
+ # @returns [Object] The generated HTML.
142
+ def to_html
143
+ XRB::Builder.fragment do |builder|
144
+ render(builder)
145
+ end
146
+ end
147
+
148
+ # Convenience method for rendering the view as a string.
149
+ # @returns [String] The generated HTML.
150
+ def to_s
151
+ to_html.to_s
152
+ end
80
153
  end
81
154
  end
data/lib/live/page.rb CHANGED
@@ -20,6 +20,8 @@ module Live
20
20
  @resolver = resolver
21
21
 
22
22
  @elements = {}
23
+ @attached = {}
24
+
23
25
  @updates = Async::Queue.new
24
26
  end
25
27
 
@@ -34,11 +36,25 @@ module Live
34
36
  element.bind(self)
35
37
  end
36
38
 
39
+ # Attach a pre-existing element to the page, so that it may later be bound to this exact instance.
40
+ # You must later detach the element when it is no longer needed.
41
+ def attach(element)
42
+ @attached[element.id] = element
43
+ end
44
+
45
+ def detach(element)
46
+ if @attached.delete(element.id)
47
+ element.close
48
+ end
49
+ end
50
+
37
51
  # Resolve a client-side element to a server side instance.
38
52
  # @parameter id [String] The unique identifier within the page.
39
53
  # @parameter data [Hash] The data associated with the element, typically stored as `data-` attributes.
40
- def resolve(id, data)
41
- @resolver.call(id, data)
54
+ def resolve(id, data = {})
55
+ @attached.fetch(id) do
56
+ @resolver.call(id, data)
57
+ end
42
58
  end
43
59
 
44
60
  # Handle an event from the client. If the element could not be found, it is silently ignored.
@@ -76,7 +92,7 @@ module Live
76
92
  when 'unbind'
77
93
  # Unbind a client-side element from a server-side element.
78
94
  if element = @elements.delete(message[1])
79
- element.close
95
+ element.close unless @attached.key?(message[1])
80
96
  else
81
97
  Console.warn(self, "Could not unbind element:", message)
82
98
  @updates.enqueue(['error', message[1], "Could not unbind element!"])
@@ -111,4 +127,4 @@ module Live
111
127
  end
112
128
  end
113
129
  end
114
- end
130
+ end
data/lib/live/resolver.rb CHANGED
@@ -43,7 +43,7 @@ module Live
43
43
  # @returns [Element] The element instance if it was allowed.
44
44
  def call(id, data)
45
45
  if klass = @allowed[data[:class]]
46
- return klass.new(id, **data)
46
+ return klass.new(id, data)
47
47
  end
48
48
  end
49
49
  end
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.12.0"
7
+ VERSION = "0.13.0"
8
8
  end
data/lib/live/view.rb CHANGED
@@ -9,70 +9,13 @@ 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
- def script(code, **options)
13
- rpc(:script, @id, code, options)
14
- end
15
-
16
- # Update the content of the client-side element by rendering this view.
17
- def update!(**options)
18
- rpc(:update, @id, self.to_html, options)
19
- end
20
-
21
- # Replace the content of the client-side element by rendering this view.
22
- # @parameter selector [String] The CSS selector to replace.
23
- # @parameter node [String] The HTML to replace.
24
- def replace(selector, fragment = nil, **options, &block)
25
- fragment ||= XRB::Builder.fragment(&block)
26
-
27
- rpc(:replace, selector, fragment.to_s, options)
28
- end
29
-
30
- # Prepend to the content of the client-side element by appending the specified element.
31
- # @parameter selector [String] The CSS selector to prepend to.
32
- # @parameter node [String] The HTML to prepend.
33
- def prepend(selector, fragment = nil, **options, &block)
34
- fragment ||= XRB::Builder.fragment(&block)
35
-
36
- rpc(:prepend, selector, fragment.to_s, options)
37
- end
38
-
39
- # Append to the content of the client-side element by appending the specified element.
40
- # @parameter selector [String] The CSS selector to append to.
41
- # @parameter node [String] The HTML to prepend.
42
- def append(selector, fragment = nil, **options, &block)
43
- fragment ||= XRB::Builder.fragment(&block)
44
-
45
- rpc(:append, selector, fragment.to_s, options)
46
- end
47
-
48
- # Remove the specified element from the client-side element.
49
- # @parameter selector [String] The CSS selector to remove.
50
- def remove(selector, **options)
51
- rpc(:remove, selector, options)
52
- end
53
-
54
- def dispatch_event(selector, type, **options)
55
- rpc(:dispatch_event, selector, event, options)
56
- end
57
-
58
- # Render the element.
59
- # @parameter builder [XRB::Builder] The HTML builder.
60
- def render(builder)
61
- end
62
-
63
12
  # @returns [Object] The generated HTML.
64
13
  def to_html
65
14
  XRB::Builder.fragment do |builder|
66
- builder.tag :div, id: @id, class: 'live', data: @data do
15
+ builder.inline_tag :div, id: @id, class: 'live', data: @data do
67
16
  render(builder)
68
17
  end
69
18
  end
70
19
  end
71
-
72
- # Convenience method for rendering the view as a string.
73
- # @returns [String] The generated HTML.
74
- def to_s
75
- to_html.to_s
76
- end
77
20
  end
78
21
  end
data/license.md CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  Copyright, 2021-2024, by Samuel Williams.
4
4
  Copyright, 2023, by Olle Jonsson.
5
+ Copyright, 2024, by Tatsuhiro Ujihisa.
5
6
 
6
7
  Permission is hereby granted, free of charge, to any person obtaining a copy
7
8
  of this software and associated documentation files (the "Software"), to deal
data/readme.md CHANGED
@@ -30,14 +30,18 @@ We welcome contributions to this project.
30
30
 
31
31
  ### Developer Certificate of Origin
32
32
 
33
- This project uses the [Developer Certificate of Origin](https://developercertificate.org/). All contributors to this project must agree to this document to have their contributions accepted.
33
+ In order to protect users of this project, we require all contributors to comply with the [Developer Certificate of Origin](https://developercertificate.org/). This ensures that all contributions are properly licensed and attributed.
34
34
 
35
- ### Contributor Covenant
35
+ ### Community Guidelines
36
36
 
37
- This project is governed by the [Contributor Covenant](https://www.contributor-covenant.org/). All contributors and participants agree to abide by its terms.
37
+ This project is best served by a collaborative and respectful environment. Treat each other professionally, respect differing viewpoints, and engage constructively. Harassment, discrimination, or harmful behavior is not tolerated. Communicate clearly, listen actively, and support one another. If any issues arise, please inform the project maintainers.
38
38
 
39
39
  ## See Also
40
40
 
41
- - [live-js](https://github.com/socketry/live-js) The client-side JavaScript library.
42
- - [morphdom](https://github.com/patrick-steele-idem/morphdom) Efficiently update the client-side HTML.
41
+ - [live-js](https://github.com/socketry/live-js) The client-side JavaScript library.
42
+ - [morphdom](https://github.com/patrick-steele-idem/morphdom) Efficiently update the client-side HTML.
43
43
  - [stimulus-reflex](https://github.com/hopsoft/stimulus_reflex) — An alternative framework which provides similar functionality.
44
+
45
+ ### Examples
46
+
47
+ - [Flappy Bird](https://github.com/socketry/flappy-bird) – A clone of the classic Flappy Bird game.
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,11 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: live
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.12.0
4
+ version: 0.13.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
8
8
  - Olle Jonsson
9
+ - Tatsuhiro Ujihisa
9
10
  autorequire:
10
11
  bindir: bin
11
12
  cert_chain:
@@ -38,7 +39,7 @@ cert_chain:
38
39
  Q2K9NVun/S785AP05vKkXZEFYxqG6EW012U4oLcFl5MySFajYXRYbuUpH6AY+HP8
39
40
  voD0MPg1DssDLKwXyt1eKD/+Fq0bFWhwVM/1XiAXL7lyYUyOq24KHgQ2Csg=
40
41
  -----END CERTIFICATE-----
41
- date: 2024-07-03 00:00:00.000000000 Z
42
+ date: 2024-07-05 00:00:00.000000000 Z
42
43
  dependencies:
43
44
  - !ruby/object:Gem::Dependency
44
45
  name: async-websocket
metadata.gz.sig CHANGED
Binary file