Last active
July 19, 2019 00:23
-
-
Save luchillo17/f05bf6fc78b710cc9f72066d87e935e2 to your computer and use it in GitHub Desktop.
ZenToRxjs source duplication in SwitchMap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { ApolloClient, gql, NormalizedCacheObject } from 'apollo-boost'; | |
import { Map } from 'immutable'; | |
import { observable } from 'mobx'; | |
import { merge } from 'rxjs'; | |
import { map, share, switchMap } from 'rxjs/operators'; | |
import { IChat } from '../components/chat-card/ChatCard.model'; | |
import { zenToRxjs } from '../shared'; | |
export const ChatStoreName = 'Chat'; | |
export class ChatStore { | |
@observable chats = Map<string, IChat>(); | |
chatsQuery = this.client.watchQuery({ | |
query: gql` | |
query { | |
ChatByUser { | |
id | |
roomNumber | |
checkIn { | |
formatted | |
} | |
checkOut { | |
formatted | |
} | |
owner { | |
id | |
name | |
picture | |
} | |
hotel { | |
id | |
name | |
} | |
} | |
} | |
`, | |
}); | |
messageQueryString = gql` | |
query($id: String!) { | |
Chat(id: $id) { | |
id | |
messages { | |
id | |
text | |
sentAt { | |
formatted | |
} | |
sender { | |
id | |
name | |
picture | |
} | |
} | |
} | |
} | |
`; | |
constructor(private client: ApolloClient<NormalizedCacheObject>) { | |
const chats$ = zenToRxjs(this.chatsQuery).pipe( | |
map((result) => result.data.ChatByUser), | |
map<IChat[], Map<string, IChat>>((chats) => { | |
return Map<string, IChat>().merge( | |
chats.reduce<Record<string, IChat>>((acum, chat) => { | |
acum[chat.id] = chat; | |
return acum; | |
}, {}), | |
); | |
}), | |
tap((chats => { console.log('Chats: ': chats) }), | |
share(), | |
); | |
chats$ | |
.pipe( | |
switchMap((chats) => { | |
const chatsWithQuery = chats.map((chat) => { | |
chat.messagesQuery = this.client.watchQuery({ | |
query: this.messageQueryString, | |
variables: { id: chat.id }, | |
}); | |
return chat; | |
}); | |
this.chats = chatsWithQuery; | |
return merge( | |
...chatsWithQuery | |
.valueSeq() | |
.map((chat) => zenToRxjs(chat.messagesQuery!)), | |
); | |
}), | |
map((result) => result.data.Chat), | |
) | |
.subscribe( | |
(chat) => { | |
this.chats.setIn([chat.id, 'messages'], chat.messages); | |
console.log('Arrived chat messages: ', chat); | |
}, | |
(error) => { | |
console.error(error); | |
}, | |
); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { FetchResult, Observable as ApolloObservable } from 'apollo-boost'; | |
import { format } from 'date-fns'; | |
import { Observable } from 'rxjs'; | |
import { IUser } from 'shared'; | |
export function getFullName(user: IUser) { | |
return `${user.firstName.trim()} ${user.lastName.trim()}`; | |
} | |
export function formatDate(date: Date) { | |
return format(date, 'h:mm A'); | |
} | |
export function zenToRxjs<T>( | |
obs: ApolloObservable<T>, | |
): Observable<FetchResult<T>> { | |
return new Observable((subscriber) => { | |
const subscription: ZenObservable.Subscription = obs.subscribe( | |
(value) => subscriber.next(value), | |
(error) => subscriber.error(error), | |
() => subscriber.complete(), | |
); | |
return () => { | |
subscription.unsubscribe(); | |
}; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment