(
acquire: Effect.Effect<WS, SocketError, RO>,
options?: {
readonly openTimeout?: Duration.Input | undefined
readonly highWaterMark?: number | undefined
/** Set to true for a single connection that cannot be reacquired. Defaults to false. */
readonly disableReconnects?: boolean | undefined
} | undefined
)
| 960 | * @since 4.0.0 |
| 961 | */ |
| 962 | export const fromWebSocket = <RO, WS extends WebSocketLike>( |
| 963 | acquire: Effect.Effect<WS, SocketError, RO>, |
| 964 | options?: { |
| 965 | readonly openTimeout?: Duration.Input | undefined |
| 966 | readonly highWaterMark?: number | undefined |
| 967 | /** Set to true for a single connection that cannot be reacquired. Defaults to false. */ |
| 968 | readonly disableReconnects?: boolean | undefined |
| 969 | } | undefined |
| 970 | ): Effect.Effect<Socket, never, Exclude<RO, Scope.Scope>> => |
| 971 | Effect.withFiber((fiber) => { |
| 972 | let currentWS: WebSocketLike | undefined |
| 973 | let terminalError: SocketError | undefined |
| 974 | const latch = Latch.makeUnsafe(false) |
| 975 | const acquireContext = fiber.context as Context.Context<RO> |
| 976 | const disableReconnects = options?.disableReconnects === true |
| 977 | const terminate = disableReconnects |
| 978 | ? (error: SocketError) => { |
| 979 | terminalError ??= error |
| 980 | latch.openUnsafe() |
| 981 | } |
| 982 | : () => latch.closeUnsafe() |
| 983 | |
| 984 | const connect: Socket["reader"] = Effect.gen(function*() { |
| 985 | const scope = yield* Effect.scope |
| 986 | const ws = yield* Scope.provide(acquire, scope) |
| 987 | if ("binaryType" in ws) { |
| 988 | ;(ws as { binaryType: string }).binaryType = "arraybuffer" |
| 989 | } |
| 990 | const pausable = isPausable(ws) |
| 991 | const highWaterMark = options?.highWaterMark ?? (pausable ? defaultHighWaterMark : undefined) |
| 992 | |
| 993 | type ReadResume = ( |
| 994 | effect: Effect.Effect<NonEmptyReadonlyArray<Uint8Array | string>, SocketError> |
| 995 | ) => void |
| 996 | |
| 997 | let open = ws.readyState === 1 |
| 998 | let paused = pausable && "isPaused" in ws && ws.isPaused === true |
| 999 | let buffer: Array<Uint8Array | string> = [] |
| 1000 | let bufferSize = 0 |
| 1001 | let overflowed = false |
| 1002 | let error: SocketError | undefined |
| 1003 | let waiter: ReadResume | undefined |
| 1004 | let openWaiter: |
| 1005 | | { |
| 1006 | readonly resume: (effect: Effect.Effect<void, SocketError>) => void |
| 1007 | readonly cleanup: () => void |
| 1008 | } |
| 1009 | | undefined |
| 1010 | |
| 1011 | function pauseWebSocket() { |
| 1012 | if (!pausable || paused) return |
| 1013 | ws.pause() |
| 1014 | paused = true |
| 1015 | } |
| 1016 | |
| 1017 | function resumeWebSocket() { |
| 1018 | if (!pausable || !paused) return |
| 1019 | ws.resume() |
no test coverage detected