Repository navigation
M6:宿主契约收进 App 级 TinyUIHost,HostSnapshot 生成宿主快照 #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| # host snapshots are uploaded as raw bytes (docs/updates.md §4.1) | ||
| **/tinyui-host/*.txt text eol=lf |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| package app.tinyui | ||
|
|
||
| import app.tinyui.schema.ComponentSchema | ||
| import app.tinyui.schema.FieldSpec | ||
| import app.tinyui.schema.PropSpec | ||
| import kotlinx.serialization.json.JsonPrimitive | ||
|
|
||
| /** The host snapshot `tinyui-host/<hostVersion>.txt` (docs/updates.md §4.1, format §6.4). */ | ||
| object HostSnapshot { | ||
| fun render(host: TinyUIHost, hostVersion: String): String { | ||
| require(hostVersion.matches(Regex("[1-9][0-9]*"))) { "hostVersion must be a positive integer, got \"$hostVersion\"" } | ||
| val components = host.components.schemas.filter { '.' in it.type }.associate { it.type to describe(it) } | ||
| return buildString { | ||
| append("hostVersion ").append(hostVersion).append('\n') | ||
| append("tinyui ").append(TinyUI.version).append("\n\n") | ||
| append("components\n") | ||
| section(components) | ||
| append("\ncapabilities\n") | ||
| section(host.capabilities.names.associateWith { "" }) | ||
| } | ||
| } | ||
|
|
||
| private fun StringBuilder.section(items: Map<String, String>) { | ||
| val width = items.keys.maxOfOrNull { it.length } ?: 0 | ||
| for ((name, rest) in items.entries.sortedBy { it.key }) { | ||
| append(" ").append(if (rest.isEmpty()) name else name.padEnd(width + 2) + rest).append('\n') | ||
| } | ||
| } | ||
|
|
||
| private fun describe(schema: ComponentSchema): String = listOfNotNull( | ||
| schema.props.entries.sortedBy { it.key }.joinToString(", ") { (name, spec) -> prop(name, spec) }.ifEmpty { null }, | ||
| calls("events", schema.events), | ||
| calls("commands", schema.commands), | ||
| "children".takeIf { schema.children }, | ||
| "layout".takeIf { schema.layout }, | ||
| ).joinToString("; ") | ||
|
|
||
| private fun prop(name: String, spec: PropSpec): String = buildString { | ||
| append(name) | ||
| if (!spec.required) append('?') | ||
| append(": ").append(if (spec is PropSpec.Enum) spec.values.sorted().joinToString("|", "enum(", ")") else spec.kind) | ||
| spec.declared?.let { append(" = ").append(if (spec is PropSpec.Str) JsonPrimitive(it as String).toString() else literal(it)) } | ||
| if (spec.initial) append(" (initial)") | ||
| } | ||
|
|
||
| private fun calls(label: String, calls: Map<String, Map<String, FieldSpec>>): String? { | ||
| if (calls.isEmpty()) return null | ||
| return calls.entries.sortedBy { it.key }.joinToString(", ", "$label ") { (name, fields) -> | ||
| fields.entries.sortedBy { it.key }.joinToString(", ", "$name(", ")") { (field, type) -> "$field: ${type.typeName}" } | ||
| } | ||
| } | ||
|
|
||
| private val FieldSpec.typeName: String | ||
| get() = when (this) { | ||
| FieldSpec.Str -> "string" | ||
| FieldSpec.Num -> "number" | ||
| FieldSpec.Bool -> "boolean" | ||
| } | ||
|
|
||
| private fun literal(value: Any): String = when (value) { | ||
| is Double -> if (value % 1.0 == 0.0 && value > -1e15 && value < 1e15) value.toLong().toString() else value.toString() | ||
| else -> value.toString() | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| package app.tinyui | ||
|
|
||
| import app.tinyui.schema.ComponentRegistry | ||
|
|
||
| /** | ||
| * What one App gives every TinyUI page, built once at startup and not changed after (docs/native-api.md §7). | ||
| * Its components and capabilities are the host contract [HostSnapshot] renders. | ||
| */ | ||
| class TinyUIHost( | ||
| val components: ComponentRegistry, | ||
|
HarlonWang marked this conversation as resolved.
Resolved
|
||
| val sink: PageSink, | ||
| val capabilities: CapabilityRegistry = CapabilityRegistry(), | ||
| ) { | ||
| init { | ||
| components.freeze() | ||
| capabilities.freeze() | ||
| } | ||
| } | ||
|
|
||
| /** App-level J3 `host.call` capabilities by name (docs/native-api.md §7). */ | ||
| class CapabilityRegistry { | ||
| private val entries = LinkedHashMap<String, HostCapability>() | ||
| private var frozen = false | ||
|
|
||
| val names: Set<String> get() = entries.keys | ||
|
|
||
| fun register(name: String, capability: HostCapability) { | ||
| require(name.isNotEmpty() && name.none(Char::isWhitespace)) { "capability name \"$name\" is empty or has whitespace" } | ||
| require(name !in FrameworkCapabilities) { "capability $name is a framework name" } | ||
| check(!frozen) { "capability $name registered after the registry went into a TinyUIHost" } | ||
| require(name !in entries) { "capability $name already registered" } | ||
| entries[name] = capability | ||
| } | ||
|
sourcery-ai[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
| internal operator fun get(name: String): HostCapability? = entries[name] | ||
|
|
||
| internal fun freeze() { frozen = true } | ||
| } | ||
|
|
||
| /** One host-defined J3 capability: [argsJson] is the object JS passed; returns JSON text, or null for `undefined`. Throw [HostException] for E3 codes. */ | ||
| fun interface HostCapability { | ||
| suspend fun call(argsJson: String, page: PageContext): String? | ||
| } | ||
|
|
||
| /** The mount a capability call came from: the page's module name and the objects its [HostServices.locals] provide. */ | ||
| class PageContext internal constructor(val name: String, private val locals: Map<PageLocal<*>, Any>) { | ||
| @Suppress("UNCHECKED_CAST") | ||
| operator fun <T : Any> get(local: PageLocal<T>): T? = locals[local] as T? | ||
| } | ||
|
|
||
| /** A typed key for an object one mount hands to capabilities, such as its screen's snackbar. */ | ||
| class PageLocal<T : Any> { | ||
| infix fun provides(value: T): PageLocalValue<T> = PageLocalValue(this, value) | ||
| } | ||
|
|
||
| class PageLocalValue<T : Any> internal constructor(val local: PageLocal<T>, val value: T) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.