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
| interface UsePromise<a> { | |
| run: () => void | |
| result: a | |
| error: Error | |
| status: 'pending' | 'rejected' | 'resolved' | 'iddle' | |
| } | |
| const useQueuedPromise = <a,>(promise: () => Promise<a>): UsePromise<a> => { | |
| const [ result, setResult ] = React.useState<a>() | |
| const [ error, setError ] = React.useState<Error>() |
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 mutations_to_odata = <T>(entity: TrackedEntity<T>, model: string) : string => { | |
| const obj = { | |
| "@odata.type": model, | |
| } | |
| entity.mutations.forEach(m => { | |
| obj[m.prop] = m.value | |
| }) | |
| return JSON.stringify(obj, null, ' ') |
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
| // functors | |
| type Option<a> = { kind: 'some', value: a } | { kind: 'none' } | |
| type Result<a, e> = { kind: 'success', value: a } | { kind: 'error', error: e } | |
| type List<a> = { value: a, next: List<a> | null } | |
| type Reader<a, i> = (i: i) => a | |
| // units | |
| const some = <a>(value: a): Option<a> => ({ kind: 'some', value }) | |
| const success = <a, e>(value: a): Result<a, e> => ({ kind: 'success', value }) | |
| const list_of = <a>(value:a): List<a> => ({value, next: null}) |
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
| class BetterMap<k, v> extends Map<k, v> { | |
| update(key:k, updater: (v:v, k:k) => v, notset:v = undefined) { | |
| if(this.has(key)) this.set(key, updater(this.get(key), key)) | |
| else this.set(key, notset) | |
| } | |
| filter(predicate: (v:v, k:k) => boolean) { | |
| const newMap = new BetterMap<k, v>() | |
| const entries = Array.from(this.entries()) | |
| for(const [key, value] of entries) { |
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 * as wp from 'workerpool' | |
| const workerpool = wp.pool() | |
| net.createServer() | |
| .listen(PORT, IP, BACKLOG) | |
| .on('connection', socket => { | |
| console.log('new connection') | |
| socket | |
| .on('data', buffer => { | |
| console.log('data') |
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 * as wp from 'workerpool' | |
| const workerpool = wp.pool() |
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
| net.createServer() | |
| .listen(PORT, IP, BACKLOG) | |
| .on('connection', socket => { | |
| console.log('new connection') | |
| socket | |
| .on('data', buffer => { | |
| console.log('data') | |
| socket.write(fibonacci(100)) | |
| console.log('done with connection') | |
| socket.end() |
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
| socket.write(compileResponse({ | |
| protocol: 'HTTP/1.1', | |
| headers: new Map(), | |
| status: 'OK', | |
| statusCode: 200, | |
| body: `<html><body><h1>Greetings</h1></body></html>` | |
| })) |
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 interface Response { | |
| status: string | |
| statusCode: number | |
| protocol: string | |
| headers: Map<string, string> | |
| body: string | |
| } | |
| const compileResponse = (r: Response): string => `${r.protocol} ${r.statusCode} ${r.status} | |
| ${Array.from(r.headers).map(kv => `${kv[0]}: ${kv[1]}`).join('\r\n')} |
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
| HTTP/1.1 200 OK | |
| Content-Type: application/text | |
| <html> | |
| <body> | |
| <h1>Greetings!</h1> | |
| </body> | |
| </html> |
NewerOlder