interfacets 0.9.9 → 0.99999
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 +4 -4
- data/LICENSE +9 -17
- data/lib/interfacets/client/actor.rb +2 -1
- data/lib/interfacets/client/assets.rb +77 -63
- data/lib/interfacets/client/bus.rb +31 -8
- data/lib/interfacets/client/channels/api.rb +141 -35
- data/lib/interfacets/client/channels/audio.rb +13 -10
- data/lib/interfacets/client/channels/base.rb +2 -2
- data/lib/interfacets/client/channels/pubsub.rb +69 -0
- data/lib/interfacets/client/channels/react/builder.rb +42 -72
- data/lib/interfacets/client/channels/react/channel.rb +7 -5
- data/lib/interfacets/client/channels/react/evaluator.rb +6 -4
- data/lib/interfacets/client/channels/timer.rb +8 -7
- data/lib/interfacets/client/channels/url.rb +39 -6
- data/lib/interfacets/client/registry.rb +10 -70
- data/lib/interfacets/client/slots.rb +45 -0
- data/lib/interfacets/client/system.rb +20 -15
- data/lib/interfacets/client/template.rb +90 -0
- data/lib/interfacets/client/utils/mruby_patches.rb +8 -4
- data/lib/interfacets/client/utils/open_struct.rb +88 -77
- data/lib/interfacets/component_registry.rb +120 -0
- data/lib/interfacets/component_schema_parser.rb +88 -0
- data/lib/interfacets/mruby/build_config.rb +8 -4
- data/lib/interfacets/mruby/entrypoint.rb +2 -2
- data/lib/interfacets/server/api.rb +44 -27
- data/lib/interfacets/server/assets.rb +69 -28
- data/lib/interfacets/server/basic_router.rb +38 -23
- data/lib/interfacets/server/bus.rb +8 -14
- data/lib/interfacets/server/registry.rb +31 -185
- data/lib/interfacets/shared/basic_routable.rb +48 -0
- data/lib/interfacets/shared/entities/bus.rb +623 -177
- data/lib/interfacets/shared/entities/changeset.rb +432 -0
- data/lib/interfacets/shared/entities/collection_proxy.rb +132 -72
- data/lib/interfacets/shared/entities/specs/handlers.rb +74 -37
- data/lib/interfacets/shared/entities/specs.rb +223 -20
- data/lib/interfacets/shared/entity.rb +124 -110
- data/lib/interfacets/shared/entity_dsl.rb +272 -0
- data/lib/interfacets/shared/facet.rb +260 -0
- data/lib/interfacets/shared/generated_store.rb +74 -35
- data/lib/interfacets/shared/refreshable.rb +85 -0
- data/lib/interfacets/shared/utils.rb +36 -32
- data/lib/interfacets/shared/validations.rb +749 -25
- data/lib/interfacets/shared/view.rb +120 -0
- data/lib/interfacets/test/component_registry.rb +82 -0
- data/lib/interfacets/test/js/inline_bus.rb +68 -33
- data/lib/interfacets/test/js/nodo_bus.rb +37 -9
- data/lib/interfacets/test/js/receivers/api.rb +17 -4
- data/lib/interfacets/test/js/receivers/audio.rb +61 -0
- data/lib/interfacets/test/js/receivers/pubsub.rb +50 -0
- data/lib/interfacets/test/js/receivers/react/node/xml_parser.rb +103 -0
- data/lib/interfacets/test/js/receivers/react/node.rb +91 -71
- data/lib/interfacets/test/js/receivers/react.rb +15 -5
- data/lib/interfacets/test/js/receivers/timer.rb +81 -0
- data/lib/interfacets/test/js/receivers/url.rb +51 -10
- data/lib/interfacets/test/standard_elements.yml +173 -0
- data/lib/interfacets/test/ui_simulator.rb +145 -0
- data/lib/interfacets/test/validation_engine.rb +184 -0
- data/lib/interfacets/test.rb +1 -7
- data/lib/interfacets/version.rb +1 -1
- data/lib/interfacets.rb +13 -2
- metadata +51 -37
- data/.rubocop.yml +0 -180
- data/LICENSE.txt +0 -21
- data/Rakefile +0 -10
- data/lib/interfacets/client/channels/page_visibility.rb +0 -21
- data/lib/interfacets/client/channels/react/dom.rb +0 -91
- data/lib/interfacets/client/channels/speech_to_text.rb +0 -100
- data/lib/interfacets/client/config.rb +0 -22
- data/lib/interfacets/client/delegator.rb +0 -37
- data/lib/interfacets/client/facet.rb +0 -26
- data/lib/interfacets/client/facet2.rb +0 -15
- data/lib/interfacets/client/facets/attributes/accessor.rb +0 -28
- data/lib/interfacets/client/facets/attributes/association.rb +0 -50
- data/lib/interfacets/client/facets/attributes/bind.rb +0 -25
- data/lib/interfacets/client/facets/attributes/collection.rb +0 -47
- data/lib/interfacets/client/facets/attributes/readonly.rb +0 -19
- data/lib/interfacets/client/facets/deserializer.rb +0 -30
- data/lib/interfacets/client/facets/schema/deserializer.rb +0 -63
- data/lib/interfacets/client/facets/schema.rb +0 -63
- data/lib/interfacets/client/facets/serializer.rb +0 -18
- data/lib/interfacets/client/view.rb +0 -47
- data/lib/interfacets/server/assets/facet.rb +0 -61
- data/lib/interfacets/server/basic_routable.rb +0 -40
- data/lib/interfacets/server/config.rb +0 -87
- data/lib/interfacets/server/facet.rb +0 -51
- data/lib/interfacets/server/facets/deserializer.rb +0 -25
- data/lib/interfacets/server/facets/schema/serializer.rb +0 -54
- data/lib/interfacets/server/facets/serializer.rb +0 -50
- data/lib/interfacets/shared/entity_collection.rb +0 -88
- data/lib/interfacets/test/browser.rb +0 -63
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Interfacets
|
|
4
|
+
module Shared
|
|
5
|
+
class View
|
|
6
|
+
class Evaluator
|
|
7
|
+
# rubocop:disable Style/ArgumentsForwarding
|
|
8
|
+
def self.call(**opts)
|
|
9
|
+
new(**opts).call
|
|
10
|
+
end
|
|
11
|
+
# rubocop:enable Style/ArgumentsForwarding
|
|
12
|
+
|
|
13
|
+
attr_reader(
|
|
14
|
+
:entity,
|
|
15
|
+
:channels,
|
|
16
|
+
:block,
|
|
17
|
+
:props,
|
|
18
|
+
:template_block,
|
|
19
|
+
:data
|
|
20
|
+
)
|
|
21
|
+
def initialize(entity:, channels:, block:, props: {}, template_block: nil)
|
|
22
|
+
@entity = entity
|
|
23
|
+
@channels = channels
|
|
24
|
+
@block = block
|
|
25
|
+
@props = props
|
|
26
|
+
@template_block = template_block
|
|
27
|
+
@data = Hash.new { |h, k| h[k] = { streams: {} } }
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def call
|
|
31
|
+
entity.validate if entity.respond_to?(:validate)
|
|
32
|
+
instance_exec(entity, &block)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def render(target, **props, &block)
|
|
36
|
+
if target.is_a?(Class) && target.respond_to?(:render) && target < Interfacets::Client::Template
|
|
37
|
+
target.render(
|
|
38
|
+
self,
|
|
39
|
+
@current_builder,
|
|
40
|
+
**props,
|
|
41
|
+
&block
|
|
42
|
+
)
|
|
43
|
+
else
|
|
44
|
+
target
|
|
45
|
+
.class
|
|
46
|
+
.view
|
|
47
|
+
.render(
|
|
48
|
+
entity: target,
|
|
49
|
+
channels: channels,
|
|
50
|
+
props: props,
|
|
51
|
+
block: block
|
|
52
|
+
)
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# rubocop:disable Style/ArgumentsForwarding
|
|
57
|
+
def render_to(channel_id, stream: "default", slots: [], **_opts, &block)
|
|
58
|
+
channel_id = channel_id.to_s
|
|
59
|
+
|
|
60
|
+
builder = (
|
|
61
|
+
channels
|
|
62
|
+
.fetch(channel_id)
|
|
63
|
+
.builder(stream.to_s)
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
@current_builder = builder
|
|
67
|
+
|
|
68
|
+
if props.any? || template_block
|
|
69
|
+
slots_obj = Interfacets::Client::Template.create(
|
|
70
|
+
self,
|
|
71
|
+
builder,
|
|
72
|
+
slots: slots,
|
|
73
|
+
&template_block
|
|
74
|
+
)
|
|
75
|
+
instance_exec(
|
|
76
|
+
builder,
|
|
77
|
+
args: props, slots: slots_obj,
|
|
78
|
+
&block
|
|
79
|
+
)
|
|
80
|
+
else
|
|
81
|
+
instance_exec(builder, &block)
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
# rubocop:enable Style/ArgumentsForwarding
|
|
85
|
+
|
|
86
|
+
def channel(name, stream: "default", &block)
|
|
87
|
+
ch = channels.fetch(name.to_s)
|
|
88
|
+
target = ch.respond_to?(:builder) ? ch.builder(stream.to_s) : ch
|
|
89
|
+
instance_exec(target, &block) if block
|
|
90
|
+
target
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
class << self
|
|
95
|
+
def view(&block)
|
|
96
|
+
@view = block if block_given?
|
|
97
|
+
@view
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
# rubocop:disable Style/ArgumentsForwarding
|
|
101
|
+
def render(**opts)
|
|
102
|
+
new.render(**opts)
|
|
103
|
+
end
|
|
104
|
+
# rubocop:enable Style/ArgumentsForwarding
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def render(entity:, channels:, props: {}, block: nil)
|
|
108
|
+
return unless self.class.view
|
|
109
|
+
|
|
110
|
+
Evaluator.call(
|
|
111
|
+
entity: entity,
|
|
112
|
+
channels: channels,
|
|
113
|
+
block: self.class.view,
|
|
114
|
+
props: props,
|
|
115
|
+
template_block: block
|
|
116
|
+
)
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
end
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require("yaml")
|
|
4
|
+
|
|
5
|
+
module Interfacets
|
|
6
|
+
module Test
|
|
7
|
+
class ComponentRegistry
|
|
8
|
+
DEFAULT_CONFIG_PATH = "config/interfacets/components.yml"
|
|
9
|
+
|
|
10
|
+
def self.load(path = DEFAULT_CONFIG_PATH)
|
|
11
|
+
new(path)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
attr_reader(
|
|
15
|
+
:contracts,
|
|
16
|
+
:path,
|
|
17
|
+
:schema_cache
|
|
18
|
+
)
|
|
19
|
+
def initialize(path)
|
|
20
|
+
@path = path
|
|
21
|
+
@schema_cache = {}
|
|
22
|
+
@contracts = load_contracts
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def find_component(name)
|
|
26
|
+
contracts[name.to_s]
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def definitions
|
|
30
|
+
contracts["definitions"] || {}
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
private
|
|
34
|
+
|
|
35
|
+
def load_contracts
|
|
36
|
+
return {} unless File.exist?(path)
|
|
37
|
+
|
|
38
|
+
contracts = YAML.load_file(path, aliases: true) || {}
|
|
39
|
+
contracts.each do |name, config|
|
|
40
|
+
next unless config.is_a?(Hash)
|
|
41
|
+
|
|
42
|
+
schema = config["schema"]
|
|
43
|
+
if schema.is_a?(String)
|
|
44
|
+
config["schema"] = load_schema_file(name, schema)
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
contracts
|
|
48
|
+
rescue Psych::SyntaxError => e
|
|
49
|
+
raise(
|
|
50
|
+
Error.new(
|
|
51
|
+
"Failed to parse component registry at #{path}: #{e.message}"
|
|
52
|
+
)
|
|
53
|
+
)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def load_schema_file(component_name, schema_path)
|
|
57
|
+
full_path = File.expand_path(
|
|
58
|
+
schema_path,
|
|
59
|
+
File.dirname(path)
|
|
60
|
+
)
|
|
61
|
+
return schema_cache[full_path] if schema_cache.key?(full_path)
|
|
62
|
+
|
|
63
|
+
raise(
|
|
64
|
+
Errno::ENOENT.new(
|
|
65
|
+
"Schema file not found for component '#{component_name}' at #{full_path}"
|
|
66
|
+
)
|
|
67
|
+
) unless File.exist?(full_path)
|
|
68
|
+
|
|
69
|
+
begin
|
|
70
|
+
@schema_cache[full_path] = YAML.load_file(full_path, aliases: true) || {}
|
|
71
|
+
rescue Psych::SyntaxError => e
|
|
72
|
+
raise(
|
|
73
|
+
Error.new(
|
|
74
|
+
"Failed to parse schema file for component '#{component_name}' " \
|
|
75
|
+
"at #{full_path}: #{e.message}"
|
|
76
|
+
)
|
|
77
|
+
)
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
@@ -1,10 +1,15 @@
|
|
|
1
|
-
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require("nodo")
|
|
2
4
|
|
|
3
5
|
module Interfacets
|
|
4
6
|
module Test
|
|
5
7
|
module Js
|
|
6
8
|
class InlineBus
|
|
7
|
-
attr_reader
|
|
9
|
+
attr_reader(
|
|
10
|
+
:receiver_index,
|
|
11
|
+
:client_system_json
|
|
12
|
+
)
|
|
8
13
|
def initialize(receiver_index:, client_system_json:)
|
|
9
14
|
@receiver_index = receiver_index
|
|
10
15
|
@client_system_json = client_system_json
|
|
@@ -15,19 +20,32 @@ module Interfacets
|
|
|
15
20
|
end
|
|
16
21
|
|
|
17
22
|
def init(hydrated_facet:)
|
|
18
|
-
client.handle(
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
23
|
+
client.handle(
|
|
24
|
+
H.j(
|
|
25
|
+
{
|
|
26
|
+
type: "interfacets:system:create_bus",
|
|
27
|
+
payload: {
|
|
28
|
+
id: "default",
|
|
29
|
+
channel_ids: ["interfacets:api", "dom", "url", "timer", "pubsub",
|
|
30
|
+
"audio"],
|
|
31
|
+
hydration: {
|
|
32
|
+
destination: { bus: "default", channel: "interfacets:api" },
|
|
33
|
+
type: "interfacets:api:hydrate",
|
|
34
|
+
payload: hydrated_facet
|
|
35
|
+
},
|
|
36
|
+
config: client_system_json,
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
)
|
|
40
|
+
)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def dispatch(channel_id, event)
|
|
44
|
+
dispatch_to_bus(
|
|
45
|
+
bus_id: "default",
|
|
46
|
+
channel_id: channel_id,
|
|
47
|
+
event: event
|
|
48
|
+
)
|
|
31
49
|
end
|
|
32
50
|
|
|
33
51
|
private
|
|
@@ -40,21 +58,43 @@ module Interfacets
|
|
|
40
58
|
|
|
41
59
|
bus_event.fetch("payload").each do |channel_event|
|
|
42
60
|
channel_id = channel_event.fetch("id")
|
|
43
|
-
receiver_index
|
|
44
|
-
|
|
61
|
+
receiver = receiver_index.fetch(channel_id)
|
|
62
|
+
|
|
63
|
+
receiver
|
|
45
64
|
.receive(
|
|
46
65
|
payload: channel_event.fetch("payload"),
|
|
47
66
|
dispatch: ->(ev) {
|
|
48
|
-
|
|
67
|
+
dispatch_to_bus(
|
|
68
|
+
bus_id:,
|
|
69
|
+
channel_id:,
|
|
70
|
+
event: ev
|
|
71
|
+
)
|
|
49
72
|
},
|
|
50
73
|
)
|
|
51
74
|
end
|
|
52
75
|
else
|
|
53
|
-
raise(
|
|
76
|
+
raise(
|
|
77
|
+
"unhandled event type: #{event}"
|
|
78
|
+
)
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# Flush any responses that may have come from the server
|
|
82
|
+
# should separate this out so that auto-flush or manual-flush
|
|
83
|
+
# is possible to simulate ordering events as needed.
|
|
84
|
+
receiver_index.each do |channel_id, receiver|
|
|
85
|
+
if receiver.respond_to?(:response_queue)
|
|
86
|
+
receiver.flush_responses.each do |response|
|
|
87
|
+
dispatch_to_bus(
|
|
88
|
+
bus_id:,
|
|
89
|
+
channel_id:,
|
|
90
|
+
event: response
|
|
91
|
+
)
|
|
92
|
+
end
|
|
93
|
+
end
|
|
54
94
|
end
|
|
55
95
|
end
|
|
56
96
|
|
|
57
|
-
def
|
|
97
|
+
def dispatch_to_bus(bus_id:, channel_id:, event:)
|
|
58
98
|
client.handle(
|
|
59
99
|
H.j(
|
|
60
100
|
{
|
|
@@ -69,20 +109,15 @@ module Interfacets
|
|
|
69
109
|
def client
|
|
70
110
|
@client ||= (
|
|
71
111
|
$asset_logger = Logger.new("/dev/null")
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
begin
|
|
75
|
-
Client::Assets.bootstrap(client_system_json.fetch("assets"))
|
|
76
|
-
ensure
|
|
77
|
-
$VERBOSE = original_verbose
|
|
78
|
-
end
|
|
79
|
-
Client::System.logger = $asset_logger
|
|
112
|
+
Client::System
|
|
113
|
+
.logger = $asset_logger
|
|
80
114
|
|
|
81
|
-
Client
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
115
|
+
Client
|
|
116
|
+
.start(
|
|
117
|
+
transmit: ->(event) {
|
|
118
|
+
handle(H.j(event))
|
|
119
|
+
},
|
|
120
|
+
)
|
|
86
121
|
)
|
|
87
122
|
end
|
|
88
123
|
end
|
|
@@ -1,10 +1,15 @@
|
|
|
1
|
-
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require("nodo")
|
|
2
4
|
|
|
3
5
|
module Interfacets
|
|
4
6
|
module Test
|
|
5
7
|
module Js
|
|
6
8
|
class NodoBus < Nodo::Core
|
|
7
|
-
attr_reader
|
|
9
|
+
attr_reader(
|
|
10
|
+
:receiver_index,
|
|
11
|
+
:client_system_json
|
|
12
|
+
)
|
|
8
13
|
def initialize(receiver_index:, client_system_json:)
|
|
9
14
|
super()
|
|
10
15
|
@receiver_index = receiver_index
|
|
@@ -12,6 +17,7 @@ module Interfacets
|
|
|
12
17
|
end
|
|
13
18
|
|
|
14
19
|
def dispatch(channel_id, event)
|
|
20
|
+
receiver_index.fetch(channel_id) # unknown channels raise KeyError, as on the inline bus
|
|
15
21
|
js_dispatch(channel_id, event)
|
|
16
22
|
render
|
|
17
23
|
end
|
|
@@ -30,17 +36,39 @@ module Interfacets
|
|
|
30
36
|
|
|
31
37
|
def render
|
|
32
38
|
receiver_index.each do |id, ch|
|
|
39
|
+
state = js_get_state(id)
|
|
40
|
+
next if state.nil?
|
|
41
|
+
|
|
33
42
|
ch.receive(
|
|
34
|
-
payload:
|
|
43
|
+
payload: state.fetch("data"),
|
|
35
44
|
dispatch: ->(e) { dispatch(id, e) }
|
|
36
45
|
)
|
|
46
|
+
|
|
47
|
+
receiver_index.each do |id, ch|
|
|
48
|
+
if ch.respond_to?(:response_queue)
|
|
49
|
+
ch.flush_responses.each do |response|
|
|
50
|
+
dispatch(id, response)
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
if js_get_logs.any? { _1.fetch("type") == "error" }
|
|
57
|
+
js_get_logs
|
|
58
|
+
.map { _1.fetch("value") }
|
|
59
|
+
.then { raise(_1.join("\n")) }
|
|
37
60
|
end
|
|
38
61
|
end
|
|
39
62
|
|
|
40
|
-
import
|
|
41
|
-
|
|
63
|
+
import(
|
|
64
|
+
app: File.join(
|
|
65
|
+
__dir__,
|
|
66
|
+
"../../../../test/wasm/app.mjs"
|
|
67
|
+
)
|
|
68
|
+
)
|
|
69
|
+
import(:fs)
|
|
42
70
|
|
|
43
|
-
function
|
|
71
|
+
function(:js_init, <<~JS)
|
|
44
72
|
async (config) => {
|
|
45
73
|
global.logs = []
|
|
46
74
|
global.console = {
|
|
@@ -62,17 +90,17 @@ module Interfacets
|
|
|
62
90
|
}
|
|
63
91
|
JS
|
|
64
92
|
|
|
65
|
-
function
|
|
93
|
+
function(:js_dispatch, <<~JS)
|
|
66
94
|
(channelName, event) => {
|
|
67
95
|
app.dispatch(channelName, event)
|
|
68
96
|
}
|
|
69
97
|
JS
|
|
70
98
|
|
|
71
|
-
function
|
|
99
|
+
function(:js_get_logs, <<~JS)
|
|
72
100
|
() => global.logs
|
|
73
101
|
JS
|
|
74
102
|
|
|
75
|
-
function
|
|
103
|
+
function(:js_get_state, <<~JS)
|
|
76
104
|
(channelName) => app.getState(channelName)
|
|
77
105
|
JS
|
|
78
106
|
end
|
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
# require_relative "./channels/react"
|
|
4
|
-
|
|
5
3
|
module Interfacets
|
|
6
4
|
module Test
|
|
7
5
|
module Js
|
|
8
6
|
module Receivers
|
|
9
7
|
class Api
|
|
10
|
-
attr_reader
|
|
8
|
+
attr_reader(
|
|
9
|
+
:router,
|
|
10
|
+
:name,
|
|
11
|
+
:response_queue
|
|
12
|
+
)
|
|
11
13
|
def initialize(name:, router:)
|
|
12
14
|
@name = name
|
|
13
15
|
@router = router
|
|
@@ -26,10 +28,21 @@ module Interfacets
|
|
|
26
28
|
return if payload.dig("streams", "default").empty?
|
|
27
29
|
|
|
28
30
|
url = payload.dig("streams", "default", "url")
|
|
31
|
+
|
|
29
32
|
payload
|
|
30
|
-
.dig(
|
|
33
|
+
.dig(
|
|
34
|
+
"streams",
|
|
35
|
+
"default",
|
|
36
|
+
"body",
|
|
37
|
+
"event",
|
|
38
|
+
"payload"
|
|
39
|
+
)
|
|
31
40
|
.then { response_queue << router.call(url).handle(_1) }
|
|
32
41
|
end
|
|
42
|
+
|
|
43
|
+
def flush_responses
|
|
44
|
+
response_queue.tap { @response_queue = [] }
|
|
45
|
+
end
|
|
33
46
|
end
|
|
34
47
|
end
|
|
35
48
|
end
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Interfacets
|
|
4
|
+
module Test
|
|
5
|
+
module Js
|
|
6
|
+
module Receivers
|
|
7
|
+
# Simulates the browser audio channel: records the events a facet asks for (play, load,
|
|
8
|
+
# request-mic, start-record, stop-record) and lets a test answer them the way the browser
|
|
9
|
+
# would.
|
|
10
|
+
class Audio
|
|
11
|
+
include(Enumerable)
|
|
12
|
+
|
|
13
|
+
attr_reader(:name, :events)
|
|
14
|
+
def initialize(name:)
|
|
15
|
+
@name = name
|
|
16
|
+
@events = []
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def receive(payload:, dispatch:)
|
|
20
|
+
@dispatch = dispatch
|
|
21
|
+
|
|
22
|
+
event = payload&.dig("streams", "default")
|
|
23
|
+
return if event.nil? || event.empty?
|
|
24
|
+
|
|
25
|
+
events << event
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def handler
|
|
29
|
+
self
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def each(&)
|
|
33
|
+
events.each(&)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def first
|
|
37
|
+
events.first
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def last
|
|
41
|
+
events.last
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def clear
|
|
45
|
+
@events = []
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# Answers a request like the browser does, e.g. `audio.respond_to(audio.last, "loaded")`:
|
|
49
|
+
# sends an event of `type` carrying the request's id (and any extra payload) back to the
|
|
50
|
+
# facet.
|
|
51
|
+
def respond_to(request, type, payload = {})
|
|
52
|
+
@dispatch.call(
|
|
53
|
+
{ "type" => type,
|
|
54
|
+
"payload" => { "id" => request.fetch("id") }.merge(payload) }
|
|
55
|
+
)
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Interfacets
|
|
4
|
+
module Test
|
|
5
|
+
module Js
|
|
6
|
+
module Receivers
|
|
7
|
+
class Pubsub
|
|
8
|
+
attr_reader(:name, :topics)
|
|
9
|
+
def initialize(name:)
|
|
10
|
+
@name = name
|
|
11
|
+
@topics = []
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
alias_method(:streams, :topics)
|
|
15
|
+
alias_method(:listening, :topics)
|
|
16
|
+
|
|
17
|
+
def receive(payload:, dispatch:)
|
|
18
|
+
@dispatch = dispatch
|
|
19
|
+
|
|
20
|
+
if payload.nil? || payload.empty? || payload.dig("streams", "default").nil?
|
|
21
|
+
@topics = []
|
|
22
|
+
return
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
listen = payload.dig("streams", "default", "listen")
|
|
26
|
+
@topics = Array(listen).map(&:to_s)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def handler
|
|
30
|
+
self
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def listening?(topic)
|
|
34
|
+
topics.include?(topic.to_s)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def notify(topic, data = {}) # rubocop:disable Naming/PredicateMethod
|
|
38
|
+
return false unless listening?(topic)
|
|
39
|
+
return false unless @dispatch
|
|
40
|
+
|
|
41
|
+
@dispatch.call(
|
|
42
|
+
{ "topic" => topic.to_s, "data" => data }
|
|
43
|
+
)
|
|
44
|
+
true
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require("nokogiri")
|
|
4
|
+
|
|
5
|
+
module Interfacets
|
|
6
|
+
module Test
|
|
7
|
+
module Js
|
|
8
|
+
module Receivers
|
|
9
|
+
class React
|
|
10
|
+
class Node
|
|
11
|
+
class XmlParser
|
|
12
|
+
attr_reader(:json, :validation_engine)
|
|
13
|
+
def initialize(json, validation_engine:)
|
|
14
|
+
@json = json
|
|
15
|
+
@validation_engine = validation_engine
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def self.element_node?(value)
|
|
19
|
+
value.is_a?(Hash) && ["interfacets:react-dom:element",
|
|
20
|
+
"interfacets:string-node"].include?(value["type"])
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# Builds the XML node for one raw element/string node (a captured prop value).
|
|
24
|
+
def self.parse_node(raw)
|
|
25
|
+
parser = new(nil, validation_engine: nil)
|
|
26
|
+
result = parser.send(:parse_element, raw)
|
|
27
|
+
result.is_a?(String) ? Nokogiri::XML::Document.new.create_text_node(result) : result
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def call
|
|
31
|
+
xml = Nokogiri::XML.fragment("<Facet/>")
|
|
32
|
+
json
|
|
33
|
+
.dig("streams", "default", "dom")
|
|
34
|
+
.map { parse_element(_1) }
|
|
35
|
+
.each { xml.add_child(_1) }
|
|
36
|
+
xml
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
private
|
|
40
|
+
|
|
41
|
+
def parse_attribute(value, top: true)
|
|
42
|
+
case value
|
|
43
|
+
when Array
|
|
44
|
+
value
|
|
45
|
+
.map { parse_attribute(_1, top: false) }
|
|
46
|
+
.then { top ? _1.to_json : _1 }
|
|
47
|
+
when Hash
|
|
48
|
+
value
|
|
49
|
+
.transform_values { parse_attribute(_1, top: false) }
|
|
50
|
+
# we don't want to call to_json on nested hashes
|
|
51
|
+
.then { top ? _1.to_json : _1 }
|
|
52
|
+
when Numeric, String, TrueClass, FalseClass, NilClass
|
|
53
|
+
value
|
|
54
|
+
else
|
|
55
|
+
raise(
|
|
56
|
+
"unknown attribute type: #{value}"
|
|
57
|
+
)
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def parse_element(el)
|
|
62
|
+
case el.fetch("type")
|
|
63
|
+
when "interfacets:string-node"
|
|
64
|
+
el.dig("attributes", "value").to_s
|
|
65
|
+
when "interfacets:react-dom:element"
|
|
66
|
+
xml = (
|
|
67
|
+
el
|
|
68
|
+
.fetch("element")
|
|
69
|
+
.tap { validation_engine&.validate_props(_1, el.fetch("attributes")) }
|
|
70
|
+
.then {
|
|
71
|
+
Nokogiri::XML
|
|
72
|
+
.fragment("<#{_1} />")
|
|
73
|
+
.children
|
|
74
|
+
.first
|
|
75
|
+
}
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
el.fetch("children").each do |child|
|
|
79
|
+
xml.add_child(parse_element(child)) if child
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# Captured elements stay in their attribute as raw node JSON; Node#attribute
|
|
83
|
+
# turns them back into Nodes. The simulator cannot know where a component
|
|
84
|
+
# would render them.
|
|
85
|
+
el.fetch("attributes").each do |name, value|
|
|
86
|
+
xml.set_attribute(name, parse_attribute(value))
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
xml
|
|
90
|
+
else
|
|
91
|
+
raise(
|
|
92
|
+
"unknown type: #{el.fetch("type")}"
|
|
93
|
+
)
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
end
|