generated from PackagrIO/goweb-template
-
-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
106 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export interface Event { | ||
event_type: string; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export interface EventSourceComplete extends Event { | ||
event_type: string; | ||
source_id: string; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export interface EventSourceSync extends Event { | ||
event_type: string; | ||
source_id: string; | ||
resource_type: string; | ||
resource_id: string; | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
|
||
import { EventBusService } from './event-bus.service'; | ||
|
||
describe('EventBusService', () => { | ||
let service: EventBusService; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({}); | ||
service = TestBed.inject(EventBusService); | ||
}); | ||
|
||
it('should be created', () => { | ||
expect(service).toBeTruthy(); | ||
}); | ||
}); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import {AfterViewInit, Injectable, OnInit} from '@angular/core'; | ||
import {NavigationEnd, Router} from '@angular/router'; | ||
import {BehaviorSubject, Observable, Subject, Subscription} from 'rxjs'; | ||
import {AuthService} from './auth.service'; | ||
import {FastenApiService} from './fasten-api.service'; | ||
import {ToastService} from './toast.service'; | ||
import {Event} from '../models/events/event'; | ||
import {EventSourceComplete} from '../models/events/event_source_complete'; | ||
import {EventSourceSync} from '../models/events/event_source_sync'; | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class EventBusService { | ||
eventBusSubscription: Subscription | undefined; | ||
eventBusSourceSyncMessages: Subject<EventSourceSync> = new Subject<EventSourceSync>(); | ||
eventBusSourceCompleteMessages: Subject<EventSourceComplete> = new Subject<EventSourceComplete>(); | ||
|
||
constructor( | ||
public router: Router, | ||
public authService: AuthService, | ||
public fastenApiService: FastenApiService, | ||
public toastService: ToastService | ||
) { | ||
|
||
// Ideally we want consistently listen to events, but only when the user is authenticated. | ||
//TODO: find a way to abort the event bus connection. | ||
this.authService.IsAuthenticatedSubject.subscribe((isAuthenticated) => { | ||
console.log("isAuthenticated changed:", isAuthenticated) | ||
if(isAuthenticated){ | ||
this.eventBusSubscription = this.fastenApiService.listenEventBus().subscribe((event: Event | EventSourceSync | EventSourceComplete)=>{ | ||
console.log("eventbus event:", event) | ||
//TODO: start toasts. | ||
if(event.event_type == "source_sync"){ | ||
this.eventBusSourceSyncMessages.next(event as EventSourceSync) | ||
} else if(event.event_type == "source_complete"){ | ||
this.eventBusSourceCompleteMessages.next(event as EventSourceComplete) | ||
} | ||
}) | ||
} else { | ||
//no longer authenticated, unsubscribe from eventbus | ||
if(this.eventBusSubscription){ | ||
this.eventBusSubscription.unsubscribe() | ||
} | ||
} | ||
}); | ||
} | ||
} |
This file contains 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