Skip to content

Instantly share code, notes, and snippets.

View mohsen1's full-sized avatar

Mohsen Azimi mohsen1

View GitHub Profile
@mohsen1
mohsen1 / reactor.js
Last active January 15, 2022 21:57
Reactor Pattern in JavaScript
function Event(name){
this.name = name;
this.callbacks = [];
}
Event.prototype.registerCallback = function(callback){
this.callbacks.push(callback);
}
function Reactor(){
this.events = {};
@mohsen1
mohsen1 / download.js
Last active March 30, 2019 05:13
Download Chromecast backgrounds
var https = require('https');
var fs = require('fs');
var url = 'https://raw.githubusercontent.com/dconnolly/chromecast-backgrounds/master/backgrounds.json';
Array.prototype.getLast = function() {
return this[this.length - 1];
};
function logFail(err){
console.log('Failed!');
@mohsen1
mohsen1 / keybindings.json.yaml
Last active November 4, 2018 19:23
Sublime Text key bindings for Visual Studio Code
[
{
"key": "shift+cmd+d",
"command": "editor.action.copyLinesDownAction"
},
{
"key": "cmd+l",
"command": "expandLineSelection"
},
@mohsen1
mohsen1 / HydratableStore.tsx
Created July 31, 2017 21:20
How to hydrate stores in the server with MobX
import * as React from 'react';
import { computed } from 'mobx';
import { inject, observer, Provider } from 'mobx-react';
import { fromPromise } from 'mobx-utils';
import {StaticRouter} from 'react-router-dom';
import { Request, Response } from 'express';
import * as ReactDOM from 'react-dom/server';
interface Item {
weight: number;
function foo(a) {
console.log(a.b)
}
foo({})
function power2(a) {
return a * a;
}
power2('string')
/**
* Create an input element
*/
function createInput(type: 'text', value: string): HTMLInputElement;
function createInput(type: 'checkbox', value: boolean): HTMLInputElement;
function createInput(type, value) {
// code
}
const input = createInput('checkbox', 'false')
/**
* Create an input element
* @param {string} type
* @param {string|boolean} value
* @return {HTMLInputElement}
*/
function createInput(type, value) {
const el = document.createElement('input');
el.type = type;
if (type === 'checkbox') {
import * as React from 'react';
import { RouteComponentProps } from 'react-router-dom';
interface LoadableProps<T> {
loader: (props: RouteComponentProps<T>) => (() => Promise<React.ComponentType<T>> | React.ComponentType<T>);
loading: React.ComponentType<T>;
}
export function Loadable<T>(loadableProps: LoadableProps<T>) {
return class LoadableComponent extends React.Component<RouteComponentProps<T>, { ResultComponent: React.ComponentType<T> }> {