Skip to content

Instantly share code, notes, and snippets.

View mikeapr4's full-sized avatar

Michael Gallagher mikeapr4

  • Buenos Aires, Argentina
View GitHub Profile
@mikeapr4
mikeapr4 / getter-reactive-caching.js
Last active May 29, 2019 08:22
example of reactive method-style getter caching
// recreation of a computed property
const computed = (vm, func) => {
vm.$watch(
func,
null, // callback not used for lazy
{ lazy: true }, // lazy means we can just flag as dirty
);
// eslint-disable-next-line no-underscore-dangle
const watcher = vm._watchers[vm._watchers.length - 1];
@mikeapr4
mikeapr4 / mutations-v-actions--first-look.js
Last active April 14, 2019 00:44
Code Samples - Mutations vs Actions
mutations: {
setName(state, name) {
state.name = name;
},
},
actions: {
setName({ commit }, name) {
commit('setName', name);
},
},
actions: {
loadBooks({ commit }) {
commit('startLoading');
get('/api/books').then((response) => {
commit('setBooks', response.data.books);
commit('stopLoading');
});
},
},
actions: {
loadBooks({ commit }) {
commit('startLoading');
get('/api/books').then((response) => {
commit('setBooks', response.data.books);
commit('stopLoading');
});
},
loadAuthors({ commit }) {
commit('startLoading');
state: { loading: false },
mutations: {
startLoading(state) {
state.loading = true;
},
stopLoading(state) {
state.loading = false;
},
},
state: { loading: 0 },
mutations: {
startLoading(state) {
state.loading += 1;
},
stopLoading(state) {
state.loading -= 1;
},
},
actions: {
async loadBooks({ commit }) {
commit('startLoading');
const response = await get('/api/books');
commit('setBooks', response.data.books);
commit('stopLoading');
},
},
actions: {
loadBooks({ commit }) {
commit('startLoading');
return get('/api/books').then((response) => {
commit('setBooks', response.data.books);
commit('stopLoading');
});
},
}
const AsyncFunction = (async () => {}).constructor;
console.log((() => {}) instanceof AsyncFunction); // false
console.log((async () => {}) instanceof AsyncFunction); // true
mutactions: {
async loadBooks({ state }) {
state.loading += 1;
const response = await get('/api/books');
state.books = response.data.books;
state.loading -= 1;
},
}