HomeEntrypoint.vala 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using Statum;
  2. using Invercargill;
  3. using Invercargill.DataStructures;
  4. namespace Example {
  5. /**
  6. * Private data carried by the bump-counter action (strongly typed). The
  7. * framework seals/reads it via GObject introspection — no mapper.
  8. */
  9. public class CounterPrivate : Object {
  10. public int delta { get; set; }
  11. }
  12. /**
  13. * Entrypoint for the home page.
  14. *
  15. * Demonstrates authoring action references (one with typed private data) and
  16. * embedding them in state so the client can invoke them via `stm-action`,
  17. * then returning them with the fluent directive builder.
  18. */
  19. public class HomeEntrypoint : StatumEntrypoint {
  20. public override async DirectiveBuilder handle() throws GLib.Error {
  21. // A counter slot whose public data carries a `bump` action reference
  22. // sealed with typed private data ({delta: 1}).
  23. var counter_public = new PropertyDictionary();
  24. counter_public.set_native<int>("value", 0);
  25. var bump_private = new CounterPrivate();
  26. bump_private.delta = 1;
  27. counter_public["bump"] = action_registry
  28. .author_private<BumpCounterAction, CounterPrivate>(bump_private)
  29. .to_element();
  30. var counter_slot = state_service.new_slot(Scope.PAGE, new State() {
  31. type_name = "counter",
  32. public_data = counter_public,
  33. private_data = new PropertyDictionary()
  34. });
  35. // A page slot whose public data carries a private-less `login`
  36. // action reference.
  37. var page_public = new PropertyDictionary();
  38. page_public["login"] = action_registry.author<LoginAction>().to_element();
  39. var page_slot = state_service.new_slot(Scope.PAGE, new State() {
  40. type_name = "page",
  41. public_data = page_public,
  42. private_data = new PropertyDictionary()
  43. });
  44. return directives().set(counter_slot).set(page_slot);
  45. }
  46. }
  47. /**
  48. * Entrypoint for the guarded dashboard page. The page is protected by a
  49. * client-side `stm-guard`; the entrypoint just greets the held `auth` slot.
  50. */
  51. public class DashboardEntrypoint : StatumEntrypoint {
  52. public override async DirectiveBuilder handle() throws GLib.Error {
  53. return directives().notify("info", "Welcome to your dashboard");
  54. }
  55. }
  56. }