message-ordering.ts 1.3 KB

1234567891011121314151617181920212223242526272829
  1. import { Subject, takeWhile } from "rxjs";
  2. import { WrappedMessage } from "../../interfaces/general.interface";
  3. export function sortMessageBasedOnDate(array: WrappedMessage[]): WrappedMessage[] {
  4. console.log(`Sorting ${array.length} messages....`)
  5. return array.sort((a, b) => {
  6. return new Date(a.timeReceived).getTime() - new Date(b.timeReceived).getTime();
  7. });
  8. }
  9. // SO concept will be that if the message behind it is received, then
  10. export async function checkMessage(message: WrappedMessage, messageChecking: Subject<WrappedMessage>): Promise<any> {
  11. return new Promise((resolve, reject) => {
  12. if (message.previousMessageID) {
  13. messageChecking.pipe(
  14. takeWhile(item => message.previousMessageID === item.thisMessageID)
  15. ).subscribe({
  16. complete: () => {
  17. resolve('previousMessageID matched')
  18. // console.log(`${message.payload.header.messageID} : Previous messageID(${message.previousMessageID}) matched`)
  19. // console.log(`matched`)
  20. }
  21. })
  22. } else {
  23. console.log('No previous messageID. This should be the first message')
  24. resolve('No previous message ID. Please Proceed.')
  25. }
  26. })
  27. }