More refactoring

This commit is contained in:
MindfulMinun committed 2022-07-16 21:50:54 -05:00
1 parent 524776ef9d
commit ad223ef14b
3 files changed
+30 -18

No files matched your search

+1 -1
View File
@@ -45,7 +45,7 @@ export function html(strings: TemplateStringsArray | string, ...exprs: unknown[]
continue
}
// Otherwise, just join the expressions together, escaping dynamic content
out += strings[i] + (exprs[i] != null ? xss("" + exprs[i]) : "")
out += strings[i] + (typeof exprs[i] != 'undefined' ? xss("" + exprs[i]) : "")
}
temp.innerHTML = out
const clone = temp.content.cloneNode(true) as DocumentFragment
+13 -10
View File
@@ -7,8 +7,8 @@ export const HTML_ESCAPES = {
">": ">",
'"': """,
"'": "'",
"/": "/"
}
"/": "/",
} as const
/**
* Removes indentation from multiline strings.
@@ -75,24 +75,27 @@ export function templateNoop(
templ: TemplateStringsArray | string,
...values: unknown[]
): string {
const strings = Array.from(typeof templ === 'string' ? [templ] : templ)
return strings.reduce((result, currentString, i) =>
result + currentString + (values[i] != null ? values[i] : ''),
if (typeof templ === 'string') return templ
return templ.reduce((result, currentString, i) =>
result + currentString + (typeof values[i] !== 'undefined' ? values[i] : ''),
'' // Start with the empty string
)
}
/**
* Promise that resolves after timeout
* Promise that resolves after timeout, optionally with a value to resolve with.
*
* ```js
* @example
* // Play hide and seek, count to 100 (seconds)
* // Both methods are identical
* wait(100e3).then(() => "Ready or not, here I come!").then(seek)
* ```
* wait(100e3, "Ready or not, here I come!").then(seek)
* @since 2020-06-23
*/
export function wait(timeout: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, timeout))
export function wait(timeout: number): Promise<void>
export function wait<T>(timeout: number, resolveWith: T): Promise<T>
export function wait<T>(timeout: number, resolveWith?: T): Promise<T | void> {
return new Promise(resolve => setTimeout(() => resolve(resolveWith), timeout))
}
/**
+16 -7
View File
@@ -239,8 +239,8 @@ export async function once<T>(source: AsyncIterable<T>, predicate: (value: T) =>
* // When awaited, this function returns a promise that resolves to
* // an *OPEN* WebSocket
* () => {
* const sock = new WebSocket()
* const [p, res, rej] = pinkyPromise()
* const sock = new WebSocket('wss://wss.example.com')
* const [p, res, rej] = pinkyPromise<typeof sock>()
* sock.onerror = err => rej(err)
* sock.onopen = () => res(sock)
* return p
@@ -249,15 +249,24 @@ export async function once<T>(source: AsyncIterable<T>, predicate: (value: T) =>
* @author MindfulMinun
* @since 2022-06-05
*/
export function pinkyPromise<T>(): [Promise<T>, (value: T) => void, (error: unknown) => unknown] {
let resolve: (value: T) => void
export function pinkyPromise(): [Promise<void>, () => void, (reason?: unknown) => void]
export function pinkyPromise<T>(): [Promise<T>, (value: T) => void, (reason?: unknown) => void]
export function pinkyPromise<T>(): [
Promise<T | void>,
(value?: T | PromiseLike<T>) => void,
(reason?: unknown) => void
] {
let resolve: (value?: T | PromiseLike<T>) => void
let reject: (error: unknown) => void
// This works because callbacks to the Promise constructor are called synchronously.
const p = new Promise<T>((yay, nay) => {
resolve = yay
reject = nay
const p = new Promise<T | void>((yay, nay) => {
resolve = value => yay(value)
reject = reason => nay(reason)
})
// HACK: This works because callbacks to the Promise constructor are called synchronously.
// In other words, they're immediately invoked, so they're available immediately after
// the assignment to `p`
return [p, resolve!, reject!]
}