interface StrInterface {
text: string
word: string
}
class Str implements StrInterface {
text = 'hello'
word: string = ''
}
Str.prototype.word = 'word'
const s = new Str()
console.log(s.text)
console.log(s.word)
setInterval(() => {
++count
Str.prototype.word = count % 2 == 0 ? 'word' : 'not word'
}, 1000)
Str.prototype.word = 'word'
import { useState } from 'react'
class Signal<T> {
#value: T
#dispatchers: Set<React.Dispatch<React.SetStateAction<T>>> = new Set()
constructor(value: T){
this.#value = value
}
get current(): T {
return this.#value
}
set current(value: T){
if(this.#value !== value){
this.#value = value
for(const dispatcher of this.#dispatchers){
dispatcher(this.#value)
}
}
}
subscribe(dispatcher: React.Dispatch<React.SetStateAction<T>>): void {
if(!(this.#dispatchers.has(dispatcher))){
this.#dispatchers.add(dispatcher)
}
}
}
const createSignal = <T>(value: T): Signal<T> => new Signal(value)
const useSignal = <T>(signal: Signal<T>): Signal<T> => {
const [, dispatcher] = useState(signal.current)
signal.subscribe(dispatcher)
return signal
}
export { createSignal, useSignal }
import { createSignal } from '../lib/signals.ts'
export const counter = createSignal(0)
import { useSignal } from '../lib/signals.ts'
import { counter } from './shared_signals.ts'
export const Button = () => {
const _counter = useSignal(counter)
return (
<button onClick={ () => ++_counter.current }>
Count is { _counter.current }
</button>
)
}
import { hydrateRoot } from 'react-dom/client'
import { Layout } from './Layout.tsx'
import { Button } from './Button.tsx'
hydrateRoot(
document,
<Layout>
<Button/>
<Button/>
<Button/>
<Button/>
<Button/>
<Button/>
<Button/>
<Button/>
<Button/>
<Button/>
</Layout>
)
exec('zstd -10', { encoding: 'buffer', maxBuffer: 1024 * 4096 }) // x4
maxBuffer : Largest amount of data in bytes allowed on stdout or stderr. If exceeded, the child process is terminated and any output is truncated. See caveat at maxBuffer and Unicode. Default: 1024 * 1024.
<body>
<select>
<option>1</option>
<option>2</option>
</select>
<input class="select-input" type="hidden"/>
<script>
const select = document.querySelector('select')
const hiddenInput = document.querySelector('.select-input')
const hiddenInputEvent = new InputEvent('change')
select?.addEventListener('change', e => {
hiddenInput.value = e.target.value
hiddenInput.dispatchEvent(hiddenInputEvent)
})
hiddenInput?.addEventListener('change', e => {
console.log(hiddenInput.value)
})
</script>
</body>
const select = document.querySelector('select')
const hiddenInput = document.querySelector('.select-input')
if(select && hiddenInput){
select.onchange = (e) => {
hiddenInput.value = e.target.value
hiddenInput.onchange?.()
}
hiddenInput.onchange = (e) => {
console.log(hiddenInput.value)
}
}
const { promise, resolve, reject } = Promise.withResolvers()
const getProducts = () => fetch('https://fakestoreapi.com/products')
try {
resolve(
(await getProducts()).json()
)
} catch (e) {
reject(e)
}
console.log(
await promise
)
import { useLoaderData } from 'react-router-dom'
type LoaderProps = {
themes: {
all: string[],
selectedId: string
}
} | null
export const Component = () => {
const data = useLoaderData() as LoaderProps
return data?.themes ? (
<>
Selected theme Id: { data.themes.selectedId }
All themes: { JSON.stringify(data.themes.all) }
</>
) : (
<>No themes</>
)
}
///
export const routes = [
{
path: '/',
element: <Component/>,
loader: () => ({
themes: {
all: [
'light',
'dark'
],
selectedId: 'light'
}
})
}
/* ... */
]