Skip to content

Instantly share code, notes, and snippets.

View ourmaninamsterdam's full-sized avatar

Justin Perry ourmaninamsterdam

View GitHub Profile
@ourmaninamsterdam
ourmaninamsterdam / number.ts
Last active September 3, 2024 16:07
Generators: random numbers
function random(max: number) {
return Math.floor(Math.random() * max)
}
function* numberGenerator(limit: number) {
let current = 0;
let attempts = 0
try {
while(current <= limit) {
(define-struct node (k v l r))
;; BinaryTree is one of:
;; - false
;; - (make-node Natural String BinaryTree BinaryTree)
(define BT0 false)
(define BT1 (make-node 1 "a" false false))
(define BT4 (make-node 4 "d"
(make-node 2 "b"
(make-node 1 "a" false false)
(make-node 3 "c" false false))
@ourmaninamsterdam
ourmaninamsterdam / mqtt-event-channel.ts
Last active November 30, 2023 12:49
WIP redux-saga/MQTT event channel
import { eventChannel } from 'redux-saga';
import { put, take } from 'redux-saga/effects';
const createMqttChannel = mqtt => {
return eventChannel(emitter => {
const mqttMsgHandler = (payload: MqttMessage) => {
recorder.mqtt(payload);
const { event } = payload;
switch (event) {
@ourmaninamsterdam
ourmaninamsterdam / taskSagas.ts
Last active November 16, 2023 20:42
redux-saga - Running long tasks then cancelling
import { createStore, applyMiddleware } from "redux";
import { all, fork, delay } from "typed-redux-saga";
import createSagaMiddleware from "redux-saga";
const sagaMiddleware = createSagaMiddleware();
const store = createStore(() => undefined, applyMiddleware(sagaMiddleware));
const task = sagaMiddleware.run(rootSaga);
function* taskSaga() {
console.log("taskSaga - start");
@ourmaninamsterdam
ourmaninamsterdam / promise-logic.ts
Last active April 17, 2023 10:06
Promise business/runtime logic error handling
async function findUser(id: number) {
console.log('Getting user...');
await sleep(1000);
// Mocked out API
if(id === 999) {
return [new Error('User not found'), undefined]
}
// index.js
require('./a');
require('./b');
// mqtt.js
class MQTT {
connect() {
const connectionId = Math.random();
console.log('connected to: ', connectionId);
this.connectionId = connectionId;
@ourmaninamsterdam
ourmaninamsterdam / expo-notifications+0.13.3.patch
Last active June 28, 2022 08:22
expo-notifications: Patch to resolve "Error: Failed to schedule notification" issue on Expo SDK 43 with SDK version 31
diff --git a/node_modules/expo-notifications/android/src/main/java/expo/modules/notifications/service/NotificationsService.kt b/node_modules/expo-notifications/android/src/main/java/expo/modules/notifications/service/NotificationsService.kt
index 8e675c4..2062951 100644
--- a/node_modules/expo-notifications/android/src/main/java/expo/modules/notifications/service/NotificationsService.kt
+++ b/node_modules/expo-notifications/android/src/main/java/expo/modules/notifications/service/NotificationsService.kt
@@ -8,6 +8,7 @@ import android.content.Intent
import android.content.pm.ActivityInfo
import android.net.Uri
import android.os.Bundle
+import android.os.Build
import android.os.Parcel
@ourmaninamsterdam
ourmaninamsterdam / retryAsync.spec.ts
Created April 6, 2022 06:41
retryAsync - retry an async function a set amount of times
import retryAsync from '~utils/retryAsync';
describe('retryAsync', () => {
describe('with retry attempts left', () => {
describe('if the async function promise rejects', () => {
it('retries the given async function 3 times before eventually throwing', async () => {
const mockAsyncFn = jest.fn().mockRejectedValue('failed');
await expect(retryAsync(3, () => mockAsyncFn())).rejects.toThrow();
expect(mockAsyncFn).toHaveBeenCalledTimes(3);
@ourmaninamsterdam
ourmaninamsterdam / method-overloading.ts
Last active February 12, 2022 10:16
Method overloading in TypeScript
// Adapted from https://howtodoinjava.com/typescript/function-overloading
class MiddleEarthResident {
name: string;
constructor(name: string) {
this.name = name;
}
}
class ShireResident extends MiddleEarthResident {
@ourmaninamsterdam
ourmaninamsterdam / Person.java
Created July 17, 2021 09:41
Java - hierarchy relationship in an enum
// From https://stackoverflow.com/questions/44654291/is-it-good-practice-to-use-ordinal-of-enum
public enum Person {
GRANDPARENT(null),
PARENT(GRANDPARENT),
CHILD(PARENT);
private final Person parent;
private Person(Person parent) {