-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathComponent.js
More file actions
105 lines (79 loc) * 2.41 KB
/
Component.js
File metadata and controls
105 lines (79 loc) * 2.41 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import dispatchEvent from './dispatchEvent'
import serializeEvent from './serializeEvent'
import reconcile from './reconcile'
import { version } from '../package.json'
export default class Component {
constructor (client, element) {
this._isShutdown = false
this.client = client
this.element = element
this._beforeConnect()
const subscription = this.client.consumer.subscriptions.create(
{
channel: 'Motion::Channel',
version,
state: this.element.getAttribute(this.client.stateAttribute)
},
{
connected: () => {
if (this._isShutdown) {
subscription.unsubscribe()
return
}
this._subscription = subscription
this._connect()
},
rejected: () => this._connectFailed(),
disconnected: () => this._disconnect(),
received: newState => this._render(newState)
}
)
this._initialSubscription = subscription
}
processMotion (name, event = null, element = event && event.currentTarget) {
if (!this._subscription) {
this.client.log('Dropped motion', name, 'on', this)
return false
}
this.client.log('Processing motion', name, 'on', this)
const extraDataForEvent = event && this.client.getExtraDataForEvent(event)
this._subscription.perform(
'process_motion',
{
name,
event: event && serializeEvent(event, extraDataForEvent, element)
}
)
return true
}
shutdown () {
this._isShutdown = true
if (this._subscription) {
this._subscription.unsubscribe()
delete this._subscription
}
this._disconnect()
}
_beforeConnect () {
this.client.log('Connecting component', this)
dispatchEvent(this.element, 'motion:before-connect')
}
_connect () {
this.client.log('Component connected', this)
dispatchEvent(this.element, 'motion:connect')
}
_connectFailed () {
this.client.log('Failed to connect component', this)
dispatchEvent(this.element, 'motion:connect-failed')
}
_disconnect () {
this.client.log('Component disconnected', this)
dispatchEvent(this.element, 'motion:disconnect')
}
_render (newState) {
dispatchEvent(this.element, 'motion:before-render')
reconcile(this.element, newState, this.client.keyAttribute)
this.client.log('Component rendered', this)
dispatchEvent(this.element, 'motion:render')
}
}