Skip to content

Instantly share code, notes, and snippets.

View endel's full-sized avatar

Endel Dreyer endel

View GitHub Profile
@endel
endel / constructor-typescript.d.ts
Created August 7, 2019 19:55
Constructor of a type in TypeScript
type AConstructorTypeOf<T> = new (...args:any[]) => T;
@endel
endel / strong-signal-usage.ts
Last active July 19, 2019 20:16
Lightweight Strong Typed Signals in TypeScript
import { createSignal } from "./strong-signal"
// declare the signal
const onNumber = createSignal<(num: number) => void>();
// ok
onNumber(function(num) {});
// compilation error! second argument not allowed
onNumber(function(num, secondArg) {});
-main Sandbox.hx
-js Sandbox.js
interface Serializer<State, API= any> {
getState() : State;
getAPI() : API;
}
class MyAPI<State> {
state: State;
my() {}
api() {}
methods() {}
// npm install nanoid
import * as generateId from "nanoid/generate";
class MyRoomHandler extends Room {
onInit () {
// this may introduce colliding `roomId`, use at your own risk
this.roomId = generateId("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ", 6);
}
}
@endel
endel / generateUserAgent.ts
Created September 13, 2018 23:41
Fake User Agent Generator
export function* generateUserAgent() {
let webkitVersion = 10;
let chromeVersion = 1000;
const so = [
'Windows NT 6.1; WOW64',
'Windows NT 6.2; Win64; x64',
"Windows NT 5.1; Win64; x64",,
'Macintosh; Intel Mac OS X 10_12_6',
"X11; Linux x86_64",
///<reference types="mocha" />
import { assert, expect } from "chai";
import { StateContainer, DataChange } from "../src";
function clone (data: any) {
return JSON.parse(JSON.stringify(data));
}
describe("StateContainer", () => {
let container: StateContainer<any>;
@endel
endel / fossil-delta-js-checksum.js
Last active October 26, 2017 13:13
Benchmarking checksum when applying delta using fossil-delta-js: https://github.com/dchest/fossil-delta-js/pull/1
var Benchmark = require('benchmark');
var fs = require('fs');
var path = require('path');
var fossilDelta = require('../fossil-delta.js');
// Silence error logging.
fossilDelta.logError = function() {}
var makeArray = function(buf) {
var a = new Array(buf.length);
@endel
endel / concat-benchmark.ts
Last active October 20, 2017 21:43
Manual concat performs better than `Array.prototype.concat `.
import * as Benchmark from "benchmark";
// Array.prototype.concat()
const concat = function(...args: any[]) {
let arr = [1,2,3]
return arr.concat(args);
};
// "concat" manually, performs better
const manual = (...args: any[]) => {
@endel
endel / new.ts
Created December 11, 2016 21:46
Colyseus.js - patch handling comparison from v0.5.x to v0.6.x (not released yet)
// ...
registerListeners () {
this.room.state.listen("cars/:id", "add", (clientId: string, value: any) => {
this.addCar(clientId, value);
});
this.room.state.listen("cars/:id", "remove", (clientId: string) => {
this.view.removeCar(clientId);
});