-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
39 lines (30 loc) · 901 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
const { reactive, inject } = require('vue')
const STORE_KEY = 'storeon'
function createStoreonPlugin (store) {
return {
install (app) {
if (process.env.NODE_ENV !== 'production' && !store) {
throw new Error(
'Please provide store to the "createStoreonPlugin" function'
)
}
store.state = reactive(store.get())
app.provide(STORE_KEY, store)
app.config.globalProperties.$storeon = store
store.on('@changed', newState => {
Object.assign(store.state, newState)
})
}
}
}
function useStoreon () {
let store = inject(STORE_KEY)
if (process.env.NODE_ENV !== 'production' && !store) {
throw new Error(
'Could not find storeon context value. ' +
'Make sure you provide store using "createStoreonPlugin" function'
)
}
return store
}
module.exports = { createStoreonPlugin, useStoreon }