Skip to content

Instantly share code, notes, and snippets.

@vedantroy
vedantroy / demo.ts
Last active December 31, 2024 09:15
SQLite-backed key-value store with JS-like object manipulation and automatic JSON serialization.
import Database from 'better-sqlite3';
import { createDatabaseClient } from './proxy.ts';
// 1) Create an in-memory DB and your table(s).
const db = new Database(':memory:');
db.exec(`
CREATE TABLE users (
id TEXT PRIMARY KEY,
data JSON
);
@Schniz
Schniz / force-sync-jazz-state.ts
Last active November 19, 2024 19:00
force sync jazz state
async function forceSync(account: Account) {
const { syncManager } = account._raw.core.node;
const peer = Object.values(syncManager.peers)[0];
if (!peer) {
throw new Error("no peer");
}
const values = Object.values(account._raw.core.node.coValues).flatMap((x) => {
if ("coValue" in x.state) {
@garth
garth / readme.md
Last active January 18, 2024 21:58
A Yjs provider for socketsupply

You can use it the same as you do with other yjs providers, but you have to pass it a socket which can be created with someting like this:

// set the peer id
const peerId = window.localStorage.getItem('peerId') ?? (await Encryption.createId())
window.localStorage.setItem('peerId', peerId)

// set the signing keys
const keySeed = window.localStorage.getItem('keySeed') ?? createId()
@itsjavi
itsjavi / components__ServiceWorkers.tsx
Last active March 5, 2025 01:12
Simple Service Worker for Next.js static assets
'use client'
import { useEffect } from 'react'
export function ServiceWorkers(): JSX.Element {
useEffect(() => {
if (typeof window === 'undefined') {
return
}
if ('serviceWorker' in navigator) {
import { signal, effect } from "@preact/signals-core";
interface CustomStorage {
getItem(key: string): void;
setItem(key: string, value: string | null): void;
}
/**
* A version of signal() that persists and recalls its value in localStorage.
*
@nichoth
nichoth / index.html
Last active August 20, 2023 20:36 — forked from dsheiko/index.html
Service-worker to prefetch remote images (with expiration) and respond with fallback one when image cannot be fetched
<!DOCTYPE html>
<html>
<head>
<title>Service-worker demo</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script>
if ( "serviceWorker" in navigator ) {
@heapwolf
heapwolf / evented-p2p.js
Created April 15, 2023 20:20
real time streaming p2p demo with socket runtime
import { Peer } from 'socket:peer'
import process from 'socket:process'
import Buffer from 'socket:buffer'
import fs from 'socket:fs'
window.onload = async () => {
const clusterId = '14ecd42...' // truncated, make your own clusterId
const publicKeyHex = 'c43c1ddd...' // truncated, make your own hex encoded key
const privateKeyHex = '46adc2f8e9077c72...' // truncated, make your own hex encoded key
import { useSignal, signal, effect } from '@preact/signals';
import { useLayoutEffect, useMemo, useRef } from 'preact/hooks';
/** @template T @typedef {T extends (infer U)[] ? U : never} Items */
/** @param {{ v, k?, f }} props */
const Item = ({ v, k, f }) => f(v, k);
/**
* Like signal.value.map(fn), but doesn't re-render.
@ststeiger
ststeiger / offline_mdn_docs.md
Last active April 24, 2025 09:08 — forked from zed-dz/offline_mdn_docs.md
Offline MDN Docs
@pesterhazy
pesterhazy / building-sync-systems.md
Last active April 26, 2025 19:08
Building an offline realtime sync engine

So you want to write a sync system for a web app with offline and realtime support? Good luck. You might find the following resources useful.

Overview articles