Skip to content

Commit

Permalink
chore: minimise dup notifications, except OF Credit + Launcher quotes
Browse files Browse the repository at this point in the history
  • Loading branch information
algreasley committed Oct 10, 2023
1 parent 8185e76 commit 88fca7d
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 59 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { lazy, Suspense } from "react"
import { lazy, Suspense, useEffect } from "react"
import styled from "styled-components"

import { Loader } from "@/client/components/Loader"
import {
registerCreditRfqCreatedNotifications,
unregisterCreditRfqCreatedNotifications,
} from "@/client/notifications"

const CreditRfqFormCore = lazy(() => import("./CreditRfqFormCore"))

Expand All @@ -17,12 +21,24 @@ const loader = (
<Loader ariaLabel="Loading New RFQ Form" minWidth="22rem" minHeight="22rem" />
)

export const CreditRfqForm = () => (
<CreditRfqFormWrapper>
<Suspense fallback={loader}>
<CreditRfqFormCore>{loader}</CreditRfqFormCore>
</Suspense>
</CreditRfqFormWrapper>
)
export const CreditRfqForm = () => {
// TODO (5569) - required, otherwise Web will not populate instrument-related columns
// Instrument data is not ready when the RFQ updates are processed, without this (must be indirect subscription)
// (should be in lazy-loaded Core module, like other registrations)
useEffect(() => {
registerCreditRfqCreatedNotifications()
return () => {
unregisterCreditRfqCreatedNotifications()
}
}, [])

return (
<CreditRfqFormWrapper>
<Suspense fallback={loader}>
<CreditRfqFormCore>{loader}</CreditRfqFormCore>
</Suspense>
</CreditRfqFormWrapper>
)
}

export default CreditRfqForm
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ import { Subscribe } from "@react-rxjs/core"
import { useEffect } from "react"
import styled from "styled-components"

import {
registerCreditRfqCreatedNotifications,
unregisterCreditRfqCreatedNotifications,
} from "@/client/notifications"
import { WithChildren } from "@/client/utils/utilityTypes"
import { registerSimulatedDealerResponses } from "@/services/credit/creditRfqResponses"

