Skip to content

Instantly share code, notes, and snippets.

View alexsasharegan's full-sized avatar

Alex Regan alexsasharegan

View GitHub Profile
<div id="root"></div>
<script src="//unpkg.com/vue/dist/vue.js"></script>
<script>
window.app = new Vue({
el: "#root",
functional: true,
render(h) {
return h('span', 'Hello λλλ')
}
function newWaitGroup(state = 0) {
const cbs = []
const wg = {}
function invoke() {
let cb
while ((cb = cbs.shift())) {
if (typeof cb != "function") {
throw new TypeError(
"A callback function should be passed to WaitGroup.wait()"
)
function processConsecutive(list, func) {
let len = list.length
let i = 0
return new Promise(resolve => {
const iterate = item => {
func(item, () => {
i++
if (i < len) {
@alexsasharegan
alexsasharegan / WrapErr.ts
Created January 28, 2018 20:27
Type safe error handling for javascript promises.
type Result<T> =
| {
ok: true
value: T
}
| {
ok: false
error: any
}
@alexsasharegan
alexsasharegan / Log.ts
Created January 28, 2018 02:14
Basic logging interface.
export interface Logger {
Log(...args: any[]): void;
Info(...args: any[]): void;
Error(...args: any[]): void;
}
export enum LogLevel {
None = 0,
Log = 1 << 0,
@alexsasharegan
alexsasharegan / wrapErr.ts
Created January 25, 2018 17:26
Go-like error handling with Typescript.
export async function wrapErr<T>(p: Promise<T>): Promise<[any, T | undefined]> {
try {
return [undefined, await p];
} catch (err) {
return [err, undefined];
}
}
let [err, value] = await wrapErr(somePromiseFunc());
if (err) {
" Specify a directory for plugins
" - For Neovim: ~/.local/share/nvim/plugged
" - Avoid using standard Vim directory names like 'plugin'
call plug#begin('~/.vim/plugged')
Plug 'sheerun/vim-polyglot'
Plug 'itchyny/lightline.vim'
Plug 'joshdick/onedark.vim'
call plug#end()
@alexsasharegan
alexsasharegan / pool.ts
Last active April 27, 2018 06:25
Generic pool using Promises.
import { EventEmitter } from "events";
/**
* FactoryFunc returns a Promise
* that resolves with type T.
*/
export type FactoryFunc<T> = () => Promise<T>;
/**
* DestructorFunc takes a type T
* and returns a promise when it has been destroyed.
@alexsasharegan
alexsasharegan / waitgroup.ts
Last active April 1, 2020 08:58
Nodejs implementation of the Golang waitgroup.
import { EventEmitter } from "events"
export class WaitGroup extends EventEmitter {
public state: number
constructor(initialState: number = 0) {
if (initialState < 0) {
throw new RangeError()
}
super()
export type Currency = string
/**
* @link https://developers.google.com/analytics/devguides/collection/analyticsjs/enhanced-ecommerce#product-data
*/
export interface ECommerceProductData {
/**
* The product ID or SKU (e.g. P67890). *Either this field or name must be set.
*/
id: string