Skip to content

Architecture: UI and logic

Ben Bucksch edited this page Jul 8, 2025 · 13 revisions

Separation between UI and logic

The UI (what you see on the screen, and the ways you can interact with it, including event handlers) and the logic of the application are strictly separated. We follow the MVC model, as defined by Smalltalk.

Logic

  • Code: app/logic/
  • Process: Frontend process
  • Language: TypeScript

The logic is build using object orientation, in pure OO classes that encapsulate the data and the operations that modify the data or give higher level analysis based on the data. This is the "Model" in Smalltalk MVC.

The logic code makes absolutely no assumptions about the UI, neither about its feature set, nor whether it's a GUI or even a commandline application - the logic code is independent of all that. The logic also also has no access to the UI at all. We put as much application code into the logic as possible, without violating the above rules.

Each class creates an API on its own, and the class must not assume anything about how other classes are implemented, other than the inherent and documented API. Different classes are not tightly coupled, but each class is a castle on its own. The API of each class must make sense from a high level point of view: If you draw a UML diagramm of all concepts that exist in your app, and the properties and functions of each concept, then each concept should map to a class, and the properties and functions must work how you would expect them to from a high level point of view, without knowing about the intrecacies of the app. Public API functions and properties should not be adapted to your current engineering need, but what inherently makes sense and what an new developer would expect. Implementation details are encapsulated within a single class - and even there, they should be kept logic and obvious.

UI

The UI (also called frontend) draws information on the screen, in a way that's most comfortable to the end user. It also handles the user input.

The UI also handles user input. That is typically in the form of event handlers. They simply call logic functions to do the actual work - a lot of the input code is a only 1 or 2 lines of code, merely calling the right logic function. This way, the UI modifies the logic object. Essentially, the user input handling only figures our which object the user wanted to manipulate, and which action the user wanted to do, and then calls the appropriate logic function.

The UI is deliberately only a thin code layer on top of the logic. The UI code only determines which object the user chose, and then calls the appropriate logic function on that object, e.g. targetFolder.copy(email) (which is almost exactly the logic function call that our UI makes when you copy an email to another folder). The UI code must not contain domain specific knowledge or code, nor complicated orcestration to get the logic into a certain state. If you find that there is lots of complicated code in the UI, then rewrite the code to make high-level functions in logic, and then simply call them from the UI.

(Smalltalk MVC concepts, code that displays data on the screen for the user to read is the "View", and the user input handling, allowing the user to change the data, is the "Controller". But beware that other frameworks like ASP.NET MVC or Ruby on Rails use the word "Controller" for the business logic, which is the "Model" in Smalltalk MVC, so please don't confuse those concepts.)

Observers

All logic objects are observable, i.e. it allows callers to "subscribe" to changes, and call the subscribers when it changes. Subscribers can be any other object.

Logic Observable

Logic objects inherit from our Observable class, which implements the subscribe() API to register subscribers and notifyObservers() to call the subscribers. We also have very helpful TypeScript decorators @notifyChangedProperty for class properties and @notifyChangedAccessor for class setters, which automatically call notifyObservers() whenever the decorated property is changed. So:

class Car extends Observable {
  @notifyChangedProperty
  passengers: number = 0;
}
let car = new Car();
...
car.passengers++; // calls observers

will automatically call observers and cause the UI to update.

Any Svelte <hbox>{$car.passengers} passengers</hbox> will automatically be updated. (See below.)

UI subscribes

When the UI displays an object, it "subscribes" to the corresponding logic object that it displays. If the properties of the logic object change, e.g. originating from the UI input handler, the logic object makes the necessary internal changes, notifies its subscribers, and the UI can update itself.

Svelte subscriptions

Svelte has a built-in feature for subscribing. E.g. in the HTML section of a Svelte component, you can write:

<hbox>Subject: {$email.subject}</hbox>

In this example, obviously, Svelte will call email.subject and insert the value into the DOM. Additionally, the $ causes Svelte to call email.subscribe() (see above) and subscribe to changes in the email object. Once the email logic object notifies the subscribers, Svelte will re-evaluate the above expression and retrieve the latest email.subject again and update the DOM.

The same works in reactive statements in the code section of a Svelte component:

$: showNew = $folder.totalNewMessages > 3;

Because of the $, Svelte subscribes to folder, and once folder changes, this entire line will be re-evaluated and showNew will be true whenever there are more than 3 new messages in this folder. You can then use this in the UI:

{#if showNew}
  <hbox>You have {$folder.totalNewMessages} new messages</hbox>
{/if}

and the message count will update automatically, and the line will disappear when there are less than 3 new messages. No other code is necessary to keep this up-to-date in the UI.

Notably, the user input handler does not need to do anything to update the UI view.

Clone this wiki locally