Skip to content

Instantly share code, notes, and snippets.

View AlexDaSoul's full-sized avatar

Alexander Dukhnovskiy AlexDaSoul

View GitHub Profile
@AlexDaSoul
AlexDaSoul / .ts
Created January 19, 2020 15:29
nga-33
this.messageGrpcService.sendMessage({ message }).subscribe(...);
@AlexDaSoul
AlexDaSoul / .ts
Last active January 19, 2020 15:28
nga-32
public messages: Observable<Message.AsObject[]> = this.chatGrpcService.getChat()
.pipe(map(res => res.messagesList));
@AlexDaSoul
AlexDaSoul / .ts
Created January 19, 2020 15:26
nga-31
public onSubmit(): void {
if (this.form.valid) {
this.authGrpcService.auth(this.form.value)
.subscribe(
res => {
this.authService.loggedIn(res.token);
this.form.reset();
this.router.navigateByUrl('/chat');
},
err => {
@AlexDaSoul
AlexDaSoul / .ts
Created January 19, 2020 15:25
nga-30
export function grpcJwtMetadata(token: string = null): Metadata {
return {
Authorization: token || localStorage.getItem(environment.token),
};
}
@AlexDaSoul
AlexDaSoul / .ts
Created January 19, 2020 15:23
nga-30
@Injectable({
providedIn: 'root',
})
export class ChatGrpcService {
constructor(private client: ChatServicePromiseClient) {
}
public getChat(): Observable<ChatList.AsObject> {
const req = new Stub();
@AlexDaSoul
AlexDaSoul / .ts
Last active January 20, 2020 20:15
nga-29
export function grpcStream<T>(client: ClientReadableStream<T>): Observable<T> {
let stream: ClientReadableStream<T> = null;
let subscriptionCounter = 0;
const data: Observable<any> = new Observable((observer: Observer<T>) => {
if (subscriptionCounter === 0) {
stream = client;
}
subscriptionCounter++;
@AlexDaSoul
AlexDaSoul / .ts
Created January 19, 2020 15:20
nga-28
@Injectable({
providedIn: 'root',
})
export class AuthGrpcService {
constructor(private client: AuthServicePromiseClient) {
}
public auth(data: AuthReq.AsObject): Observable<AuthRes.AsObject> {
const req = new AuthReq();
@AlexDaSoul
AlexDaSoul / .ts
Created January 19, 2020 15:20
nga-27
export function grpcUnary<T>(promise): Observable<T> {
return from(promise).pipe(
map((response: jspb.Message) => response.toObject()),
catchError((error: Status) => {
if (error.code === StatusCode.UNAUTHENTICATED) {
jwtAuthError$.next();
}
return throwError(error);
}),
@AlexDaSoul
AlexDaSoul / .ts
Created January 19, 2020 15:19
nga-26
@Injectable({
providedIn: 'root',
})
export class AuthGrpcService {
constructor(private client: AuthServicePromiseClient) {
}
public auth(data: AuthReq.AsObject): Observable<AuthRes.AsObject> {
const req = new AuthReq();
@AlexDaSoul
AlexDaSoul / .ts
Created January 19, 2020 15:18
nga-25
@Injectable({
providedIn: 'root',
})
export class AuthService {
private ngOnDestroy$ = new Subject<void>();
private loggedInSubject$ = new ReplaySubject<boolean>(1);
constructor(
private router: Router,