javascript
import { useState, useRef, useEffect, useCallback } from 'react';

export function useOnScreen(options = {}) {
  const [node, setNode] = useState(null);
  const [isIntersecting, setIsIntersecting] = useState(false);

Infinite Scroll in React with an IntersectionObserver useOnScreen Hook

react hooks intersection-observer
by codesnips 3 tabs
typescript
export interface QueryCodec<T> {
  parse: (raw: string | null) => T;
  serialize: (value: T) => string | null;
}

export function stringParam(fallback = ""): QueryCodec<string> {

Sync Typed Form State to the URL Query String with a useQueryState Hook

react hooks url-state
by codesnips 3 tabs
python
from datetime import date, datetime
from decimal import Decimal, InvalidOperation
from enum import Enum

from pydantic import BaseModel, field_validator

Parsing and Normalizing a Transactions CSV into Typed Records with Pydantic

csv pydantic data-parsing
by codesnips 2 tabs
rust
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AppConfig {
    #[serde(default)]
    pub server: ServerConfig,

Layered Config Loading From TOML File and Environment Variables in Rust

config serde toml
by codesnips 3 tabs
ruby
class Cart < ApplicationRecord
  has_many :cart_items, dependent: :destroy

  EXPIRY_WINDOW = 2.hours

  scope :active, -> { where(state: :active) }

Expire Stale Shopping Carts With a Rails Scope and a Recurring Reaper Job

rails activerecord background-jobs
by codesnips 3 tabs
java
@MappedSuperclass
@FilterDef(
    name = "tenantFilter",
    parameters = @ParamDef(name = "tenantId", type = String.class)
)
@Filter(name = "tenantFilter", condition = "tenant_id = :tenantId")

Enforce Multi-Tenant Row Isolation With Hibernate @Filter Enabled Per Request in Spring Boot

spring-boot hibernate jpa
by codesnips 4 tabs
javascript
function createReadiness() {
  let ready = true;

  function markUnready() {
    ready = false;
  }

Graceful HTTP Server Shutdown on SIGTERM With In-Flight Request Draining in Node.js

nodejs http graceful-shutdown
by codesnips 3 tabs
typescript
import type { Redis } from "ioredis";

export interface RawEntry {
  member: string;
  score: number;
}

Redis-Backed Paginated Leaderboard With a Repository and Ranking Service

redis leaderboard pagination
by codesnips 3 tabs
rust
use serde::Deserialize;

#[derive(Debug, Deserialize)]
pub struct Page<T> {
    pub items: Vec<T>,
    #[serde(default)]

Streaming a Paginated HTTP API as a Rust Iterator

rust iterator pagination
by codesnips 3 tabs
ruby
module Middleware
  class RateLimiter
    def initialize(app, redis:, limit:, window:)
      @app = app
      @limiter = SlidingWindowLimiter.new(redis: redis, limit: limit, window: window)
      @limit = limit

Sliding-Window API Rate Limiting with a Rack Middleware and Redis in Rails

rails rack redis
by codesnips 3 tabs
javascript
export const MAX_VISIBLE = 4;

const DEFAULTS = {
  variant: "info",
  duration: 4000,
};

Toast Notification Queue in React with Context and useReducer

react context-api usereducer
by codesnips 3 tabs
java
package com.example.billing.config;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration

Type-Safe Spring Boot Config with @ConfigurationProperties Records and Validation

spring-boot configuration validation
by codesnips 4 tabs