Skip to content

Instantly share code, notes, and snippets.

@wesleybliss
wesleybliss / KBus.kt
Created December 3, 2018 21:18
Kotlin event bus example
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.subjects.PublishSubject
/**
* Created by adrielcafe on 20/12/17.
*/
object KBus {
val disposables = mutableMapOf<Any, CompositeDisposable>()
val publishSubject = PublishSubject.create<Any>()!!
@wesleybliss
wesleybliss / memoized_fib.js
Created August 31, 2018 19:43
Memoized Fibonacci
const fib = n => (n < 2) ? n : fib(n - 1) + fib(n - 2)
const fibSexy = (() => {
const cache = {}
const f = n => {
const value = (n in cache)
? cache[n]
@wesleybliss
wesleybliss / filter-keys.js
Created June 1, 2018 06:30
Filters an object returning only properties specified
/**
* Filters an object returning only properties specified
*
* @param {Object} obj Source object
* @param {Array} keys List of (strings) properties to keep
* @return {Object} Object with only props specified in `keys`
*/
export const filterKeys = (obj, keepKeys, dropKeys) => {
if (typeof obj !== 'object')
@wesleybliss
wesleybliss / debounce.js
Created April 28, 2018 09:33
Debounce a function
/**
* Creates a debounced monad to allow a method to be called
* many times, but only execute after N seconds has elapsed
*
* @param {Function} func Method to call
* @param {Number} wait Timeout, in milliseconds
* @param {Boolean} [immediate] Optionally skip the wait
* @return {Function} Debounced monad that can be called multiple times
*/
export function debounce(func, wait, immediate) {
@wesleybliss
wesleybliss / throttle.js
Created April 23, 2018 08:06
Throttle (not debounce) a method - similar to lodash, but simpler
const throttle = (fn, delay) => {
let lastCall = 0
return (...args) => {
const now = (new Date).getTime()
if (now - lastCall < delay) return
lastCall = now
return fn(...args)
}
}
@wesleybliss
wesleybliss / attempt.js
Created April 23, 2018 08:02
Attempt a function waiting for a condition a number of times before failing
const attempt = (fn, delay, max, count = 0) => {
let pendingResolve = null
let pendingReject = null
const pendingPromise = new Promise((resolve, reject) => {
pendingResolve = resolve
pendingReject = reject
})
@wesleybliss
wesleybliss / force-rcn-chat.js
Created April 23, 2018 00:39
Force RCN chat to open, even when it says all reps are busy
const ev = document.createEvent('MouseEvents')
ev.initEvent('click', true, true)
document.querySelector('a[data-moxie-start-engagement="chat"]').dispatchEvent(ev)
@wesleybliss
wesleybliss / array-chunk.js
Created April 21, 2018 05:19
Group a flat array in to a multidimensional one, by chunk size (number of elements per sub-array)
const arrayChunk = (array, chunkSize) =>
Array(Math.ceil(array.length / chunkSize))
.fill()
.map((_, index) => index * chunkSize)
.map(it => array.slice(it, it + chunkSize))
@wesleybliss
wesleybliss / react-async-setstate.js
Created April 20, 2018 23:50
Asynchronous setState for React
// e.g. src/lib/helpers.js
export const createAsyncSetState = context => {
return function(newState) {
return new Promise(resolve => {
context.setState(newState, resolve)
})
}.bind(context)
}
// Usage, e.g. MyComponent.jsx
@wesleybliss
wesleybliss / nginx-node-proxy.conf
Created March 29, 2018 22:37
Example NGINX config for giving Node processes their own config
~ cat /etc/nginx/sites-available/example.com
# HTTP - redirect all requests to HTTPS:
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
# listen [::]:80 ipv6only=on;
return 301 https://$server_name$request_uri;
}