Skip to content

Instantly share code, notes, and snippets.

View WimJongeneel's full-sized avatar
:octocat:

Wim Jongeneel WimJongeneel

:octocat:
View GitHub Profile
@WimJongeneel
WimJongeneel / useQueuedPromise.tsx
Last active February 20, 2022 18:46
UseQueuedPromise: a React hook that runs a promise and uses a one-sized queue to ensure only one promise is running at the same time. Scheduling a new promise will overwrite any scheduled promises and the final result will always be the result of the last promise that was sent to the hook.
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>()
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, ' ')
@WimJongeneel
WimJongeneel / functors-fmap.ts
Last active February 21, 2020 21:33
Funtors and fmap in TypeScript
// 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})
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) {
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')
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')
socket.write(fibonacci(100))
console.log('done with connection')
socket.end()
socket.write(compileResponse({
protocol: 'HTTP/1.1',
headers: new Map(),
status: 'OK',
statusCode: 200,
body: `<html><body><h1>Greetings</h1></body></html>`
}))
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')}
HTTP/1.1 200 OK
Content-Type: application/text
<html>
<body>
<h1>Greetings!</h1>
</body>
</html>