Skip to content

Commit

Permalink
Move workers into services
Browse files Browse the repository at this point in the history
  • Loading branch information
chidozieononiwu committed Apr 19, 2024
1 parent 839a0b6 commit d50ab47
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import { CommentItemModel, ReviewContent, } from 'src/app/_models/review';
import { APIRevision, CodeHuskNode, CreateLinesOfTokensMessage, ReviewPageWorkerMessageDirective } from 'src/app/_models/revision';
import { CommentsService } from 'src/app/_services/comments/comments.service';
import { ReviewsService } from 'src/app/_services/reviews/reviews.service';
import { WorkerService } from 'src/app/_services/worker/worker.service';

@Component({
selector: 'app-review-page',
templateUrl: './review-page.component.html',
styleUrls: ['./review-page.component.scss']
})
export class ReviewPageComponent implements OnInit, OnDestroy, AfterViewInit {
export class ReviewPageComponent implements OnInit, AfterViewInit {
reviewId : string | null = null;
activeApiRevisionId : string | null = null;
diffApiRevisionId : string | null = null;
Expand All @@ -21,23 +22,18 @@ export class ReviewPageComponent implements OnInit, OnDestroy, AfterViewInit {
reviewComments : CommentItemModel[] | undefined = [];
revisionSidePanel : boolean | undefined = undefined;
reviewPageNavigation : TreeNode[] = [];
apiTreeBuilder: Worker | undefined = undefined;
tokenBuilder: Worker | undefined = undefined;
apiTreeNodeData: BehaviorSubject<CodeHuskNode | null> = new BehaviorSubject<CodeHuskNode | null>(null);
tokenLineData: BehaviorSubject<CreateLinesOfTokensMessage | null> = new BehaviorSubject<CreateLinesOfTokensMessage | null>(null);

sideMenu: MenuItem[] | undefined;

constructor(private route: ActivatedRoute, private reviewsService: ReviewsService, private commentsService: CommentsService) {}
constructor(private route: ActivatedRoute, private reviewsService: ReviewsService, private commentsService: CommentsService, private workerService: WorkerService) {}

ngOnInit() {
this.reviewId = this.route.snapshot.paramMap.get('reviewId');
this.activeApiRevisionId = this.route.snapshot.queryParamMap.get('activeApiRevisionId');
this.diffApiRevisionId = this.route.snapshot.queryParamMap.get('diffApiRevisionId');

this.apiTreeBuilder = new Worker(new URL('../../_workers/review-page.worker', import.meta.url));
this.tokenBuilder = new Worker(new URL('../../_workers/review-page.worker', import.meta.url));

this.registerWorkerEventHandler();
this.loadReviewContent(this.reviewId!, this.activeApiRevisionId, this.diffApiRevisionId);

Expand All @@ -62,34 +58,29 @@ export class ReviewPageComponent implements OnInit, OnDestroy, AfterViewInit {
});
}

ngOnDestroy() {
this.apiTreeBuilder!.terminate();
this.tokenBuilder!.terminate();
}

registerWorkerEventHandler() {
this.apiTreeBuilder!.onmessage = ({ data }) => {
this.workerService.onMessageFromApiTreeBuilder().subscribe(data => {
if (data.directive === ReviewPageWorkerMessageDirective.CreatePageNavigation) {
this.reviewPageNavigation = data.navTree as TreeNode[];
}

if (data.directive === ReviewPageWorkerMessageDirective.PassToTokenBuilder) {
data.directive = ReviewPageWorkerMessageDirective.BuildTokens;
this.tokenBuilder!.postMessage(data);
this.workerService.postToTokenBuilder(data);
}

if (data.directive === ReviewPageWorkerMessageDirective.CreateCodeLineHusk) {
if (data.nodeData) {
this.apiTreeNodeData.next(data.nodeData);
}
}
};
});

this.tokenBuilder!.onmessage = ({ data }) => {
this.workerService.onMessageFromTokenBuilder().subscribe(data => {
if (data.directive === ReviewPageWorkerMessageDirective.CreateLineOfTokens) {
this.tokenLineData.next(data);
}
}
});
}

loadReviewContent(reviewId: string, activeApiRevisionId: string | null = null, diffApiRevisionId: string | null = null) {
Expand All @@ -100,7 +91,7 @@ export class ReviewPageComponent implements OnInit, OnDestroy, AfterViewInit {
directive: ReviewPageWorkerMessageDirective.BuildAPITree,
apiTree : this.reviewContent!.apiForest
};
this.apiTreeBuilder!.postMessage(message);
this.workerService.postToApiTreeBuilder(message);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';

import { WorkerService } from './worker.service';

describe('WorkerService', () => {
let service: WorkerService;

beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(WorkerService);
});

it('should be created', () => {
expect(service).toBeTruthy();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';

@Injectable({
providedIn: 'root'
})
export class WorkerService {
private apiTreeBuilder : Worker;
private tokenBuilder : Worker;

constructor() {
this.apiTreeBuilder = new Worker(new URL('../../_workers/review-page.worker', import.meta.url));
this.tokenBuilder = new Worker(new URL('../../_workers/review-page.worker', import.meta.url));
}

postToApiTreeBuilder(message: any) {
this.apiTreeBuilder.postMessage(message);
}

postToTokenBuilder(message: any) {
this.tokenBuilder.postMessage(message);
}

onMessageFromApiTreeBuilder(): Observable<any> {
return new Observable(observer => {
this.apiTreeBuilder.onmessage = ({ data }) => {
observer.next(data);
};
});
}

onMessageFromTokenBuilder(): Observable<any> {
return new Observable(observer => {
this.tokenBuilder.onmessage = ({ data }) => {
observer.next(data);
};
});
}
}

0 comments on commit d50ab47

Please sign in to comment.