Skip to content

Instantly share code, notes, and snippets.

@typoerr
typoerr / material-colors.d.ts
Last active October 2, 2020 02:48
material-colors.d.ts
export declare var red: {
50: string;
100: string;
200: string;
300: string;
400: string;
500: string;
600: string;
700: string;
800: string;
// tslint:disable:no-console
(function sw(self: ServiceWorkerGlobalScope) {
self.addEventListener('install', e => {
console.log('sw:install', e)
})
self.addEventListener('activate', e => {
// e.waitUntil(self.clients.claim())
console.log('sw:activate', e)
})
self.addEventListener('message', e => {
@typoerr
typoerr / getin.ts
Created January 28, 2018 04:32
typed rxjs/operator/pluck
import { Observable } from 'rxjs/Observable'
import { of } from 'rxjs/observable/of'
import { map } from 'rxjs/operators'
/**
* Typesafe rxjs/operators/plunk
* @example
* const state$ = of({ counter: { count: 0 } })
* state$.pipe(getIn('counter', 'count')) //-> Observable<number>
*/
export function combine<A, R1, R>(funcs: [Func1<A, R1>]): (value: A) => [R1]
export function combine<A, R1, R>(funcs: [Func1<A, R1>], project: (value: [R1]) => R): Func1<A, R>
export function combine<A, R1, R2, R>(funcs: [Func1<A, R1>, Func1<A, R2>]): Func1<A, [R1, R2]>
export function combine<A, R1, R2, R>(funcs: [Func1<A, R1>, Func1<A, R2>], project: (value: [R1, R2]) => R): Func1<A, R>
export function combine<A, R1, R2, R3, R>(funcs: [Func1<A, R1>, Func1<A, R2>, Func1<A, R3>]): Func1<A, [R1, R2, R3]>
export function combine<A, R1, R2, R3, R>(funcs: [Func1<A, R1>, Func1<A, R2>, Func1<A, R3>], project: (value: [R1, R2, R3]) => R): Func1<A, R>
export function combine<A, R1, R2, R3, R4, R>(funcs: [Func1<A, R1>, Func1<A, R2>, Func1<A, R3>, Func1<A, R4>]): Func1<A, [R1, R2, R3, R4]>
export function combine<A, R1, R2, R3, R4, R>(
funcs: [Func1<A, R1>, Func1<A, R2>, Func1<A, R3>, Func1<A, R4>],
@typoerr
typoerr / fsa.ts
Created January 26, 2018 00:00
typescript-fsa + payload mapper
export interface AnyAction {
type: any
}
export interface Action<P = any> extends AnyAction {
type: string
payload: P
[key: string]: any
}
@typoerr
typoerr / example.ts
Last active January 25, 2018 00:39
flux using rxjs inspired by redux + redux-observable
import { Observable } from 'rxjs'
import { Dispatcher, select, createActionFactory } from './index'
import { bootFlux } from './boot-flux'
/* actions */
interface Actions {
INCREMENT: number
DECREMENT: number
INCREMENT_ASYNC_REQUEST: any
INCREMENT_ASYNC_COMMIT: number
@typoerr
typoerr / rxjs-state-management.ts
Last active January 17, 2018 07:45
State management by RxJS
import { Observable, Subject, BehaviorSubject } from 'rxjs'
interface State {
counter: { count: number },
user: { name: string, age: number }
}
const models = {
count(intent: { inc$: Observable<number>, dec$: Observable<number> }) {
type S = State['counter']
declare module 'idb-schema' {
export = Schema
class Schema {
readonly _current: { version: number, store: Schema.StoreDescription }
readonly _stores: { [key: string]: Schema.StoreDescription }
readonly _versions: Schema.VersionMap
stores(): Schema.StoreDescription[]
version(): number // current version
import { EventEmitter2 } from 'eventemitter2';
import { Stream, fromEvent, mergeArray } from 'most';
import { MiddlewareAPI, Middleware, Dispatch } from 'redux';
// ==================================================================
// EventSource
// ==================================================================
export type Listener<T, K extends keyof T> = (arg: T[K]) => any;
export class TypedEventEmitter<T = any> extends EventEmitter2 {
declare var navigator: Navigator;
// navigator.serviceWorker.readyはactivate直前に発火するため、controllerの存在確認には信用できない
// そのためcontrollerchangeイベントを待つ必要がある
export default function awaitServiceWorker() {
return new Promise<ServiceWorker>((resolve) => {
const resolved = () => resolve(navigator.serviceWorker.controller!);
if (navigator.serviceWorker.controller) {
resolved();