Skip to content

Instantly share code, notes, and snippets.

Copying and pasting from StackOverflow

Cameron Nokes ccnokes

Copying and pasting from StackOverflow
Block or report user

Report or block ccnokes

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
ccnokes / idObj.ts
Last active Jul 29, 2019
Sometimes in React you need a unique `key` to represent an object's identity. Instead of generating an id when you fetch it from an API or generating an id on the fly or using indexes (both will lead to bugs), you can use this which gives you a stable, unique id for an object. Demo: Demo with React: htt…
View idObj.ts
let map = new WeakMap(); // weakly holds all object refs (works in IE11+)
let n = 0; // global counter for ids
export function idObj(obj: any) {
if (map.has(obj)) {
return map.get(obj);
} else {
let key = String(++n);
map.set(obj, key);
return key;
ccnokes / rafScheduler.ts
Created Jun 18, 2019
requestAnimationFrame scheduler
View rafScheduler.ts
* A function for batching RAFs together
export default function RAFScheduler() {
let queue = [];
let rafId;
let scheduled = false;
const DURATION = 10;
return function scheduleRaf(cb: () => void) {
ccnokes /
Created Mar 28, 2019
Convert all stashes to patches in a git repository
stash_count=$(git stash list | wc -l)
while [[ $i -lt $stash_count ]]; do
git stash show -p stash@{$i} > "stash-$i.patch"
i=$(( $i + 1 ))
ccnokes / Batch.ts
Last active Apr 2, 2019
A class that manages the logic of batching items together within a timeframe and then passing those items to a function. Sandbox:
View Batch.ts
class Deferred<Type> {
promise: Promise<Type>;
// TODO type these
resolve: any;
reject: any;
then: any;
catch: any;
constructor() {
this.promise = new Promise((resolve, reject) => {
ccnokes / SparseList.ts
Created Feb 22, 2019
A data structure that wraps a sparse array. Useful for when you know the total size of a list and need to represent it in its full size, but are lazy loading in sections of it. Sandbox:
View SparseList.ts
class SparseList<T = any> {
private list: T[];
emptySlotsCount: number;
constructor(size: number) {
this.list = new Array(size);
this.emptySlotsCount = size;
get size() {
ccnokes / RICScheduler.ts
Last active Feb 23, 2019
RequestIdleCallback scheduler. Create an instance and use it globally to ensure that tasks are efficiently run in as few requestIdleCallbacks as possible (as described here Sandbox:
View RICScheduler.ts
class RICScheduler {
private queue: { task: () => void, id: number }[] = [];
private running = false;
private nextId = 0;
private ricId;
private id() {
return this.nextId++;
ccnokes / withNgService.tsx
Last active Feb 19, 2019
withNgService HOC for injecting modules from Angular.JS's dependency injector into a React component
View withNgService.tsx
import * as React from 'react';
import hoistStatics from 'hoist-non-react-statics';
declare var angular: any;
let injector;
// NOTE you probably can't run this at the top level of a module because angular takes time to load and bootstrap
export default function getNgService<T = any>(serviceName: string): T {
if (!injector) {
ccnokes / initContext.tsx
Created Feb 1, 2019
A function that returns a React.Context and a mapContextToProps HOC so that you can inject context onto props, like react-redux's `connect` function
View initContext.tsx
import * as React from 'react';
const defaultMapper = (ctx) => ctx
export function initContext<CtxType>() {
const Context = React.createContext<CtxType | null>(null);
function mapContextToProps<Props>(Component: React.ComponentType<Props & CtxType>, mapper: (ctx: CtxType) => any = defaultMapper): React.ComponentType<Props> {
return (props: Props) => (
{(ctx) => <Component {...props} {...mapper(ctx)} />}
ccnokes / withProps.tsx
Created Jan 25, 2019
withProps HOC. For when you want to inject arbitrary stuff into a component
View withProps.tsx
import * as React from 'react';
export default function withProps<InjectProps, Props = {}>(Component: React.ComponentType<Props & InjectProps>, props: InjectProps): React.ComponentClass<Pick<Props, Exclude<keyof Props, keyof InjectProps>>> {
return class extends React.Component<Props> {
static displayName = `withProps(${Component.displayName ||})`;
static WrappedComponent = Component;
render() {
return <Component {...this.props} {...props} />
ccnokes / middleware.js
Created Jan 22, 2019
Really basic express-like middleware implementation
View middleware.js
class Middleware {
private middlewares = [];
push(...middlewares) {
this.middlewares.push.apply(this.middlewares, middlewares);
run(...args) {
let i = 0;
const next = () => {
if (i < this.middlewares.length) {
You can’t perform that action at this time.