Skip to content

Instantly share code, notes, and snippets.

@sbatson5
sbatson5 / throttled-actions.js
Created September 27, 2019 15:16
Throttled Vuex Actions
import { api, throttle } from '@/utils';
const actions = {
setEvents({ commit }, payload) {
throttle(function* () {
const { zip, radius } = payload;
yield api.get('/events/general')
.then(response => commit('SET_GENERAL_EVENTS', response.data)
@sbatson5
sbatson5 / concurrency.js
Created September 27, 2019 15:09
Throttling Vuex actions
import store from '@/store';
/**
* A simple class we create just for tracking our running functions
*/
class Concurrency {
get(key) {
return this[key] || {};
}
function* genFunction() {
yield 'Hi there!';
yield 'Is this working?';
yield 'Bye 👋';
};
const ourMethod = genFunction();
ourMethod.next();
// {value: "Hi there!", done: false}
@sbatson5
sbatson5 / actions.js
Last active September 27, 2019 15:15
actions.js
const actions = {
setEvents({ commit }, payload) {
const { zip, radius } = payload;
api.get('/events/general')
.then(response => commit('SET_GENERAL_EVENTS', response.data)
.catch(catchError);
api.get(`/events/zip/${zip}`, { params: { radius } })
@sbatson5
sbatson5 / Install.php
Last active September 6, 2019 20:11
Install script for CraftCMS plugin
<?php
namespace upstatement\jobsboard\migrations;
use upstatement\jobsboard\Jobsboard;
use Craft;
use craft\config\DbConfig;
use craft\db\Migration;
@sbatson5
sbatson5 / Job.php
Last active September 6, 2019 19:25
Table attributes for CraftCMS Job Element
<?php
namespace upstatement\jobsboard\elements;
use upstatement\jobsboard\Jobsboard;
// A bunch of other imports
use Craft;
use craft\base\Element;
class Job extends Element {
@sbatson5
sbatson5 / elements.twig
Created September 6, 2019 18:11
Elements index page
{% extends '_layouts/elementindex' %}
{% set title = 'Jobs' %}
{% set elementType = 'upstatement\\jobsboard\\elements\\Job' %}
@sbatson5
sbatson5 / mutations.js
Created September 6, 2019 14:21
Mutations example
const mutations = {
...
'LAST_DISPATCHED_ACTION': (state, actionObject) => {
state.lastDispatchedAction = actionObject;
},
...
}
@sbatson5
sbatson5 / actions.js
Last active August 30, 2019 14:41
Our error handled actions
import firebase from 'nativescript-plugin-firebase';
import { api } from '@/utils';
import store from '@/store';
/**
* If we get an auth token failure, refresh it with firebase and try calling the same action again.
* Otherwise, log an error listing which one failed
*/
const catchError = error => {
const { status, data } = error.response;
@sbatson5
sbatson5 / retry.js
Created August 30, 2019 14:34
Retry last vuex action
Object.defineProperty(store, 'retryLastAction', {
value() {
const { name, payload } = store.state.lastDispatchedAction;
return store.dispatch(name, payload);
},
});