Merge pull request #582 from FoseFx/fosefx-ts-msg

Types for messages
This commit is contained in:
Ajay Ramachandran 2020-12-19 14:50:53 -05:00 committed by GitHub
commit 1bab5063aa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 71 additions and 7 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);
@ -115,7 +116,7 @@ const skipNoticeContentContainer: ContentContainer = () => ({
//get messages from the background script and the popup
chrome.runtime.onMessage.addListener(messageListener);
function messageListener(request: any, sender: unknown, 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":
@ -172,7 +173,6 @@ function messageListener(request: any, sender: unknown, sendResponse: (response:
break;
case "submitTimes":
submitSponsorTimes();
break;
}
}
@ -1222,7 +1222,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
@ -1423,7 +1423,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: unknown, 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 {