Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/joanllenas/tea-ts

An experimental port of The Elm Architecture to TypeScript
https://github.com/joanllenas/tea-ts

elm elm-architecture typescript

Last synced: about 2 months ago
JSON representation

An experimental port of The Elm Architecture to TypeScript

Awesome Lists containing this project

README

        

# Tea-ts

An experimental port of [The Elm Architecture](https://guide.elm-lang.org/architecture/) to Typescript.

## Program

```ts
// Simple Program
function simple(
init: () => Model,
update: (msg: Msg, model: Model) => Model,
view: (model: Model) => Html.Html,
);

// Program with managed effects
function advanced(
init: (flags: Flags) => [Model, Eff],
update: (msg: Msg, model: Model) => [Model, Eff],
effects: (eff: Eff) => Effect.EffectFn,
subscriptions: (model: Model) => Subscription.Sub,
view: (model: Model) => Html.Html,
);
```

## Counter

```ts
import * as Tea from './tea/program';
import * as Message from './tea/message';
import * as Html from './tea/html';

type Model = {
count: number;
};

type Msg = Message.Msg<'Increment', number> | Message.Msg<'Decrement'>;

const msg: Message.MsgRecord = {
Increment: (n: number) => Message.msg('Increment', n),
Decrement: () => Message.msg('Decrement'),
};

const init = (): Model => ({
count: 0,
});

const update = (msg: Msg, model: Model): Model => {
switch (msg.name) {
case 'Increment': {
return { count: model.count + msg.payload };
}
case 'Decrement': {
return { count: model.count - 1 };
}
}
};

const view = (model: Model): Html.Html => {
return Html.div(
[],
[
Html.h3([], [Html.text('Simple')]),
Html.button([Html.onClick(() => msg.Increment(2))], [Html.text('+')]),
Html.button([Html.onClick(() => msg.Decrement())], [Html.text('-')]),
Html.div([], [Html.text('Count: ' + model.count)]),
],
);
};

Tea.simple(init, update, view).run(document.getElementById('app')!);
```