Skip to content

Commit

Permalink
refactor: migrate to Rxjs op
Browse files Browse the repository at this point in the history
  • Loading branch information
czabaj committed Aug 12, 2024
1 parent 6ef648d commit 8c9dd36
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/Rxjs.res
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ type timeinterval<'a> = { interval: int, value: 'a }
@module("rxjs") external withLatestFrom7: (t<'ca, 'sa, 'a>, t<'cb, 'sb, 'b>, t<'cc, 'sc, 'c>, t<'cd, 'sd, 'd>, t<'ce, 'se, 'e>, t<'cf, 'sf, 'f>, t<'cg, 'sg, 'g>) => operatorFunction<'cz, 'sz, 'z, ('z, 'a, 'b, 'c, 'd, 'e, 'f, 'g)> = "withLatestFrom"

/**
* This is a helper function which allows use ReScript built-in pipe operator
* This is a helper function that allows use ReScript built-in pipe operator
* instead of `pipeN`. The ReScript compiler then "evaporates" this `op`
* function so it compiles to simple nested chain of function calls.
* See `keepMap` below for an example of how to use it.
Expand Down
8 changes: 4 additions & 4 deletions src/pages/MyPlaces/MyPlaces.res
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ module Pure = {

let pageDataRx = (auth, firestore) => {
open Rxjs
let currentUserRx = Rxfire.user(auth)->pipe(keepMap(Null.toOption))
let currentUserRx = Rxfire.user(auth)->op(keepMap(Null.toOption))
let userPlacesRx =
currentUserRx->pipe(
currentUserRx->op(
switchMap((user: Firebase.User.t) => Db.placesByUserIdRx(firestore, user.uid)),
)
combineLatest2(currentUserRx, userPlacesRx)
Expand Down Expand Up @@ -144,8 +144,8 @@ let make = () => {
~email=currentUser.email->Null.getExn,
~password=values.oldPassword,
)
let _ = await Firebase.Auth.reauthenticateWithCredential(. currentUser, credential)
let _ = await Firebase.Auth.updatePassword(.
let _ = await Firebase.Auth.reauthenticateWithCredential(currentUser, credential)
let _ = await Firebase.Auth.updatePassword(
currentUser,
~newPassword=values.newPassword,
)
Expand Down
36 changes: 20 additions & 16 deletions src/pages/Place/Place.res
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,22 @@ let pageDataRx = (auth, firestore, placeId) => {
let placeRef = Db.placeDocument(firestore, placeId)
let placeRx = Rxfire.docData(placeRef)
let chargedKegsRx = Db.allChargedKegsRx(firestore, placeId)
let placeTapsRx = placeRx->pipe2(
distinctUntilChanged((prev, curr) =>
switch ((prev: option<FirestoreModels.place>), curr) {
| (Some(prevPlace), Some(currPlace)) => prevPlace.taps == currPlace.taps
| _ => false
}
),
map((maybePlace: option<FirestoreModels.place>, _) =>
maybePlace->Option.map(place => place.taps)
),
)
let tapsWithKegsRx = combineLatest2(placeTapsRx, chargedKegsRx)->pipe(
let placeTapsRx =
placeRx
->op(
distinctUntilChanged((prev, curr) =>
switch ((prev: option<FirestoreModels.place>), curr) {
| (Some(prevPlace), Some(currPlace)) => prevPlace.taps == currPlace.taps
| _ => false
}
),
)
->op(
map((maybePlace: option<FirestoreModels.place>, _) =>
maybePlace->Option.map(place => place.taps)
),
)
let tapsWithKegsRx = combineLatest2(placeTapsRx, chargedKegsRx)->op(
map((data, _) => {
switch data {
| (Some(placeTaps), chargedKegs) =>
Expand All @@ -49,7 +53,7 @@ let pageDataRx = (auth, firestore, placeId) => {
}
}),
)
let unfinishedConsumptionsByUserRx = chargedKegsRx->pipe(
let unfinishedConsumptionsByUserRx = chargedKegsRx->op(
map((chargedKegs, _) => {
let consumptionsByUser = Map.make()
chargedKegs->Array.forEach(keg =>
Expand All @@ -65,7 +69,7 @@ let pageDataRx = (auth, firestore, placeId) => {
let recentConsumptionsByUserRx = combineLatest2(
unfinishedConsumptionsByUserRx,
recentlyFinishedKegsRx,
)->pipe(
)->op(
map(((unfinishedConsumptionsByUser, recentlyFinishedKegs), _) => {
let recentConsumptionsByUser = ObjectUtils.structuredClone(unfinishedConsumptionsByUser)
recentlyFinishedKegs->Array.forEach(keg =>
Expand All @@ -88,15 +92,15 @@ let pageDataRx = (auth, firestore, placeId) => {
->Map.fromArray
}),
)
let personsAllRx = Db.PersonsIndex.allEntriesSortedRx(firestore, ~placeId)->pipe(
let personsAllRx = Db.PersonsIndex.allEntriesSortedRx(firestore, ~placeId)->op(
map((personsAllEntries: array<(string, Db.personsAllRecord)>, _) => {
let (active, inactive) =
personsAllEntries->Belt.Array.partition(((_, {preferredTap})) => preferredTap !== None)
let all = Map.fromArray(personsAllEntries)
(all, active, inactive)
}),
)
let currentUserRx = Rxfire.user(auth)->pipe(keepMap(Null.toOption))
let currentUserRx = Rxfire.user(auth)->op(keepMap(Null.toOption))
combineLatest6(
placeRx,
personsAllRx,
Expand Down
8 changes: 4 additions & 4 deletions src/pages/PlacePersonsSetting/PlacePersonsSetting.res
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ type dialogState =
let pageDataRx = (auth, firestore, placeId) => {
open Rxjs
let placeRef = Db.placeDocument(firestore, placeId)
let placeRx = Rxfire.docData(placeRef)->pipe(keepSome)
let placeRx = Rxfire.docData(placeRef)->op(keepSome)
let allChargedKegsRx = Db.allChargedKegsRx(firestore, placeId)
let unfinishedConsumptionsByUserRx = allChargedKegsRx->pipe(
let unfinishedConsumptionsByUserRx = allChargedKegsRx->op(
map((chargedKegs, _) => {
let consumptionsByUser = Map.make()
chargedKegs->Array.forEach(keg =>
Expand All @@ -24,7 +24,7 @@ let pageDataRx = (auth, firestore, placeId) => {
consumptionsByUser
}),
)
let pendingTransactionsByUserRx = allChargedKegsRx->pipe(
let pendingTransactionsByUserRx = allChargedKegsRx->op(
map((chargedKegs: array<Db.kegConverted>, _) => {
let kegByDonor = Map.make()
chargedKegs->Array.forEach(keg => {
Expand All @@ -50,7 +50,7 @@ let pageDataRx = (auth, firestore, placeId) => {
}),
)
let personsAllRx = Db.PersonsIndex.allEntriesSortedRx(firestore, ~placeId)
let currentUserRx = Rxfire.user(auth)->pipe(keepMap(Null.toOption))
let currentUserRx = Rxfire.user(auth)->op(keepMap(Null.toOption))
combineLatest5(
placeRx,
personsAllRx,
Expand Down
4 changes: 2 additions & 2 deletions src/pages/PlaceSetting/PlaceSetting.res
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
let pageDataRx = (auth, firestore, placeId) => {
open Rxjs
let placeRef = Db.placeDocument(firestore, placeId)
let placeRx = Rxfire.docData(placeRef)->pipe(keepSome)
let placeRx = Rxfire.docData(placeRef)->op(keepSome)
let personsAllRx = Db.PersonsIndex.allEntriesSortedRx(firestore, ~placeId)
let chargedKegsRx = Db.allChargedKegsRx(firestore, placeId)
let currentUserRx = Rxfire.user(auth)->pipe(keepMap(Null.toOption))
let currentUserRx = Rxfire.user(auth)->op(keepMap(Null.toOption))
combineLatest4(placeRx, personsAllRx, chargedKegsRx, currentUserRx)
}

Expand Down
4 changes: 2 additions & 2 deletions src/pages/ShareLinkResolver/ShareLinkResolver.res
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ let pageDataRx = (auth, firestore, ~linkId) => {
}),
)
combineLatest2(currentUserRx, shareLinkPlaceRx)
->pipe2(
first(),
->op(first())
->op(
tap(((currentUser, maybeShareLinkPlace): (Firebase.User.t, option<FirestoreModels.place>)) => {
switch maybeShareLinkPlace {
| None => ()
Expand Down
6 changes: 3 additions & 3 deletions src/utils/DomUtils.res
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ let isOnlineRx = {
open Rxjs
open! Webapi.Dom
merge2(
fromEvent(. window, "online")->pipe(map((_, _) => true)),
fromEvent(. window, "offline")->pipe(map((_, _) => false)),
)->pipe(startWith((window->Webapi.Dom.Window.navigator).onLine))
fromEvent(window, "online")->pipe(map((_, _) => true)),
fromEvent(window, "offline")->pipe(map((_, _) => false)),
)->op(startWith((window->Webapi.Dom.Window.navigator).onLine))
}

let useIsOnline = () => {
Expand Down

0 comments on commit 8c9dd36

Please sign in to comment.