Skip to content

Instantly share code, notes, and snippets.

View lokshunhung's full-sized avatar
🐱

LS Hung lokshunhung

🐱
  • Hong Kong
View GitHub Profile
@lokshunhung
lokshunhung / RequestAnimationFrame_Renderer.ts
Created May 19, 2025 05:08
fps limited requestAnimationFrame
class Renderer {
#targetDelay: number;
#onTick: (elapsed: number) => void;
#lastTickTimestamp: number = 0;
#animationId?: number;
constructor(fps: number, onTick: (elapsed: number) => void) {
this.#targetDelay = 1_000 / fps;
this.#onTick = onTick;
@lokshunhung
lokshunhung / .ruby-version
Last active November 25, 2024 10:33
React Native generate Xcodeproj Xcworkspace (expo prebuild)
2.7.2
@lokshunhung
lokshunhung / Dockerfile
Last active July 23, 2024 04:49
Dockerfile for ntopng + nProbe
FROM ubuntu:22.04
LABEL org.opencontainers.image.authors="[email protected]"
RUN apt update --yes && \
apt install --yes --quiet \
wget lsb-release gnupg libelf1 && \
wget --quiet http://apt.ntop.org/22.04/all/apt-ntop.deb && \
dpkg --install apt-ntop.deb && \
\
@lokshunhung
lokshunhung / service-workers.md
Created March 15, 2024 03:03 — forked from Rich-Harris/service-workers.md
Stuff I wish I'd known sooner about service workers

Stuff I wish I'd known sooner about service workers

I recently had several days of extremely frustrating experiences with service workers. Here are a few things I've since learned which would have made my life much easier but which isn't particularly obvious from most of the blog posts and videos I've seen.

I'll add to this list over time – suggested additions welcome in the comments or via twitter.com/rich_harris.

Use Canary for development instead of Chrome stable

Chrome 51 has some pretty wild behaviour related to console.log in service workers. Canary doesn't, and it has a load of really good service worker related stuff in devtools.

@lokshunhung
lokshunhung / DictionaryKeyPath.swift
Created February 15, 2024 15:16
Swift DictionaryKeyPath
public struct DictionaryKey {
public typealias Path = WritableKeyPath<Self, Self>
public var keys: [String]
public init(keys: [String] = []) {
self.keys = keys
}
public subscript(key: String) -> Self {
@lokshunhung
lokshunhung / WebSocket.swift
Last active January 4, 2024 08:04
Swift Foundation WebSocket
import Foundation
public final class WebSocket {
public typealias OnOpen = (URLSessionWebSocketTask, _ protocol: String?) -> Void
public typealias OnClose = (URLSessionWebSocketTask, _ closeCode: URLSessionWebSocketTask.CloseCode, _ reason: Data?) -> Void
public typealias OnMessage = (URLSessionWebSocketTask.Message) -> Void
public typealias OnError = (Error) -> Void
public enum ReadyState: Int {
case initial = -1, connecting = 0, `open` = 1, closing = 2, closed = 3
@lokshunhung
lokshunhung / AsyncKVOSequence.swift
Last active December 14, 2023 08:29
KVO in swift concurrency
import Foundation
struct AsyncKVOSequence<Object: NSObject, Property>: AsyncSequence {
typealias Element = NSKeyValueObservedChange<Property>
let storage: Storage
init(_ object: Object,
_ keyPath: KeyPath<Object, Property>,
_ options: NSKeyValueObservingOptions = []) {
self.storage = Storage(
object: object,
@lokshunhung
lokshunhung / OptionalProxy.swift
Created November 9, 2023 12:13
Proxy for reference types, like SwiftUI.Binding
import Foundation
@propertyWrapper
public struct OptionalProxy<Value> where Value: OptionalProtocol {
public var wrappedValue: Value {
get { get() }
set { set(newValue) }
}
public let get: () -> Value
@lokshunhung
lokshunhung / serializeRequest.js
Last active September 19, 2023 04:49
Serialize browser fetch Request
// @ts-check
"use strict";
/** @param {*} value @returns {value is string} */
const isString = value => typeof value === "string";
/** @param {*} value @returns {value is RegExp} */
const isRegExp = value => value instanceof RegExp;
class Santinizer {
#redactStr;
@lokshunhung
lokshunhung / StringInd.swift
Last active July 21, 2023 03:52
StringInd.swift
public struct Ind {
private let s: String
public let index: String.Index
public init(_ s: String, _ index: String.Index) {
self.s = s
self.index = index
}
public init(_ s: String, _ f: (String) -> String.Index) {