Skip to content

Instantly share code, notes, and snippets.

@Fl0pZz

Fl0pZz/store.js Secret

Created February 11, 2017 18:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fl0pZz/84e80242f649e0b80dba0457c6525942 to your computer and use it in GitHub Desktop.
Save Fl0pZz/84e80242f649e0b80dba0457c6525942 to your computer and use it in GitHub Desktop.
import Vue from 'vue'
import Vuex from 'vuex'
import notification from './notifications/index'
import toast from './toasts/index'
Vue.use(Vuex)
const debug = process.env.NODE_ENV !== 'production'
const store = new Vuex.Store({
strict: debug,
modules: {
notification,
toast
}
})
if (module.hot) {
module.hot.accept([
'./notifications/index',
'./toasts/index'
], () => {
store.hotUpdate({
modules: {
notification: require('./notifications/index').default,
toast: require('./toasts/index').default
}
})
})
}
export default store
export const INIT_QUEUE = 'INIT_QUEUE'
export const DISPLAY = 'DISPLAY'
export const UPDATE_FLAG = 'UPDATE_FLAG'
import * as types from './toast-mutation-types'
export default {
[types.INIT_QUEUE] (state, size) {
state.init = true
state._max_toasts = size
state.free = size
for (let i = 0; i < size; ++i) {
state.flags[i] = null
}
},
[types.DISPLAY] (state, { id, notification }) {
let i = state.flags.findIndex(elem => (elem === null))
if (i !== -1) {
state.flags[i] = {id, notification}
state.free -= 1
}
},
[types.UPDATE_FLAG] (state, { index, val }) {
state.flags[index] = val
if (val === null) state.free += 1
}
}
import { mapGetters } from 'vuex'
import Toast from './toast/Toast'
import { DISPLAY } from 'store/toasts/toast-mutation-types'
const MAX_NOTICES = 5
export default {
name: 'toast-manager',
components: {
Toast
},
watch: {
isEmpty (empty) {
if (!empty && this.$store.getters['toast/canDisplay']) {
this.$store.dispatch('notification/pop_back')
.then(pair => { this.$store.commit(DISPLAY, pair) }) // <-- вот тут
}
},
canDisplay (able) {
if (able && !this.$store.getters['notification/isEmpty']) {
this.$store.dispatch('notification/pop_back')
.then(pair => { this.$store.commit(DISPLAY, pair) }) // <-- вот тут
}
}
},
created () {
this.$store.dispatch('toast/init', MAX_NOTICES)
},
computed: {
...mapGetters({
isEmpty: 'notification/isEmpty',
canDisplay: 'toast/canDisplay',
toastStackSize: 'toast/toastStackSize'
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment