Skip to content

Commit 56e8b9a

Browse files
author
Maxime Mangel
committed
Release version 2.0.0
1 parent e2b2a94 commit 56e8b9a

File tree

3 files changed

+168
-1
lines changed

3 files changed

+168
-1
lines changed

demo/src/App.fs.js

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import { Record, Union } from "../fable_modules/fable-library.4.1.3/Types.js";
2+
import { record_type, string_type, union_type } from "../fable_modules/fable-library.4.1.3/Reflection.js";
3+
import { bounce, update as update_1, create, SelfMessage$1_$reflection, State_$reflection as State_$reflection_1 } from "../../src/Debouncer.fs.js";
4+
import { Cmd_map, Cmd_batch, Cmd_none } from "../fable_modules/Fable.Elmish.4.0.1/cmd.fs.js";
5+
import { fromSeconds } from "../fable_modules/fable-library.4.1.3/TimeSpan.js";
6+
import { ofArray, empty, singleton } from "../fable_modules/fable-library.4.1.3/List.js";
7+
import { div } from "../fable_modules/Fulma.3.0.0/Elements/Form/Field.fs.js";
8+
import { label } from "../fable_modules/Fulma.3.0.0/Elements/Form/Label.fs.js";
9+
import { div as div_1 } from "../fable_modules/Fulma.3.0.0/Elements/Form/Control.fs.js";
10+
import { input } from "../fable_modules/Fulma.3.0.0/Elements/Form/./Input.fs.js";
11+
import { Option, IInputType } from "../fable_modules/Fulma.3.0.0/Elements/Form/Input.fs.js";
12+
import { equals } from "../fable_modules/fable-library.4.1.3/Util.js";
13+
import { ProgramModule_mkProgram, ProgramModule_run } from "../fable_modules/Fable.Elmish.4.0.1/program.fs.js";
14+
import { Program_withReactSynchronous } from "../fable_modules/Fable.Elmish.React.4.0.0/react.fs.js";
15+
import "./scss/main.scss";
16+
17+
18+
export class State extends Union {
19+
constructor(tag, fields) {
20+
super();
21+
this.tag = tag;
22+
this.fields = fields;
23+
}
24+
cases() {
25+
return ["Initial", "IsTyping", "StoppedTyping"];
26+
}
27+
}
28+
29+
export function State_$reflection() {
30+
return union_type("Demo.State", [], State, () => [[], [], []]);
31+
}
32+
33+
export class Model extends Record {
34+
constructor(Debouncer, UserInput, State) {
35+
super();
36+
this.Debouncer = Debouncer;
37+
this.UserInput = UserInput;
38+
this.State = State;
39+
}
40+
}
41+
42+
export function Model_$reflection() {
43+
return record_type("Demo.Model", [], Model, () => [["Debouncer", State_$reflection_1()], ["UserInput", string_type], ["State", State_$reflection()]]);
44+
}
45+
46+
export class Msg extends Union {
47+
constructor(tag, fields) {
48+
super();
49+
this.tag = tag;
50+
this.fields = fields;
51+
}
52+
cases() {
53+
return ["DebouncerSelfMsg", "ChangeValue", "EndOfInput", "Reset"];
54+
}
55+
}
56+
57+
export function Msg_$reflection() {
58+
return union_type("Demo.Msg", [], Msg, () => [[["Item", SelfMessage$1_$reflection(Msg_$reflection())]], [["Item", string_type]], [], []]);
59+
}
60+
61+
function init(_arg) {
62+
return [new Model(create(), "", new State(0, [])), Cmd_none()];
63+
}
64+
65+
function update(msg, model) {
66+
switch (msg.tag) {
67+
case 0: {
68+
const patternInput_1 = update_1(msg.fields[0], model.Debouncer);
69+
return [new Model(patternInput_1[0], model.UserInput, model.State), patternInput_1[1]];
70+
}
71+
case 2: {
72+
const patternInput_2 = bounce(fromSeconds(2.5), "reset_demo", new Msg(3, []), model.Debouncer);
73+
return [new Model(patternInput_2[0], model.UserInput, new State(2, [])), Cmd_batch(singleton(Cmd_map((arg_1) => (new Msg(0, [arg_1])), patternInput_2[1])))];
74+
}
75+
case 3:
76+
return [new Model(model.Debouncer, "", new State(0, [])), Cmd_none()];
77+
default: {
78+
const patternInput = bounce(fromSeconds(1.5), "user_input", new Msg(2, []), model.Debouncer);
79+
return [new Model(patternInput[0], msg.fields[0], new State(1, [])), Cmd_batch(singleton(Cmd_map((arg) => (new Msg(0, [arg])), patternInput[1])))];
80+
}
81+
}
82+
}
83+
84+
function view(model, dispatch) {
85+
let matchValue;
86+
return div(empty(), ofArray([label(empty(), singleton((matchValue = model.State, (matchValue.tag === 1) ? "Waiting for more keystrokes... " : ((matchValue.tag === 2) ? "You stop typing. I will soon reset the demo" : "Type here, I will detect when you stop typing")))), div_1(empty(), singleton(input(ofArray([new Option(1, [new IInputType(0, [])]), new Option(13, [(ev) => {
87+
dispatch(new Msg(1, [ev.target.value]));
88+
}]), new Option(8, [model.UserInput]), new Option(4, [equals(model.State, new State(2, []))])]))))]));
89+
}
90+
91+
export function start(id) {
92+
ProgramModule_run(Program_withReactSynchronous(id, ProgramModule_mkProgram(() => init(void 0), update, view)));
93+
}
94+
95+
window.startDemo = ((id) => {
96+
start(id);
97+
});
98+

