ここまでサーバーは値を答えてきた。今度は 画面を答える。
画面はファイルだ
{ "type": "page", "title": "Desk",
"content": { "type": "center",
"child": { "type": "linear", "direction": "vertical", "spacing": 14, "alignment": "center", "children": [
{ "type": "text", "text": "WAITING", "style": { "fontSize": 16, "letterSpacing": 4, "color": "#6b7280" } },
{ "type": "text", "text": "{{waiting}}", "style": { "fontSize": 88, "fontWeight": "bold", "color": "#111827" } },
{ "type": "button", "label": "Admit one", "onTap": { "type": "tool", "tool": "desk.admit", "params": { "count": 1 } } } ] } } }
六行だ。{{waiting}} のところに値が入り、ボタンが 3 編目で作った道具を呼ぶ。
サーバーが差し出す
server.addResource(
uri: 'ui://desk',
name: 'Desk screen',
description: 'The desk screen, served as a document',
mimeType: 'application/json',
handler: (uri, params) async => ReadResourceResult(contents: [
ResourceContentInfo(
uri: 'ui://desk',
mimeType: 'application/json',
text: File(_screenPath).readAsStringSync(),
)
]),
);
ResourcesCapability も入れねばならない。
capabilities: ServerCapabilities(
tools: ToolsCapability(listChanged: true),
resources: ResourcesCapability(listChanged: true),
),
毎リクエスト読むことが要点だ
readAsStringSync() がハンドラの中にある。起動時に一度読んで変数に持てば当然速いのに、そうしなかった。
持ってしまえば 画面を直すたびにサーバーを再起動せねばならない。 その瞬間、この編が主張すること — ファイルを直せば別のアプリになる — が嘘になる。
/// Read fresh on every request, deliberately. A screen cached at boot is a
/// screen you have to restart the server to change, and then the claim above
/// stops being true.
性能が問題になる地点が来たらキャッシュしてよいが、ファイルの変更を検知して無効化 せねばならない。ただ持つのは機能を失うことだ。