From 0eb1f61d43044dcd2fe52e9d9e7e3b42968d90c9 Mon Sep 17 00:00:00 2001 From: Lalle <29478339+LalleSX@users.noreply.github.com> Date: Wed, 19 Apr 2023 04:14:28 +0200 Subject: [PATCH] Added types to classes --- src/General/Get.js | 7 ---- src/classes/{Board.js => Board.ts} | 14 ++++++-- .../{CatalogThread.js => CatalogThread.ts} | 6 ++++ ...ThreadNative.js => CatalogThreadNative.ts} | 11 ++++-- src/classes/{Connection.js => Connection.ts} | 35 ++++++++++--------- src/classes/{ShimSet.js => ShimSet.ts} | 10 +++--- src/classes/SimpleDict.ts | 16 +++------ 7 files changed, 55 insertions(+), 44 deletions(-) rename src/classes/{Board.js => Board.ts} (68%) rename src/classes/{CatalogThread.js => CatalogThread.ts} (72%) rename src/classes/{CatalogThreadNative.js => CatalogThreadNative.ts} (62%) rename src/classes/{Connection.js => Connection.ts} (51%) rename src/classes/{ShimSet.js => ShimSet.ts} (80%) diff --git a/src/General/Get.js b/src/General/Get.js index 23e37d4..8478690 100644 --- a/src/General/Get.js +++ b/src/General/Get.js @@ -1,13 +1,6 @@ import { Conf, g } from '../globals/globals' import $ from '../platform/$' -/* - * decaffeinate suggestions: - * DS101: Remove unnecessary use of Array.from - * DS102: Remove unnecessary code created because of implicit returns - * DS207: Consider shorter variations of null checks - * Full docs: https://github.com/decaffeinate/decaffeinate/blob/main/docs/suggestions.md - */ var Get = { url(type, IDs, ...args) { let f, site diff --git a/src/classes/Board.js b/src/classes/Board.ts similarity index 68% rename from src/classes/Board.js rename to src/classes/Board.ts index 5b68197..994b28e 100644 --- a/src/classes/Board.js +++ b/src/classes/Board.ts @@ -1,6 +1,8 @@ import BoardConfig from '../General/BoardConfig' import { d, g } from '../globals/globals' import SimpleDict from './SimpleDict' +import Thread from './Thread' +import Post from './Post' /* * decaffeinate suggestions: @@ -8,6 +10,12 @@ import SimpleDict from './SimpleDict' * Full docs: https://github.com/decaffeinate/decaffeinate/blob/main/docs/suggestions.md */ export default class Board { + ID: string + boardID: string + siteID: string + threads: SimpleDict + posts: SimpleDict + config: string | { cooldowns: { threads: number; replies: number; images: number; }; } toString() { return this.ID } @@ -20,12 +28,12 @@ export default class Board { this.posts = new SimpleDict() this.config = BoardConfig.boards?.[this.ID] || {} - g.boards[this] = this + g.boards[this.ID] = this } cooldowns() { - const c2 = (this.config || {}).cooldowns || {} - const c = { + const c2: { threads: number; replies: number; images: number; } = this.config.cooldowns || {} + const c: { thread: number; reply: number; image: number; thread_global: number; } = { thread: c2.threads || 0, reply: c2.replies || 0, image: c2.images || 0, diff --git a/src/classes/CatalogThread.js b/src/classes/CatalogThread.ts similarity index 72% rename from src/classes/CatalogThread.js rename to src/classes/CatalogThread.ts index 829ec4d..d4e1d96 100644 --- a/src/classes/CatalogThread.js +++ b/src/classes/CatalogThread.ts @@ -1,6 +1,12 @@ import $ from '../platform/$' +import Board from './Board' +import Thread from './Thread' export default class CatalogThread { + ID: string + thread: Thread + board: Board + nodes: { root: any; thumb: any; icons: any; postCount: any; fileCount: any; pageCount: any; replies: any } toString() { return this.ID } diff --git a/src/classes/CatalogThreadNative.js b/src/classes/CatalogThreadNative.ts similarity index 62% rename from src/classes/CatalogThreadNative.js rename to src/classes/CatalogThreadNative.ts index 38de62a..d13c863 100644 --- a/src/classes/CatalogThreadNative.js +++ b/src/classes/CatalogThreadNative.ts @@ -4,17 +4,22 @@ import Board from './Board' import Thread from './Thread' export default class CatalogThreadNative { + boardID: Board + nodes: { root: any; thumb: any } + siteID: number + threadID: number + ID: string toString() { return this.ID } - constructor(root) { + constructor(root: HTMLElement) { const thumb = $(g.SITE.selectors.catalog.thumb, root); this.nodes = { root, thumb }; this.siteID = g.SITE.ID; this.boardID = thumb.parentNode.pathname.split(/\/+/)[1]; - this.board = g.boards[this.boardID] ?? new Board(this.boardID); + this.boardID = g.boards[g.BOARD.ID] ?? new Board(this.boardID); this.ID = this.threadID = +root.dataset.id || $(root).data('id'); - this.thread = this.board.threads.get(this.ID) ?? new Thread(this.ID, this.board); + this.threadID = this.boardID.threads.get(this.ID) ?? new Thread(this.ID, g.BOARD.ID); } } diff --git a/src/classes/Connection.js b/src/classes/Connection.ts similarity index 51% rename from src/classes/Connection.js rename to src/classes/Connection.ts index 647cfb9..2178de6 100644 --- a/src/classes/Connection.js +++ b/src/classes/Connection.ts @@ -1,13 +1,16 @@ import $ from '../platform/$' import { g } from '../globals/globals' -/* - * decaffeinate suggestions: - * DS102: Remove unnecessary code created because of implicit returns - * Full docs: https://github.com/decaffeinate/decaffeinate/blob/main/docs/suggestions.md - */ +interface ConnectionCallback { + [key: string]: (value: any) => void +} + export default class Connection { - constructor(target, origin, cb = {}) { + private target: Window | HTMLIFrameElement + private origin: string + private cb: ConnectionCallback + + constructor(target: Window | HTMLIFrameElement, origin: string, cb: ConnectionCallback = {}) { this.send = this.send.bind(this) this.onMessage = this.onMessage.bind(this) this.target = target @@ -16,22 +19,22 @@ export default class Connection { $.on(window, 'message', this.onMessage) } - targetWindow() { - if (this.target instanceof window.HTMLIFrameElement) { - return this.target.contentWindow + private targetWindow(): Window { + if (this.target instanceof HTMLIFrameElement) { + return this.target.contentWindow as Window } else { - return this.target + return this.target as Window } } - send(data) { - return this.targetWindow().postMessage( + public send(data: any): void { + this.targetWindow().postMessage( `${g.NAMESPACE}${JSON.stringify(data)}`, this.origin, ) } - onMessage(e) { + public onMessage(e: MessageEvent): void { if ( e.source !== this.targetWindow() || e.origin !== this.origin || @@ -41,9 +44,9 @@ export default class Connection { return } const data = JSON.parse(e.data.slice(g.NAMESPACE.length)) - for (var type in data) { - var value = data[type] - if ($.hasOwn(this.cb, type)) { + for (const type in data) { + const value = data[type] + if (Object.prototype.hasOwnProperty.call(this.cb, type)) { this.cb[type](value) } } diff --git a/src/classes/ShimSet.js b/src/classes/ShimSet.ts similarity index 80% rename from src/classes/ShimSet.js rename to src/classes/ShimSet.ts index e011569..6ab0081 100644 --- a/src/classes/ShimSet.js +++ b/src/classes/ShimSet.ts @@ -5,21 +5,23 @@ */ import $ from '../platform/$' class ShimSet { + elements: string + size: number constructor() { this.elements = $.cache() this.size = 0 } - has(value) { - return value in this.elements + has(value: string) { + return !!this.elements[value] } - add(value) { + add(value: string) { if (this.elements[value]) { return } this.elements[value] = true return this.size++ } - delete(value) { + delete(value: string) { if (!this.elements[value]) { return } diff --git a/src/classes/SimpleDict.ts b/src/classes/SimpleDict.ts index fd4e3e3..aeba701 100644 --- a/src/classes/SimpleDict.ts +++ b/src/classes/SimpleDict.ts @@ -1,11 +1,5 @@ import $ from '../platform/$' -/* - * decaffeinate suggestions: - * DS101: Remove unnecessary use of Array.from - * DS102: Remove unnecessary code created because of implicit returns - * Full docs: https://github.com/decaffeinate/decaffeinate/blob/main/docs/suggestions.md - */ export default class SimpleDict { keys: string[] @@ -13,7 +7,7 @@ export default class SimpleDict { this.keys = [] } - push(key, data: T) { + push(key: string, data: T) { key = `${key}` if (!this[key]) { this.keys.push(key) @@ -21,8 +15,8 @@ export default class SimpleDict { return (this[key] = data) } - rm(key) { - let i + rm(key: string) { + let i: number key = `${key}` if ((i = this.keys.indexOf(key)) !== -1) { this.keys.splice(i, 1) @@ -30,13 +24,13 @@ export default class SimpleDict { } } - forEach(fn) { + forEach(fn: (data: T) => void) { for (var key of [...Array.from(this.keys)]) { fn(this[key]) } } - get(key): T { + get(key: string): T | undefined { if (key === 'keys') { return undefined } else {