Skip to content

Instantly share code, notes, and snippets.

Avatar
🇬🇧
🇧🇷

David Alberto Adler mfbx9da4

🇬🇧
🇧🇷
View GitHub Profile
@mfbx9da4
mfbx9da4 / App.tsx
Last active March 7, 2022 10:12
Github issue realm-js flatlist + listener poll for transaction
View App.tsx
import React, { useEffect, useLayoutEffect, useState } from 'react'
import { FlatList, SafeAreaView } from 'react-native'
import RNFS from 'react-native-fs'
import Realm from 'realm'
export function App() {
const r = useWaitForRealm()
const [initialized, setInitialized] = useState(false)
useEffect(() => {
@mfbx9da4
mfbx9da4 / App.tsx
Last active March 6, 2022 19:44
Github issue realm-js flatlist + listener
View App.tsx
import React, { useEffect, useLayoutEffect, useState } from 'react'
import { FlatList, SafeAreaView } from 'react-native'
import RNFS from 'react-native-fs'
import Realm from 'realm'
export function App() {
const r = useWaitForRealm()
const [initialized, setInitialized] = useState(false)
@mfbx9da4
mfbx9da4 / Overlay.tsx
Created February 21, 2022 10:02
Shared element transition overlay
View Overlay.tsx
import React, { useEffect } from 'react'
import { Animated, StyleSheet, TouchableOpacity, View } from 'react-native'
import {
nodeFromRef,
SharedElement as OriginalSharedElement,
SharedElementNode,
SharedElementTransition,
} from 'react-native-shared-element'
import { assert } from './utils/assert'
import { createObservable } from './utils/observable'
View typesrcript_non_null_assertion_transform.js
/**
* The transformer signature is based on https://github.com/cevek/ttypescript#program
* Need to use https://github.com/microsoft/TypeScript/blob/bae0f508184280c59d2865a35efc63be362eacfa/src/compiler/factory/nodeFactory.ts
* The goal is to conver `a!.m()` to `if (!a) { throw new Error('Attempted to use nullish value "a"'} else { a.m() }`
* https://astexplorer.net/#/gist/9ec2af3e8c15fd2cde848941e14f566b/d9ddca954379374f98a4097d9bde4c346dac8567
*/
export default function (program) {
const checker = program.getTypeChecker()
return (context) => {
return (sourceFile) => {
@mfbx9da4
mfbx9da4 / BroadcastMethods.ts
Last active January 2, 2022 14:48
Remote procedure calls (RPCs) using BroadcastChannel in deno
View BroadcastMethods.ts
// ---- START IMPORTS ----
export type AssertionExtra = (Record<string, unknown> & { name?: ErrorCode }) | ErrorCode
export function assert(predicate: any, message: string, extra: AssertionExtra = {}): asserts predicate {
if (!predicate) {
extra = typeof extra === 'string' ? { name: extra } : extra
if (!('name' in extra)) {
extra.name = ErrorCode.AssertionError
}
View websocketsDenoDeploy.ts
import { serve } from 'https://deno.land/std/http/server.ts'
// import { assert, ErrorCode } from './assert.ts'
export enum ErrorCode {
RaceNotFound = 'RaceNotFound',
RaceMemberNotFound = 'RaceMemberNotFound',
RaceAlreadyExists = 'RaceAlreadyExists',
RaceMemberAlreadyExists = 'RaceMemberAlreadyExists',
AssertionError = 'AssertionError',
}
View wrapMethods.js
const wrap = (obj, method) => {
const oldMethod = obj[method]
async function innerMethod(...args) {
const start = Date.now()
try {
const ret = await oldMethod.call(obj, ...args)
console.log(method, 'took', Date.now() - start, args[0])
return ret
} catch (error) {
console.log(method, 'took', Date.now() - start)
@mfbx9da4
mfbx9da4 / observeProperty.ts
Last active January 26, 2022 15:09
Observe whenever a property of an object is changed
View observeProperty.ts
/** Intercepts writes to any property of an object */
export function observeProperty<T extends Object, K extends keyof T>(
obj: T,
property: K,
onChanged: (val: T[K]) => void,
customDescriptor?: PropertyDescriptor | undefined
) {
let val = obj[property]
Object.defineProperty(obj, property, {
get() {
View transactionTest.ts
import AWS from 'aws-sdk'
import { uuid } from 'short-uuid'
import { yesno } from 'yesno-http'
yesno.spy()
const documentClient = new AWS.DynamoDB.DocumentClient({
apiVersion: 'latest',
})
View haltonSequence.js
function area(image) {
return image.width * image.height;
}
/** https://en.wikipedia.org/wiki/Halton_sequence */
function halton(index, base) {
let fraction = 1;
let result = 0;
while (index > 0) {
fraction /= base;