-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync.js
234 lines (184 loc) · 7.32 KB
/
sync.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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
sync = {}
sync.localId = Math.random()
sync.foreignId = 2 // must be != localId
sync.init = function () {
/*
inits handled in arrays to group their code more intuitively
*/
sync.onDocLoad = []
sync.onYLoad = []
sync.onModelLoad = []
// Doc-load
$(document).ready( function () {
sync.onDocLoad.forEach(function(callback){callback()})
})
// Y-creation
sync.onDocLoad.push(function () {
// The Y-object creation takes a fair amount of time, blocking the UI
Y({
db: {
name: 'memory'
},
connector: {
name: 'websockets-client',
room: 'Anatomy2.0-y-js-demo-v1.1.0',
},
sourceDir: location.pathname + 'bower_components',
share: {
showcase: 'Map'
}
}).then(function (y) {
sync.y = y
// for debugging
window.y = y
// catching errors because else Y-js would eat them
try { sync.onYLoad.forEach(function(callback){callback()}) }
catch(err) { console.log('Error in Y-load callbacks: ', err)}
})
})
// x3d-object loaded
sync.modelLoaded = function () {
sync.x3dRoot = $('x3d')[0]
sync.onModelLoad.forEach(function(callback){callback()})
}
// eye candy: start with fullshown object
sync.onModelLoad.push(function() {
x3dExtensions.normalizeCamera(sync.x3dRoot.runtime)
})
//////// viewport sync
/*
How to decide on whether to apply received remote state and whether to send local state:
When local change is caused by remote-induced animation, the change is not echoed
When local change is caused by local reasons, the change is propagated, but echoed remote changes are neglected
(An echo can be created by local or remote)
There are events which cause chairmanship of local or remote id
see https://github.com/x3dom/x3dom/blob/master/src/Viewarea.js
propagate?
viewpointChanged
|------- moving (onDrag, onMoveView) ✓
|------- animating
|------- mixing (showAll, onDoubleClick,… → animateTo)
| |------- local mixing ✓
| |------- custom remote mixing ✗
|------- navigating, (navigateTo == true) (✓)
(✓) = ignored for simplicity's sake
animations are calling viewarea._scene.getViewpoint().setView()
movements are changing viewarea._transMat and viewarea._rotMat
To stay informed about the chairmanship, every modifying function is hooked
*/
// limiting the amount of messages sent by Y-js
// numbers in ms
sync.sendInterval = 200
sync.receiveInterval = 200
sync.animDuration = 350
sync.chairmanId = sync.localId
// viewport: remote → local
sync.onYLoad.push(function () {
sync.remoteViewChanged = function (events) {
receivedView = sync.y.share.showcase.get('view_matrix')
if (receivedView == null) { return }
// only set new view if not created from yourself
if (receivedView.peerId == sync.localId) { return }
x3dExtensions.setView( sync.x3dRoot.runtime, receivedView, sync.animDuration )
sync.chairmanId = receivedView.peerId
}
sync.y.share.showcase.observePath(
['view_matrix']
, sync.intervalBarrier(sync.remoteViewChanged, sync.receiveInterval)
)
})
// viewport: local → remote
sync.onModelLoad.push(function () {
sync.localViewChanged = function (evt) {
if (!sync.loadFlags.y) { return }
// block if event was triggered by mixing-animation caused from remote state
if (sync.chairmanId != sync.localId) { return }
var currentView = x3dExtensions.getView(sync.x3dRoot.runtime)
currentView.peerId = sync.localId
sync.y.share.showcase.set('view_matrix', currentView)
}
$('#viewport').on(
'viewpointChanged'
, sync.intervalBarrier(sync.localViewChanged, sync.sendInterval)
)
sync.viewarea = sync.x3dRoot.runtime.canvas.doc._viewarea
var viewarea = sync.viewarea
var setViewareaHook = function (functionName, peerId) {
var oldFunc = viewarea[functionName]
viewarea[functionName] = function () {
sync.chairmanId = peerId
return oldFunc.apply(viewarea, arguments)
}
}
// hooks for observing chairmanship
setViewareaHook('animateTo', sync.localId)
setViewareaHook('onDrag', sync.localId)
setViewareaHook('onMoveView', sync.localId)
})
//////// resync all, document & model & y-object must be ready for this
sync.loadFlags = {document:false, y:false, model:false}
sync.oneLoaded = function () {
if (sync.loadFlags.document && sync.loadFlags.y && sync.loadFlags.model) {
sync.remoteViewChanged()
}
}
sync.onYLoad.push(function () {
sync.loadFlags.y = true
sync.oneLoaded()
})
sync.onDocLoad.push(function () {
sync.loadFlags.document = true;
sync.oneLoaded()
})
sync.onDocLoad.push(function () {
sync.loadFlags.model = true;
sync.oneLoaded()
})
}
//////////////// utility functions
/*
utility function, similar to Knockout.js' rate-limiter (http://knockoutjs.com/documentation/rateLimit-observable.html)
Here is an exaple sequence diagram:
e←event e e e e e e e
[ ]←interval [ ][ ][ ] [ ]
✓←passfunction() ✓ ✗ ✗-----✓ ✗ ✗ ✗-----✓ ✓
*/
sync.intervalBarrier = function (passFunction, interval) {
var state = {}
state.interval = interval || 1000
state.lastPassed = 0
state.passFunction = passFunction
return function () {
if (state.timeout != null)
return
var now = new Date() .getTime()
var timeToWait = (state.lastPassed + state.interval) - now
if (timeToWait < 0) { timeToWait = 0 }
state.timeout = setTimeout(
function (state) { return function () {
state.timeout = null
state.lastPassed = new Date() .getTime()
state.passFunction()
} } (state)
, timeToWait
)
}
}
/*
utility-function to prevent subscription from echoing
case is given when subscription changes the ViewModel's value
*/
sync.switchableSubscription = function (observable, func) {
return {
observable: observable,
subscription: observable.subscribe(func),
turnOff: function () {
// "subscription.isDisposed = true/false" is easier, but "isDisposed" is obfuscated (called "R" when I tested)
this.subscription.dispose()
},
turnOn: function () {
// reassign subscription
this.subscription = this.observable.subscribe( func )
}
}
}