src/syncers/base.js   A
last analyzed

Size

Lines of Code 141

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
nc 1
dl 0
loc 141
rs 10
c 1
b 0
f 0
ccs 37
cts 37
cp 1
noi 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A base.js ➔ ??? 0 21 2
1
import {each, warn} from '../utils'
2
3
export default class BaseFeathersSyncer {
4
5
	/**
6
	 * Create a syncer for feathers
7
	 *
8
	 * @param Vue
9
	 * @param vm
10
	 * @param path
11
	 * @param settings
12
	 */
13
	constructor(Vue, vm, path, settings) {
14 35
		this.Vue = Vue
15 35
		this.vm = vm
16 35
		this.path = path
17 35
		this.settings = settings
18
19 35
		this.filters = {}
20 35
		this.unwatchers = {}
21 35
		this.events = {
22
			loaded: settings.loaded,
23
			error: settings.errored
24
		}
25
26 35
		Vue.util.defineReactive(this, 'state', this._initialState())
27 35
		Vue.util.defineReactive(this, 'loading', true)
28
29 35
		this._id = 'idField' in settings ? settings.idField : Vue.$syncer.idField
30
31 35
		const client = Vue.$syncer.feathers
32 35
		this.service = client.service(this.settings.service)
33
	}
34
35
	/**
36
	 * Cleanup after oneself
37
	 */
38
	destroy() {
39 35
		each(this.unwatchers, unwatcher => {
40 164
			unwatcher()
41
		})
42
43 35
		this.state = this._initialState()
44 35
		this.vm = null
45 35
		this.settings = null
46 35
		this.Vue = null
47 35
		this.service = null
48
	}
49
50
	/**
51
	 * Hook into feathers and set up value observers
52
	 *
53
	 * @returns {*}
54
	 */
55
	ready() {
56 35
		this._listenForServiceEvent('created', this.onItemCreated.bind(this))
57 35
		this._listenForServiceEvent('updated', this.onItemUpdated.bind(this))
58 35
		this._listenForServiceEvent('patched', this.onItemUpdated.bind(this))
59 35
		this._listenForServiceEvent('removed', this.onItemRemoved.bind(this))
60
61 35
		return this._bindComputedValues()
62
	}
63
64
	/**
65
	 * Refresh syncer's value
66
	 */
67
	refresh() {
68 5
		return this._loadNewState()
69
	}
70
71
	/**
72
	 * Handle errors loading the state
73
	 *
74
	 * @param error
75
	 * @private
76
	 */
77
	_handleStateLoadingError(error) {
78 3
		this.loading = false
79 3
		this._fireEvent('error', error)
80
	}
81
82
	/**
83
	 * Register service listener and unlistener
84
	 *
85
	 * @param event
86
	 * @param callback
87
	 * @private
88
	 */
89
	_listenForServiceEvent(event, callback) {
90
		/* istanbul ignore next */
91
		if (process.env.NODE_ENV !== 'production') {
92
			const origCallback = callback
93
			callback = (...args) => {
94
				if (this.Vue === null) {
95
					warn('Removed event listener is being called. Please update feathers-socket-commons package.')
96
					return
97
				}
98
99
				origCallback(...args)
100
			}
101
		}
102
103 140
		this.service.on(event, callback)
104 140
		this.unwatchers['service-' + event] = () => {
105 140
			this.service.off(event, callback)
106
		}
107
	}
108
109
	/**
110
	 * Wrapper for loading current state
111
	 *
112
	 * @returns {Promise.<T>}
113
	 * @private
114
	 */
115
	_loadNewState() {
116 42
		this.loading = true
117 42
		return this._loadState()
118
	}
119
120
	/**
121
	 * Mark as everything's now loaded
122
	 *
123
	 * @private
124
	 */
125
	_newStateLoaded() {
126 37
		this.loading = false
127 37
		this._fireEvent('loaded')
128
	}
129
130
	/**
131
	 * Fire event on both listeners in settings and instance
132
	 *
133
	 * @private
134
	 */
135
	_fireEvent(event, ...args) {
136 40
		if (event in this.events && this.events[event]) {
137 2
			this.events[event].apply(this.vm, args)
138
		}
139 40
		this.vm.$emit(`syncer-${event}`, this.path, ...args)
140
	}
141
}
142