Skip to content

Instantly share code, notes, and snippets.

Avatar

Christian Chown christianchown

View GitHub Profile
@christianchown
christianchown / MiddlewareDispatch.ts
Last active May 24, 2020
Convert any useReducer() dispatch to be able to allow its actions to be passed to Redux-like middleware
View MiddlewareDispatch.ts
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 Dec 5, 2019
Custom async Redux middleware
View userMiddleware.js
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 / ConnectedProps.ts
Last active Nov 23, 2020
ConnectedProps - the missing TypeScript helper for Redux
View ConnectedProps.ts
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 / JunctionPoint.cs
Created Nov 3, 2018
C# console app to scan a directory and replace <JUNCTION>s with the files that they point at
View JunctionPoint.cs
// 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;
@christianchown
christianchown / ReadPlist.m
Created Apr 25, 2018
React Native iOS NativeModule to read a plist as a string
View ReadPlist.m
//
// ReadPlist.m
//
#import <UIKit/UIKit.h>
#import <React/RCTBridgeModule.h>
#import <React/RCTUtils.h>
#import <Foundation/NSData.h>
#import <Foundation/Foundation.h>
@christianchown
christianchown / actions.js
Created Apr 19, 2018
Push redux-like actions to Firebase, and retrieve responses
View actions.js
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 / syncedRetrievePromise.js
Created Apr 19, 2018
Synchronise a Firebase Database with a Redux Store
View syncedRetrievePromise.js
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);
You can’t perform that action at this time.