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 $ 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

View File

@ -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<Thread>
posts: SimpleDict<Post>
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,

View File

@ -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
}

View File

@ -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);
}
}

View File

@ -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)
}
}

View File

@ -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
}

View File

@ -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<T> {
keys: string[]
@ -13,7 +7,7 @@ export default class SimpleDict<T> {
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<T> {
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<T> {
}
}
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 {