Expand Down Expand Up @@ -64,11 +60,8 @@ const CreditRfqFooter = styled.footer`
const CreditRfqFormCore = ({ children }: WithChildren) => {
useEffect(() => {
const subscription = registerSimulatedDealerResponses()
registerCreditRfqCreatedNotifications()

return () => {
subscription.unsubscribe()
unregisterCreditRfqCreatedNotifications()
}
}, [])

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { useEffect } from "react"
import styled from "styled-components"

import {
registerCreditQuoteAcceptedNotifications,
registerCreditQuoteReceivedNotifications,
unregisterCreditQuoteAcceptedNotifications,
unregisterCreditQuoteReceivedNotifications,
} from "@/client/notifications"
import { WithChildren } from "@/client/utils/utilityTypes"
Expand All @@ -20,9 +22,13 @@ const CreditRfqsCoreWrapper = styled.div`
const CreditRfqsCore = ({ children }: WithChildren) => {
useEffect(() => {
registerCreditQuoteReceivedNotifications()
// the most logical place for accepted notifications,
// for all cases except the NLP-based nested RFQ ticket
registerCreditQuoteAcceptedNotifications()

return () => {
unregisterCreditQuoteReceivedNotifications()
unregisterCreditQuoteAcceptedNotifications()
}
}, [])

Expand Down
32 changes: 24 additions & 8 deletions packages/client/src/client/App/Trades/CreditTrades.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { lazy, Suspense } from "react"
import { lazy, Suspense, useEffect } from "react"
import styled from "styled-components"

import { Loader } from "@/client/components/Loader"
import {
registerCreditQuoteAcceptedNotifications,
unregisterCreditQuoteAcceptedNotifications,
} from "@/client/notifications"

const TradesCore = lazy(() => import("./CoreCreditTrades"))

Expand All @@ -12,12 +16,24 @@ const TradesWrapper = styled.article`
background: ${({ theme }) => theme.core.darkBackground};
`

export const CreditTrades = () => (
<TradesWrapper>
<Suspense fallback={<Loader />}>
<TradesCore />
</Suspense>
</TradesWrapper>
)
export const CreditTrades = () => {
// TODO (5569) - for now, we need to register for RFQ "accepted" here
// .. otherwise, in OpenFin, there is no past data in the Credit Blotter
// only processes endStateOfTheWorld rfq update
useEffect(() => {
registerCreditQuoteAcceptedNotifications()
return () => {
unregisterCreditQuoteAcceptedNotifications()
}
}, [])

return (
<TradesWrapper>
<Suspense fallback={<Loader />}>
<TradesCore />
</Suspense>
</TradesWrapper>
)
}

export default CreditTrades
55 changes: 32 additions & 23 deletions packages/client/src/client/notifications.openfin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as CSS from "csstype"
import {
addEventListener,
addEventListener as addOpenFinNotificationsEventListener,
ContainerTemplateFragment,
create,
CustomTemplateOptions,
Expand Down Expand Up @@ -316,7 +316,7 @@ export const registerFxTradeNotifications = (
(message: { tradeId: number }) => setFxTradeRowHighlight(message.tradeId),
)

addEventListener(
addOpenFinNotificationsEventListener(
"notification-action",
handler || handleHighlightFxBlotterAction,
)
Expand All @@ -336,9 +336,10 @@ export const registerFxTradeNotifications = (

export const unregisterFxTradeNotifications = () => {
if (executionSubscription) {
areFxTradeNotificationsRegistered = false
executionSubscription.unsubscribe()
executionSubscription = null
}
areFxTradeNotificationsRegistered = false
}

let areCreditRfqCreatedNotificationsRegistered = false
Expand All @@ -352,28 +353,30 @@ export const registerCreditRfqCreatedNotifications = (
}
areCreditRfqCreatedNotificationsRegistered = true

if (creditRfqCreatedSubscription == null) {
fin.InterApplicationBus.subscribe(
{ uuid: "*" },
TOPIC_HIGHLIGHT_CREDIT_RFQ,
(message: { rfqId: number }) => {
setCreditRfqCardHighlight(message.rfqId)
},
)
fin.InterApplicationBus.subscribe(
{ uuid: "*" },
TOPIC_HIGHLIGHT_CREDIT_RFQ,
(message: { rfqId: number }) => {
setCreditRfqCardHighlight(message.rfqId)
},
)

addEventListener("notification-action", handler || handleHighlightRfqAction)
addOpenFinNotificationsEventListener(
"notification-action",
handler || handleHighlightRfqAction,
)

creditRfqCreatedSubscription = createdCreditConfirmation$.subscribe(
(rfqRquest) => {
sendCreditRfqCreatedNotification(rfqRquest)
},
)
}
creditRfqCreatedSubscription = createdCreditConfirmation$.subscribe(
(rfqRquest) => {
sendCreditRfqCreatedNotification(rfqRquest)
},
)
}

export const unregisterCreditRfqCreatedNotifications = () => {
if (creditRfqCreatedSubscription != null) {
if (creditRfqCreatedSubscription) {
creditRfqCreatedSubscription.unsubscribe()
creditRfqCreatedSubscription = null
}
areCreditRfqCreatedNotificationsRegistered = false
}
Expand All @@ -397,7 +400,10 @@ export const registerCreditQuoteReceivedNotifications = (
},
)

addEventListener("notification-action", handler || handleHighlightRfqAction)
addOpenFinNotificationsEventListener(
"notification-action",
handler || handleHighlightRfqAction,
)

creditQuoteReceivedSubscription = lastQuoteReceived$.subscribe({
next: (quote) => {
Expand All @@ -413,8 +419,9 @@ export const registerCreditQuoteReceivedNotifications = (
}

export const unregisterCreditQuoteReceivedNotifications = () => {
if (creditQuoteReceivedSubscription != null) {
if (creditQuoteReceivedSubscription) {
creditQuoteReceivedSubscription.unsubscribe()
creditQuoteReceivedSubscription = null
}
areCreditQuoteReceivedNotificationsRegistered = false
}
Expand All @@ -429,6 +436,7 @@ export const registerCreditQuoteAcceptedNotifications = (
return
}
areCreditQuoteAcceptedNotificationsRegistered = true

fin.InterApplicationBus.subscribe(
{ uuid: "*" },
TOPIC_HIGHLIGHT_CREDIT_BLOTTER,
Expand All @@ -437,7 +445,7 @@ export const registerCreditQuoteAcceptedNotifications = (
},
)

addEventListener(
addOpenFinNotificationsEventListener(
"notification-action",
handler || handleHighlightCreditBlotterAction,
)
Expand All @@ -448,8 +456,9 @@ export const registerCreditQuoteAcceptedNotifications = (
}

export const unregisterCreditQuoteAcceptedNotifications = () => {
if (creditQuoteAcceptedSubscription != null) {
if (creditQuoteAcceptedSubscription) {
creditQuoteAcceptedSubscription.unsubscribe()
creditQuoteAcceptedSubscription = null
}
areCreditQuoteAcceptedNotificationsRegistered = false
}
56 changes: 43 additions & 13 deletions packages/client/src/client/notifications.web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,20 @@ const notificationsGranted = () =>
}
})

// NOTE: All the guard code complication below is due to:
// a) the delay of the notificationsGranted promise - in development mode effects, unreg happens before reg
// b) other calls of the same registration in the same context causes duplicate subs

let executionSubscription: Subscription | null = null

export async function registerFxTradeNotifications() {
try {
await notificationsGranted()

if (executionSubscription) {
return
}

// send trade executed for this tab only (driven from executeTrade ACK)
executionSubscription = executions$.subscribe({
next: (executionTrade) => {
Expand All @@ -121,29 +129,41 @@ export async function registerFxTradeNotifications() {
export function unregisterFxTradeNotifications() {
if (executionSubscription) {
executionSubscription.unsubscribe()
executionSubscription = null
}
}

let creditRfqCreatedSubscription: Subscription | null = null

export function registerCreditRfqCreatedNotifications() {
// send rfq created alerts for this tab only (driven from credit createRfq ACK)
creditRfqCreatedSubscription = createdCreditConfirmation$.subscribe({
next: (createdRFQRequest) => {
sendCreditRfqCreatedNotification(createdRFQRequest)
},
error: (e) => {
console.error(e)
},
complete: () => {
console.error("Credit notifications RFQ created stream completed!?")
},
})
export async function registerCreditRfqCreatedNotifications() {
try {
await notificationsGranted()

if (creditRfqCreatedSubscription) {
return
}

// send rfq created alerts for this tab only (driven from credit createRfq ACK)
creditRfqCreatedSubscription = createdCreditConfirmation$.subscribe({
next: (createdRFQRequest) => {
sendCreditRfqCreatedNotification(createdRFQRequest)
},
error: (e) => {
console.error(e)
},
complete: () => {
console.error("Credit notifications RFQ created stream completed!?")
},
})
} catch (e) {
console.error(e)
}
}

export function unregisterCreditRfqCreatedNotifications() {
if (creditRfqCreatedSubscription) {
creditRfqCreatedSubscription.unsubscribe()
creditRfqCreatedSubscription = null
}
}

Expand All @@ -153,6 +173,10 @@ export async function registerCreditQuoteReceivedNotifications() {
try {
await notificationsGranted()

if (creditQuoteReceivedSubscription) {
return
}

// send quote alerts for every live tab connected to BE (driven from rfq update feed)
creditQuoteReceivedSubscription = lastQuoteReceived$.subscribe({
next: (quote) => {
Expand All @@ -173,6 +197,7 @@ export async function registerCreditQuoteReceivedNotifications() {
export function unregisterCreditQuoteReceivedNotifications() {
if (creditQuoteReceivedSubscription) {
creditQuoteReceivedSubscription.unsubscribe()
creditQuoteReceivedSubscription = null
}
}

Expand All @@ -182,6 +207,10 @@ export async function registerCreditQuoteAcceptedNotifications() {
try {
await notificationsGranted()

if (creditQuoteAcceptedSubscription) {
return
}

// send accepted quote alerts for this tab only (driven from credit accept ACK)
creditQuoteAcceptedSubscription = acceptedRfqWithQuote$.subscribe({
next: (rfqWithQuote) => {
Expand All @@ -202,5 +231,6 @@ export async function registerCreditQuoteAcceptedNotifications() {
export function unregisterCreditQuoteAcceptedNotifications() {
if (creditQuoteAcceptedSubscription) {
creditQuoteAcceptedSubscription.unsubscribe()
creditQuoteAcceptedSubscription = null
}
}

0 comments on commit 88fca7d

Please sign in to comment.