-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Tim Smart
committed
Mar 14, 2022
1 parent
c16402c
commit 208203b
Showing
6 changed files
with
220 additions
and
42 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import * as T from "@effect-ts/core/Effect" | ||
import * as S from "@effect-ts/core/Effect/Experimental/Stream" | ||
import * as SC from "@effect-ts/core/Effect/Schedule" | ||
import { pipe } from "@effect-ts/core/Function" | ||
import { tag } from "@effect-ts/core/Has" | ||
import { _A } from "@effect-ts/core/Utils" | ||
import { RawData } from "ws" | ||
import { log } from "../Log" | ||
import { GatewayPayload } from "../types" | ||
import * as WS from "../WS" | ||
|
||
export type Message = GatewayPayload | WS.Reconnect | ||
|
||
export interface OpenOpts { | ||
url?: string | ||
version?: number | ||
encoding?: Encoding | ||
outgoing: WS.OutboundStream<Message> | ||
} | ||
|
||
export interface Encoding { | ||
type: "json" | "etf" | ||
encode: (p: GatewayPayload) => string | Buffer | ArrayBuffer | ||
decode: (p: RawData) => GatewayPayload | ||
} | ||
|
||
export const jsonEncoding: Encoding = { | ||
type: "json", | ||
encode: (p) => JSON.stringify(p), | ||
decode: (p) => JSON.parse(p.toString("utf8")), | ||
} | ||
|
||
const makeOutgoing = (s: S.UIO<Message>, e: Encoding): WS.OutboundStream => | ||
pipe( | ||
s, | ||
S.map((data) => { | ||
if (data === WS.Reconnect) { | ||
return data | ||
} | ||
|
||
return e.encode(data) | ||
}) | ||
) | ||
|
||
const openImpl = ({ | ||
url = "wss://gateway.discord.gg/", | ||
version = 9, | ||
encoding = jsonEncoding, | ||
outgoing, | ||
}: OpenOpts) => | ||
pipe( | ||
WS.open( | ||
`${url}?v=${version}&encoding=${encoding.type}`, | ||
makeOutgoing(outgoing, encoding) | ||
), | ||
S.unwrap, | ||
S.onError((e) => | ||
e._tag === "Fail" ? log(serviceTag, "error", e.value) : T.unit | ||
), | ||
S.retry(SC.exponential(10)), | ||
S.map(encoding.decode) | ||
) | ||
|
||
export type Connection = ReturnType<typeof openImpl> | ||
|
||
// Service definition | ||
const serviceTag = "DiscordWSService" as const | ||
const makeDiscordWS = T.succeed({ | ||
_tag: serviceTag, | ||
open: openImpl, | ||
} as const) | ||
export interface DiscordWS extends _A<typeof makeDiscordWS> {} | ||
export const DiscordWS = tag<DiscordWS>() | ||
export const LiveDiscordWS = T.toLayer(DiscordWS)(makeDiscordWS) | ||
|
||
// Helpers | ||
export const open = (opts: OpenOpts) => | ||
T.accessService(DiscordWS)(({ open }) => open(opts)) |
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,18 @@ | ||
import * as T from "@effect-ts/core/Effect" | ||
import { tag } from "@effect-ts/core/Has" | ||
import { _A } from "@effect-ts/core/Utils" | ||
|
||
const service = { | ||
_tag: "LogService", | ||
log: (...args: any[]) => | ||
T.succeedWith(() => { | ||
console.error(...args) | ||
}), | ||
} as const | ||
|
||
export type Log = typeof service | ||
export const Log = tag<Log>() | ||
export const LiveLog = T.toLayer(Log)(T.succeed(service)) | ||
|
||
export const log = (...args: any[]) => | ||
T.accessServiceM(Log)(({ log }) => log(...args)) |
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 |
---|---|---|
@@ -1,24 +1,22 @@ | ||
import { Effect as T, pipe } from "@effect-ts/core" | ||
import * as S from "@effect-ts/core/Effect/Experimental/Stream" | ||
import { exponential } from "@effect-ts/core/Effect/Schedule" | ||
import * as Q from "@effect-ts/core/Effect/Queue" | ||
import * as R from "@effect-ts/node/Runtime" | ||
import * as DiscordWS from "./DiscordWS" | ||
import { LiveLog, log } from "./Log" | ||
import * as WS from "./WS" | ||
|
||
pipe( | ||
T.accessServiceM(WS.WS)(({ open }) => | ||
open("wss://gateway.discord.gg/?v=9&encoding=json") | ||
), | ||
T.chain(({ read, write }) => | ||
pipe( | ||
read, | ||
S.retry(exponential(10)), | ||
S.forEach((data) => | ||
T.succeedWith(() => { | ||
console.error(data.toString("utf8")) | ||
}) | ||
) | ||
) | ||
Q.makeUnbounded<DiscordWS.Message>(), | ||
T.map(S.fromQueue()), | ||
T.chain((outgoing) => | ||
T.accessService(DiscordWS.DiscordWS)(({ open }) => open({ outgoing })) | ||
), | ||
T.chain(S.forEach((payload) => log(payload))), | ||
|
||
T.provideSomeLayer(LiveLog), | ||
T.provideSomeLayer(WS.LiveWS), | ||
T.provideSomeLayer(DiscordWS.LiveDiscordWS), | ||
|
||
R.runMain | ||
) |
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
Oops, something went wrong.