Skip to content

Instantly share code, notes, and snippets.

View christianchown's full-sized avatar

Christian Chown christianchown

View GitHub Profile
@christianchown
christianchown / ReadPlist.m
Created April 25, 2018 14:34
React Native iOS NativeModule to read a plist as a string
//
// ReadPlist.m
//
#import <UIKit/UIKit.h>
#import <React/RCTBridgeModule.h>
#import <React/RCTUtils.h>
#import <Foundation/NSData.h>
#import <Foundation/Foundation.h>
@christianchown
christianchown / ConnectedProps.ts
Last active September 19, 2021 09:58
ConnectedProps - the missing TypeScript helper for Redux
import { InferableComponentEnhancerWithProps } from 'react-redux';
export default type ConnectedProps<T> = T extends InferableComponentEnhancerWithProps<infer Props, infer _> ? Props : never;
// credit: https://github.com/Voronar
@christianchown
christianchown / MiddlewareDispatch.ts
Last active May 24, 2020 19:18
Convert any useReducer() dispatch to be able to allow its actions to be passed to Redux-like middleware
import React from 'react';
export type ReducerAction = Record<string, any> & {
type: string;
};
type Middleware = (a: ReducerAction) => void | Promise<void>;
type Middlewares = Middleware[];
@christianchown
christianchown / userMiddleware.js
Last active December 5, 2019 21:54
Custom async Redux middleware
const userMiddleware = ({ getState, dispatch }) => next => async action => {
const result = next(action);
switch (action.type) {
case SOME_ACTION:
const asyncResult = await somethingAsync();
dispatch(anotherAction(asyncResult));
break;
case SOME_OTHER_ACTION:
const { slice: { stateVariable } } = getState();
await someProcess(stateVariable);
@christianchown
christianchown / syncedRetrievePromise.js
Created April 19, 2018 06:07
Synchronise a Firebase Database with a Redux Store
const syncedRetrievePromise = async (
ref,
dispatchValue,
dispatchKeyValue,
) => {
// retrieve and dispatch the current value of the ref
const data = await ref.once('value');
const val = data.val();
dispatchValue(val);
@christianchown
christianchown / actions.js
Created April 19, 2018 08:13
Push redux-like actions to Firebase, and retrieve responses
export const pushActionPromise = async (firebase, type, payload) => {
// get the key for the new action about to be created
const actionRef = firebase.database().ref('action');
const newKey = actionRef.push().key;
// push the action
const fullPayload = {
uid: firebase.auth().currentUser.uid,
type,
payload,
@christianchown
christianchown / JunctionPoint.cs
Created November 3, 2018 14:49
C# console app to scan a directory and replace <JUNCTION>s with the files that they point at
// JunctionPoint.cs comes from
// https://www.codeproject.com/Articles/15633/Manipulating-NTFS-Junction-Points-in-NET
//
// Full credit to Jeff Brown (https://www.codeproject.com/script/Membership/View.aspx?mid=1994253)
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.Win32.SafeHandles;