instant_record 0.1.0 → 0.3.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 +4 -4
- data/CHANGELOG.md +58 -0
- data/README.md +267 -61
- data/app/controllers/instant_record/bootstraps_controller.rb +39 -12
- data/app/controllers/instant_record/events_controller.rb +58 -6
- data/app/controllers/instant_record/files_controller.rb +116 -0
- data/app/controllers/instant_record/mutations_controller.rb +84 -5
- data/app/controllers/instant_record/native_screens_controller.rb +60 -0
- data/app/controllers/instant_record/records_controller.rb +86 -8
- data/app/controllers/instant_record/shells_controller.rb +64 -0
- data/app/controllers/instant_record/sync_notices_controller.rb +81 -0
- data/app/helpers/instant_record/sync_notices_helper.rb +41 -0
- data/app/jobs/instant_record/shell_build_job.rb +16 -0
- data/app/models/instant_record/change.rb +36 -3
- data/app/models/instant_record/mutation_applier.rb +633 -50
- data/app/views/instant_record/sync_notices/_notices.html.erb +63 -0
- data/compiled/README.md +272 -0
- data/compiled/pipeline/browser_migrations.rb +18 -0
- data/compiled/pipeline/build.rb +13 -0
- data/compiled/pipeline/build_native.rb +14 -0
- data/compiled/pipeline/native/host.c +78 -0
- data/compiled/pipeline/native/instant_record_native.h +57 -0
- data/compiled/pipeline/native_shim.c +149 -0
- data/compiled/pipeline/reactor_shim.c +66 -0
- data/compiled/pipeline/sqlite/rh_shim.c +5 -0
- data/compiled/pipeline/sqlite_release.rb +43 -0
- data/compiled/toolchain.env.example +22 -0
- data/compiled/web/index.html +20 -0
- data/compiled/web/package-lock.json +884 -0
- data/compiled/web/package.json +17 -0
- data/compiled/web/src/app-host-core.js +556 -0
- data/compiled/web/src/app-host.js +16 -0
- data/compiled/web/src/branch-manager.js +140 -0
- data/compiled/web/src/branch-preview.js +397 -0
- data/compiled/web/src/changes.js +191 -0
- data/compiled/web/src/faults.js +172 -0
- data/compiled/web/src/flight-recorder.js +347 -0
- data/compiled/web/src/history-fill.js +68 -0
- data/compiled/web/src/http-headers.js +100 -0
- data/compiled/web/src/leader-election.js +705 -0
- data/compiled/web/src/main.js +608 -0
- data/compiled/web/src/manifest.js +112 -0
- data/compiled/web/src/opfs-wasi-core.js +109 -0
- data/compiled/web/src/opfs-wasi-file.js +8 -0
- data/compiled/web/src/reactor-runtime.js +1692 -0
- data/compiled/web/src/redactor.js +518 -0
- data/compiled/web/src/release-updates.js +48 -0
- data/compiled/web/src/replay.js +338 -0
- data/compiled/web/src/schema-recovery.js +204 -0
- data/compiled/web/src/server-request.js +68 -0
- data/compiled/web/src/sha256.js +98 -0
- data/compiled/web/src/shell.js +52 -0
- data/compiled/web/src/snapshot-store.js +38 -0
- data/compiled/web/src/sqlite-redactor.js +111 -0
- data/compiled/web/src/storage-backend.js +159 -0
- data/compiled/web/src/stream-leader.js +675 -0
- data/compiled/web/src/sync-context.js +91 -0
- data/compiled/web/src/sync-drain.js +104 -0
- data/compiled/web/src/sync-schedule.js +9 -0
- data/compiled/web/src/wasi-minimal.js +275 -0
- data/compiled/web/src/worker.js +39 -0
- data/compiled/web/vite.config.js +27 -0
- data/config/routes.rb +25 -0
- data/db/migrate/20261119000201_add_scope_to_instant_record_changes.rb +7 -0
- data/db/migrate/20261121000001_create_instant_record_sync_notices.rb +21 -0
- data/db/migrate/20261121000101_add_partition_to_instant_record_changes.rb +8 -0
- data/db/migrate/20261121230300_add_delay_line_to_instant_record_outbox.rb +10 -0
- data/exe/instant_record_mcp +6 -0
- data/frontend/LiveList.js +67 -0
- data/frontend/chord.js +79 -0
- data/frontend/file.js +65 -0
- data/frontend/files.js +738 -0
- data/frontend/focus.js +10 -0
- data/frontend/held.js +192 -0
- data/frontend/http.js +76 -0
- data/frontend/index.js +53 -0
- data/frontend/keyboard.js +56 -0
- data/frontend/live.js +298 -0
- data/frontend/notices.js +118 -0
- data/frontend/package.json +15 -0
- data/frontend/range.js +10 -0
- data/frontend/selection.js +47 -0
- data/frontend/shortcuts.js +25 -0
- data/frontend/sync-notices.js +130 -0
- data/frontend/undo.js +48 -0
- data/frontend/window.js +181 -0
- data/lib/generators/instant_record/install/install_generator.rb +18 -30
- data/lib/generators/instant_record/install/templates/instant_record.rb +34 -0
- data/lib/generators/instant_record/ios/ios_generator.rb +71 -0
- data/lib/generators/instant_record/ios/templates/App.swift.tt +35 -0
- data/lib/generators/instant_record/ios/templates/Info.plist +11 -0
- data/lib/generators/instant_record/ios/templates/LibraryEngine.swift +30 -0
- data/lib/instant_record/branch/sqlite_store.rb +142 -0
- data/lib/instant_record/branch.rb +388 -0
- data/lib/instant_record/client/notifier.rb +8 -12
- data/lib/instant_record/client/rebaser.rb +565 -0
- data/lib/instant_record/client/transport.rb +64 -42
- data/lib/instant_record/client.rb +1600 -169
- data/lib/instant_record/compiled_site.rb +85 -0
- data/lib/instant_record/compiler/app_stage.rb +212 -0
- data/lib/instant_record/compiler/ast_lowerer.rb +659 -0
- data/lib/instant_record/compiler/browser_migrations.rb +161 -0
- data/lib/instant_record/compiler/entry_builder.rb +832 -0
- data/lib/instant_record/compiler/fts5_smoke.rb +205 -0
- data/lib/instant_record/compiler/gem_vendor.rb +194 -0
- data/lib/instant_record/compiler/host_adapters.rb +506 -0
- data/lib/instant_record/compiler/kotlin_codegen.rb +209 -0
- data/lib/instant_record/compiler/manifest.rb +178 -0
- data/lib/instant_record/compiler/native_build.rb +586 -0
- data/lib/instant_record/compiler/native_contracts.rb +338 -0
- data/lib/instant_record/compiler/path_scrubber.rb +31 -0
- data/lib/instant_record/compiler/reachability_tracer.rb +1188 -0
- data/lib/instant_record/compiler/route_analyzer.rb +623 -0
- data/lib/instant_record/compiler/runner.rb +42 -0
- data/lib/instant_record/compiler/schema_lock.rb +606 -0
- data/lib/instant_record/compiler/source_pages.rb +77 -0
- data/lib/instant_record/compiler/static_linter.rb +770 -0
- data/lib/instant_record/compiler/swift_codegen.rb +341 -0
- data/lib/instant_record/compiler/toolchain.rb +219 -0
- data/lib/instant_record/compiler/wasm_build.rb +704 -0
- data/lib/instant_record/compiler/wasm_tree.rb +518 -0
- data/lib/instant_record/compiler.rb +277 -0
- data/lib/instant_record/configuration.rb +104 -0
- data/lib/instant_record/engine.rb +68 -18
- data/lib/instant_record/file_store.rb +162 -0
- data/lib/instant_record/files.rb +92 -0
- data/lib/instant_record/hlc.rb +387 -0
- data/lib/instant_record/host.rb +66 -0
- data/lib/instant_record/inertia/renderer.rb +52 -0
- data/lib/instant_record/inertia.rb +222 -0
- data/lib/instant_record/mcp/cli.rb +319 -0
- data/lib/instant_record/mcp/host_process.rb +229 -0
- data/lib/instant_record/mcp/http_exchange.rb +233 -0
- data/lib/instant_record/mcp/replica.rb +556 -0
- data/lib/instant_record/mcp/runtime.rb +406 -0
- data/lib/instant_record/mcp/server.rb +429 -0
- data/lib/instant_record/mcp.rb +47 -0
- data/lib/instant_record/native/contract.rb +367 -0
- data/lib/instant_record/native/node.rb +125 -0
- data/lib/instant_record/native/screen.rb +366 -0
- data/lib/instant_record/native/xcode_project.rb +344 -0
- data/lib/instant_record/native.rb +41 -0
- data/lib/instant_record/outbox_mutation.rb +81 -9
- data/lib/instant_record/outbox_upcaster.rb +161 -0
- data/lib/instant_record/read_tracker.rb +610 -0
- data/lib/instant_record/routing.rb +33 -0
- data/lib/instant_record/runtime_scoped.rb +4 -0
- data/lib/instant_record/shell.rb +486 -0
- data/lib/instant_record/sync_actions.rb +800 -0
- data/lib/instant_record/sync_metadata.rb +17 -2
- data/lib/instant_record/sync_notice.rb +234 -0
- data/lib/instant_record/sync_partition.rb +63 -0
- data/lib/instant_record/sync_scope.rb +300 -0
- data/lib/instant_record/sync_shape.rb +102 -0
- data/lib/instant_record/sync_window.rb +92 -15
- data/lib/instant_record/syncable.rb +313 -17
- data/lib/instant_record/testing/authoritative_server.rb +547 -0
- data/lib/instant_record/testing/chaos_simulator.rb +429 -0
- data/lib/instant_record/testing/cruby_host_bridge.rb +370 -0
- data/lib/instant_record/testing/fault_network.rb +140 -0
- data/lib/instant_record/testing/parity_suite.rb +435 -0
- data/lib/instant_record/testing/runtimes/base.rb +184 -0
- data/lib/instant_record/testing/runtimes/cruby_runtime.rb +255 -0
- data/lib/instant_record/testing/runtimes/native_runtime.rb +346 -0
- data/lib/instant_record/testing/runtimes/wasm_runtime.rb +371 -0
- data/lib/instant_record/testing/wasm_host_bridge.mjs +240 -0
- data/lib/instant_record/testing.rb +333 -0
- data/lib/instant_record/trace/redactor.rb +278 -0
- data/lib/instant_record/trace/replayer.rb +126 -0
- data/lib/instant_record/trace/schema_extractor.rb +37 -0
- data/lib/instant_record/trace.rb +22 -0
- data/lib/instant_record/version.rb +1 -1
- data/lib/instant_record.rb +107 -37
- data/lib/tasks/instant_record.rake +299 -47
- data/native/ios/InstantRecordNative/Package.swift +12 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/AppHost.swift +706 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/Engine.swift +64 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/FileStore.swift +1048 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/HTTPEngine.swift +36 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/Icons.swift +19 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/Node.swift +64 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/ScreenView.swift +481 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/Screens.swift +94 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/StreamClient.swift +74 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/SyncNotice.swift +282 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/SyncSchedule.swift +153 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/Theme.swift +140 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/Transport.swift +108 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/TypedContracts.swift +237 -0
- data/native/ios/README.md +341 -0
- data/sig/instant_record/inertia.rbs +47 -0
- data/sig/instant_record/native.rbs +38 -0
- metadata +194 -18
- data/lib/generators/instant_record/install/templates/database.js +0 -32
- data/lib/generators/instant_record/install/templates/index.html +0 -69
- data/lib/generators/instant_record/install/templates/rails.sw.js +0 -750
- data/lib/instant_record/local_schema.rb +0 -75
- data/lib/instant_record/pglite_compat.rb +0 -40
|
@@ -0,0 +1,367 @@
|
|
|
1
|
+
module InstantRecord
|
|
2
|
+
module Native
|
|
3
|
+
# Raised in CRuby / tests when a screen's rendered `swift` / `component`
|
|
4
|
+
# node does not match the typed contract declared on that screen.
|
|
5
|
+
class ContractError < StandardError; end
|
|
6
|
+
|
|
7
|
+
# One scalar, nested object, or list field in a typed native prop contract.
|
|
8
|
+
class ContractField
|
|
9
|
+
SCALAR_TYPES = %w[string int float bool date].freeze
|
|
10
|
+
KINDS = %i[scalar one many many_scalar].freeze
|
|
11
|
+
|
|
12
|
+
attr_reader :name, :kind, :type, :optional, :type_name, :fields
|
|
13
|
+
|
|
14
|
+
def initialize(name:, kind:, type: nil, optional: false, type_name: nil, fields: [])
|
|
15
|
+
@name = name.to_s
|
|
16
|
+
@kind = kind
|
|
17
|
+
@type = type&.to_s
|
|
18
|
+
@optional = !!optional
|
|
19
|
+
@type_name = type_name&.to_s
|
|
20
|
+
@fields = fields.freeze
|
|
21
|
+
validate_definition!
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def optional? = @optional
|
|
25
|
+
def scalar? = @kind == :scalar
|
|
26
|
+
def one? = @kind == :one
|
|
27
|
+
def many? = @kind == :many
|
|
28
|
+
def many_scalar? = @kind == :many_scalar
|
|
29
|
+
|
|
30
|
+
def ==(other)
|
|
31
|
+
other.is_a?(ContractField) &&
|
|
32
|
+
name == other.name &&
|
|
33
|
+
kind == other.kind &&
|
|
34
|
+
type == other.type &&
|
|
35
|
+
optional == other.optional &&
|
|
36
|
+
type_name == other.type_name &&
|
|
37
|
+
fields == other.fields
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
private
|
|
41
|
+
|
|
42
|
+
def validate_definition!
|
|
43
|
+
unless @name.match?(/\A[a-z][a-zA-Z0-9_]*\z/)
|
|
44
|
+
raise ArgumentError, "Invalid contract field name #{@name.inspect}: use lowerCamelCase or snake_case starting with a letter"
|
|
45
|
+
end
|
|
46
|
+
unless KINDS.include?(@kind)
|
|
47
|
+
raise ArgumentError, "Unknown contract field kind #{@kind.inspect}"
|
|
48
|
+
end
|
|
49
|
+
if (@kind == :scalar || @kind == :many_scalar) && !SCALAR_TYPES.include?(@type)
|
|
50
|
+
raise ArgumentError, "Unknown contract type #{@type.inspect} for field #{@name.inspect} (expected one of #{SCALAR_TYPES.join(', ')})"
|
|
51
|
+
end
|
|
52
|
+
if @type_name && !@type_name.match?(/\A[A-Z][A-Za-z0-9]*\z/)
|
|
53
|
+
raise ArgumentError, "Invalid nested type name #{@type_name.inspect} for field #{@name.inspect}: use PascalCase"
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# One typed parameter on a native action, carried either in `args` or `values`.
|
|
59
|
+
class ActionParam
|
|
60
|
+
SLOTS = %i[arg value].freeze
|
|
61
|
+
|
|
62
|
+
attr_reader :name, :type, :slot, :optional
|
|
63
|
+
|
|
64
|
+
def initialize(name:, type: "string", slot: :arg, optional: false)
|
|
65
|
+
@name = name.to_s
|
|
66
|
+
@type = type.to_s
|
|
67
|
+
@slot = slot
|
|
68
|
+
@optional = !!optional
|
|
69
|
+
|
|
70
|
+
unless @name.match?(/\A[a-z][a-zA-Z0-9_]*\z/)
|
|
71
|
+
raise ArgumentError, "Invalid action parameter name #{@name.inspect}"
|
|
72
|
+
end
|
|
73
|
+
unless ContractField::SCALAR_TYPES.include?(@type)
|
|
74
|
+
raise ArgumentError, "Unknown action parameter type #{@type.inspect} for #{@name.inspect}"
|
|
75
|
+
end
|
|
76
|
+
unless SLOTS.include?(@slot)
|
|
77
|
+
raise ArgumentError, "Unknown action parameter slot #{@slot.inspect}"
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def optional? = @optional
|
|
82
|
+
def arg? = @slot == :arg
|
|
83
|
+
def value? = @slot == :value
|
|
84
|
+
|
|
85
|
+
def ==(other)
|
|
86
|
+
other.is_a?(ActionParam) &&
|
|
87
|
+
name == other.name &&
|
|
88
|
+
type == other.type &&
|
|
89
|
+
slot == other.slot &&
|
|
90
|
+
optional == other.optional
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# One typed action a native view can dispatch back to its Ruby screen.
|
|
95
|
+
class ContractAction
|
|
96
|
+
attr_reader :name, :params
|
|
97
|
+
|
|
98
|
+
def initialize(name:, params: [])
|
|
99
|
+
@name = name.to_s
|
|
100
|
+
@params = params.freeze
|
|
101
|
+
unless @name.match?(/\A[a-z][a-zA-Z0-9_]*\z/)
|
|
102
|
+
raise ArgumentError, "Invalid contract action name #{@name.inspect}"
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def args = @params.select(&:arg?)
|
|
107
|
+
def values = @params.select(&:value?)
|
|
108
|
+
|
|
109
|
+
def ==(other)
|
|
110
|
+
other.is_a?(ContractAction) && name == other.name && params == other.params
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
# A declarative typed prop and action contract for a native view component
|
|
115
|
+
# (SwiftUI / Jetpack Compose), acting as the single source of truth for
|
|
116
|
+
# CRuby runtime validation and Prism codegen into Swift/Kotlin.
|
|
117
|
+
class Contract
|
|
118
|
+
attr_reader :component, :screen, :fields, :actions
|
|
119
|
+
|
|
120
|
+
def initialize(component:, fields: [], actions: [], screen: nil)
|
|
121
|
+
@component = component.to_s
|
|
122
|
+
@screen = screen&.to_s
|
|
123
|
+
@fields = fields.freeze
|
|
124
|
+
@actions = actions.freeze
|
|
125
|
+
unless @component.match?(/\A[A-Z][A-Za-z0-9]*\z/)
|
|
126
|
+
raise ArgumentError, "Invalid native component name #{@component.inspect}: use PascalCase (e.g. ConversationView)"
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def ==(other)
|
|
131
|
+
other.is_a?(Contract) &&
|
|
132
|
+
component == other.component &&
|
|
133
|
+
fields == other.fields &&
|
|
134
|
+
actions == other.actions
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
# Validate a rendered `swift` Node against this contract's fields.
|
|
138
|
+
def validate!(node)
|
|
139
|
+
validate_fields!(node, @fields, @component)
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
private
|
|
143
|
+
|
|
144
|
+
def validate_fields!(node, expected_fields, path)
|
|
145
|
+
by_name = expected_fields.each_with_object({}) { |field, map| map[field.name] = field }
|
|
146
|
+
seen = {}
|
|
147
|
+
|
|
148
|
+
node.children.each do |child|
|
|
149
|
+
case child.kind
|
|
150
|
+
when "prop"
|
|
151
|
+
name = child.props["name"].to_s
|
|
152
|
+
field = by_name[name]
|
|
153
|
+
unless field
|
|
154
|
+
raise ContractError, "#{path}: undeclared prop #{name.inspect}"
|
|
155
|
+
end
|
|
156
|
+
unless field.scalar?
|
|
157
|
+
raise ContractError, "#{path}.#{name}: expected #{field.kind} but emitted scalar prop"
|
|
158
|
+
end
|
|
159
|
+
actual_type = child.props["type"].to_s
|
|
160
|
+
unless actual_type == field.type
|
|
161
|
+
raise ContractError, "#{path}.#{name}: expected type #{field.type.inspect}, got #{actual_type.inspect}"
|
|
162
|
+
end
|
|
163
|
+
seen[name] = true
|
|
164
|
+
when "record"
|
|
165
|
+
name = child.props["name"].to_s
|
|
166
|
+
field = by_name[name]
|
|
167
|
+
unless field
|
|
168
|
+
raise ContractError, "#{path}: undeclared nested record #{name.inspect}"
|
|
169
|
+
end
|
|
170
|
+
unless field.one?
|
|
171
|
+
raise ContractError, "#{path}.#{name}: expected #{field.kind} but emitted nested record"
|
|
172
|
+
end
|
|
173
|
+
validate_fields!(child, field.fields, "#{path}.#{name}")
|
|
174
|
+
seen[name] = true
|
|
175
|
+
when "props"
|
|
176
|
+
name = child.props["name"].to_s
|
|
177
|
+
field = by_name[name]
|
|
178
|
+
unless field
|
|
179
|
+
raise ContractError, "#{path}: undeclared list prop #{name.inspect}"
|
|
180
|
+
end
|
|
181
|
+
if field.many?
|
|
182
|
+
child.children.each_with_index do |item, index|
|
|
183
|
+
unless item.kind == "record"
|
|
184
|
+
raise ContractError, "#{path}.#{name}[#{index}]: expected record item, got #{item.kind.inspect}"
|
|
185
|
+
end
|
|
186
|
+
validate_fields!(item, field.fields, "#{path}.#{name}[#{index}]")
|
|
187
|
+
end
|
|
188
|
+
elsif field.many_scalar?
|
|
189
|
+
child.children.each_with_index do |item, index|
|
|
190
|
+
unless item.kind == "prop"
|
|
191
|
+
raise ContractError, "#{path}.#{name}[#{index}]: expected scalar prop item, got #{item.kind.inspect}"
|
|
192
|
+
end
|
|
193
|
+
actual_type = item.props["type"].to_s
|
|
194
|
+
unless actual_type == field.type
|
|
195
|
+
raise ContractError, "#{path}.#{name}[#{index}]: expected type #{field.type.inspect}, got #{actual_type.inspect}"
|
|
196
|
+
end
|
|
197
|
+
end
|
|
198
|
+
else
|
|
199
|
+
raise ContractError, "#{path}.#{name}: expected #{field.kind} but emitted list"
|
|
200
|
+
end
|
|
201
|
+
seen[name] = true
|
|
202
|
+
else
|
|
203
|
+
raise ContractError, "#{path}: unexpected child node kind #{child.kind.inspect}"
|
|
204
|
+
end
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
expected_fields.each do |field|
|
|
208
|
+
next if field.optional? || seen[field.name]
|
|
209
|
+
|
|
210
|
+
raise ContractError, "#{path}: missing required prop #{field.name.inspect} (#{field.kind == :scalar ? field.type : field.kind})"
|
|
211
|
+
end
|
|
212
|
+
end
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
# Plain-Ruby builder yielded to `contract "Name" do |c| ... end`.
|
|
216
|
+
class ContractBuilder
|
|
217
|
+
def initialize(component, screen: nil, nested: false)
|
|
218
|
+
@component = component
|
|
219
|
+
@screen = screen
|
|
220
|
+
@nested = nested
|
|
221
|
+
@fields = []
|
|
222
|
+
@field_names = {}
|
|
223
|
+
@actions = []
|
|
224
|
+
@action_names = {}
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
def prop(name, type = :string, optional: false)
|
|
228
|
+
add_field(ContractField.new(name: name, kind: :scalar, type: type, optional: optional))
|
|
229
|
+
end
|
|
230
|
+
|
|
231
|
+
def string(name, optional: false) = prop(name, :string, optional: optional)
|
|
232
|
+
def int(name, optional: false) = prop(name, :int, optional: optional)
|
|
233
|
+
def float(name, optional: false) = prop(name, :float, optional: optional)
|
|
234
|
+
def bool(name, optional: false) = prop(name, :bool, optional: optional)
|
|
235
|
+
def date(name, optional: false) = prop(name, :date, optional: optional)
|
|
236
|
+
|
|
237
|
+
def one(name, as: nil, optional: false)
|
|
238
|
+
raise ArgumentError, "one #{name.inspect} requires a block describing its fields" unless block_given?
|
|
239
|
+
|
|
240
|
+
nested = ContractBuilder.new(@component, screen: @screen, nested: true)
|
|
241
|
+
yield nested
|
|
242
|
+
add_field(ContractField.new(name: name, kind: :one, optional: optional, type_name: as, fields: nested.fields))
|
|
243
|
+
end
|
|
244
|
+
|
|
245
|
+
def many(name, of: nil, as: nil, optional: false)
|
|
246
|
+
if of
|
|
247
|
+
raise ArgumentError, "many #{name.inspect} with of: cannot also take a block" if block_given?
|
|
248
|
+
|
|
249
|
+
add_field(ContractField.new(name: name, kind: :many_scalar, type: of, optional: optional))
|
|
250
|
+
else
|
|
251
|
+
raise ArgumentError, "many #{name.inspect} requires either of: or a block" unless block_given?
|
|
252
|
+
|
|
253
|
+
nested = ContractBuilder.new(@component, screen: @screen, nested: true)
|
|
254
|
+
yield nested
|
|
255
|
+
add_field(ContractField.new(name: name, kind: :many, optional: optional, type_name: as, fields: nested.fields))
|
|
256
|
+
end
|
|
257
|
+
end
|
|
258
|
+
|
|
259
|
+
def action(name)
|
|
260
|
+
raise ArgumentError, "Actions may only be declared at the top level of a contract" if @nested
|
|
261
|
+
|
|
262
|
+
key = name.to_s
|
|
263
|
+
raise ArgumentError, "Duplicate contract action #{key.inspect} in #{@component}" if @action_names[key]
|
|
264
|
+
|
|
265
|
+
builder = ActionBuilder.new(key)
|
|
266
|
+
yield builder if block_given?
|
|
267
|
+
@action_names[key] = true
|
|
268
|
+
@actions << builder.build
|
|
269
|
+
end
|
|
270
|
+
|
|
271
|
+
def fields = @fields
|
|
272
|
+
|
|
273
|
+
def build
|
|
274
|
+
Contract.new(component: @component, fields: @fields, actions: @actions, screen: @screen)
|
|
275
|
+
end
|
|
276
|
+
|
|
277
|
+
private
|
|
278
|
+
|
|
279
|
+
def add_field(field)
|
|
280
|
+
if @field_names[field.name]
|
|
281
|
+
raise ArgumentError, "Duplicate contract field #{field.name.inspect} in #{@component}"
|
|
282
|
+
end
|
|
283
|
+
@field_names[field.name] = true
|
|
284
|
+
@fields << field
|
|
285
|
+
end
|
|
286
|
+
end
|
|
287
|
+
|
|
288
|
+
# Plain-Ruby builder yielded to `c.action :reply do |a| ... end`.
|
|
289
|
+
class ActionBuilder
|
|
290
|
+
def initialize(name)
|
|
291
|
+
@name = name
|
|
292
|
+
@params = []
|
|
293
|
+
@param_names = {}
|
|
294
|
+
end
|
|
295
|
+
|
|
296
|
+
def arg(name, type = :string, optional: false)
|
|
297
|
+
add_param(ActionParam.new(name: name, type: type, slot: :arg, optional: optional))
|
|
298
|
+
end
|
|
299
|
+
|
|
300
|
+
def value(name, type = :string, optional: false)
|
|
301
|
+
add_param(ActionParam.new(name: name, type: type, slot: :value, optional: optional))
|
|
302
|
+
end
|
|
303
|
+
|
|
304
|
+
def build
|
|
305
|
+
ContractAction.new(name: @name, params: @params)
|
|
306
|
+
end
|
|
307
|
+
|
|
308
|
+
private
|
|
309
|
+
|
|
310
|
+
def add_param(param)
|
|
311
|
+
if @param_names[param.name]
|
|
312
|
+
raise ArgumentError, "Duplicate action parameter #{param.name.inspect} in action #{@name}"
|
|
313
|
+
end
|
|
314
|
+
@param_names[param.name] = true
|
|
315
|
+
@params << param
|
|
316
|
+
end
|
|
317
|
+
end
|
|
318
|
+
|
|
319
|
+
# Mixin loaded only in CRuby (not vendored into Spinel builds) that adds
|
|
320
|
+
# class-level `contract` declarations and render-time contract validation
|
|
321
|
+
# onto InstantRecord::Native::Screen.
|
|
322
|
+
module ScreenContracts
|
|
323
|
+
module ClassMethods
|
|
324
|
+
def contracts
|
|
325
|
+
@contracts ||= {}
|
|
326
|
+
end
|
|
327
|
+
|
|
328
|
+
def contract(name)
|
|
329
|
+
builder = ContractBuilder.new(name.to_s, screen: self.name)
|
|
330
|
+
yield builder if block_given?
|
|
331
|
+
built = builder.build
|
|
332
|
+
if contracts.key?(built.component)
|
|
333
|
+
raise ArgumentError, "Duplicate contract #{built.component.inspect} on #{self.name || 'Screen'}"
|
|
334
|
+
end
|
|
335
|
+
contracts[built.component] = built
|
|
336
|
+
end
|
|
337
|
+
end
|
|
338
|
+
|
|
339
|
+
def render
|
|
340
|
+
root = super
|
|
341
|
+
validate_contracts!(root)
|
|
342
|
+
root
|
|
343
|
+
end
|
|
344
|
+
|
|
345
|
+
private
|
|
346
|
+
|
|
347
|
+
def validate_contracts!(root)
|
|
348
|
+
declared = self.class.contracts
|
|
349
|
+
return if declared.empty?
|
|
350
|
+
|
|
351
|
+
validate_tree_contracts!(root, declared)
|
|
352
|
+
end
|
|
353
|
+
|
|
354
|
+
def validate_tree_contracts!(node, declared)
|
|
355
|
+
if node.kind == "swift"
|
|
356
|
+
contract = declared[node.text]
|
|
357
|
+
contract.validate!(node) if contract
|
|
358
|
+
else
|
|
359
|
+
node.children.each { |child| validate_tree_contracts!(child, declared) }
|
|
360
|
+
end
|
|
361
|
+
end
|
|
362
|
+
end
|
|
363
|
+
|
|
364
|
+
Screen.extend(ScreenContracts::ClassMethods)
|
|
365
|
+
Screen.prepend(ScreenContracts)
|
|
366
|
+
end
|
|
367
|
+
end
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
module InstantRecord
|
|
2
|
+
module Native
|
|
3
|
+
# One element of a screen. Every element is this class with these fields,
|
|
4
|
+
# whatever it draws: the compiler that builds the phone app infers one type
|
|
5
|
+
# for a whole tree, and the renderer switches on `kind`.
|
|
6
|
+
#
|
|
7
|
+
# `link_params` are the params of the screen a link opens ("params" in the
|
|
8
|
+
# JSON). The accessor is not called `params`: the compiler types a method
|
|
9
|
+
# by its name across the whole app, and a Rails app already has a `params`
|
|
10
|
+
# that holds anything.
|
|
11
|
+
class Node
|
|
12
|
+
# The phone's compiler reads these (inline RBS): each is a hash of
|
|
13
|
+
# strings, and a screen that hands it any other kind fails the build.
|
|
14
|
+
# @rbs @link_params: Hash[String, String]
|
|
15
|
+
# @rbs @args: Hash[String, String]
|
|
16
|
+
# @rbs @props: Hash[String, String]
|
|
17
|
+
|
|
18
|
+
attr_reader :kind, :props, :children
|
|
19
|
+
attr_accessor :id, :text, :action, :link_params, :args
|
|
20
|
+
|
|
21
|
+
def initialize(kind)
|
|
22
|
+
@kind = kind
|
|
23
|
+
@id = ""
|
|
24
|
+
@text = ""
|
|
25
|
+
@action = ""
|
|
26
|
+
@link_params = Node.strings
|
|
27
|
+
@args = Node.strings
|
|
28
|
+
@props = Node.strings
|
|
29
|
+
@children = []
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# An empty hash of strings by name. Not `{}`: the phone's compiler types
|
|
33
|
+
# a hash by what it holds, an empty literal holds nothing it can go by,
|
|
34
|
+
# and it would then treat a hash of strings given in its place as
|
|
35
|
+
# another kind of hash.
|
|
36
|
+
#: () -> Hash[String, String]
|
|
37
|
+
def self.strings
|
|
38
|
+
none = { "" => "" }
|
|
39
|
+
none.clear
|
|
40
|
+
none
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def set(name, value)
|
|
44
|
+
@props[name] = value if value.length > 0
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# Written by hand so the compiled app and CRuby produce the same bytes.
|
|
48
|
+
# Not `to_json`: JSON.generate calls that with an argument, and the
|
|
49
|
+
# compiled app's runtime gives every object one of its own.
|
|
50
|
+
def json
|
|
51
|
+
out = +""
|
|
52
|
+
write(out)
|
|
53
|
+
out
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def write(out)
|
|
57
|
+
out << "{\"kind\":" << Node.quote(@kind)
|
|
58
|
+
out << ",\"id\":" << Node.quote(@id)
|
|
59
|
+
out << ",\"text\":" << Node.quote(@text)
|
|
60
|
+
out << ",\"action\":" << Node.quote(@action)
|
|
61
|
+
# Three loops and not one method called three times: the phone's
|
|
62
|
+
# compiler gives each hash the type of what it was handed, and a
|
|
63
|
+
# shared method would have to convert between them.
|
|
64
|
+
out << ",\"params\":{"
|
|
65
|
+
first = true
|
|
66
|
+
@link_params.each do |name, value|
|
|
67
|
+
out << "," unless first
|
|
68
|
+
first = false
|
|
69
|
+
out << Node.quote(name.to_s) << ":" << Node.quote(value.to_s)
|
|
70
|
+
end
|
|
71
|
+
out << "}"
|
|
72
|
+
out << ",\"args\":{"
|
|
73
|
+
first = true
|
|
74
|
+
@args.each do |name, value|
|
|
75
|
+
out << "," unless first
|
|
76
|
+
first = false
|
|
77
|
+
out << Node.quote(name.to_s) << ":" << Node.quote(value.to_s)
|
|
78
|
+
end
|
|
79
|
+
out << "}"
|
|
80
|
+
out << ",\"props\":{"
|
|
81
|
+
first = true
|
|
82
|
+
@props.each do |name, value|
|
|
83
|
+
out << "," unless first
|
|
84
|
+
first = false
|
|
85
|
+
out << Node.quote(name.to_s) << ":" << Node.quote(value.to_s)
|
|
86
|
+
end
|
|
87
|
+
out << "}"
|
|
88
|
+
out << ",\"children\":["
|
|
89
|
+
first = true
|
|
90
|
+
@children.each do |child|
|
|
91
|
+
out << "," unless first
|
|
92
|
+
first = false
|
|
93
|
+
child.write(out)
|
|
94
|
+
end
|
|
95
|
+
out << "]}"
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
HEX = "0123456789abcdef".freeze
|
|
99
|
+
|
|
100
|
+
def self.quote(value)
|
|
101
|
+
out = +"\""
|
|
102
|
+
value.each_char do |char|
|
|
103
|
+
code = char.ord
|
|
104
|
+
if char == "\""
|
|
105
|
+
out << "\\\""
|
|
106
|
+
elsif char == "\\"
|
|
107
|
+
out << "\\\\"
|
|
108
|
+
elsif char == "\n"
|
|
109
|
+
out << "\\n"
|
|
110
|
+
elsif char == "\r"
|
|
111
|
+
out << "\\r"
|
|
112
|
+
elsif char == "\t"
|
|
113
|
+
out << "\\t"
|
|
114
|
+
elsif code < 32
|
|
115
|
+
out << "\\u00" << HEX[code / 16] << HEX[code % 16]
|
|
116
|
+
else
|
|
117
|
+
out << char
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
out << "\""
|
|
121
|
+
out
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
end
|