Skip to content

Instantly share code, notes, and snippets.

@Fl0pZz Fl0pZz/store.js Secret
Created Feb 11, 2017

Embed
What would you like to do?
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
You can’t perform that action at this time.