Skip to content

Commit

Permalink
examples use component function
Browse files Browse the repository at this point in the history
  • Loading branch information
limemloh committed Aug 28, 2019
1 parent d8fc066 commit 947b756
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 188 deletions.
49 changes: 15 additions & 34 deletions examples/continuous-time/index.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,27 @@
import {
Behavior,
map,
snapshot,
stepper,
Stream,
time
} from "@funkia/hareactive";
import { elements, fgo, modelView, runComponent } from "../../src/index";
import { map, snapshot, stepper, Stream, time } from "@funkia/hareactive";
import { elements as E, runComponent, component } from "../../src/index";

const { p, button, h1 } = elements;
const formatTime = (t: number) => new Date(t).toTimeString().split(" ")[0];

const formatTime = (t: number): string =>
new Date(t).toTimeString().split(" ")[0];

type ToView = {
time: Behavior<number>;
message: Behavior<string>;
};

type ViewOut = {
type On = {
snapClick: Stream<any>;
};

const model = fgo(function*({ snapClick }: ViewOut) {
const main = component<On>((on, start) => {
const msgFromClick = map(
(t) => "You last pressed the button at " + formatTime(t),
snapshot(time, snapClick)
snapshot(time, on.snapClick)
);
const message = yield stepper(
"You've not clicked the button yet",
msgFromClick
const message = start(
stepper("You've not clicked the button yet", msgFromClick)
);
return { time, message };
});

const view = ({ time, message }: ToView) => [
h1("Continuous time example"),
p(map(formatTime, time)),
p(button("Click to snap time").use({ snapClick: "click" })),
p(message)
];

const main = modelView<ToView, ViewOut>(model, view)();
return [
E.h1("Continuous time example"),
E.p(map(formatTime, time)),
E.p(E.button("Click to snap time").use({ snapClick: "click" })),
E.p(message)
];
});

runComponent("#mount", main);
17 changes: 7 additions & 10 deletions examples/counters/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
import { Behavior, stepper, Stream } from "@funkia/hareactive";
import { go } from "@funkia/jabz";
import { elements, fgo, runComponent, view, component } from "../../../src";
import { elements as E, runComponent, view, component } from "../../../src";
import { main1 } from "./version1";
import { main2 } from "./version2";
import { main3 } from "./version3";
import { main4 } from "./version4";

const { button, div } = elements;

const numberToApp = { "1": main1, "2": main2, "3": main3, "4": main4 };

type AppId = keyof (typeof numberToApp);

const selectorButton = (n: AppId, selected: Behavior<AppId>) =>
view(
button(
E.button(
{
class: ["btn btn-default", { active: selected.map((m) => n === m) }]
},
Expand All @@ -26,23 +23,23 @@ type On = {
selectVersion: Stream<AppId>;
};

type FromModel = {
type Output = {
selected: Behavior<AppId>;
};

const versionSelector = component<On, FromModel>((on, start) => {
const versionSelector = component<On, Output>((on, start) => {
const selected = start(stepper("1", on.selectVersion));
return div({ class: "btn-group" }, [
return E.div({ class: "btn-group" }, [
selectorButton("1", selected).use({ selectVersion: "selectVersion" }),
selectorButton("2", selected).use({ selectVersion: "selectVersion" }),
selectorButton("3", selected).use({ selectVersion: "selectVersion" }),
selectorButton("4", selected).use({ selectVersion: "selectVersion" })
]).output({ selected });
});

const main = component<FromModel>((on) => {
const main = component<Output>((on) => {
const currentApp = on.selected.map((n: AppId) => numberToApp[n]);
return [versionSelector.use({ selected: "selected" }), div(currentApp)];
return [versionSelector.use({ selected: "selected" }), E.div(currentApp)];
});

runComponent("#mount", main);
9 changes: 4 additions & 5 deletions examples/counters/src/version1.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { elements } from "../../../src";
const { div, button } = elements;
import { elements as E } from "../../../src";

// Counter
const counterView = div([
const counterView = E.div([
"Counter ",
1,
" ",
button({ class: "btn btn-default" }, "+"),
E.button({ class: "btn btn-default" }, "+"),
" ",
button({ class: "btn btn-default" }, " - ")
E.button({ class: "btn btn-default" }, " - ")
]);

export const main1 = counterView;
9 changes: 4 additions & 5 deletions examples/counters/src/version2.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { accum, Stream, combine } from "@funkia/hareactive";
import { elements, component } from "../../../src";
const { div, button } = elements;
import { elements as E, component } from "../../../src";

type On = {
incrementClick: Stream<any>;
Expand All @@ -13,15 +12,15 @@ const counter = component<On>((on, start) => {
const changes = combine(increment, decrement);
const count = start(accum((n, m) => n + m, 0, changes));

return div([
return E.div([
"Counter ",
count,
" ",
button({ class: "btn btn-default" }, " + ").use({
E.button({ class: "btn btn-default" }, " + ").use({
incrementClick: "click"
}),
" ",
button({ class: "btn btn-default" }, " - ").use({
E.button({ class: "btn btn-default" }, " - ").use({
decrementClick: "click"
})
]);
Expand Down
17 changes: 8 additions & 9 deletions examples/counters/src/version3.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Behavior, map, accum, scan, Stream } from "@funkia/hareactive";

import { elements, list, component } from "../../../src";
const { br, li, button, h1, ul } = elements;
import { elements as E, list, component } from "../../../src";

const add = (n: number, m: number) => n + m;
const apply = <A, B>(f: (a: A) => B, a: A) => f(a);
Expand All @@ -18,15 +17,15 @@ const counter = () =>
component<CounterOn, CounterOutput>((on, start) => {
const count = start(accum(add, 0, on.delta));

return li([
return E.li([
"Counter ",
count,
" ",
button({ class: "btn btn-default" }, " + ").use((o) => ({
E.button({ class: "btn btn-default" }, " + ").use((o) => ({
delta: o.click.mapTo(1)
})),
" ",
button({ class: "btn btn-default" }, " - ").use((o) => ({
E.button({ class: "btn btn-default" }, " - ").use((o) => ({
delta: o.click.mapTo(-1)
}))
]).output({ count });
Expand All @@ -44,12 +43,12 @@ const counterList = component<ListOn>(({ addCounter }, start) => {
);
const counterIds = start(accum(apply, [0], appendCounterFn));
return [
h1("Counters"),
button({ class: "btn btn-primary" }, "Add counter").use({
E.h1("Counters"),
E.button({ class: "btn btn-primary" }, "Add counter").use({
addCounter: "click"
}),
br,
ul(list(counter, counterIds))
E.br,
E.ul(list(counter, counterIds))
];
});

Expand Down
21 changes: 10 additions & 11 deletions examples/counters/src/version4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import {
moment
} from "@funkia/hareactive";

import { list, elements, component } from "../../../src";
const { ul, li, p, br, button, h1 } = elements;
import { list, elements as E, component } from "../../../src";

const add = (n: number, m: number) => n + m;
const apply = <A>(f: (a: A) => A, a: A) => f(a);
Expand All @@ -35,19 +34,19 @@ const counter = (id: Id) =>
const deleteS = on.deleteClick.mapTo(id);
const count = start(accum(add, 0, on.delta));

return li([
return E.li([
"Counter ",
count,
" ",
button({ class: "btn btn-default" }, " + ").use((o) => ({
E.button({ class: "btn btn-default" }, " + ").use((o) => ({
delta: o.click.mapTo(1)
})),
" ",
button({ class: "btn btn-default" }, " - ").use((o) => ({
E.button({ class: "btn btn-default" }, " - ").use((o) => ({
delta: o.click.mapTo(-1)
})),
" ",
button({ class: "btn btn-default" }, "x").use({
E.button({ class: "btn btn-default" }, "x").use({
deleteClick: "click"
})
]).output({ count, deleteS });
Expand Down Expand Up @@ -75,13 +74,13 @@ const counterList = component<ToModel>((on, start) => {
const modifications = combine(appendCounterFn, removeCounterIdFn);
const counterIds = start(accum(apply, [0, 1, 2], modifications));
return [
h1("Counters"),
p(["Sum ", sum]),
button({ class: "btn btn-primary" }, "Add counter").use({
E.h1("Counters"),
E.p(["Sum ", sum]),
E.button({ class: "btn btn-primary" }, "Add counter").use({
addCounter: "click"
}),
br,
ul(
E.br,
E.ul(
list((n) => counter(n).use((o) => o), counterIds).use((o) => ({
listOut: o
}))
Expand Down
107 changes: 40 additions & 67 deletions examples/drag/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,79 +1,52 @@
import {
map,
streamFromEvent,
stepper,
toggle,
snapshot,
switcher,
Behavior,
combine,
lift,
runNow,
Stream,
accum
} from "@funkia/hareactive";
import { runComponent, elements, go, fgo, modelView } from "../../../src";
const { div } = elements;
import * as H from "@funkia/hareactive";
import { streamFromEvent } from "@funkia/hareactive/dom";
import { runComponent, elements as E, component } from "../../../src";

type Point = { x: number; y: number };

const mousemove = streamFromEvent(window, "mousemove");
const mousePosition = runNow(
stepper({ x: 0, y: 0 }, mousemove.map((e) => ({ x: e.pageX, y: e.pageY })))
);
const mousemove = streamFromEvent(window, "mousemove", (e) => ({
x: e.pageX,
y: e.pageY
}));

const addPoint = (p1: Point, p2: Point) => ({
x: p1.x + p2.x,
y: p1.y + p2.y
});

type BoxModelInput = {
startDrag: Stream<void>;
endDrag: Stream<void>;
startDrag: H.Stream<void>;
endDrag: H.Stream<void>;
};

const boxModel = fgo(function*(
{ startDrag, endDrag }: BoxModelInput,
color: string
) {
const startDragAt = snapshot(mousePosition, startDrag);
const dragOffset = map(
(p) => map((p2) => ({ x: p2.x - p.x, y: p2.y - p.y }), mousePosition),
startDragAt
);
const offset: Behavior<Point> = yield switcher(
Behavior.of({ x: 0, y: 0 }),
combine(dragOffset, endDrag.mapTo(Behavior.of({ x: 0, y: 0 })))
);
const committed: Behavior<Point> = yield accum(
addPoint,
{ x: 0, y: 0 },
snapshot(offset, endDrag)
);
const position = lift(addPoint, committed, offset);
const isBeingDragged = yield toggle(false, startDrag, endDrag);
return { isBeingDragged, position };
});

type BoxViewInput = {
position: Behavior<Point>;
isBeingDragged: Behavior<boolean>;
};

const boxView = ({ isBeingDragged, position }: BoxViewInput, color: string) =>
div({
class: ["box", { dragged: isBeingDragged }],
style: {
background: color,
left: position.map(({ x }) => x + "px"),
top: position.map(({ y }) => y + "px")
}
}).use({ startDrag: "mousedown", endDrag: "mouseup" });

const box = modelView(boxModel, boxView);

const main = go(function*() {
yield box("red");
});

runComponent("#mount", main);
const box = (color: string) =>
component<BoxModelInput>((on, start) => {
const mousePosition = start(H.stepper({ x: 0, y: 0 }, mousemove));
const startDragAt = H.snapshot(mousePosition, on.startDrag);
const dragOffset = H.map(
(p) => H.map((p2) => ({ x: p2.x - p.x, y: p2.y - p.y }), mousePosition),
startDragAt
);
const offset: H.Behavior<Point> = start(
H.switcher(
H.Behavior.of({ x: 0, y: 0 }),
H.combine(dragOffset, on.endDrag.mapTo(H.Behavior.of({ x: 0, y: 0 })))
)
);
const committed: H.Behavior<Point> = start(
H.accum(addPoint, { x: 0, y: 0 }, H.snapshot(offset, on.endDrag))
);
const position = H.lift(addPoint, committed, offset);
const isBeingDragged = start(H.toggle(false, on.startDrag, on.endDrag));

return E.div({
class: ["box", { dragged: isBeingDragged }],
style: {
background: color,
left: position.map(({ x }) => x + "px"),
top: position.map(({ y }) => y + "px")
}
}).use({ startDrag: "mousedown", endDrag: "mouseup" });
});

runComponent("#mount", box("red"));
Loading

0 comments on commit 947b756

Please sign in to comment.