Skip to content
This repository has been archived by the owner on Sep 7, 2020. It is now read-only.

Commit

Permalink
refactor: introduce helper function for handling option
Browse files Browse the repository at this point in the history
  • Loading branch information
coderbyheart committed Jan 22, 2020
1 parent 7017c3d commit 8409587
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Chat/Twilio/api.ts
Expand Up @@ -12,6 +12,7 @@ import * as TE from 'fp-ts/lib/TaskEither'
import { pipe } from 'fp-ts/lib/pipeable'
import { Option, fromNullable } from 'fp-ts/lib/Option'
import { Paginator } from 'twilio-chat/lib/interfaces/paginator'
import { getOrElse } from '../../fp-ts.util'

type ErrorInfo = {
type: string
Expand Down Expand Up @@ -110,8 +111,7 @@ export const connectToChannel = async ({
pipe(
fetchSubscribedChannels(client),
TE.map(maybeAlreadyJoinedChannel(context)),
TE.map(TE.fromOption<void>(() => undefined)),
chain(TE.orElse(joinChannel({ client, channel: context }))),
getOrElse(joinChannel({ client, channel: context })),
),
),
)()
9 changes: 9 additions & 0 deletions src/fp-ts.util.ts
@@ -0,0 +1,9 @@
import { TaskEither, taskEither } from 'fp-ts/lib/TaskEither'
import { Option } from 'fp-ts/lib/Option'
import { getOptionM } from 'fp-ts/lib/OptionT'

const M = getOptionM(taskEither)
export const getOrElse = <E, A>(
onNone: () => TaskEither<E, A>,
): ((ma: TaskEither<E, Option<A>>) => TaskEither<E, A>) => ma =>
M.getOrElse(ma, onNone)

0 comments on commit 8409587

Please sign in to comment.