typescript
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable, throwError } from 'rxjs';
import { catchError, shareReplay } from 'rxjs/operators';

export interface Project {

Cache and Deduplicate HTTP GET Requests with shareReplay in an Angular Service

angular rxjs caching
by codesnips 3 tabs
ruby
class ApplicationPolicy
  attr_reader :user, :record

  def initialize(user, record)
    @user = user
    @record = record

Enforcing Controller Authorization with a Pundit-Style Policy Object in Rails

rails authorization pundit
by codesnips 4 tabs
ruby
class User < ApplicationRecord
  has_secure_password

  RESET_TOKEN_TTL = 30.minutes

  def self.reset_verifier

Signed Password Reset Tokens with ActiveSupport::MessageVerifier in Rails

rails authentication password-reset
by codesnips 3 tabs
rust
use tokio::sync::mpsc::{self, Sender};
use tokio::sync::mpsc::error::TrySendError;

use crate::email::{EmailJob, EmailMessage};
use crate::worker::EmailWorker;

Retry-Aware Email Queue With a Tokio mpsc Worker and Exponential Backoff

rust tokio async
by codesnips 3 tabs
typescript
import { useCallback, useEffect, useRef, useState } from "react";

export function useIntersectionObserver(options?: IntersectionObserverInit) {
  const [isIntersecting, setIsIntersecting] = useState(false);
  const nodeRef = useRef<Element | null>(null);
  const optionsKey = JSON.stringify(options ?? {});

Infinite-Scroll Feed with a React IntersectionObserver Hook and Cursor Pagination

react hooks intersection-observer
by codesnips 3 tabs
go
package validate

import (
	"regexp"
	"unicode/utf8"
)

Per-Field Signup Validation Errors With a Reusable Validator in Go

go validation http
by codesnips 2 tabs
python
from dataclasses import dataclass


class ConfigError(Exception):
    pass

Typed Argparse Subcommands with Dataclasses and Handler Dispatch

argparse cli dataclasses
by codesnips 3 tabs
typescript
import { Injectable } from '@angular/core';
import { HttpClient, HttpParams } from '@angular/common/http';
import { Observable, of } from 'rxjs';
import { catchError } from 'rxjs/operators';

export interface SearchResult {

Debounced Typeahead Search in Angular with switchMap and Cancellation

angular rxjs typeahead
by codesnips 3 tabs
go
package sse

type Broker struct {
	subscribers map[chan []byte]struct{}
	subscribe   chan chan []byte
	unsubscribe chan chan []byte

Server-Sent Events in Go with http.Flusher and Context Cancellation

sse streaming http
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
python
from werkzeug.routing import BaseConverter
from werkzeug.exceptions import NotFound
from flask import abort


class ModelConverter(BaseConverter):

Auto-Loading SQLAlchemy Models in Flask Routes With a Custom URL Converter

flask werkzeug sqlalchemy
by codesnips 3 tabs
python
import hashlib
from functools import wraps

from flask import request, make_response
from redis import Redis

Flask ETag Caching for an Expensive Endpoint with Conditional 304 Handling

flask http-caching etag
by codesnips 2 tabs