1
|
|
|
import {each, noop, some} from './utils' |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Install mixin onto the Vue instance |
5
|
|
|
* |
6
|
|
|
* @param {Vue} Vue - Vue |
7
|
|
|
*/ |
8
|
|
|
export default function (Vue) { |
9
|
|
|
const VueVersion = Number(Vue.version && Vue.version.split('.')[0]) |
10
|
|
|
const initHook = VueVersion && VueVersion > 1 ? 'beforeCreate' : 'init' |
11
|
|
|
|
12
|
|
|
return { |
13
|
|
|
[initHook]: beforeCreate(Vue), |
14
|
|
|
created: created(), |
15
|
|
|
beforeDestroy: beforeDestroy(), |
16
|
|
|
computed: { |
17
|
|
|
$loadingSyncers: loadingStateGetter |
18
|
|
|
}, |
19
|
|
|
methods: { |
20
|
|
|
$refreshSyncers: refreshSyncers |
21
|
|
|
} |
22
|
|
|
} |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/* |
26
|
|
|
* Before creation hook |
27
|
|
|
* |
28
|
|
|
* @param {Vue} Vue - Vue |
29
|
|
|
*/ |
30
|
|
|
function beforeCreate(Vue) { |
31
|
|
|
return function () { |
32
|
|
|
this._syncers = {} |
33
|
|
|
|
34
|
|
|
const SyncCreator = Vue.$syncer.driver |
35
|
|
|
let synced = this.$options.sync |
36
|
|
|
if (synced) { |
37
|
|
|
// Set up each syncer |
38
|
|
|
each(synced, (settings, key) => { |
39
|
|
|
this._syncers[key] = new SyncCreator(Vue, this, key, settings) |
40
|
|
|
|
41
|
|
|
Object.defineProperty(this, key, { |
42
|
|
|
get: () => { |
43
|
|
|
return this._syncers[key] ? this._syncers[key].state : null |
44
|
|
|
}, |
45
|
|
|
set: noop, |
46
|
|
|
enumerable: true, |
47
|
|
|
configurable: true |
48
|
|
|
}) |
49
|
|
|
}) |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* After creation hook |
56
|
|
|
*/ |
57
|
|
|
function created() { |
58
|
|
|
return function () { |
59
|
|
|
// Start syncers |
60
|
|
|
each(this._syncers, syncer => { |
61
|
|
|
syncer.ready() |
62
|
|
|
}) |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Before destruction hook |
68
|
|
|
*/ |
69
|
|
|
function beforeDestroy() { |
70
|
|
|
return function () { |
71
|
|
|
each(this._syncers, (syncer, key) => { |
72
|
|
|
syncer.destroy() |
73
|
|
|
delete this._syncers[key] |
74
|
|
|
}) |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Get loading state of the syncers |
80
|
|
|
* |
81
|
|
|
* @returns {boolean} |
82
|
|
|
*/ |
83
|
|
|
function loadingStateGetter() { |
84
|
|
|
if (Object.keys(this._syncers).length > 0) { |
85
|
|
|
return some(this._syncers, syncer => { |
86
|
|
|
return syncer.loading |
87
|
|
|
}) |
88
|
|
|
} |
89
|
|
|
return false |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Refresh syncers state |
94
|
|
|
* |
95
|
|
|
* @param {string|string[]} [keys] - Syncers to refresh |
|
|
|
|
96
|
|
|
*/ |
97
|
|
|
function refreshSyncers(keys) { |
98
|
|
|
if (typeof keys === 'string') { |
99
|
|
|
keys = [keys] |
100
|
|
|
} |
101
|
|
|
if (!keys) { |
102
|
|
|
keys = Object.keys(this._syncers) |
103
|
|
|
} |
104
|
|
|
return Promise.all(keys.map(key => { |
105
|
|
|
return this._syncers[key].refresh() |
106
|
|
|
})) |
107
|
|
|
} |
108
|
|
|
|