Skip to content

Instantly share code, notes, and snippets.

View cazala's full-sized avatar
🤸‍♂️

Juan Cazala cazala

🤸‍♂️
View GitHub Profile
@cazala
cazala / environment-javascript.js
Last active October 16, 2016 02:14
How to tell if inside a Browser, WebWorker or NodeJS from Javascript
if (typeof window === 'object') {
// Browser
} else if (typeof importScripts === 'function') {
// WebWorker
} else if (typeof process === 'object' && typeof require === 'function') {
// NodeJS
} else {
// shell
}
import React from 'react';
export default ({ condition, children }) => condition ? React.Children.only(children); : null;
@cazala
cazala / m.js
Created December 13, 2017 16:13
var libUrl = null;
var scriptTag = Array.prototype.slice
.call(document.getElementsByTagName("script"))
.filter(x => /\?proxy?/.test(x.src));
if (scriptTag.length > 0) {
libUrl = scriptTag[0].src.split("m.js")[0];
} else {
throw new Error("missing '?proxy' query parameter in your proxy url!");
}
@cazala
cazala / guide.md
Created January 2, 2018 16:19 — forked from menduz/guide.md
Frontend React + TypeScript guidelines

Directory Structure

The sources of the project follows this structure:

/src
  /app
    /{domain}
      /actions.ts
 /actions.spec.ts
@cazala
cazala / Server.ts
Last active May 16, 2018 22:14
Decentraland Shoal Scene - Server
// Server
const app = require('express')()
const server = require('http').createServer(app)
const wss = new WebSocketServer({ server })
const clients: Set<RemoteScene> = new Set()
wss.on('connection', (ws, req) => {
const transport = WebSocketTransport(ws)
const client = new RemoteScene(transport)
clients.add(client)
// Sea
const { Sea } = require('shoaling')
const sea = new Sea(38, 1200, 300, 1200) // simulate 38 fish on a 'sea' of 1200x1200 (X and Z) and 300 deep (Y)
sea.start(64) // update every 64 milliseconds
setInterval(() => {
render(sea)
clients.forEach(client => client.forceUpdate())
}, 100) // render scene every 100 ms
import { createElement, ScriptableScene, ISimplifiedNode } from 'metaverse-api'
import { Sea } from './components/Sea'
let cachedScene: ISimplifiedNode | null = null
export function render(sea: any) {
cachedScene = Sea({sea})
}
export default class RemoteScene extends ScriptableScene {
async render() {
@cazala
cazala / Vector.js
Last active July 16, 2018 23:39
Generic javascript vector class
function Vector(x, y, z) {
this.x = x || 0;
this.y = y || 0;
this.z = z || 0;
}
Vector.prototype = {
negative: function() {
return new Vector(-this.x, -this.y, -this.z);
},
-import { groupMovesByColor } from '~/modules/game/selectors';
+import { groupMovesByColor } from '../modules/game/selectors';
// scene/server/Store.ts
const store = require('../../../src/store').default
const {
initSquares,
squareClick
} = require('../../../src/modules/squares/actions')
store.dispatch(initSquares())
export { initSquares, squareClick }
export default store