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 SkipNoticeComponent from "./components/SkipNoticeComponent";
import SubmissionNotice from "./render/SubmissionNotice";
import { Message, MessageResponse } from "./messageTypes";
// Hack to get the CSS loaded on permission-based sites (Invidious)
utils.wait(() => Config.config !== null, 5000, 10).then(addCSS);
@ -27,7 +28,7 @@ let sponsorVideoID: VideoID = null;
// JSON video info
let videoInfo: any = null;
//the channel this video is about
let channelID;
let channelID: string;
// Skips are scheduled to ensure precision.
// Skips are rescheduled every seeking event.
@ -112,7 +113,7 @@ const skipNoticeContentContainer: ContentContainer = () => ({
//get messages from the background script and the popup
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
switch(request.message){
case "update":
@ -169,7 +170,6 @@ function messageListener(request: any, sender: any, sendResponse: (response: any
break;
case "submitTimes":
submitSponsorTimes();
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 it isn't visible, there is no data
@ -1410,7 +1410,7 @@ function dontShowNoticeAgain() {
closeAllSkipNotices();
}
function sponsorMessageStarted(callback) {
function sponsorMessageStarted(callback: (response: MessageResponse) => void) {
video = document.querySelector('video');
//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,11 +2,12 @@ import Config from "./config";
import Utils from "./utils";
import { SponsorTime, SponsorHideType } from "./types";
import { Message, MessageResponse } from "./messageTypes";
const utils = new Utils();
interface MessageListener {
(request: any, sender: any, callback: (response: any) => void): void;
}
(request: Message, sender: unknown, sendResponse: (response: MessageResponse) => void): void;
}
class MessageHandler {
messageListener: MessageListener;
@ -15,7 +16,7 @@ class MessageHandler {
this.messageListener = messageListener;
}
sendMessage(id: number, request, callback?) {
sendMessage(id: number, request: Message, callback?) {
if (this.messageListener) {
this.messageListener(request, null, callback);
} else {

View file

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