simplify counter-list example
This commit is contained in:
Родитель
6db3c23812
Коммит
0f82357ec4
|
@ -2,37 +2,32 @@
|
|||
|
||||
import * as Counter from "./counter";
|
||||
import {html, forward, thunk} from "reflex";
|
||||
import {always} from "./util";
|
||||
|
||||
/*::
|
||||
import type {ID, Tagged, ByName, Model, Action} from "./counter-list"
|
||||
import type {ID, Model, Action} from "./counter-list"
|
||||
import type {Address, DOM} from "reflex";
|
||||
*/
|
||||
|
||||
const Add =
|
||||
{ type: "Add"
|
||||
, source: void(0)
|
||||
, create: () => Add
|
||||
}
|
||||
() =>
|
||||
( { type: "Add"
|
||||
}
|
||||
)
|
||||
|
||||
const Remove =
|
||||
{ type: "Remove"
|
||||
, source: void(0)
|
||||
, create: () => Remove
|
||||
}
|
||||
() =>
|
||||
( { type: "Remove"
|
||||
}
|
||||
)
|
||||
|
||||
export const Modify =
|
||||
(name/*:ID*/, action/*:Counter.Action*/)/*:Tagged<"Modify", ByName<Counter.Action>>*/ =>
|
||||
const Modify =
|
||||
(name/*:ID*/) =>
|
||||
(action/*:Counter.Action*/)/*:Action*/ =>
|
||||
( { type: "Modify"
|
||||
, source: {name, action}
|
||||
}
|
||||
)
|
||||
|
||||
const by =
|
||||
(name/*:ID*/)/*:(action:Counter.Action) => Tagged<"Modify", ByName<Counter.Action>>*/ =>
|
||||
(action/*:Counter.Action*/) =>
|
||||
Modify(name, action)
|
||||
|
||||
export const init =
|
||||
()/*:Model*/ =>
|
||||
( { nextID: 1
|
||||
|
@ -94,7 +89,7 @@ const viewNamed =
|
|||
( String(model.name)
|
||||
, Counter.view
|
||||
, model.counter
|
||||
, forward(address, by(model.name))
|
||||
, forward(address, Modify(model.name))
|
||||
)
|
||||
|
||||
export const view =
|
||||
|
@ -106,17 +101,17 @@ export const view =
|
|||
( { key: "controls"
|
||||
}
|
||||
, [ html.button
|
||||
( { key: "remove"
|
||||
, onClick: forward(address, Remove.create)
|
||||
}
|
||||
, ["Remove"]
|
||||
)
|
||||
, html.button
|
||||
( { key: "add"
|
||||
, onClick: forward(address, Add.create)
|
||||
, onClick: forward(address, Add)
|
||||
}
|
||||
, ["Add"]
|
||||
)
|
||||
, html.button
|
||||
( { key: "remove"
|
||||
, onClick: forward(address, Remove)
|
||||
}
|
||||
, ["Remove"]
|
||||
)
|
||||
]
|
||||
)
|
||||
, html.div
|
||||
|
|
|
@ -15,27 +15,20 @@ export type Model =
|
|||
, counters: Array<NamedCounter>
|
||||
}
|
||||
|
||||
|
||||
export type Tagged <type, action> =
|
||||
{ type: type
|
||||
, source: action
|
||||
}
|
||||
|
||||
export type ByName <action> =
|
||||
{ name: ID
|
||||
, action: action
|
||||
}
|
||||
|
||||
export type Action
|
||||
= Tagged<"Add", void>
|
||||
| Tagged<"Remove", void>
|
||||
| Tagged<"Modify", ByName<Counter.Action>>
|
||||
= { type: "Add" }
|
||||
| { type: "Remove" }
|
||||
| { type: "Modify"
|
||||
, source:
|
||||
{ name: ID
|
||||
, action: Counter.Action
|
||||
}
|
||||
}
|
||||
|
||||
declare export var Add:Tagged<"Add", void>
|
||||
declare export var Remove:Tagged<"Remove", void>
|
||||
declare export function Modify
|
||||
(id:ID, action:Counter.Action):
|
||||
Tagged<"Modify", ByName<Counter.Action>>
|
||||
( name:ID
|
||||
, action:Counter.Action
|
||||
):Action
|
||||
|
||||
|
||||
declare export function init (): Model
|
||||
|
|
|
@ -8,14 +8,16 @@ import type {Address, DOM} from "reflex"
|
|||
*/
|
||||
|
||||
const Increment =
|
||||
{ type: "Increment"
|
||||
, create: () => Increment
|
||||
}
|
||||
() =>
|
||||
( { type: "Increment"
|
||||
}
|
||||
)
|
||||
|
||||
const Decrement =
|
||||
{ type: "Decrement"
|
||||
, create: () => Decrement
|
||||
}
|
||||
() =>
|
||||
( { type: "Decrement"
|
||||
}
|
||||
)
|
||||
|
||||
export const init =
|
||||
(value/*:number*/)/*:Model*/ =>
|
||||
|
@ -25,18 +27,18 @@ export const update =
|
|||
( model/*:Model*/
|
||||
, action/*:Action*/
|
||||
)/*:Model*/ =>
|
||||
( action.type === Increment.type
|
||||
( action.type === "Increment"
|
||||
? { value: model.value + 1 }
|
||||
: action.type === Decrement.type
|
||||
: action.type === "Decrement"
|
||||
? { value: model.value - 1 }
|
||||
: model
|
||||
)
|
||||
|
||||
const counterStyle = {
|
||||
value: {
|
||||
fontWeight: "bold"
|
||||
const counterStyle =
|
||||
{ value:
|
||||
{ fontWeight: "bold"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export const view =
|
||||
|
@ -48,7 +50,7 @@ export const view =
|
|||
}
|
||||
, [ html.button
|
||||
( { key: "decrement"
|
||||
, onClick: forward(address, Decrement.create)
|
||||
, onClick: forward(address, Decrement)
|
||||
}
|
||||
, ["-"]
|
||||
)
|
||||
|
@ -60,7 +62,7 @@ export const view =
|
|||
)
|
||||
, html.button
|
||||
( { key: "increment"
|
||||
, onClick: forward(address, Increment.create)
|
||||
, onClick: forward(address, Increment)
|
||||
}
|
||||
, ["+"]
|
||||
)
|
||||
|
|
|
@ -2,22 +2,15 @@
|
|||
|
||||
import type {Address, DOM} from "reflex"
|
||||
|
||||
type Tag <type> =
|
||||
{ type: type
|
||||
}
|
||||
|
||||
export type Model =
|
||||
{ value: number
|
||||
}
|
||||
|
||||
export type Action
|
||||
= Tag<"Increment">
|
||||
| Tag<"Decrement">
|
||||
= {type: "Increment"}
|
||||
| {type: "Decrement"}
|
||||
|
||||
|
||||
declare export var Increment:Tag<"Increment">
|
||||
declare export var Decrement:Tag<"Decrement">
|
||||
|
||||
declare export function init (value:number):
|
||||
Model
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче