Skip to content

Instantly share code, notes, and snippets.

Avatar
🎪

Nikola Ristić rista404

🎪
View GitHub Profile
View cached-json-fetch.js
// @flow
import cache from 'memory-cache'
import fetch from 'isomorphic-fetch'
const TTL_MILISECONDS: number = 10 * 60 * 1000 // ten minutes
export default async function<T>(url: string, options?: Object): Promise<T> {
const cachedResponse = cache.get(url)
if (cachedResponse) return cachedResponse
@rista404
rista404 / wrap-text-usage.js
Last active Sep 3, 2018
Utility to wrap text in canvas, or to get text sizing info (number of lines, height, positions).
View wrap-text-usage.js
import { wrapText } from './wrap-text'
// Drawing and highlighting words (twitter style)
wrapText({
ctx,
text: text,
x: 0,
y: 0,
maxWidth: 500,
lineHeight: fontSize * 1.2,
@rista404
rista404 / hdup.sh
Created Feb 3, 2019
Shortcut for creating a heroku docker app
View hdup.sh
function hdup() {
if [ "$1" != "" ]
then
heroku apps:create $1 --region eu -s container
echo "build:\n docker:\n web: Dockerfile" > heroku.yml
touch Dockerfile
else
echo "Please provide an app name."
fi
}
@rista404
rista404 / example_usage.tsx
Last active Jun 5, 2021
React hook for easier polling of data.
View example_usage.tsx
function Component() {
const [items, setItems] = useState([])
async function fetchItems(abortSignal: AbortSignal) {
try {
const res = await fetch(`/api/items/`, { signal: abortSignal })
const resp = await res.json()
if (res.ok && resp.ok) {
setItems(resp.items)
} else {
View WithSwipe.js
import React from 'react'
export default class WithSwipe extends React.Component {
xDown = null
yDown = null
handleTouchStart = evt => {
this.xDown = evt.touches[0].clientX
this.yDown = evt.touches[0].clientY
}