Skip to content

Instantly share code, notes, and snippets.

View nitaking's full-sized avatar

Satoshi Nitawaki nitaking

View GitHub Profile
@nitaking
nitaking / main.dart
Created October 30, 2023 05:29
rustic-sunshine-3259
class User {
final int id;
final String name;
const User({
required this.id,
required this.name,
});
}
@nitaking
nitaking / main.dart
Last active September 7, 2023 01:07
enum Animal {
Cat,
Dog,
}
hello(Animal animal) {
return switch (animal) {
Animal.Cat => 'is Cat',
Animal.Dog => 'is Dog',
};
import 'dart:async';
import 'package:hooks_riverpod/hooks_riverpod.dart';
extension AutoDisposeRefCache on AutoDisposeRef {
static final Map<String, Timer> _timersMap = {};
// Keeps the provider alive for [duration] since when it was first created
void cacheFor(Duration duration, {String? cacheKey}) {
if (cacheKey != null && _timersMap.containsKey(cacheKey)) {
@nitaking
nitaking / gist:6c5ff1ca73317a4bf897bf1e54765bd3
Created August 30, 2023 06:32
auto_dispose_extension.dart
import 'dart:async';
import 'package:hooks_riverpod/hooks_riverpod.dart';
extension AutoDisposeRefCache on AutoDisposeRef {
static final Map<String, Timer> _timersMap = {};
// Keeps the provider alive for [duration] since when it was first created
void cacheFor(Duration duration, {String? cacheKey}) {
if (cacheKey != null && _timersMap.containsKey(cacheKey)) {
const isSSR = () => typeof window === 'undefined';
const UserScheme = z.object({
name: z.string().nonempty().max(50),
age: z.number().min(1).max(200),
});
type Values = z.infer<typeof UserScheme>;
type Forms = {
setName: (value: string) => void;
setAge: (value: number) => void;
valid: boolean;
select id,
start_at,
# date_add(start_at, interval -DAY(start_at) + 1 DAY),
end_at
from sample_data;
# [
# {
# "id": 1,
# "start_at": "2022-04-01 20:57:07",
@nitaking
nitaking / useOnBrowserBack.ts
Created May 25, 2022 10:42
ブラウザバックしたときに処理を実行したいときに使用するhooks
import { useEffect } from 'react';
/**
* ブラウザバックしたときに処理を実行したいときに使用するhooks
*/
export const useOnBrowserBack = (onBrowserBack: () => void) => {
useEffect(() => {
window.history.pushState(null, '', window.location.pathname);
window.addEventListener('popstate', onBrowserBack);
return () => {
@nitaking
nitaking / useOnReloadAlert.tsx
Last active May 25, 2022 10:41
リロード時に離脱防止を防ぐ
import { useEffect } from 'react';
/**
* 一度入力した後にリロードすると、確認ダイアログを出す。
*/
export const useOnReloadAlert = () => {
useEffect(() => {
window.history.pushState(null, '', window.location.pathname);
const onReload = (event: BeforeUnloadEvent) => {
event.preventDefault();
function chunk(arr, size) {
return arr.reduce(
(newarr, _, i) => (i % size ? newarr : [...newarr, arr.slice(i, i + size)]),
[]
)
}