3.1. Quickstart¶
Each flavor exposes the same shape: wire a message hub and dispatcher, build a
leaf ComponentVM, then compose child tabs in a CompositeVM. Names follow
each language's idiom, and the construction and selection behavior is the same
in every flavor. Each snippet is a complete program. The C# snippet uses
top-level statements, and the Swift snippet runs in a throwing context such as
main.swift.
Install VMx first, as the Installation page describes.
C# and Rust use a VMx checkout, not a published package. Each program prints
one line when it runs: Selected tab: Settings.
To start a new application instead, use a starter. The Application Starters table names the published starters for TypeScript Node, React and Vite, the Python console and Swift packages. The TypeScript, Python and Swift pages also name their starters.
3.1.1. C#¶
using VMx.Components;
using VMx.Composites;
using VMx.Services;
IMessageHub hub = new MessageHub();
IDispatcher dispatcher = RxDispatcher.Immediate();
var homeTab = ComponentVM<TabModel>.Builder()
.Name("home-tab")
.Model(new TabModel("Home"))
.Services(hub, dispatcher)
.Build();
var settingsTab = ComponentVM<TabModel>.Builder()
.Name("settings-tab")
.Model(new TabModel("Settings"))
.Services(hub, dispatcher)
.Build();
var tabs = CompositeVM<ComponentVM<TabModel>>.Builder()
.Name("tab-bar")
.Services(hub, dispatcher)
.Children(() => [homeTab, settingsTab])
.Build();
tabs.Construct();
tabs.Current = settingsTab;
Console.WriteLine($"Selected tab: {tabs.Current?.Model.Title}");
public record TabModel(string Title);
3.1.2. Python¶
from dataclasses import dataclass
from vmx.components import ComponentVMOf
from vmx.composites import CompositeVM
from vmx.messages import Message
from vmx.services import MessageHub, RxDispatcher
@dataclass(frozen=True)
class TabModel:
title: str
hub: MessageHub[Message] = MessageHub()
dispatcher = RxDispatcher.immediate()
home_tab = (
ComponentVMOf[TabModel].builder()
.name("home-tab")
.model(TabModel("Home"))
.services(hub, dispatcher)
.build()
)
settings_tab = (
ComponentVMOf[TabModel].builder()
.name("settings-tab")
.model(TabModel("Settings"))
.services(hub, dispatcher)
.build()
)
tabs = (
CompositeVM[ComponentVMOf[TabModel]].builder()
.name("tab-bar")
.services(hub, dispatcher)
.children(lambda: [home_tab, settings_tab])
.build()
)
tabs.construct()
tabs.current = settings_tab
print(f"Selected tab: {tabs.current.model.title}") # Selected tab: Settings
3.1.3. TypeScript¶
import { ComponentVMOf, CompositeVM, MessageHub, RxDispatcher } from "@thekaveh/vmx";
interface TabModel {
title: string;
}
const hub = new MessageHub();
const dispatcher = RxDispatcher.immediate();
const homeTab = ComponentVMOf.builder<TabModel>()
.name("home-tab")
.model({ title: "Home" })
.services(hub, dispatcher)
.build();
const settingsTab = ComponentVMOf.builder<TabModel>()
.name("settings-tab")
.model({ title: "Settings" })
.services(hub, dispatcher)
.build();
const tabs = CompositeVM.builder<ComponentVMOf<TabModel>>()
.name("tab-bar")
.services(hub, dispatcher)
.children(() => [homeTab, settingsTab])
.build();
tabs.construct();
tabs.current = settingsTab;
console.log(`Selected tab: ${tabs.current?.model.title}`);
3.1.4. Swift¶
import VMx
struct TabModel: Equatable {
let title: String
}
let hub = MessageHub()
let dispatcher = ImmediateDispatcher.INSTANCE
let homeTab = try ComponentVMOf<TabModel>.builder()
.name("home-tab")
.model(TabModel(title: "Home"))
.services(hub: hub, dispatcher: dispatcher)
.build()
let settingsTab = try ComponentVMOf<TabModel>.builder()
.name("settings-tab")
.model(TabModel(title: "Settings"))
.services(hub: hub, dispatcher: dispatcher)
.build()
let tabs = try CompositeVM<ComponentVMOf<TabModel>>.builder()
.name("tab-bar")
.services(hub: hub, dispatcher: dispatcher)
.children { [homeTab, settingsTab] }
.build()
try tabs.construct()
tabs.current = settingsTab
print("Selected tab: \(tabs.current?.model.title ?? "none")")
3.1.5. Rust¶
use vmx::{ComponentVm, CompositeVm, MessageHub, NullDispatcher, VmxResult};
#[derive(Clone, PartialEq)]
struct TabModel {
title: String,
}
fn main() -> VmxResult<()> {
let hub = MessageHub::new();
let dispatcher = NullDispatcher::new();
let home_tab = ComponentVm::with_model(
"home-tab",
TabModel { title: "Home".to_string() },
hub.clone(),
dispatcher,
);
let settings_tab = ComponentVm::with_model(
"settings-tab",
TabModel { title: "Settings".to_string() },
hub.clone(),
dispatcher,
);
let tabs = CompositeVm::with_services("tab-bar", hub, dispatcher);
tabs.add(home_tab.clone())?;
tabs.add(settings_tab.clone())?;
tabs.construct()?;
tabs.select_component(&settings_tab)?;
if let Some(tab) = tabs.current() {
println!("Selected tab: {}", tab.model().title);
}
Ok(())
}
3.1.6. Next Steps¶
Continue with the tutorial for your language: C#, Python, TypeScript, Swift, or Rust.