Skip to content

Instantly share code, notes, and snippets.

View RoryKelly's full-sized avatar

Rory Kelly RoryKelly

View GitHub Profile
@RoryKelly
RoryKelly / StateMachine.kt
Created December 9, 2020 22:09
Kotlin Flow State Machine
fun <T> Flow<T>.stateMachine(configuration: StateChanges<T>.() -> Unit): Flow<StateChanges<T>> = flow {
var oldState: T? = null
distinctUntilChanged().collect { value ->
emit(StateChanges(oldState, value))
oldState = value
}
}.onEach {
configuration(it)
}
@RoryKelly
RoryKelly / FlowRecorder.kt
Created November 14, 2020 23:40
FlowRecorder for recording emissions from flows.
/**
* Starts subscribing / collecting the given Flow and records it's emission to verify them later
*/
suspend fun <T> Flow<T>.recordEmissions(
recordingScope: CoroutineScope = GlobalScope,
verify: suspend SharedFlow<T>.() -> Unit
) {
val recording = shareIn(recordingScope, SharingStarted.Eagerly, Int.MAX_VALUE)
verify(recording)
}
@RoryKelly
RoryKelly / Headphone Monitor.kt
Last active October 15, 2020 14:41
Flow that Immediately emits a set of AudioDeviceInfo's, i.e the current audio devices that are connected, followed by updates as devices are connect and disconnected. Useful for monitoring currently connected headphones
package com.klefmusic.eightbars.utils
import android.content.Context
import android.media.AudioDeviceCallback
import android.media.AudioDeviceInfo
import android.media.AudioManager
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.channels.sendBlocking
/**
* Current state of an object. for example the state of a view or the state of a hardware device.
*/
interface State
/**
* A result modifies state.
*/
interface Result
@RoryKelly
RoryKelly / flatten.ts
Created May 29, 2019 08:02
Flatten graphql info using 'graphql-parse-resolve-info'
import {FieldsByTypeName, parseResolveInfo, ResolveTree} from 'graphql-parse-resolve-info';
const parsedResolveInfoFragment = parseResolveInfo(info) as ResolveTree;
const curation = await ctx.prismaBinding.query.curation(
{
where: {
install new version as seperate cluster
https://www.postgresql.org/download/windows/
stop both servers
- find the data dir
postgres=# show data_directory; data_directory
- use pg_ctrl to stop both clusters
@RoryKelly
RoryKelly / DatabaseBinding.ts
Last active February 24, 2019 23:19
A terminating ApolloLink that can be used with a schema only version of postgraphile.
import {ApolloLink} from 'apollo-link';
import {makeRemoteExecutableSchema} from 'graphql-tools';
import { HTTPLinkDataloader } from "http-link-dataloader";
import * as b from '../generated/Binding'
import loadSchema from "../tools/LoadSchema";
import { BatchHttpLink } from "apollo-link-batch-http";
import fetch from 'node-fetch';
import {Pool} from 'pg';
import {GraphQLSchema} from 'graphql';
import {GraphileApolloLink} from './GraphileApolloLink';
@RoryKelly
RoryKelly / MainActivity.kt
Last active February 9, 2018 10:03
Utility for creating view models.
import android.arch.lifecycle.ViewModel
import android.arch.lifecycle.ViewModelProviders
import android.support.v4.app.FragmentActivity
import kotlin.reflect.KClass
class MainActivity : AppCompatActivity() {
val viewModel by bindViewModel(this, MainViewModel::class)
override fun onCreate(savedInstanceState: Bundle?) {
@RoryKelly
RoryKelly / App.js
Last active January 24, 2022 05:58
App to demonstrate problems using react navigation with react redux
/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/
import React from 'react';
import {combineReducers, createStore} from 'redux'
import {Button, Text, View} from "react-native";
import {addNavigationHelpers, NavigationActions, StackNavigator} from "react-navigation";
@RoryKelly
RoryKelly / StartRecordingActivity.kt
Created June 26, 2017 11:01
Neat function to bind ViewModels on android
class StartRecordingActivity : ToolbarActivity() {
private val startRecordingViewModel: StartRecordingViewModel by bindViewModel(this, StartRecordingViewModel::class)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
/* your ui code */
}
}