-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add proper type inference by way of Subscription and SubscriptionWith…
…Handler. (#82)
- Loading branch information
1 parent
d434fbd
commit d6813f9
Showing
8 changed files
with
199 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
open ReasonUrql; | ||
|
||
module SubscribeRandomFloat = [%graphql | ||
{| | ||
subscription subscribeFloat { | ||
newFloat @bsDecoder(fn: "Js.Float.toString") | ||
} | ||
|} | ||
]; | ||
|
||
let handler = (prevSubscriptions, subscription) => { | ||
switch (prevSubscriptions) { | ||
| Some(subs) => Array.append(subs, [|subscription|]) | ||
| None => [|subscription|] | ||
}; | ||
}; | ||
|
||
[@bs.scope "Math"] [@bs.val] external random: unit => float = "random"; | ||
[@bs.scope "Math"] [@bs.val] external floor: float => int = "floor"; | ||
[@bs.send] external toString: (int, int) => string = "toString"; | ||
|
||
let getRandomInt = (max: int) => { | ||
floor(random() *. float_of_int(max)); | ||
}; | ||
|
||
let getRandomHex = () => { | ||
let encode = random() *. float_of_int(16777215) |> floor; | ||
let hex = encode->toString(16); | ||
{j|#$hex|j}; | ||
}; | ||
|
||
let request = SubscribeRandomFloat.make(); | ||
|
||
[@react.component] | ||
let make = () => { | ||
<SubscriptionWithHandler request handler> | ||
...{({response}) => | ||
switch (response) { | ||
| Fetching => <text> "Loading"->React.string </text> | ||
| Data(d) => | ||
Array.mapi( | ||
(index, datum) => | ||
<rect | ||
x={ | ||
datum##newFloat; | ||
} | ||
y={index === 0 ? datum##newFloat : d[index - 1]##newFloat} | ||
stroke={getRandomHex()} | ||
fill="none" | ||
height={getRandomInt(30) |> string_of_int} | ||
width={getRandomInt(30) |> string_of_int} | ||
/>, | ||
d, | ||
) | ||
|> React.array | ||
| Error(_e) => <text> "Error"->React.string </text> | ||
| NotFound => <text> "Not Found"->React.string </text> | ||
} | ||
} | ||
</SubscriptionWithHandler>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters