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.
Files changed (90) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +9 -17
  3. data/lib/interfacets/client/actor.rb +2 -1
  4. data/lib/interfacets/client/assets.rb +77 -63
  5. data/lib/interfacets/client/bus.rb +31 -8
  6. data/lib/interfacets/client/channels/api.rb +141 -35
  7. data/lib/interfacets/client/channels/audio.rb +13 -10
  8. data/lib/interfacets/client/channels/base.rb +2 -2
  9. data/lib/interfacets/client/channels/pubsub.rb +69 -0
  10. data/lib/interfacets/client/channels/react/builder.rb +42 -72
  11. data/lib/interfacets/client/channels/react/channel.rb +7 -5
  12. data/lib/interfacets/client/channels/react/evaluator.rb +6 -4
  13. data/lib/interfacets/client/channels/timer.rb +8 -7
  14. data/lib/interfacets/client/channels/url.rb +39 -6
  15. data/lib/interfacets/client/registry.rb +10 -70
  16. data/lib/interfacets/client/slots.rb +45 -0
  17. data/lib/interfacets/client/system.rb +20 -15
  18. data/lib/interfacets/client/template.rb +90 -0
  19. data/lib/interfacets/client/utils/mruby_patches.rb +8 -4
  20. data/lib/interfacets/client/utils/open_struct.rb +88 -77
  21. data/lib/interfacets/component_registry.rb +120 -0
  22. data/lib/interfacets/component_schema_parser.rb +88 -0
  23. data/lib/interfacets/mruby/build_config.rb +8 -4
  24. data/lib/interfacets/mruby/entrypoint.rb +2 -2
  25. data/lib/interfacets/server/api.rb +44 -27
  26. data/lib/interfacets/server/assets.rb +69 -28
  27. data/lib/interfacets/server/basic_router.rb +38 -23
  28. data/lib/interfacets/server/bus.rb +8 -14
  29. data/lib/interfacets/server/registry.rb +31 -185
  30. data/lib/interfacets/shared/basic_routable.rb +48 -0
  31. data/lib/interfacets/shared/entities/bus.rb +623 -177
  32. data/lib/interfacets/shared/entities/changeset.rb +432 -0
  33. data/lib/interfacets/shared/entities/collection_proxy.rb +132 -72
  34. data/lib/interfacets/shared/entities/specs/handlers.rb +74 -37
  35. data/lib/interfacets/shared/entities/specs.rb +223 -20
  36. data/lib/interfacets/shared/entity.rb +124 -110
  37. data/lib/interfacets/shared/entity_dsl.rb +272 -0
  38. data/lib/interfacets/shared/facet.rb +260 -0
  39. data/lib/interfacets/shared/generated_store.rb +74 -35
  40. data/lib/interfacets/shared/refreshable.rb +85 -0
  41. data/lib/interfacets/shared/utils.rb +36 -32
  42. data/lib/interfacets/shared/validations.rb +749 -25
  43. data/lib/interfacets/shared/view.rb +120 -0
  44. data/lib/interfacets/test/component_registry.rb +82 -0
  45. data/lib/interfacets/test/js/inline_bus.rb +68 -33
  46. data/lib/interfacets/test/js/nodo_bus.rb +37 -9
  47. data/lib/interfacets/test/js/receivers/api.rb +17 -4
  48. data/lib/interfacets/test/js/receivers/audio.rb +61 -0
  49. data/lib/interfacets/test/js/receivers/pubsub.rb +50 -0
  50. data/lib/interfacets/test/js/receivers/react/node/xml_parser.rb +103 -0
  51. data/lib/interfacets/test/js/receivers/react/node.rb +91 -71
  52. data/lib/interfacets/test/js/receivers/react.rb +15 -5
  53. data/lib/interfacets/test/js/receivers/timer.rb +81 -0
  54. data/lib/interfacets/test/js/receivers/url.rb +51 -10
  55. data/lib/interfacets/test/standard_elements.yml +173 -0
  56. data/lib/interfacets/test/ui_simulator.rb +145 -0
  57. data/lib/interfacets/test/validation_engine.rb +184 -0
  58. data/lib/interfacets/test.rb +1 -7
  59. data/lib/interfacets/version.rb +1 -1
  60. data/lib/interfacets.rb +13 -2
  61. metadata +51 -37
  62. data/.rubocop.yml +0 -180
  63. data/LICENSE.txt +0 -21
  64. data/Rakefile +0 -10
  65. data/lib/interfacets/client/channels/page_visibility.rb +0 -21
  66. data/lib/interfacets/client/channels/react/dom.rb +0 -91
  67. data/lib/interfacets/client/channels/speech_to_text.rb +0 -100
  68. data/lib/interfacets/client/config.rb +0 -22
  69. data/lib/interfacets/client/delegator.rb +0 -37
  70. data/lib/interfacets/client/facet.rb +0 -26
  71. data/lib/interfacets/client/facet2.rb +0 -15
  72. data/lib/interfacets/client/facets/attributes/accessor.rb +0 -28
  73. data/lib/interfacets/client/facets/attributes/association.rb +0 -50
  74. data/lib/interfacets/client/facets/attributes/bind.rb +0 -25
  75. data/lib/interfacets/client/facets/attributes/collection.rb +0 -47
  76. data/lib/interfacets/client/facets/attributes/readonly.rb +0 -19
  77. data/lib/interfacets/client/facets/deserializer.rb +0 -30
  78. data/lib/interfacets/client/facets/schema/deserializer.rb +0 -63
  79. data/lib/interfacets/client/facets/schema.rb +0 -63
  80. data/lib/interfacets/client/facets/serializer.rb +0 -18
  81. data/lib/interfacets/client/view.rb +0 -47
  82. data/lib/interfacets/server/assets/facet.rb +0 -61
  83. data/lib/interfacets/server/basic_routable.rb +0 -40
  84. data/lib/interfacets/server/config.rb +0 -87
  85. data/lib/interfacets/server/facet.rb +0 -51
  86. data/lib/interfacets/server/facets/deserializer.rb +0 -25
  87. data/lib/interfacets/server/facets/schema/serializer.rb +0 -54
  88. data/lib/interfacets/server/facets/serializer.rb +0 -50
  89. data/lib/interfacets/shared/entity_collection.rb +0 -88
  90. data/lib/interfacets/test/browser.rb +0 -63
