This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import React from 'react' | |
| /** | |
| * A variant of `React.useState` which allows the `inputState` to change over time as well. | |
| * Important: This hook is synchronous / single-render-pass (i.e. doesn't use `useEffect` or `setState` directly). | |
| */ | |
| const useStateWithReactiveInput = <T>(inputState: T): [T, (newState: T | ((prev: T) => T)) => void] => { | |
| const [_, rerender] = React.useState(0) | |
| const stateRef = React.useMemo<{ current: T }>(() => ({ current: inputState }), []) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Internal application error: TypeError: cannot read headers: request closed | |
| at Object.get headerList (ext:deno_fetch/23_request.js:115:17) | |
| at Request.request.<computed> (ext:deno_fetch/23_request.js:565:60) | |
| at Request.get [headers] (ext:deno_fetch/23_request.js:252:46) | |
| at Request.get headers (ext:deno_fetch/23_request.js:452:16) | |
| at NativeRequest.get headers (https://deno.land/x/[email protected]/http_server_native_request.ts:72:26) | |
| at new Request (https://deno.land/x/[email protected]/request.ts:150:21) | |
| at new Context (https://deno.land/x/[email protected]/context.ts:172:20) | |
| at Application.#handleRequest (https://deno.land/x/[email protected]/application.ts:450:17) | |
| at Application.listen (https://deno.land/x/[email protected]/application.ts:656:28) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { effect } from '@preact/signals-core' | |
| export function effectWithPreviousValue<T>( | |
| initialValue: T, | |
| compute: (prev: T) => T | |
| ): () => void { | |
| let prev = initialValue | |
| return effect(() => { | |
| prev = compute(prev) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { signal } from '@preact/signals' | |
| export type ComplexSignal<T> = { | |
| value: T | |
| peek: T | |
| subscribe: (cb: (t: T) => void) => () => void | |
| update: (cb: (t: T) => void) => void | |
| } | |
| export function complexSignal<T>(initialValue: T): ComplexSignal<T> { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| document.querySelectorAll('button[aria-label="Toggle diff contents"]').forEach(b => b.click()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const bytes = await Deno.readFile(new URL('./thumbnail.png', import.meta.url)) | |
| const encoder = new TextEncoder() | |
| const contents = encoder.encode( | |
| `export const thumbnail = new Blob(new Uint8Array(${ | |
| JSON.stringify(Array.from(bytes)) | |
| }), { type: 'image/png' })` | |
| ) | |
| await Deno.writeFile(new URL('./hardcoded-thumbnail.ts', import.meta.url), contents) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const status = 301 | |
| addEventListener('fetch', event => { | |
| const url = new URL(event.request.url) | |
| url.hostname = url.hostname.replace(/^www\./, '') | |
| event.respondWith(Response.redirect(url.toString(), status)) | |
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { assertEquals } from 'https://deno.land/[email protected]/testing/asserts.ts' | |
| Deno.test('async iterator throw in next should throw in for scope', async () => { | |
| const results: number[] = [] | |
| const numbers = (() => { | |
| let counter = 0 | |
| const iterator: AsyncIterator<number> = { | |
| // deno-lint-ignore require-await |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export class Deferred<T = void, E = Error> { | |
| #promise: Promise<T> | |
| // deno-lint-ignore ban-ts-comment | |
| // @ts-ignore | |
| resolve: (arg: T) => void | |
| // deno-lint-ignore ban-ts-comment | |
| // @ts-ignore | |
| reject: (arg: E) => void | |
| constructor() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| console.log('Fetching...') | |
| const res = await fetch('https://example.com') | |
| const body = new Uint8Array(await res.arrayBuffer()) | |
| console.log('---') | |
| await Deno.stdout.write(body) | |
| console.log('---') | |
| console.log('Done.') |