Skip to content

Instantly share code, notes, and snippets.

@kulor
kulor / redux-offline-action.js
Created May 3, 2018 15:03
Example Redux Offline action
const followUser = userId => ({
type: 'FOLLOW_USER_REQUEST',
payload: { userId },
meta: {
offline: {
// the network action to execute:
effect: { url: '/api/follow', method: 'POST', body: JSON.stringify({ userId }) },
// action to dispatch when effect succeeds:
commit: { type: 'FOLLOW_USER_COMMIT', meta: { userId } },
// action to dispatch if network action fails permanently:
@kulor
kulor / redux-offline-config.js
Created May 3, 2018 15:02
Example Redux Offline config with network detection
const reduxOfflineConfig = {
...,
detectNetwork: callback => {
setInterval(async () => {
try {
await fetch('yourbackend.com/ping', { method: 'HEAD' })
callback({
online: true
})
} catch(e) {
@kulor
kulor / redux-persist-config.js
Created May 3, 2018 15:01
Example configuration for Redux Persist
import storage from 'redux-persist/lib/storage' // defaults to localStorage for web and AsyncStorage for react-native
const reduxPersistConfig = {
key: 'root',
storage: storage,
blacklist: ['sensitive-data', 'ephemeral-state']
// Or:
whitelist: ['list-of-foos', 'list-of-bars']
}
@kulor
kulor / persist-gate.js
Created May 3, 2018 14:58
Configuring a store with a persist gate
...
const Loading = () => (<div>Loading...</div>)
const App = () => {
return (
<Provider store={store}>
<PersistGate loading={<Loading />} persistor={persistor}>
<RootComponent />
</PersistGate>
</Provider>
@kulor
kulor / create_inventory_item.py
Created May 3, 2018 14:56
Example idempotent resource creation
def create_inventory_item(request):
body_unicode = request.body.decode('utf-8')
body = json.loads(body_unicode)
idempotent_id = body.get('idempotent_id')
item = InventoryModel.objects.get(idempotent_id=idempotent_id)
if item:
return JsonResponse(item) # Will include the backend primary key ID
else:
InventoryModel.objects.create(
@kulor
kulor / Lodash disparity.txt
Last active April 21, 2017 21:08
Lodash.min.js CDNJS vs. JSDELIVR
# CDNJS - 25.6KB
curl 'https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js' --silent -H 'Accept-Encoding: gzip,deflate' --write-out '%{size_download}\n' --output /dev/null
25594
# JSDELIVR - 24.2KB
curl 'https://cdn.jsdelivr.net/lodash/4.17.4/lodash.min.js' --silent -H 'Accept-Encoding: gzip,deflate' --write-out '%{size_download}\n' --output /dev/null
24161
@kulor
kulor / dropshadow-ie7.css
Created August 3, 2012 13:32
ie image dropshadow support
/*
Dropshadow handling in IE vs. native box-shadow support
- Normal box-shadow on .dropshadow
/////////////////////
/ +===============+ /
/ | | /
/ | | /
/ | | /
@kulor
kulor / .jshintrc
Created July 13, 2012 09:13
My JSHint preferences
{
"predef": [
"define",
"require"
],
"es5" : false,
"browser" : true,
"boss" : false,
"curly": true,
@kulor
kulor / move_files.sh
Created May 23, 2012 10:22
Change a list of filenames
for i in `find *.png`; do echo mv $i `echo $i | sed 's/\([0-9]*\).*\.png/\1.png/'`; done
@kulor
kulor / async.js
Created May 8, 2012 11:55
Send off multiple http requests and deal with response
var async = require('async'); // npm install async
async.parallel({
foo: function(callback){
http.get({host:'localhost', port:9999, path:'/foo'}, function (res) {
res.on('data', function (data) {
callback(null, JSON.parse(data.toString()));
});
})
},
bar: function(callback){