rage-pulse 0.1.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 +7 -0
- data/.dockerignore +6 -0
- data/.standard.yml +2 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +10 -0
- data/Dockerfile +42 -0
- data/LICENSE.txt +21 -0
- data/README.md +353 -0
- data/Rakefile +32 -0
- data/fly.toml +22 -0
- data/lib/pulse/assets/pulse.min.js +3 -0
- data/lib/pulse/cable/view_channel.rb +95 -0
- data/lib/pulse/component.rb +153 -0
- data/lib/pulse/component_root_validator.rb +240 -0
- data/lib/pulse/event.rb +29 -0
- data/lib/pulse/fingerprint.rb +51 -0
- data/lib/pulse/invalid_component_root_error.rb +4 -0
- data/lib/pulse/marker.rb +13 -0
- data/lib/pulse/model_cache.rb +25 -0
- data/lib/pulse/page.rb +145 -0
- data/lib/pulse/phlex/auto_id.rb +16 -0
- data/lib/pulse/phlex/event_handlers.rb +46 -0
- data/lib/pulse/phlex/meta_elements.rb +12 -0
- data/lib/pulse/rage_extension.rb +25 -0
- data/lib/pulse/scripts.rb +45 -0
- data/lib/pulse/server_events.rb +98 -0
- data/lib/pulse/state.rb +64 -0
- data/lib/pulse/store.rb +38 -0
- data/lib/pulse/version.rb +5 -0
- data/lib/pulse/watching.rb +89 -0
- data/lib/pulse.rb +38 -0
- data/lib/rage-pulse.rb +1 -0
- metadata +127 -0
data/lib/pulse/page.rb
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Pulse::Page
|
|
4
|
+
def initialize(state)
|
|
5
|
+
@state = state
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
def path
|
|
9
|
+
@state.url
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def navigate(url)
|
|
13
|
+
@navigate_url = url
|
|
14
|
+
@state.server_events.update_page(self)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def query
|
|
18
|
+
@query ||= Query.new(self, @state.query_snapshot)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def local_storage
|
|
22
|
+
@local_storage ||= LocalStorage.new(self, @state.local_storage_snapshot)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def url
|
|
26
|
+
query_string = @state.query_snapshot
|
|
27
|
+
|
|
28
|
+
if query_string.nil? || query_string.empty?
|
|
29
|
+
path
|
|
30
|
+
else
|
|
31
|
+
"#{path}?#{query_string}"
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def __pulse_dump
|
|
36
|
+
data = {}
|
|
37
|
+
|
|
38
|
+
if @navigate_url
|
|
39
|
+
data[:url] = @navigate_url
|
|
40
|
+
@navigate_url = nil
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
if @query&.changed
|
|
44
|
+
@query.changed = false
|
|
45
|
+
data[:query] = @query.__pulse_dump
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
if @local_storage&.changed
|
|
49
|
+
@local_storage.changed = false
|
|
50
|
+
data[:local_storage] = @local_storage.__pulse_dump
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
data
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
class HashAccess
|
|
57
|
+
attr_accessor :changed
|
|
58
|
+
|
|
59
|
+
def initialize(page, data, root: self)
|
|
60
|
+
@page = page
|
|
61
|
+
@data = data
|
|
62
|
+
@root = root
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def [](key)
|
|
66
|
+
process_nested_value(@data[key])
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def key?(key)
|
|
70
|
+
@data.key?(key)
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
alias_method :has_key?, :key?
|
|
74
|
+
|
|
75
|
+
def fetch(...)
|
|
76
|
+
process_nested_value(@data.fetch(...))
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def delete(...)
|
|
80
|
+
@data.delete(...).tap { trigger_change }
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def update(...)
|
|
84
|
+
@data.update(...).tap { trigger_change }
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def []=(key, value)
|
|
88
|
+
(@data[key] = value).tap { trigger_change }
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def to_h
|
|
92
|
+
@data
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def inspect
|
|
96
|
+
@data.inspect
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
private
|
|
100
|
+
|
|
101
|
+
def process_nested_value(value)
|
|
102
|
+
if value.is_a?(Hash)
|
|
103
|
+
HashAccess.new(@page, value, root: @root)
|
|
104
|
+
elsif value.is_a?(Array)
|
|
105
|
+
value.map { |v| process_nested_value(v) }
|
|
106
|
+
else
|
|
107
|
+
value
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def trigger_change
|
|
112
|
+
@root.changed = true
|
|
113
|
+
Pulse.__state.server_events.update_page(@page)
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
class Query < HashAccess
|
|
118
|
+
def initialize(page, query_string)
|
|
119
|
+
data = if query_string.nil? || query_string.empty?
|
|
120
|
+
{}
|
|
121
|
+
else
|
|
122
|
+
Iodine::Rack::Utils.parse_nested_query(query_string)
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
super(page, data)
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def __pulse_dump
|
|
129
|
+
Rack::Utils.build_nested_query(to_h)
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
class LocalStorage < HashAccess
|
|
134
|
+
def initialize(page, snapshot)
|
|
135
|
+
super(
|
|
136
|
+
page,
|
|
137
|
+
snapshot ? JSON.parse(snapshot, symbolize_names: true) : {}
|
|
138
|
+
)
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
def __pulse_dump
|
|
142
|
+
JSON.dump(to_h)
|
|
143
|
+
end
|
|
144
|
+
end
|
|
145
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
module Pulse::Phlex::AutoId
|
|
2
|
+
body_content_elements = Phlex::HTML::StandardElements.instance_methods + %i[tag] - %i[html head title style]
|
|
3
|
+
|
|
4
|
+
body_content_elements.each do |method_name|
|
|
5
|
+
define_method(method_name) do |**attributes, &block|
|
|
6
|
+
if @__pending_pulse_id
|
|
7
|
+
# TODO: users setting IDs manually will prevent events handlers
|
|
8
|
+
# from being called via `Fiber[:__pulse_state]`
|
|
9
|
+
attributes = attributes.merge(id: pulse_id) unless attributes.has_key?(:id)
|
|
10
|
+
@__pending_pulse_id = false
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
super(**attributes, &block)
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
module Pulse::Phlex::EventHandlers
|
|
2
|
+
elements = Phlex::HTML::StandardElements.instance_methods +
|
|
3
|
+
Phlex::HTML::VoidElements.instance_methods -
|
|
4
|
+
%i[html head title style]
|
|
5
|
+
|
|
6
|
+
elements.each do |method_name|
|
|
7
|
+
define_method method_name do |click: nil, input: nil, change: nil, submit: nil, keydown: nil, keyup: nil, **attributes, &block|
|
|
8
|
+
return super(**attributes, &block) unless click || input || change || submit || keydown || keyup
|
|
9
|
+
|
|
10
|
+
attributes = attributes.merge(on(:click, click)) if click
|
|
11
|
+
attributes = attributes.merge(on(:input, input)) if input
|
|
12
|
+
attributes = attributes.merge(on(:change, change)) if change
|
|
13
|
+
attributes = attributes.merge(on(:submit, submit)) if submit
|
|
14
|
+
attributes = attributes.merge(on(:keydown, keydown)) if keydown
|
|
15
|
+
attributes = attributes.merge(on(:keyup, keyup)) if keyup
|
|
16
|
+
|
|
17
|
+
Pulse.__state.interactive_components[__component_id] = self if Pulse.__state.live?
|
|
18
|
+
|
|
19
|
+
super(**attributes, &block)
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def on(event, handler, key: nil, debounce: nil, throttle: nil)
|
|
24
|
+
if debounce || throttle
|
|
25
|
+
raise ArgumentError, "`#{event}` handler can't use both `debounce` and `throttle`" if debounce && throttle
|
|
26
|
+
raise ArgumentError, "`#{event}` handler `debounce` must be a positive number of milliseconds" if debounce.to_i < 0
|
|
27
|
+
raise ArgumentError, "`#{event}` handler `throttle` must be a positive number of milliseconds" if throttle.to_i < 0
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
raise ArgumentError, "`#{event}` can't use `key`" if key && event != :keyup && event != :keydown
|
|
31
|
+
|
|
32
|
+
options = {"data-pulse-#{event}" => handler.to_s, "data-pulse-component-id" => __component_id}
|
|
33
|
+
options["data-pulse-#{event}-debounce"] = debounce if debounce
|
|
34
|
+
options["data-pulse-#{event}-throttle"] = throttle if throttle
|
|
35
|
+
options["data-pulse-#{event}-key"] = key if key
|
|
36
|
+
|
|
37
|
+
options
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
private
|
|
41
|
+
|
|
42
|
+
# stable key for the weak map that is safe to compare by identity
|
|
43
|
+
def __component_id
|
|
44
|
+
object_id
|
|
45
|
+
end
|
|
46
|
+
end
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
module Pulse::Phlex::MetaElements
|
|
2
|
+
%i[doctype meta html head title style script].each do |method_name|
|
|
3
|
+
define_method(method_name) do |**attributes, &block|
|
|
4
|
+
if @__is_replace
|
|
5
|
+
# no-op
|
|
6
|
+
block&.call if method_name == :html
|
|
7
|
+
else
|
|
8
|
+
super(**attributes, &block)
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Pulse::RageExtension < Rage::Extension
|
|
4
|
+
configure do
|
|
5
|
+
config.router.form_actions = true
|
|
6
|
+
|
|
7
|
+
config.renderer :phlex do |component, **props|
|
|
8
|
+
component_instance = component.new(**props)
|
|
9
|
+
Pulse.__state.root_component ||= component_instance
|
|
10
|
+
headers["content-type"] = "text/html"
|
|
11
|
+
component_instance.call
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
initializer do
|
|
16
|
+
Pulse::Component.prepend(Pulse::Phlex::AutoId)
|
|
17
|
+
Pulse::Component.prepend(Pulse::Phlex::MetaElements)
|
|
18
|
+
Pulse::Component.prepend(Pulse::Phlex::EventHandlers)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
routes do
|
|
22
|
+
mount Rage::Cable.application(namespace: Pulse::Cable, protocol: :raw_websocket_json), at: "__pulse/cable"
|
|
23
|
+
mount Pulse::Scripts, at: Pulse::Scripts::MOUNT_PATH, via: :get
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "digest"
|
|
4
|
+
|
|
5
|
+
##
|
|
6
|
+
# Phlex mixin that emits the Pulse client. Include it in your layout and call
|
|
7
|
+
# `pulse_scripts` in <head> or at the end of <body>.
|
|
8
|
+
#
|
|
9
|
+
# The client is served from the gem by `Pulse::RageExtension` under a fingerprinted URL,
|
|
10
|
+
# so browsers can cache it indefinitely.
|
|
11
|
+
#
|
|
12
|
+
module Pulse::Scripts
|
|
13
|
+
JAVASCRIPT_PATH = File.expand_path("assets/pulse.min.js", __dir__)
|
|
14
|
+
MOUNT_PATH = "/__pulse/scripts"
|
|
15
|
+
|
|
16
|
+
HEADERS = {
|
|
17
|
+
"content-type" => "text/javascript; charset=utf-8",
|
|
18
|
+
"cache-control" => "public, max-age=31536000, immutable"
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
class << self
|
|
22
|
+
# @private
|
|
23
|
+
def call(_)
|
|
24
|
+
[200, HEADERS.dup, [script]]
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# @return [String] the fingerprinted URL of the client bundle
|
|
28
|
+
def url
|
|
29
|
+
@url ||= begin
|
|
30
|
+
digest = Digest::SHA256.hexdigest(script)[0, 16]
|
|
31
|
+
"#{MOUNT_PATH}/pulse-#{digest}.js"
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
private
|
|
36
|
+
|
|
37
|
+
def script
|
|
38
|
+
@script ||= File.binread(JAVASCRIPT_PATH)
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def pulse_scripts
|
|
43
|
+
script(src: Pulse::Scripts.url, defer: true)
|
|
44
|
+
end
|
|
45
|
+
end
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Pulse::ServerEvents
|
|
4
|
+
def initialize(write_closure)
|
|
5
|
+
@write_closure = write_closure
|
|
6
|
+
@batch = []
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def with_batched_events
|
|
10
|
+
@batched_events = true
|
|
11
|
+
yield
|
|
12
|
+
ensure
|
|
13
|
+
@batched_events = false
|
|
14
|
+
emit_batched_events
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def replace_component(component)
|
|
18
|
+
emit(:replace, component) { {action: "replace", html: component.__pulse_dump(true)} }
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def replace_html(html)
|
|
22
|
+
@html_seq ||= (1..).each
|
|
23
|
+
emit(:replace, @html_seq.next) { {action: "replace", html:} }
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def append_component(component, selector)
|
|
27
|
+
emit(:append, component) { {action: "append", selector:, html: component.__pulse_dump} }
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def prepend_component(component, selector)
|
|
31
|
+
emit(:prepend, component) { {action: "prepend", selector:, html: component.__pulse_dump} }
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def remove_component(component_id)
|
|
35
|
+
emit(:remove, component_id) { {action: "remove", id: component_id} }
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def update_page(page)
|
|
39
|
+
emit(:page) { {action: "page", **page.__pulse_dump} }
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
private
|
|
43
|
+
|
|
44
|
+
def replace_events
|
|
45
|
+
@replace_events ||= {}
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def append_events
|
|
49
|
+
@append_events ||= {}
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def prepend_events
|
|
53
|
+
@prepend_events ||= {}
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def remove_events
|
|
57
|
+
@remove_events ||= {}
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def emit(event_type, event_id = event_type, &block)
|
|
61
|
+
unless @batched_events
|
|
62
|
+
@write_closure&.call(yield)
|
|
63
|
+
return
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
registry = case event_type
|
|
67
|
+
when :replace
|
|
68
|
+
replace_events
|
|
69
|
+
when :append
|
|
70
|
+
append_events
|
|
71
|
+
when :prepend
|
|
72
|
+
prepend_events
|
|
73
|
+
when :remove
|
|
74
|
+
remove_events
|
|
75
|
+
when :page
|
|
76
|
+
# we only need the latest representation of `page`, so queue it
|
|
77
|
+
@page_event = block
|
|
78
|
+
return
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
unless registry.has_key?(event_id)
|
|
82
|
+
registry[event_id] = true
|
|
83
|
+
@batch << yield
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def emit_batched_events
|
|
88
|
+
@batch << @page_event.call if @page_event
|
|
89
|
+
@write_closure&.call(@batch)
|
|
90
|
+
ensure
|
|
91
|
+
@batch.clear
|
|
92
|
+
@replace_events&.clear
|
|
93
|
+
@append_events&.clear
|
|
94
|
+
@prepend_events&.clear
|
|
95
|
+
@remove_events&.clear
|
|
96
|
+
@page_event = nil
|
|
97
|
+
end
|
|
98
|
+
end
|
data/lib/pulse/state.rb
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Pulse::State
|
|
4
|
+
attr_accessor :root_component
|
|
5
|
+
attr_reader :local_storage_snapshot, :query_snapshot, :url
|
|
6
|
+
|
|
7
|
+
def initialize(write_closure)
|
|
8
|
+
@write_closure = write_closure
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def live?
|
|
12
|
+
!!@write_closure
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def interactive_components
|
|
16
|
+
@interactive_components ||= ObjectSpace::WeakMap.new
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def stores
|
|
20
|
+
@stores ||= {}
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def server_events
|
|
24
|
+
@events ||= Pulse::ServerEvents.new(@write_closure)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def page
|
|
28
|
+
@page ||= Pulse::Page.new(self)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def local_storage_snapshot=(local_storage_snapshot)
|
|
32
|
+
@local_storage_snapshot = local_storage_snapshot
|
|
33
|
+
@page = nil
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def query_snapshot=(query_snapshot)
|
|
37
|
+
@query_snapshot = query_snapshot
|
|
38
|
+
@page = nil
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def url=(url)
|
|
42
|
+
@url = url
|
|
43
|
+
@page = nil
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def schedule_cleanup(actor, &block)
|
|
47
|
+
@cleanup ||= {}
|
|
48
|
+
prev_cleanup = @cleanup[actor]
|
|
49
|
+
|
|
50
|
+
@cleanup[actor] = -> do
|
|
51
|
+
prev_cleanup&.call
|
|
52
|
+
block.call
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def run_cleanup(actor)
|
|
57
|
+
@cleanup&.delete(actor)&.call
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def reload
|
|
61
|
+
@root_component = @stores = nil
|
|
62
|
+
@cleanup&.each_value(&:call)&.clear
|
|
63
|
+
end
|
|
64
|
+
end
|
data/lib/pulse/store.rb
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Pulse::Store
|
|
4
|
+
class << self
|
|
5
|
+
def attribute(name, default: nil)
|
|
6
|
+
define_method(name) { attributes.fetch(name, default) }
|
|
7
|
+
|
|
8
|
+
define_method "#{name}=" do |value|
|
|
9
|
+
attributes[name] = value
|
|
10
|
+
|
|
11
|
+
components.each do |component, block|
|
|
12
|
+
block&.call(self)
|
|
13
|
+
component.replace
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
value
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def __register_component(component, &block)
|
|
22
|
+
components[component] = block
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def __unregister_component(component)
|
|
26
|
+
components.delete(component)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
private
|
|
30
|
+
|
|
31
|
+
def attributes
|
|
32
|
+
@attributes ||= {}
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def components
|
|
36
|
+
@components ||= {}
|
|
37
|
+
end
|
|
38
|
+
end
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
module Pulse::Watching
|
|
2
|
+
def self.broadcast(model)
|
|
3
|
+
unless model.is_a?(ActiveRecord::Base)
|
|
4
|
+
raise ArgumentError, "should be an Active Record model"
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
Rage::PubSub.publish(model.to_global_id(app: "pulse").to_s, topic: model)
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def watch(object, &)
|
|
11
|
+
if object.is_a?(ActiveRecord::Base)
|
|
12
|
+
watch_model(object, &)
|
|
13
|
+
elsif (object.is_a?(Class) && object <= Pulse::Store) || object.is_a?(Pulse::Store)
|
|
14
|
+
watch_store(object, &)
|
|
15
|
+
else
|
|
16
|
+
raise ArgumentError, "should be an Active Record model or an instance of Pulse::Store"
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
private
|
|
21
|
+
|
|
22
|
+
def watch_model(model, &block)
|
|
23
|
+
pulse_state = Pulse.__state
|
|
24
|
+
return model unless pulse_state.live?
|
|
25
|
+
|
|
26
|
+
wrapped = SimpleDelegator.new(model)
|
|
27
|
+
|
|
28
|
+
__add_subscription do
|
|
29
|
+
Rage::PubSub.subscribe(topic: model, subscription_id: self) do |gid|
|
|
30
|
+
Fiber.schedule do
|
|
31
|
+
Fiber[:__pulse_state] = pulse_state
|
|
32
|
+
|
|
33
|
+
new_model = Pulse::ModelCache.fetch(gid)
|
|
34
|
+
wrapped.__setobj__(new_model)
|
|
35
|
+
block&.call(new_model)
|
|
36
|
+
new_model.nil? ? remove : replace
|
|
37
|
+
rescue => e
|
|
38
|
+
Rage.logger.error("#{e.class} (#{e.message}):\n#{e.backtrace.join("\n")}")
|
|
39
|
+
Rage::Errors.report(e)
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
pulse_state.schedule_cleanup(self) do
|
|
44
|
+
Rage::PubSub.unsubscribe(topic: model, subscription_id: self)
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
wrapped
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def watch_store(store_or_class, &block)
|
|
52
|
+
unless Pulse.__state.live?
|
|
53
|
+
return store_or_class.is_a?(Class) ? store_or_class.new : store_or_class
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
store = if store_or_class.is_a?(Class)
|
|
57
|
+
Pulse.__state.stores[store_or_class] ||= store_or_class.new
|
|
58
|
+
else
|
|
59
|
+
store_or_class
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
__add_subscription do
|
|
63
|
+
store.__register_component(self, &block)
|
|
64
|
+
|
|
65
|
+
Pulse.__state.schedule_cleanup(self) do
|
|
66
|
+
store.__unregister_component(self)
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
store
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def __add_subscription(&block)
|
|
74
|
+
(@__subscriptions ||= []) << block
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def __activate_subscriptions
|
|
78
|
+
if @__subscriptions
|
|
79
|
+
@__subscriptions&.each(&:call)
|
|
80
|
+
@__subscriptions = nil
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
protected
|
|
85
|
+
|
|
86
|
+
def __cleanup_subscriptions
|
|
87
|
+
Pulse.__state.run_cleanup(self)
|
|
88
|
+
end
|
|
89
|
+
end
|
data/lib/pulse.rb
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Pulse
|
|
4
|
+
autoload :Component, "pulse/component"
|
|
5
|
+
autoload :ComponentRootValidator, "pulse/component_root_validator"
|
|
6
|
+
autoload :InvalidComponentRootError, "pulse/invalid_component_root_error"
|
|
7
|
+
|
|
8
|
+
module Cable
|
|
9
|
+
autoload :ViewChannel, "pulse/cable/view_channel"
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
module Phlex
|
|
13
|
+
autoload :AutoId, "pulse/phlex/auto_id"
|
|
14
|
+
autoload :MetaElements, "pulse/phlex/meta_elements"
|
|
15
|
+
autoload :EventHandlers, "pulse/phlex/event_handlers"
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def self.broadcast(...)
|
|
19
|
+
Watching.broadcast(...)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def self.__state
|
|
23
|
+
Fiber[:__pulse_state] ||= State.new(nil)
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
require_relative "pulse/version"
|
|
28
|
+
require_relative "pulse/scripts"
|
|
29
|
+
require_relative "pulse/marker"
|
|
30
|
+
require_relative "pulse/watching"
|
|
31
|
+
require_relative "pulse/fingerprint"
|
|
32
|
+
require_relative "pulse/event"
|
|
33
|
+
require_relative "pulse/store"
|
|
34
|
+
require_relative "pulse/state"
|
|
35
|
+
require_relative "pulse/page"
|
|
36
|
+
require_relative "pulse/model_cache"
|
|
37
|
+
require_relative "pulse/server_events"
|
|
38
|
+
require_relative "pulse/rage_extension"
|
data/lib/rage-pulse.rb
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
require "pulse"
|