Added types to classes

This commit is contained in:
Lalle 2023-04-19 04:14:28 +02:00
parent 241ff1eb7a
commit 0eb1f61d43
No known key found for this signature in database
GPG Key ID: A6583D207A8F6B0D
7 changed files with 55 additions and 44 deletions

View File

@ -1,13 +1,6 @@
import { Conf, g } from '../globals/globals' import { Conf, g } from '../globals/globals'
import $ from '../platform/$' 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 = { var Get = {
url(type, IDs, ...args) { url(type, IDs, ...args) {
let f, site let f, site

View File

@ -1,6 +1,8 @@
import BoardConfig from '../General/BoardConfig' import BoardConfig from '../General/BoardConfig'
import { d, g } from '../globals/globals' import { d, g } from '../globals/globals'
import SimpleDict from './SimpleDict' import SimpleDict from './SimpleDict'
import Thread from './Thread'
import Post from './Post'
/* /*
* decaffeinate suggestions: * decaffeinate suggestions:
@ -8,6 +10,12 @@ import SimpleDict from './SimpleDict'
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/main/docs/suggestions.md * Full docs: https://github.com/decaffeinate/decaffeinate/blob/main/docs/suggestions.md
*/ */
export default class Board { export default class Board {
ID: string
boardID: string
siteID: string
threads: SimpleDict<Thread>
posts: SimpleDict<Post>
config: string | { cooldowns: { threads: number; replies: number; images: number; }; }
toString() { toString() {
return this.ID return this.ID
} }
@ -20,12 +28,12 @@ export default class Board {
this.posts = new SimpleDict() this.posts = new SimpleDict()
this.config = BoardConfig.boards?.[this.ID] || {} this.config = BoardConfig.boards?.[this.ID] || {}
g.boards[this] = this g.boards[this.ID] = this
} }
cooldowns() { cooldowns() {
const c2 = (this.config || {}).cooldowns || {} const c2: { threads: number; replies: number; images: number; } = this.config.cooldowns || {}
const c = { const c: { thread: number; reply: number; image: number; thread_global: number; } = {
thread: c2.threads || 0, thread: c2.threads || 0,
reply: c2.replies || 0, reply: c2.replies || 0,
image: c2.images || 0, image: c2.images || 0,

View File

@ -1,6 +1,12 @@
import $ from '../platform/$' import $ from '../platform/$'
import Board from './Board'
import Thread from './Thread'
export default class CatalogThread { 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() { toString() {
return this.ID return this.ID
} }

View File

@ -4,17 +4,22 @@ import Board from './Board'
import Thread from './Thread' import Thread from './Thread'
export default class CatalogThreadNative { export default class CatalogThreadNative {
boardID: Board
nodes: { root: any; thumb: any }
siteID: number
threadID: number
ID: string
toString() { toString() {
return this.ID return this.ID
} }
constructor(root) { constructor(root: HTMLElement) {
const thumb = $(g.SITE.selectors.catalog.thumb, root); const thumb = $(g.SITE.selectors.catalog.thumb, root);
this.nodes = { root, thumb }; this.nodes = { root, thumb };
this.siteID = g.SITE.ID; this.siteID = g.SITE.ID;
this.boardID = thumb.parentNode.pathname.split(/\/+/)[1]; 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.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);
} }
} }

View File

@ -1,13 +1,16 @@
import $ from '../platform/$' import $ from '../platform/$'
import { g } from '../globals/globals' import { g } from '../globals/globals'
/* interface ConnectionCallback {
* decaffeinate suggestions: [key: string]: (value: any) => void
* DS102: Remove unnecessary code created because of implicit returns }
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/main/docs/suggestions.md
*/
export default class Connection { 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.send = this.send.bind(this)
this.onMessage = this.onMessage.bind(this) this.onMessage = this.onMessage.bind(this)
this.target = target this.target = target
@ -16,22 +19,22 @@ export default class Connection {
$.on(window, 'message', this.onMessage) $.on(window, 'message', this.onMessage)
} }
targetWindow() { private targetWindow(): Window {
if (this.target instanceof window.HTMLIFrameElement) { if (this.target instanceof HTMLIFrameElement) {
return this.target.contentWindow return this.target.contentWindow as Window
} else { } else {
return this.target return this.target as Window
} }
} }
send(data) { public send(data: any): void {
return this.targetWindow().postMessage( this.targetWindow().postMessage(
`${g.NAMESPACE}${JSON.stringify(data)}`, `${g.NAMESPACE}${JSON.stringify(data)}`,
this.origin, this.origin,
) )
} }
onMessage(e) { public onMessage(e: MessageEvent): void {
if ( if (
e.source !== this.targetWindow() || e.source !== this.targetWindow() ||
e.origin !== this.origin || e.origin !== this.origin ||
@ -41,9 +44,9 @@ export default class Connection {
return return
} }
const data = JSON.parse(e.data.slice(g.NAMESPACE.length)) const data = JSON.parse(e.data.slice(g.NAMESPACE.length))
for (var type in data) { for (const type in data) {
var value = data[type] const value = data[type]
if ($.hasOwn(this.cb, type)) { if (Object.prototype.hasOwnProperty.call(this.cb, type)) {
this.cb[type](value) this.cb[type](value)
} }
} }

View File

@ -5,21 +5,23 @@
*/ */
import $ from '../platform/$' import $ from '../platform/$'
class ShimSet { class ShimSet {
elements: string
size: number
constructor() { constructor() {
this.elements = $.cache() this.elements = $.cache()
this.size = 0 this.size = 0
} }
has(value) { has(value: string) {
return value in this.elements return !!this.elements[value]
} }
add(value) { add(value: string) {
if (this.elements[value]) { if (this.elements[value]) {
return return
} }
this.elements[value] = true this.elements[value] = true
return this.size++ return this.size++
} }
delete(value) { delete(value: string) {
if (!this.elements[value]) { if (!this.elements[value]) {
return return
} }

View File

@ -1,11 +1,5 @@
import $ from '../platform/$' 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<T> { export default class SimpleDict<T> {
keys: string[] keys: string[]
@ -13,7 +7,7 @@ export default class SimpleDict<T> {
this.keys = [] this.keys = []
} }
push(key, data: T) { push(key: string, data: T) {
key = `${key}` key = `${key}`
if (!this[key]) { if (!this[key]) {
this.keys.push(key) this.keys.push(key)
@ -21,8 +15,8 @@ export default class SimpleDict<T> {
return (this[key] = data) return (this[key] = data)
} }
rm(key) { rm(key: string) {
let i let i: number
key = `${key}` key = `${key}`
if ((i = this.keys.indexOf(key)) !== -1) { if ((i = this.keys.indexOf(key)) !== -1) {
this.keys.splice(i, 1) this.keys.splice(i, 1)
@ -30,13 +24,13 @@ export default class SimpleDict<T> {
} }
} }
forEach(fn) { forEach(fn: (data: T) => void) {
for (var key of [...Array.from(this.keys)]) { for (var key of [...Array.from(this.keys)]) {
fn(this[key]) fn(this[key])
} }
} }
get(key): T { get(key: string): T | undefined {
if (key === 'keys') { if (key === 'keys') {
return undefined return undefined
} else { } else {