src/Debouncer.fs.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { Union, Record } from "../demo/fable_modules/fable-library.4.1.3/Types.js";
2+
import { union_type, record_type, class_type, int32_type, string_type } from "../demo/fable_modules/fable-library.4.1.3/Reflection.js";
3+
import { remove, tryFind, add, empty } from "../demo/fable_modules/fable-library.4.1.3/Map.js";
4+
import { comparePrimitives } from "../demo/fable_modules/fable-library.4.1.3/Util.js";
5+
import { some, map, defaultArg } from "../demo/fable_modules/fable-library.4.1.3/Option.js";
6+
import { Cmd_none, Cmd_OfPromise_either } from "../demo/fable_modules/Fable.Elmish.4.0.1/cmd.fs.js";
7+
import { PromiseBuilder__Delay_62FBFDE1, PromiseBuilder__Run_212F1D4B } from "../demo/fable_modules/Fable.Promise.3.2.0/Promise.fs.js";
8+
import { promise } from "../demo/fable_modules/Fable.Promise.3.2.0/PromiseImpl.fs.js";
9+
import { singleton } from "../demo/fable_modules/fable-library.4.1.3/List.js";
10+
11+
export class State extends Record {
12+
constructor(PendingMessages) {
13+
super();
14+
this.PendingMessages = PendingMessages;
15+
}
16+
}
17+
18+
export function State_$reflection() {
19+
return record_type("Thoth.Elmish.Debouncer.State", [], State, () => [["PendingMessages", class_type("Microsoft.FSharp.Collections.FSharpMap`2", [string_type, int32_type])]]);
20+
}
21+
22+
export function create() {
23+
return new State(empty({
24+
Compare: comparePrimitives,
25+
}));
26+
}
27+
28+
export class SelfMessage$1 extends Union {
29+
constructor(tag, fields) {
30+
super();
31+
this.tag = tag;
32+
this.fields = fields;
33+
}
34+
cases() {
35+
return ["Timeout", "OnError"];
36+
}
37+
}
38+
39+
export function SelfMessage$1_$reflection(gen0) {
40+
return union_type("Thoth.Elmish.Debouncer.SelfMessage`1", [gen0], SelfMessage$1, () => [[["id", string_type], ["appMsg", gen0]], [["Item", class_type("System.Exception")]]]);
41+
}
42+
43+
export function bounce(delay, id, msgToSend, currentState) {
44+
return [new State(add(id, defaultArg(map((y) => (1 + y), tryFind(id, currentState.PendingMessages)), 1), currentState.PendingMessages)), Cmd_OfPromise_either(() => PromiseBuilder__Run_212F1D4B(promise, PromiseBuilder__Delay_62FBFDE1(promise, () => ((new Promise(resolve => setTimeout(resolve, ~~delay))).then(() => (Promise.resolve([id, msgToSend])))))), void 0, (tupledArg) => (new SelfMessage$1(0, [tupledArg[0], tupledArg[1]])), (arg_3) => (new SelfMessage$1(1, [arg_3])))];
45+
}
46+
47+
export function update(selfMessage, currentState) {
48+
if (selfMessage.tag === 0) {
49+
const id = selfMessage.fields[0];
50+
const remainingMessages = (defaultArg(tryFind(id, currentState.PendingMessages), 0) - 1) | 0;
51+
if (remainingMessages === 0) {
52+
return [new State(remove(id, currentState.PendingMessages)), singleton((dispatch) => {
53+
dispatch(selfMessage.fields[1]);
54+
})];
55+
}
56+
else if (remainingMessages > 0) {
57+
return [new State(add(id, remainingMessages, currentState.PendingMessages)), Cmd_none()];
58+
}
59+
else {
60+
console.warn(some("Invalid debouncer state: there was no state information for the supplier id"));
61+
return [currentState, Cmd_none()];
62+
}
63+
}
64+
else {
65+
console.error(some(selfMessage.fields[0].message));
66+
return [currentState, Cmd_none()];
67+
}
68+
}
69+

src/Thoth.Elmish.Debouncer.fsproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<RepositoryUrl>https://github.com/thoth-org/Thoth.Elmish.Debouncer</RepositoryUrl>
88
<PackageTags>fable;fsharp;json</PackageTags>
99
<Authors>Maxime Mangel</Authors>
10-
<Version>1.0.0</Version>
10+
<Version>2.0.0</Version>
1111
</PropertyGroup>
1212
<PropertyGroup>
1313
<TargetFramework>netstandard2.0</TargetFramework>

0 commit comments

Comments
 (0)