Skip to content

Instantly share code, notes, and snippets.

View capaj's full-sized avatar
🏠
Always working-be it from home or elsewhere

Jiri Spac capaj

🏠
Always working-be it from home or elsewhere
View GitHub Profile
/* global localStorage */
import {observable, autorunAsync} from 'mobx'
import _ from 'lodash'
function storedObservable (key, defaultValue, debounce) {
let fromStorage = localStorage.getItem(key)
const defaultClone = _.cloneDeep(defaultValue) // we don't want to modify the given object, because userscript might want to use the original object to reset the state back to default values some time later
if (fromStorage) {
_.merge(defaultClone, JSON.parse(fromStorage))
}
@capaj
capaj / serve.js
Last active August 29, 2021 14:14
socket.io acknowledge node.js sample
var io = require('socket.io')(8090);
io.on('connection', function (socket) {
console.log('connected')
socket.on('ferret', function (name, fn) {
console.log('ferret')
fn('woot');
});
});
failed with "unknown: Property body[0] of BlockStatement expected node to be of a type ["Statement"] but instead got "MemberExpression""
@capaj
capaj / component.js
Last active June 22, 2016 12:40
react stateful function component
import React from 'react'
import {observer} from 'mobx-react'
import {observable} from 'mobx'
const state = observable({
value: 0
})
const Comp = (props) => {
return <div onClick={() => state.value++}>click to increase counter value: {state.value}</div>
make[1]: Entering directory '/home/capaj/git_projects/koa/benchmarks'
1 middleware
18730.10
5 middleware
17620.69
10 middleware
16914.27
make[1]: Entering directory '/home/capaj/git_projects/koa/benchmarks'
1 middleware
12657.20
5 middleware
11990.98
10 middleware
12123.40
mobX create observable, autorun and dispose x 357,481 ops/sec Β±5.15% (72 runs sampled)
proxevable create observable, autorun and dispose x 79,279 ops/sec Β±4.75% (71 runs sampled)
Fastest is mobX create observable, autorun and dispose
Community Packages (43) /home/capaj/.atom/packages
β”œβ”€β”€ [email protected]
β”œβ”€β”€ [email protected]
β”œβ”€β”€ [email protected]
β”œβ”€β”€ [email protected]
β”œβ”€β”€ [email protected]
β”œβ”€β”€ [email protected]
β”œβ”€β”€ [email protected]
β”œβ”€β”€ [email protected]
β”œβ”€β”€ [email protected]

This rule should only be enforced for node.js > 0.12 There is no point in declaring a module as var. Module should always be a const if assigned on the same line as the require statement is. If you want to require module "A" or module "B" into a single variable, it should be done like this:

var aOrB
if (cond) {
  aOrB = require('A')  
} else {
  aOrB = require('B')
}
@capaj
capaj / onDidSave-atom.js
Created October 27, 2015 19:10
onDidSave atom editor
atom.workspace.observeTextEditors(function(editor){
editor.onDidSave(function(){console.log('saved')})
})