Skip to content

Instantly share code, notes, and snippets.

@selfcontained
selfcontained / promise-throttler.js
Created February 15, 2018 00:27
Promise Throttler for Async Function
// Example refreshAuth fn that might call Google
function refreshAuth (oldToken, done) {
console.log('actually refreshing the old token', oldToken)
setTimeout(() => {
done(null, getNewToken())
}, 2000)
}
// Pretend we're issuing new tokens
let token = 1
const slapp = require('slapp')
slapp.message('^(hi|hello|hey).*', ['direct_mention', 'direct_message'], helloHandler)
// Pull slapp message handler into a function you can export and test
function helloHandler (msg, text, greeting) {
msg.say(`${greeting}, how are you?`)
}
// write a test for just the handler
@selfcontained
selfcontained / lazy-load.js
Last active May 26, 2017 16:40
Lazy Load React component pattern
import React from 'react'
class LazyLoad extends React.Component {
constructor () {
super(...arguments)
this.state = {
isLoaded: false
}
}

Keybase proof

I hereby claim:

  • I am selfcontained on github.
  • I am selfcontained (https://keybase.io/selfcontained) on keybase.
  • I have a public key whose fingerprint is 9F85 586A BB68 A98F 5DA3 6155 6EEE 9E14 4105 3575

To claim this, I am signing this object:

@selfcontained
selfcontained / slapp-threaded-messages.js
Created January 19, 2017 23:20
Example of using Slack threaded messages with Slapp
const Slapp = require('slapp')
const BeepBoopContext = require('slapp-context-beepboop')
const BeepBoopConvo = require('slapp-convo-beepboop')
const smb = require('slack-message-builder')
let slapp = Slapp({
verify_token: process.env.SLACK_VERIFY_TOKEN,
context: BeepBoopContext(),
convo_store: BeepBoopConvo()
})
var Slapp = require('slapp')
var slapp = Slapp(
// ..slapp options
)
// Add this line ⤵️
require('beepboop-slapp-presence-polyfill')(slapp)
@selfcontained
selfcontained / slapp.js
Created August 9, 2016 18:19
Slapp Firebase setup
'use strict'
const Slapp = require('slapp')
const Context = require('./context')
const ConvoStore = require('./convo-store')
module.exports = (server, db) => {
let app = Slapp({
verify_token: process.env.SLACK_VERIFY_TOKEN,
context: Context(db),
@selfcontained
selfcontained / convo-store.js
Created August 9, 2016 18:15
Slapp Firebase conversation store
'use strict'
// Slim wrapper around data module for Slapp ConvoStore
module.exports = (db) => {
return {
set (id, params, done) {
db.saveConvo(id, params, done)
},
get (id, done) {
db.getConvo(id, done)
@selfcontained
selfcontained / context.js
Created August 9, 2016 18:05
Slapp Firebase context middleware
'use strict'
// Slapp context middleware function
// Looks up team info from db and enriches request
module.exports = (db) => {
return (req, res, next) => {
let teamId = req.slapp.meta.team_id
db.getTeam(teamId, (err, team) => {
if (err) {
@selfcontained
selfcontained / db.js
Last active February 1, 2017 16:19
Slapp Firebase wrapper
'use strict'
const path = require('path')
const firebase = require('firebase-admin')
module.exports = () => {
const firebaseDBUrl = process.env.FIREBASE_DB_URL
const serviceAccountBase64 = process.env.FIREBASE_SERVICE_ACCOUNT_BASE64
firebase.initializeApp({