This repository was archived by the owner on Apr 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.tsx
81 lines (63 loc) · 2.39 KB
/
demo.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// ----- Imports ----- //
import { app, log, none, Effect, giveNative, AppEvent } from './src/app';
import React from 'react';
// Pretends that this is running in an iOS app.
import './src/mockWebview';
import './src/mockNative';
// ----- State ----- //
type State = { num: number, name: string, status: string };
// ----- Update ----- //
type Msg
= { kind: 'Increment' }
| { kind: 'Decrement' }
| { kind: 'SetName', value: string }
| { kind: 'Logged' }
| { kind: 'LogWords', value: string }
| { kind: 'HelloNative' }
| { kind: 'NativeParcel', value: string }
;
function update(state: State, message: Msg): [State, Effect<Msg>] {
switch (message.kind) {
case 'Increment':
return [ { ...state, num: state.num + 1 }, none() ];
case 'Decrement':
return [ { ...state, num: state.num - 1 }, none() ];
case 'SetName':
return [ { ...state, name: message.value }, none() ];
case 'Logged':
return [ { ...state, status: 'Logged' }, none() ];
case 'LogWords':
return [ state, log('hello', { kind: 'Logged' }) ];
case 'HelloNative':
return [ state, giveNative('Hello Native!') ];
case 'NativeParcel':
return [ { ...state, status: message.value }, none() ];
default:
return [ state, none() ];
}
}
// ----- Events ----- //
const events = (_state: State): AppEvent<Msg> =>
({
kind: 'NativeParcel',
toMsg: (parcel: string): Msg => ({ kind: 'NativeParcel', value: parcel }),
});
// ----- View ----- //
function view(state: State, sendMsg: (m: Msg) => void): React.ReactElement {
return (
<div>
The state is: {state.num}
<div>
<button onClick={() => sendMsg({ kind: 'Increment' })}>+</button>
<button onClick={() => sendMsg({ kind: 'Decrement' })}>-</button>
</div>
<p>Hello {state.name}</p>
<input onChange={(evt) => sendMsg({ kind: 'SetName', value: evt.target.value })} />
<p>{state.status}</p>
<button onClick={() => sendMsg({ kind: 'LogWords', value: 'Clicked'})}>Click me!</button>
<button onClick={() => sendMsg({ kind: 'HelloNative' })}>Talk to Native Layer</button>
</div>
);
}
// ----- Run ----- //
app({ num: 5, name: 'world', status: '' }, update, view, events );