Parts#
A part is a whole app shown and used inside another app. The app that includes it, the parent, places the part's views on its own screens, reads some of its state, runs some of its actions and hears some of its events. The part keeps its own state, actions, rules and permissions, and offers the parent only what it lists under exports.
The examples of this topic are a pair: jar, a savings jar that is useful by itself, and gift, a gift fund that includes the jar as a part named jar. Both are whole examples, printed in full at the end of this topic.
When to reach for a part, and when not#
Reach for a part when a piece of an app is an app in its own right:
- A piece several apps share. One savings jar inside a gift fund, a trip planner and a wedding planner, written once.
- A piece useful alone and inside something larger. The person uses the jar by itself today, and the gift fund shows the same jar tomorrow.
Most apps are one definition, and should stay one. Two mistakes:
- Splitting one app into parts to tidy it. Every value the parent reads and every action it runs must then be exported and named across the boundary. Keep one app, with several views and a
typesentry for each record shape. - A part for what a view or a type already does. A piece of screen used in two places is a view; a record shape used in two places is a type (see
viewsandstate).
What a part cannot do: it is fixed when the app loads. The parent cannot choose a part, open one or add one while it runs. Each name under parts is one instance with its own state; two jars are two names, each with its own entry.
The included app's side: exports#
The part lists what a parent may use. This is the jar's:
JSON
{
"inputs": { "goal": "state.goal" },
"state": ["saved", "remaining"],
"actions": ["putIn"],
"events": ["goalReached"],
"views": ["summary"]
}
| Kind | What it lists | What the parent may do with it |
|---|---|---|
inputs |
An input name and the path of a stored entry it fills. | Give it a value with with. While the parent drives it, the entry is read-only inside the part. |
state |
Entries, stored or derived. | Read parts. in any expression. |
actions |
Actions. | Run { "run": "jar.putIn" } in its steps. |
events |
Events the part raises with an emit step. |
Hear { "event": "jar.goalReached" } in a rule. |
views |
Views. | Place { "part": "jar", "view": "summary" } among its nodes. |
Everything else stays private. The parent cannot read the jar's goal (it gives it instead), run setGoal or empty, or show the home view. The jar runs its own rules and asks for its own permissions wherever it is shown. A view you export should not offer to change a driven input: inside the gift fund, the jar's summary shows the goal but has no form for it.
The parent's side#
The parent names the definition it includes under uses.definitions, by its id and a range of versions, under an alias of its choosing:
JSON
{
"catalogs": {
"core": { "id": "org.openappdefinition.core", "version": "^0.2.0" }
},
"definitions": {
"jarApp": { "id": "com.example.jar", "version": "^1.0.0" }
}
}
Then it gives the part a name under parts, with the alias as its definition:
JSON
{
"jar": {
"definition": "jarApp",
"with": { "goal": "=state.price" },
"capabilities": { "notify": "notify" },
"grants": { "goalNotice": { "rate": { "count": 1, "per": "P1D" } } }
}
}
withgives each exported input an expression of the parent's. It follows the parent: when the price changes, the jar's goal changes with it. Its type must fit the input's (expression.typeMismatchotherwise).- Reading state. The gift fund derives the weeks to go from the jar:
JSON
{ "type": "integer", "derive": "ceil(parts.jar.state.remaining / 20)", "description": "Weeks to go at 20 dollars a week." }
- Running an action. A step runs the part's action by the part's name and the action's, with its input:
JSON
{
"description": "Put this week's 20 dollars in the jar.",
"steps": [ { "run": "jar.putIn", "with": { "amount": 20 } } ]
}
- Hearing an event. A rule of the parent triggers on the part's event;
eventholds what the part'semitgave (hereevent.saved):
JSON
{
"description": "When the jar reaches the price, note the day.",
"when": { "event": "jar.goalReached" },
"do": [ { "set": "state.savedOn", "to": "=env.today" } ]
}
- Placing a view. A part node takes
part,viewand, if you like,idandwhen, and nothing else:
JSON
{ "part": "jar", "view": "summary" }
Capabilities and permissions#
A part's capability that the parent does not map is bound for the part as if it ran alone. capabilities maps one of the part's aliases to one of the parent's, which must have the same contract; the parent declares it under its own capabilities even when only the part uses it. The jar's notify goes through the gift fund's.
The part's grant requests are put to the person as the app's, named with the part: the person is asked for jar.goalNotice, with the jar's reason. Under grants, the parent may narrow a request (a lower rate, a shorter duration, fewer scopes, an added where) or decline it with false, and then nobody is asked for it. It can never widen one. The gift fund lets the jar notify once a day instead of three times.
A part's service is answered by the parent's sample document, as when the app runs: under the parent's alias for a capability it maps, and under the part's name and alias joined by a dot for one it does not (see sample-data).
How to work#
- Write the part first, as an app of its own, with its
exports. Save it, then check it and look at it by itself. - Write the parent and save it under another name. The tools find the part among the person's apps by its
idand the range underuses.definitions, whatever its name or file; where several versions fit, the highest wins. - Leave
digestout while both are drafts: it pins one exact text of the part, and the part changes with every change you make.sourceis not needed either. - A change to the part is seen the next time the parent is shown or looked at. A change that removes an export raises the part's major version, and the parent's range must follow it:
^2.0.0for 2.0.0. - Taking back a change of the parent brings back the parent only; the part stays as it is now.
- Download writes one file with copies of the apps it includes, so it opens alone. A file may carry such copies; they are used as they came. To change one, take the draft apart with unpack_app.
What the tools say when a part cannot be used, and what to do:
| They say | Do |
|---|---|
| No app has the id | Save the part first, or fix the id. |
| None fits the range, and the versions there are | Write a range that one of them fits. |
| A digest that differs | Leave digest out. |
| The part does not pass its checks, with its first problem | Fix the part first, then try the parent again. |
| A cycle, or more than 8 levels of parts | Take an include out; a part never includes its parent. |
| More than 16 parts in all, or more of them than one definition holds (2.5 MiB) | Use fewer parts. |
The whole pair#
The jar, the part:
JSON
{
"oad": "0.2",
"id": "com.example.jar",
"version": "1.0.0",
"meta": {
"name": "Savings jar",
"description": "Money put aside toward a goal: what is saved, what is left, and a word when the goal is reached."
},
"uses": {
"catalogs": {
"core": { "id": "org.openappdefinition.core", "version": "^0.2.0" }
}
},
"capabilities": {
"notify": { "contract": "core:notify", "description": "Tells you when the jar reaches its goal.", "required": false }
},
"grants": {
"goalNotice": { "capability": "notify", "scopes": ["notify.self"], "reason": "Tell you when the jar reaches its goal.", "rate": { "count": 3, "per": "P1D" }, "required": false }
},
"state": {
"goal": { "type": "number", "initial": 100, "description": "How much to save, in US dollars." },
"saved": { "type": "number", "initial": 0, "description": "How much is in the jar, in US dollars." },
"remaining": { "type": "number", "derive": "max(state.goal - state.saved, 0)", "description": "How much is still to save." }
},
"actions": {
"putIn": {
"description": "Put money in the jar.",
"input": { "amount": { "type": "number", "description": "How much, in US dollars." } },
"steps": [
{ "set": "state.saved", "to": "=state.saved + input.amount" },
{
"if": "state.saved >= state.goal && state.saved - input.amount < state.goal",
"then": [
{ "emit": "goalReached", "with": { "saved": "=state.saved" } },
{ "call": "notify.send", "with": { "title": "Goal reached", "body": "=`The jar holds ${state.saved} dollars.`" }, "onError": [] }
]
}
]
},
"setGoal": {
"description": "Change how much to save.",
"input": { "goal": { "type": "number", "description": "The new goal, in US dollars." } },
"steps": [ { "set": "state.goal", "to": "=input.goal" } ]
},
"empty": {
"description": "Take everything out of the jar.",
"confirm": { "message": "=`The ${state.saved} dollars in the jar are taken out.`" },
"steps": [ { "set": "state.saved", "to": 0 } ]
}
},
"exports": {
"inputs": { "goal": "state.goal" },
"state": ["saved", "remaining"],
"actions": ["putIn"],
"events": ["goalReached"],
"views": ["summary"]
},
"surfaces": {
"visual": {
"start": "home",
"views": {
"home": {
"title": "Savings jar",
"state": { "goal": { "type": "number?", "description": "The goal being typed." } },
"body": {
"control": "core:stack",
"children": [
{ "control": "core:progress", "props": { "label": "Saved", "value": "=state.saved", "max": "=state.goal", "format": "currency", "currency": "USD" } },
{ "control": "core:button", "props": { "label": "Put in 10 dollars", "emphasis": "high" }, "on": { "press": { "run": "putIn", "with": { "amount": 10 } } } },
{
"control": "core:form",
"props": { "submitLabel": "Set the goal" },
"on": { "submit": [ { "run": "setGoal", "with": { "goal": "=default(view.goal, state.goal)" } }, { "set": "view.goal", "to": null } ] },
"children": [ { "control": "core:numberInput", "props": { "label": "Goal in dollars", "min": 1, "required": true }, "bind": { "value": "view.goal" } } ]
},
{ "control": "core:button", "props": { "label": "Empty the jar", "emphasis": "low", "destructive": true }, "on": { "press": { "run": "empty" } } }
]
}
},
"summary": {
"title": "Savings jar",
"body": {
"control": "core:stack",
"children": [
{ "control": "core:progress", "props": { "label": "Saved", "value": "=state.saved", "max": "=state.goal", "format": "currency", "currency": "USD" } },
{ "control": "core:text", "props": { "value": "=state.remaining == 0 ? 'The goal is reached.' : `${state.remaining} dollars to go.`" } }
]
}
}
}
}
}
}
The gift fund, the parent:
JSON
{
"oad": "0.2",
"id": "com.example.gift",
"version": "1.0.0",
"meta": {
"name": "Gift fund",
"description": "Saving for one gift with a savings jar: the gift's price is the jar's goal, and the app notes the day enough is saved."
},
"uses": {
"catalogs": {
"core": { "id": "org.openappdefinition.core", "version": "^0.2.0" }
},
"definitions": {
"jarApp": { "id": "com.example.jar", "version": "^1.0.0" }
}
},
"parts": {
"jar": {
"definition": "jarApp",
"with": { "goal": "=state.price" },
"capabilities": { "notify": "notify" },
"grants": { "goalNotice": { "rate": { "count": 1, "per": "P1D" } } }
}
},
"capabilities": {
"notify": { "contract": "core:notify", "description": "Passed to the savings jar, so it can tell you when the gift is paid for.", "required": false }
},
"state": {
"gift": { "type": "string", "initial": "A new bike", "description": "The gift to save for." },
"price": { "type": "number", "initial": 240, "description": "What the gift costs, in US dollars." },
"savedOn": { "type": "date?", "initial": null, "description": "The day the jar reached the price." },
"weeksLeft": { "type": "integer", "derive": "ceil(parts.jar.state.remaining / 20)", "description": "Weeks to go at 20 dollars a week." }
},
"actions": {
"saveWeek": {
"description": "Put this week's 20 dollars in the jar.",
"steps": [ { "run": "jar.putIn", "with": { "amount": 20 } } ]
},
"setPrice": {
"description": "Change what the gift costs.",
"input": { "price": { "type": "number", "description": "The new price, in US dollars." } },
"steps": [ { "set": "state.price", "to": "=input.price" }, { "set": "state.savedOn", "to": null } ]
}
},
"rules": {
"noteTheDay": {
"description": "When the jar reaches the price, note the day.",
"when": { "event": "jar.goalReached" },
"do": [ { "set": "state.savedOn", "to": "=env.today" } ]
}
},
"surfaces": {
"visual": {
"start": "home",
"views": {
"home": {
"title": "=state.gift",
"state": { "price": { "type": "number?", "description": "The price being typed." } },
"body": {
"control": "core:stack",
"children": [
{ "control": "core:text", "props": { "value": "=state.savedOn != null ? `Enough was saved on ${formatDate(state.savedOn, 'long')}.` : (state.weeksLeft == 0 ? 'Enough is saved.' : `${state.weeksLeft} weeks to go at 20 dollars a week.`)" } },
{ "part": "jar", "view": "summary" },
{ "control": "core:button", "props": { "label": "Save this week's 20 dollars", "emphasis": "high" }, "on": { "press": { "run": "saveWeek" } } },
{
"control": "core:form",
"props": { "submitLabel": "Set the price" },
"on": { "submit": [ { "run": "setPrice", "with": { "price": "=default(view.price, state.price)" } }, { "set": "view.price", "to": null } ] },
"children": [ { "control": "core:numberInput", "props": { "label": "Price in dollars", "min": 1, "required": true }, "bind": { "value": "view.price" } } ]
}
]
}
}
}
}
}
}
Next#
viewsfor the views a part exports and the nodes around them.capabilitiesfor requests and how a part's are asked.mistakesfor the mistakes made most with parts.