refactor(types): add strong types to messages

This commit is contained in:
Max Baumann 2020-12-15 19:37:48 +01:00
parent 70667a43d7
commit cd4f5fc667
No known key found for this signature in database
GPG key ID: 82EEA14C1523D1BB
4 changed files with 75 additions and 11 deletions

View file

@ -12,6 +12,7 @@ import PreviewBar from "./js-components/previewBar";
import SkipNotice from "./render/SkipNotice"; import SkipNotice from "./render/SkipNotice";
import SkipNoticeComponent from "./components/SkipNoticeComponent"; import SkipNoticeComponent from "./components/SkipNoticeComponent";
import SubmissionNotice from "./render/SubmissionNotice"; import SubmissionNotice from "./render/SubmissionNotice";
import { Message, MessageResponse } from "./messageTypes";
// Hack to get the CSS loaded on permission-based sites (Invidious) // Hack to get the CSS loaded on permission-based sites (Invidious)
utils.wait(() => Config.config !== null, 5000, 10).then(addCSS); utils.wait(() => Config.config !== null, 5000, 10).then(addCSS);
@ -27,7 +28,7 @@ let sponsorVideoID: VideoID = null;
// JSON video info // JSON video info
let videoInfo: any = null; let videoInfo: any = null;
//the channel this video is about //the channel this video is about
let channelID; let channelID: string;
// Skips are scheduled to ensure precision. // Skips are scheduled to ensure precision.
// Skips are rescheduled every seeking event. // Skips are rescheduled every seeking event.
@ -112,7 +113,7 @@ const skipNoticeContentContainer: ContentContainer = () => ({
//get messages from the background script and the popup //get messages from the background script and the popup
chrome.runtime.onMessage.addListener(messageListener); chrome.runtime.onMessage.addListener(messageListener);
function messageListener(request: any, sender: any, sendResponse: (response: any) => void): void { function messageListener(request: Message, sender: unknown, sendResponse: (response: MessageResponse) => void): void {
//messages from popup script //messages from popup script
switch(request.message){ switch(request.message){
case "update": case "update":
@ -169,7 +170,6 @@ function messageListener(request: any, sender: any, sendResponse: (response: any
break; break;
case "submitTimes": case "submitTimes":
submitSponsorTimes(); submitSponsorTimes();
break; break;
} }
} }
@ -1209,7 +1209,7 @@ function updateSponsorTimesSubmitting(getFromConfig = true) {
} }
} }
async function changeStartSponsorButton(showStartSponsor, uploadButtonVisible) { async function changeStartSponsorButton(showStartSponsor: boolean, uploadButtonVisible: boolean): Promise<boolean> {
if(!sponsorVideoID) return false; if(!sponsorVideoID) return false;
//if it isn't visible, there is no data //if it isn't visible, there is no data
@ -1410,7 +1410,7 @@ function dontShowNoticeAgain() {
closeAllSkipNotices(); closeAllSkipNotices();
} }
function sponsorMessageStarted(callback) { function sponsorMessageStarted(callback: (response: MessageResponse) => void) {
video = document.querySelector('video'); video = document.querySelector('video');
//send back current time //send back current time

63
src/messageTypes.ts Normal file
View file

@ -0,0 +1,63 @@
//
// Message and Response Types
//
import { SponsorTime } from "./types";
interface BaseMessage {
from?: string;
}
interface DefaultMessage {
message:
"update"
| "sponsorStart"
| "sponsorDataChanged"
| "isInfoFound"
| "getVideoID"
| "getChannelID"
| "isChannelWhitelisted"
| "submitTimes";
}
interface BoolValueMessage {
message: "whitelistChange";
value: boolean;
}
interface ChangeStartSponsorButtonMessage {
message: "changeStartSponsorButton";
showStartSponsor: boolean;
uploadButtonVisible: boolean;
}
export type Message = BaseMessage & (DefaultMessage | BoolValueMessage | ChangeStartSponsorButtonMessage);
interface IsInfoFoundMessageResponse {
found: boolean;
sponsorTimes: SponsorTime[];
}
interface GetVideoIdResponse {
videoID: string;
}
interface GetChannelIDResponse {
channelID: string;
}
interface SponsorStartResponse {
time: number;
}
interface IsChannelWhitelistedResponse {
value: boolean;
}
export type MessageResponse =
IsInfoFoundMessageResponse
| GetVideoIdResponse
| GetChannelIDResponse
| SponsorStartResponse
| IsChannelWhitelistedResponse;

View file

@ -2,10 +2,11 @@ import Config from "./config";
import Utils from "./utils"; import Utils from "./utils";
import { SponsorTime, SponsorHideType } from "./types"; import { SponsorTime, SponsorHideType } from "./types";
import { Message, MessageResponse } from "./messageTypes";
const utils = new Utils(); const utils = new Utils();
interface MessageListener { interface MessageListener {
(request: any, sender: any, callback: (response: any) => void): void; (request: Message, sender: unknown, sendResponse: (response: MessageResponse) => void): void;
} }
class MessageHandler { class MessageHandler {
@ -15,7 +16,7 @@ class MessageHandler {
this.messageListener = messageListener; this.messageListener = messageListener;
} }
sendMessage(id: number, request, callback?) { sendMessage(id: number, request: Message, callback?) {
if (this.messageListener) { if (this.messageListener) {
this.messageListener(request, null, callback); this.messageListener(request, null, callback);
} else { } else {

View file

@ -3,7 +3,7 @@ import SkipNoticeComponent from "./components/SkipNoticeComponent";
interface ContentContainer { interface ContentContainer {
(): { (): {
vote: (type: any, UUID: any, category?: string, skipNotice?: SkipNoticeComponent) => void, vote: (type: number, UUID: string, category?: string, skipNotice?: SkipNoticeComponent) => void,
dontShowNoticeAgain: () => void, dontShowNoticeAgain: () => void,
unskipSponsorTime: (segment: SponsorTime) => void, unskipSponsorTime: (segment: SponsorTime) => void,
sponsorTimes: SponsorTime[], sponsorTimes: SponsorTime[],
@ -15,9 +15,9 @@ interface ContentContainer {
onMobileYouTube: boolean, onMobileYouTube: boolean,
sponsorSubmissionNotice: SubmissionNotice, sponsorSubmissionNotice: SubmissionNotice,
resetSponsorSubmissionNotice: () => void, resetSponsorSubmissionNotice: () => void,
changeStartSponsorButton: (showStartSponsor: any, uploadButtonVisible: any) => Promise<boolean>, changeStartSponsorButton: (showStartSponsor: boolean, uploadButtonVisible: boolean) => Promise<boolean>,
previewTime: (time: number, unpause?: boolean) => void, previewTime: (time: number, unpause?: boolean) => void,
videoInfo: any, videoInfo: VideoInfo,
getRealCurrentTime: () => number getRealCurrentTime: () => number
} }
} }