@@ -0,0 +1,432 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Interfacets
4
+ module Shared
5
+ module Entities
6
+ # A [from, to] pair that also answers to :from/:to (and "from"/"to").
7
+ # It is a real Array (not a Hash with #to_ary) so that `from, to = entry`
8
+ # destructures under mruby, whose multiple assignment never calls #to_ary.
9
+ class DiffEntry < Array
10
+ def self.between(from, to)
11
+ new.push(from, to)
12
+ end
13
+
14
+ def [](key)
15
+ case key
16
+ when :from, "from", 0
17
+ super(0)
18
+ when :to, "to", 1
19
+ super(1)
20
+ end
21
+ end
22
+
23
+ def to_hash
24
+ { from: self[0], to: self[1] }
25
+ end
26
+
27
+ def ==(other)
28
+ if other.is_a?(Hash)
29
+ to_hash == other
30
+ else
31
+ super
32
+ end
33
+ end
34
+ end
35
+
36
+ class DiffHash < Hash
37
+ def self.from(hash)
38
+ new.tap do |diff|
39
+ hash.each do |k, v|
40
+ diff[k.to_s] = v
41
+ end
42
+ end
43
+ end
44
+
45
+ # Must not route through #fetch: under mruby, Hash#fetch is patched
46
+ # (MrubyPatches::HashExt) to call self[], which would recurse forever.
47
+ def [](key)
48
+ super(key.to_s)
49
+ end
50
+
51
+ # rubocop:disable Style/ArgumentsForwarding, Naming/BlockForwarding
52
+ def fetch(key, *args, &block)
53
+ super(key.to_s, *args, &block)
54
+ end
55
+ # rubocop:enable Style/ArgumentsForwarding, Naming/BlockForwarding
56
+
57
+ def key?(key)
58
+ super(key.to_s)
59
+ end
60
+ alias_method(:include?, :key?)
61
+ alias_method(:member?, :key?)
62
+ alias_method(:has_key?, :key?)
63
+ end
64
+
65
+ class AttributeList < Array
66
+ def include?(item)
67
+ return false if item.nil?
68
+
69
+ (item.respond_to?(:to_sym) && super(item.to_sym)) ||
70
+ (item.respond_to?(:to_s) && super(item.to_s))
71
+ end
72
+ end
73
+
74
+ class CollectionChangeset
75
+ attr_reader(
76
+ :entity,
77
+ :name,
78
+ :baseline,
79
+ :proxy
80
+ )
81
+ def initialize(entity:, name:, baseline:, proxy: nil)
82
+ @entity = entity
83
+ @name = name&.to_s
84
+ @baseline = baseline ? baseline.dup : []
85
+ @proxy = proxy
86
+ end
87
+
88
+ def current
89
+ if entity && name
90
+ entity
91
+ .association(name)
92
+ .get
93
+ .to_a
94
+ elsif proxy
95
+ proxy.to_a
96
+ else
97
+ raise(
98
+ ArgumentError.new(
99
+ "CollectionChangeset needs either entity: and name:, or proxy:"
100
+ )
101
+ )
102
+ end
103
+ end
104
+
105
+ def added
106
+ baseline_ids = baseline.to_h { [entity_identifier(_1), true] }
107
+ current.reject { baseline_ids.key?(entity_identifier(_1)) }
108
+ end
109
+
110
+ def removed
111
+ current_ids = current.to_h { [entity_identifier(_1), true] }
112
+ baseline.reject { current_ids.key?(entity_identifier(_1)) }
113
+ end
114
+
115
+ def modified
116
+ baseline_ids = baseline.to_h { [entity_identifier(_1), true] }
117
+ current.select do |item|
118
+ baseline_ids.key?(entity_identifier(item)) &&
119
+ (item.respond_to?(:changed?) ? item.changed? : false)
120
+ end
121
+ end
122
+
123
+ def any?
124
+ added.any? || removed.any? || modified.any? || order_changed?
125
+ end
126
+ alias_method(:changed?, :any?)
127
+
128
+ def empty?
129
+ !any?
130
+ end
131
+
132
+ def order_changed?
133
+ current.map { entity_identifier(_1) } != baseline.map { entity_identifier(_1) }
134
+ end
135
+
136
+ def revert!
137
+ target_proxy = (
138
+ if entity && name
139
+ entity.association(name).get
140
+ else
141
+ proxy
142
+ end
143
+ )
144
+
145
+ target_proxy&.replace(baseline)
146
+
147
+ if entity && name && entity.respond_to?("#{name}=")
148
+ entity.send("#{name}=", baseline.dup)
149
+ end
150
+
151
+ baseline.each do |item|
152
+ item.changeset.revert! if item.respond_to?(:changeset)
153
+ end
154
+
155
+ self
156
+ end
157
+
158
+ private
159
+
160
+ def entity_identifier(item)
161
+ if item.respond_to?(:internal_entity_id)
162
+ item.internal_entity_id
163
+ else
164
+ item.object_id
165
+ end
166
+ end
167
+ end
168
+
169
+ class Changeset
170
+ def self.for(entity)
171
+ new(entity).tap(&:checkpoint!)
172
+ end
173
+
174
+ attr_reader(
175
+ :entity,
176
+ :baseline_accessors,
177
+ :baseline_references,
178
+ :baseline_collections
179
+ )
180
+ def initialize(entity)
181
+ @entity = entity
182
+ @baseline_accessors = {}
183
+ @baseline_references = {}
184
+ @baseline_collections = {}
185
+ end
186
+
187
+ def checkpoint!(visited = {})
188
+ return self if visited[self]
189
+
190
+ visited[self] = true
191
+
192
+ @baseline_accessors = {}
193
+ @baseline_references = {}
194
+ @baseline_collections = {}
195
+
196
+ entity
197
+ .class
198
+ .accessors
199
+ .each_key do |name|
200
+ val = entity.respond_to?(name) ? entity.send(name) : nil
201
+ baseline_accessors[name.to_s] = Utils.deep_dup(val)
202
+ end
203
+
204
+ entity
205
+ .class
206
+ .associations
207
+ .each do |name, spec|
208
+ name_str = name.to_s
209
+ if spec.type == :reference
210
+ child = entity.association(name_str).get
211
+ baseline_references[name_str] = child
212
+ child.changeset.checkpoint!(visited) if child.respond_to?(:changeset)
213
+ elsif spec.type == :collection
214
+ items = (
215
+ entity
216
+ .association(name_str)
217
+ .get
218
+ .to_a
219
+ )
220
+ baseline_collections[name_str] = items.dup
221
+ items.each do |item|
222
+ item.changeset.checkpoint!(visited) if item.respond_to?(:changeset)
223
+ end
224
+ end
225
+ end
226
+
227
+ self
228
+ end
229
+
230
+ def checkpoint_accessor!(name, value)
231
+ baseline_accessors[name.to_s] = Utils.deep_dup(value)
232
+ end
233
+
234
+ def any?
235
+ changed?
236
+ end
237
+
238
+ def empty?
239
+ !any?
240
+ end
241
+
242
+ def changed?(name = nil)
243
+ if name.nil?
244
+ entity
245
+ .class
246
+ .accessors
247
+ .each_key
248
+ .any? { accessor_changed?(_1) } ||
249
+ entity
250
+ .class
251
+ .associations
252
+ .values
253
+ .any? { association_changed?(_1.name) }
254
+ else
255
+ name_str = name.to_s
256
+ if entity
257
+ .class
258
+ .accessors
259
+ .key?(name_str)
260
+ accessor_changed?(name_str)
261
+ elsif entity
262
+ .class
263
+ .associations
264
+ .key?(name_str)
265
+ association_changed?(name_str)
266
+ else
267
+ false
268
+ end
269
+ end
270
+ end
271
+
272
+ def original(name)
273
+ name_str = name.to_s
274
+ if entity
275
+ .class
276
+ .accessors
277
+ .key?(name_str)
278
+ Utils.deep_dup(baseline_accessors[name_str])
279
+ elsif entity
280
+ .class
281
+ .associations
282
+ .key?(name_str)
283
+ spec = entity.class.associations[name_str]
284
+ if spec.type == :reference
285
+ baseline_references[name_str]
286
+ elsif spec.type == :collection
287
+ (baseline_collections[name_str] || []).dup
288
+ end
289
+ end
290
+ end
291
+ alias_method(:server_value, :original)
292
+
293
+ def attributes
294
+ changed_keys = (
295
+ entity
296
+ .class
297
+ .accessors
298
+ .keys
299
+ .select { accessor_changed?(_1) }
300
+ .map(&:to_sym)
301
+ )
302
+ AttributeList.new(changed_keys)
303
+ end
304
+
305
+ def diff
306
+ diff_hash = {}
307
+ entity
308
+ .class
309
+ .accessors
310
+ .each_key do |acc|
311
+ next unless accessor_changed?(acc)
312
+
313
+ old_val = Utils.deep_dup(baseline_accessors[acc.to_s])
314
+ new_val = entity.send(acc)
315
+ diff_hash[acc.to_s] = DiffEntry.between(old_val, new_val)
316
+ end
317
+ DiffHash.from(diff_hash)
318
+ end
319
+
320
+ def revert!(name = nil)
321
+ if name.nil?
322
+ entity
323
+ .class
324
+ .accessors
325
+ .each_key do |acc|
326
+ revert_accessor(acc)
327
+ end
328
+
329
+ entity
330
+ .class
331
+ .associations
332
+ .each do |assoc_name, spec|
333
+ if spec.type == :reference
334
+ revert_reference(assoc_name)
335
+ elsif spec.type == :collection
336
+ collection_changeset(assoc_name).revert!
337
+ end
338
+ end
339
+ else
340
+ name_str = name.to_s
341
+ if entity
342
+ .class
343
+ .accessors
344
+ .key?(name_str)
345
+ revert_accessor(name_str)
346
+ elsif entity
347
+ .class
348
+ .associations
349
+ .key?(name_str)
350
+ spec = entity.class.associations[name_str]
351
+ if spec.type == :reference
352
+ revert_reference(name_str)
353
+ elsif spec.type == :collection
354
+ collection_changeset(name_str).revert!
355
+ end
356
+ end
357
+ end
358
+
359
+ self
360
+ end
361
+
362
+ def collection_changeset(name)
363
+ name_str = name.to_s
364
+ CollectionChangeset.new(
365
+ entity: entity,
366
+ name: name_str,
367
+ baseline: baseline_collections[name_str] || [],
368
+ )
369
+ end
370
+
371
+ private
372
+
373
+ def accessor_changed?(name)
374
+ name_str = name.to_s
375
+ return false unless baseline_accessors.key?(name_str)
376
+
377
+ baseline_val = baseline_accessors[name_str]
378
+ current_val = entity.send(name_str)
379
+ baseline_val != current_val
380
+ end
381
+
382
+ def association_changed?(name)
383
+ name_str = name.to_s
384
+ spec = entity.class.associations[name_str]
385
+ return false unless spec
386
+
387
+ if spec.type == :reference
388
+ reference_changed?(name_str)
389
+ elsif spec.type == :collection
390
+ collection_changeset(name_str).any?
391
+ else
392
+ false
393
+ end
394
+ end
395
+
396
+ def reference_changed?(name)
397
+ name_str = name.to_s
398
+ baseline_child = baseline_references[name_str]
399
+ current_child = entity.association(name_str).get
400
+
401
+ return false if baseline_child.nil? && current_child.nil?
402
+ return true if baseline_child.nil? || current_child.nil?
403
+
404
+ if baseline_child != current_child
405
+ true
406
+ elsif current_child.respond_to?(:changed?)
407
+ current_child.changed?
408
+ else
409
+ false
410
+ end
411
+ end
412
+
413
+ def revert_accessor(name)
414
+ name_str = name.to_s
415
+ return unless baseline_accessors.key?(name_str)
416
+
417
+ entity.send(
418
+ "#{name_str}=",
419
+ Utils.deep_dup(baseline_accessors[name_str])
420
+ )
421
+ end
422
+
423
+ def revert_reference(name)
424
+ name_str = name.to_s
425
+ baseline_child = baseline_references[name_str]
426
+ entity.send("#{name_str}=", baseline_child)
427
+ baseline_child&.changeset&.revert!
428
+ end
429
+ end
430
+ end
431
+ end
432
+ end