Skip to content

Instantly share code, notes, and snippets.

View mminer's full-sized avatar

Matthew Miner mminer

View GitHub Profile
@mminer
mminer / SharedTexture.cpp
Created December 8, 2018 00:14
Gets a shared texture handle from an Unreal texture.
#include "SharedTexture.h"
FSharedTexture FSharedTexture::FromRenderTarget(const UTextureRenderTarget2D& RenderTarget)
{
const FTextureRHIRef TextureRHI = RenderTarget.Resource->TextureRHI;
if (TextureRHI == nullptr)
{
return {};
}
@mminer
mminer / restore-docker-volumes.sh
Created November 26, 2018 20:56
Restores Docker volumes from tar archives.
#!/usr/bin/env bash
usage() {
cat <<EOF
Usage: $0 VOLUME [VOLUME ...]
Restore Docker volumes from tar archives.
EOF
}
@mminer
mminer / backup-docker-volumes.sh
Last active November 26, 2018 20:55
Back up Docker volumes to tar archives.
#!/usr/bin/env bash
usage() {
cat <<EOF
Usage: $0 VOLUME [VOLUME ...]
Back up Docker volumes to tar archives.
EOF
}
@mminer
mminer / notify_event.sql
Created September 18, 2018 19:30
PostgreSQL trigger function to send notifications when table changes.
-- Adapted from http://coussej.github.io/2015/09/15/Listening-to-generic-JSON-notifications-from-PostgreSQL-in-Go/
CREATE OR REPLACE FUNCTION notify_event() RETURNS TRIGGER AS $$
DECLARE
data JSON;
notification JSON;
BEGIN
-- Skip notification if row doesn't actually change in UPDATE.
-- We can accomplish the same thing with a WHERE clause in the CREATE TRIGGER statement,
-- but only if the trigger watches exclusively for UPDATE events.
@mminer
mminer / withHashRouter.js
Created August 23, 2018 18:25
React higher-order component that provides basic hash-based routing.
// Usage:
//
// const YourComponent = ({ activePageHash }) => ...
// export default withHashRouter(YourComponent, ['#valid-page-1', '#valid-page-2']);
import { Component, createElement as h } from 'react';
// Falls back to the initial page if the hash specifies an unknown one.
function replaceHashWithValidPage(validPageHashes) {
if (!validPageHashes) {
@mminer
mminer / groupBy.js
Created August 21, 2018 20:33
Groups an array of objects by a common key.
function groupBy(values, key) {
return values.reduce((accumulator, value) => {
const id = value[key];
accumulator[id] = accumulator[id] || [];
accumulator[id].push(value);
return accumulator;
}, {});
}
@mminer
mminer / sse.js
Last active August 22, 2018 18:53
Server-side implementation of Server Side Events (SSE) for Node.js + Express.
// Usage:
//
// const SSE = require('./sse');
// const sse = new SSE();
// ...
// app.get('/updates', sse.routeHandler);
// ...
// sse.send('Some string', 'some-event-name');
const { EventEmitter } = require('events');
@mminer
mminer / cookies.js
Created July 30, 2018 17:55
Functions to access and set cookies.
// Adapted from https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie/Simple_document.cookie_framework
function getExpirationArg(end) {
if (!end) {
return '';
}
switch (end.constructor) {
case Number:
return end === Infinity ?
#!/usr/bin/env bash
# Watches a file and prints when a specified variable value changes.
# We assume the file is a list of key=value pairs separated by newlines.
if [ $# -ne 2 ]; then
echo "Usage: $0 FILE KEY"
exit 1
fi
@mminer
mminer / isClient.cpp
Created May 11, 2018 22:38
Unreal one-liner to check if instance is a client.
auto isClient = GEngine->GetNetMode(GetWorld()) == NM_Client);