Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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-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: