Skip to content

Instantly share code, notes, and snippets.

@lbatteau
lbatteau / sse-fastapi-redis.py
Last active October 13, 2024 22:30
Server-Sent Events in FastAPI with async Redis Pub/Sub
from aioredis import Channel, Redis
from fastapi import FastAPI
from fastapi.params import Depends
from fastapi_plugins import depends_redis, redis_plugin
from sse_starlette.sse import EventSourceResponse
from starlette.responses import HTMLResponse
html = """
<!DOCTYPE html>
<html>
@ruslux
ruslux / ws_app.py
Last active September 19, 2024 12:30
aioredis fastapi websocket pubsub
import uvicorn
from aioredis import create_pool
from fastapi import FastAPI
from starlette.websockets import WebSocket
app = FastAPI()
REDIS_URL = 'redis://redis:6379'
REDIS_DB = 0
@rtatarinov4xxi
rtatarinov4xxi / useRequest.ts
Last active August 26, 2023 03:51
useRequest hook with Axios by typescript (with examples)
import { useState } from 'react';
import axios from 'axios';
type RequestConfig = {
url?: string;
baseURL?: string;
initialIsLoading?: boolean;
withCredentials?: boolean;
onSuccess?: Function;
onError?: Function;
@dbisso
dbisso / state.js
Last active September 21, 2024 23:47
Simple state management in vanilla JS
function State() {
this.actions = {};
this.subscriptions = [];
this.history = [];
}
State.prototype.subscribe = function(element, action, callback) {
this.subscriptions[action] = this.subscriptions[action] || [];
this.subscriptions[action].push(function(data) {
callback.apply(element, data);

The issue:

..mobile browsers will wait approximately 300ms from the time that you tap the button to fire the click event. The reason for this is that the browser is waiting to see if you are actually performing a double tap.

(from a new defunct https://developers.google.com/mobile/articles/fast_buttons article)

touch-action CSS property can be used to disable this behaviour.

touch-action: manipulation The user agent may consider touches that begin on the element only for the purposes of scrolling and continuous zooming. Any additional behaviors supported by auto are out of scope for this specification.

@thisismitch
thisismitch / haproxy.cfg
Last active October 18, 2024 15:19
Let's Encrypt Auto-Renewal script for HAProxy
global
log /dev/log local0
log /dev/log local1 notice
chroot /var/lib/haproxy
stats socket /run/haproxy/admin.sock mode 660 level admin
stats timeout 30s
user haproxy
group haproxy
daemon
maxconn 2048
@paulirish
paulirish / what-forces-layout.md
Last active November 15, 2024 16:45
What forces layout/reflow. The comprehensive list.

What forces layout / reflow

All of the below properties or methods, when requested/called in JavaScript, will trigger the browser to synchronously calculate the style and layout*. This is also called reflow or layout thrashing, and is common performance bottleneck.

Generally, all APIs that synchronously provide layout metrics will trigger forced reflow / layout. Read on for additional cases and details.

Element APIs

Getting box metrics
  • elem.offsetLeft, elem.offsetTop, elem.offsetWidth, elem.offsetHeight, elem.offsetParent
@urschrei
urschrei / hexgrid.py
Last active May 4, 2022 11:57
Python Hexgrid
import math
def calculate_polygons(startx, starty, endx, endy, radius):
"""
Calculate a grid of hexagon coordinates of the given radius
given lower-left and upper-right coordinates
Returns a list of lists containing 6 tuples of x, y point coordinates
These can be used to construct valid regular hexagonal polygons
@jamesbrobb
jamesbrobb / multiform.py
Last active July 4, 2023 18:53 — forked from michelts/gist:1029336
django multiform mixin and view that allows the submission of a) All forms b) Grouped forms c) An individual form
class MultiFormMixin(ContextMixin):
form_classes = {}
prefixes = {}
success_urls = {}
grouped_forms = {}
initial = {}
prefix = None
success_url = None
@ur001
ur001 / decorators.py
Created May 28, 2014 13:20
Django models decorator for tracking fields changes
def track_field_changes(only=None, exclude=()):
"""
Django models decorator for tracking fields changes
:only: fields to track for changes (all otherwise)
:exclude: fields to exclude from tracking
Adds to model instance:
get_old_value(field_name) — old value of given field
is_changed(field_name=None) — is any field (or given field) is changed