-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathsync.coffee
1076 lines (964 loc) · 39.7 KB
/
sync.coffee
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
class bucket
constructor: (@s, @name, b_opts) ->
@jd = @s.jd
@options = @jd.deepCopy @s.options
for own name, val of b_opts
@options[name] = val
@chan = @options['n']
@space = "#{@options['app_id']}/#{@name}"
@username = @options['username']
@namespace = "#{@username}:#{@space}"
@clientid = null
try
@clientid = localStorage.getItem "#{@namespace}/clientid"
catch error
@clientid = null
if not @clientid? or @clientid.indexOf("sjs") != 0
@clientid = "sjs-#{@s.bversion}-#{@uuid(5)}"
try
localStorage.setItem "#{@namespace}/clientid"
catch error
console.log "#{@name}: couldnt set clientid"
@cb_events = ['notify', 'notify_init', 'notify_version', 'local', 'get', 'ready', 'notify_pending', 'error']
@cbs = {}
@cb_e = @cb_l = @cb_ni = @cb_np = null
@cb_n = @cb_nv = @cb_r = ->
@initialized = false
@authorized = false
@data =
last_cv: 0
ccid: @uuid()
store: {}
send_queue: []
send_queue_timer: null
@started = false
if not ('nostore' of @options)
@_load_meta()
@loaded = @_load_data()
console.log "#{@name}: localstorage loaded #{@loaded} entities"
else
console.log "#{@name}: not loading from localstorage"
@loaded = 0
@_last_pending = null
@_send_backoff = 15000
@_backoff_max = 120000
@_backoff_min = 15000
console.log "#{@namespace}: bucket created: opts: #{JSON.stringify(@options)}"
S4: ->
(((1+Math.random())*0x10000)|0).toString(16).substring(1)
uuid: (n) ->
n = n || 8
s = @S4()
while n -= 1
s += @S4()
return s
now: ->
new Date().getTime()
on: (event, callback) =>
if event in @cb_events
@cbs[event] = callback
switch event
when 'get', 'local'
@cb_l = callback
when 'notify'
@cb_n = callback
when 'notify_version'
@cb_nv = callback
when 'notify_pending'
@cb_np = callback
when 'notify_init'
@cb_ni = callback
when 'ready'
@cb_r = callback
when 'error'
@cb_e = callback
else
throw new Error("unsupported callback event")
show_data: =>
if not @s.supports_html5_storage()
return
total = 0
for own key, datastr of localStorage
console.log "[#{key}]: #{datastr}"
total = total + 1
console.log "#{total} total"
_save_meta: ->
if not @s.supports_html5_storage()
return false
console.log "#{@name}: save_meta ccid:#{@data.ccid} cv:#{@data.last_cv}"
try
localStorage.setItem "#{@namespace}/ccid", @data.ccid
localStorage.setItem "#{@namespace}/last_cv", @data.last_cv
catch error
return false
return true
_load_meta: =>
if not @s.supports_html5_storage()
return
@data.ccid = localStorage.getItem "#{@namespace}/ccid"
@data.ccid ?= 1
@data.last_cv = localStorage.getItem "#{@namespace}/last_cv"
@data.last_cv ?= 0
console.log "#{@name}: load_meta ccid:#{@data.ccid} cv:#{@data.last_cv}"
_verify: (data) =>
if !('object' of data) or !('version' of data)
return false
if @jd.entries(data['object']) == 0
if 'last' of data and @jd.entries(data['last']) > 0
return true
else
return false
else
if !(data['version']?)
return false
return true
_load_data: =>
if not @s.supports_html5_storage()
return
prefix = "#{@namespace}/e/"
p_len = prefix.length
loaded = 0
if localStorage.length == 0
return 0
for i in [0..localStorage.length-1]
key = localStorage.key(i)
if key? and key.substr(0, p_len) is prefix
id = key.substr(p_len, key.length-p_len)
try
data = JSON.parse localStorage.getItem key
catch error
data = null
if not data?
continue
if @_verify data
data['check'] = null
@data.store[id] = data
loaded = loaded + 1
else
console.log "ignoring CORRUPT data: #{JSON.stringify(data)}"
@_remove_entity id
return loaded
_save_entity: (id) =>
if not @s.supports_html5_storage()
return false
key = "#{@namespace}/e/#{id}"
store_data = @data.store[id]
datastr = JSON.stringify(store_data)
try
localStorage.setItem key, datastr
catch error
return false
ret_data = JSON.parse localStorage.getItem key
if @jd.equals store_data, ret_data
# console.log "saved #{key} len: #{datastr.length}"
return true
else
# console.log "ERROR STORING ENTITY: store: #{JSON.stringify(store_data)}, retrieve: #{JSON.stringify(ret_data)}"
return false
_remove_entity: (id) =>
if not @s.supports_html5_storage()
return false
key = "#{@namespace}/e/#{id}"
try
localStorage.removeItem key
catch error
return false
return true
start: =>
console.log "#{@space}: started initialized: #{@initialized} authorized: #{@authorized}"
@namespace = "#{@username}:#{@space}"
@started = true
@first = false
if not @authorized
if @s.connected
@first = true
if 'limit' of @options
index_query = "i:1:::#{@options['limit']}"
else
index_query = "i:1:::40"
@irequest_time = @now()
@index_request = true
@notify_index = {}
opts =
app_id: @options.app_id
token: @options.token
name: @name
clientid: @clientid
build: @s.bversion
if not @initialized
opts.cmd = index_query
@send("init:#{JSON.stringify(opts)}")
console.log "#{@name}: sent init #{JSON.stringify(opts)} waiting for auth"
else
console.log "#{@name}: waiting for connect"
return
if not @initialized
if not @first
@notify_index = {}
@_refresh_store()
else
console.log "#{@name}: retrieve changes from start"
@retrieve_changes()
on_data: (data) =>
if data.substr(0, 5) == "auth:"
user = data.substr(5)
if user is "expired"
console.log "auth expired"
@started = false
if @cb_e?
@cb_e "auth"
return
else
@username = user
@authorized = true
if @initialized
@start()
else if data.substr(0, 4) == "cv:?"
console.log "#{@name}: cv out of sync, refreshing index"
setTimeout => @_refresh_store()
else if data.substr(0, 2) == "c:"
changes = JSON.parse(data.substr(2))
if @data.last_cv is "0" and changes.length is 0 and not @cv_check
@cv_check = true
@_refresh_store()
@on_changes changes
else if data.substr(0, 2) == "i:"
console.log "#{@name}: index msg received: #{@now() - @irequest_time}"
@on_index_page JSON.parse(data.substr(2))
else if data.substr(0, 2) == "e:"
key_end = data.indexOf("\n")
evkey = data.substr(2, key_end-2)
version = evkey.substr(evkey.lastIndexOf('.')+1)
key = evkey.substr(0, evkey.lastIndexOf('.'))
entitydata = data.substr(key_end+1)
if entitydata is "?"
@on_entity_version null, key, version
else
entity = JSON.parse(entitydata)
@on_entity_version entity['data'], key, version
else
console.log "unknown message: #{data}"
send: (message) =>
console.log "sending: #{@chan}:#{message}"
@s.send("#{@chan}:#{message}")
_refresh_store: =>
# load the index
console.log "#{@name}: _refresh_store(): loading index"
if 'limit' of @options
index_query = "i:1:::#{@options['limit']}"
else
index_query = "i:1:::40"
@send(index_query)
@irequest_time = @now()
@index_request = true
return
on_index_page: (response) =>
now = @now()
elapsed = now - @irequest_time
console.log "#{@name}: index response time: #{elapsed}"
console.log "#{@name}: on_index_page(): index page received, current= #{response['current']}"
console.log response
loaded = 0
for item in response['index']
@notify_index[item['id']] = false
loaded++
setTimeout do(item) =>
=> @on_entity_version(item['d'], item['id'], item['v'])
if not ('mark' of response) or 'limit' of @options
@index_request = false
if 'current' of response
@data.last_cv = response['current']
@_save_meta()
else
@data.last_cv = 0
if loaded is 0
@_index_loaded()
else
@index_request = true
console.log "#{@name}: index last process time: #{@now() - now}, page_delay: #{@options['page_delay']}"
mark = response['mark']
page_req = (mark) =>
@send("i:1:#{mark}::100")
@irequest_time = @now()
setTimeout( (do(mark) =>
=> page_req(mark)), @options['page_delay'])
on_index_error: =>
console.log "#{@name}: index doesnt exist or other error"
load_versions: (id, versions) =>
if not (id of @data.store)
return false
min = Math.max(@data.store[id]['version'] - (versions+1), 1)
for v in [min..@data.store[id]['version']-1]
console.log "#{@name}: loading version #{id}.#{v}"
@send("e:#{id}.#{v}")
get_version: (id, version) =>
evkey = "#{id}.#{version}"
@send("e:#{evkey}")
on_entity_version: (data, id, version) =>
console.log "#{@name}: on_entity_version(#{data}, #{id}, #{version})"
if data?
data_copy = @jd.deepCopy(data)
else
data_copy = null
if @initialized is false and @cb_ni?
notify_cb = @cb_ni
else
notify_cb = @cb_n
if id of @data.store and 'last' of @data.store[id] and @jd.entries(@data.store[id]['last'])
data_copy = @jd.deepCopy(@data.store[id]['last'])
notify_cb id, data_copy, null
@notify_index[id] = true
@_check_update(id)
else if id of @data.store and version < @data.store[id]['version']
@cb_nv id, data_copy, version
else
if not (id of @data.store)
@data.store[id] = {}
@data.store[id]['id'] = id
@data.store[id]['object'] = data
@data.store[id]['version'] = parseInt(version)
notify_cb id, data_copy, version
@notify_index[id] = true
to_load = 0
for own nid of @notify_index
if @notify_index[nid] is false
to_load++
if to_load is 0 and @index_request is false
@_index_loaded()
_index_loaded: =>
console.log "#{@name}: index loaded, initialized: #{@initialized}"
if @initialized is false
@cb_r()
@initialized = true
console.log "#{@name}: retrieve changes from index loaded"
@retrieve_changes()
# id: id of object
# new_object: latest server version of object (includes latest diff)
# orig_object: the previous server version of object that the client had
# diff: the newly received incoming diff (orig_object + diff = new_object)
_notify_client: (key, new_object, orig_object, diff, version) =>
console.log "#{@name}: _notify_client(#{key}, #{new_object}, #{orig_object}, #{JSON.stringify(diff)})"
if not @cb_l?
console.log "#{@name}: no get callback, notifying without transform"
@cb_n key, new_object, version
return
c_object = @cb_l key
t_object = null
t_diff = null
cursor = null
offsets = []
if @jd.typeOf(c_object) is 'array'
element = c_object[2]
fieldname = c_object[1]
c_object = c_object[0]
cursor = @s._captureCursor element
if cursor
offsets[0] = cursor['startOffset']
if 'endOffset' of cursor
offsets[1] = cursor['endOffset']
if c_object? and orig_object?
# console.log "going to do object diff new diff: #{JSON.stringify(diff)}"
o_diff = @jd.object_diff orig_object, c_object
console.log "client/server version diff: #{JSON.stringify(o_diff)}"
if @jd.entries(o_diff) is 0
console.log "local diff 0 entries"
t_diff = diff
t_object = orig_object
else
console.log "o_diff"
console.log o_diff
console.log "orig_object"
console.log orig_object
console.log "c_object"
console.log c_object
console.log "client modified doing transform"
# client has local modifications, so we need to transform to apply new
# changes to client's data
# this transforms >o_diff< which is the local client modification with
# respect to >diff< which is the new incoming change received from server
# orig_object + diff = new_object
# orig_object + o_diff (clients local modifications) = c_object (current
# client data)
# new client data (that includes both diffs) = orig_object + diff + T(o_diff)
# new client data = (orig_object + diff) + T(o_diff)
# new client data = new_object + T(o_diff)
# new client data = new_object + t_diff
t_diff = @jd.transform_object_diff o_diff, diff, orig_object
t_object = new_object
if cursor
new_data = @jd.apply_object_diff_with_offsets t_object, t_diff, fieldname, offsets
if element? and 'value' of element
element['value'] = new_data[fieldname]
cursor['startOffset'] = offsets[0]
if offsets.length > 1
cursor['endOffset'] = offsets[1]
if cursor['startOffset'] >= cursor['endOffset']
cursor['collapsed'] = true
@s._restoreCursor element, cursor
else
console.log "in regular apply_object_diff"
console.log "t_object"
console.log t_object
console.log "t_diff"
console.log t_diff
new_data = @jd.apply_object_diff t_object, t_diff
# console.log "transformed diff: #{JSON.stringify(t_diff)}"
# console.log "#{@name}: notifying client of new data for #{key}: #{JSON.stringify(new_data)}"
@cb_n key, new_data, version
else if new_object
@cb_n key, new_object, version
else
@cb_n key, null, null
_check_update: (id) =>
console.log "#{@name}: _check_update(#{id})"
if not (id of @data.store)
return false
s_data = @data.store[id]
if s_data['change']
found = false
for change in @data.send_queue
if change['id'] is s_data['change']['id'] and change['ccid'] is s_data['change']['ccid']
found = true
if !found
@_queue_change s_data['change']
return true
return false
if s_data['check']?
return false
if 'last' of s_data and @jd.equals s_data['object'], s_data['last']
delete s_data['last']
@_remove_entity id
return false
change = @_make_change id
if change?
s_data['change'] = change
@_queue_change change
else
@_remove_entity id
return true
update: (id, object) =>
if arguments.length is 1
if @cb_l?
object = @cb_l id
if @jd.typeOf(object) is 'array'
object = object[0]
else
throw new Error("missing 'local' callback")
console.log "#{@name}: update(#{id})"
if not id? and not object?
return false
if id?
if id.length is 0 or id.indexOf('/') isnt -1
return false
else
id = @uuid()
if not (id of @data.store)
@data.store[id] =
'id' : id
'object' : {}
'version' : null
'change' : null
'check' : null
s_data = @data.store[id]
s_data['last'] = @jd.deepCopy(object)
s_data['modified'] = @s._time()
@_save_entity id
for change in @data.send_queue
if String(id) is change['id']
console.log "#{@name}: update(#{id}) found pending change, aborting"
return null
if s_data['check']?
clearTimeout(s_data['check'])
s_data['check'] = setTimeout((do(id, s_data) =>
=>
s_data['check'] = null
s_data['change'] = @_make_change id
delete s_data['last']
@_save_entity id
@_queue_change s_data['change']
), @options['update_delay'])
return id
# create change objects
_make_change: (id) =>
# console.log "#{@name}: _make_change(#{id})"
s_data = @data.store[id]
change =
'id' : String(id)
'ccid' : @uuid()
if not @initialized
if 'last' of s_data
c_object = s_data['last']
else
return null
else
if @cb_l?
c_object = @cb_l id
if @jd.typeOf(c_object) is 'array'
c_object = c_object[0]
else
if 'last' of s_data
c_object = s_data['last']
else
return null
if s_data['version']?
change['sv'] = s_data['version']
if c_object is null and s_data['version']?
change['o'] = '-'
console.log "#{@name}: deletion requested for #{id}"
else if c_object? and s_data['object']?
change['o'] = 'M'
if 'sendfull' of s_data
change['d'] = @jd.deepCopy c_object
delete s_data['sendfull']
else
change['v'] = @jd.object_diff s_data['object'], c_object
if @jd.entries(change['v']) is 0
change = null
else
change = null
# console.log "_make_change(#{id}) returning: #{JSON.stringify(change)}"
return change
_queue_change: (change) =>
if not change?
return
console.log "_queue_change(#{change['id']}:#{change['ccid']}): sending"
@data.send_queue.push change
@send("c:#{JSON.stringify(change)}")
@_check_pending()
if @data.send_queue_timer?
clearTimeout(@data.send_queue_timer)
@data.send_queue_timer = setTimeout @_send_changes, @_send_backoff
_send_changes: =>
if @data.send_queue.length is 0
console.log "#{@name}: send_queue empty, done"
@data.send_queue_timer = null
return
if not @s.connected
console.log "#{@name}: _send_changes: not connected"
else
for change in @data.send_queue
console.log "#{@name}: sending change: #{JSON.stringify(change)}"
@send("c:#{JSON.stringify(change)}")
@_send_backoff = @_send_backoff * 2
if @_send_backoff > @_backoff_max
@_send_backoff = @_backoff_max
@data.send_queue_timer = setTimeout @_send_changes, @_send_backoff
retrieve_changes: =>
console.log "#{@name}: requesting changes since cv:#{@data.last_cv}"
@send("cv:#{@data.last_cv}")
# @sio.send("cv:#{@data.last_cv}")
return
on_changes: (response) =>
check_updates = []
reload_needed = false
@_send_backoff = @_backoff_min
console.log "#{@name}: on_changes(): response="
console.log response
for change in response
id = change['id']
console.log "#{@name}: processing id=#{id}"
pending_to_delete = []
for pending in @data.send_queue
if change['clientid'] is @clientid and id is pending['id']
# console.log "#{@name}: deleting change for id #{id}"
change['local'] = true
pending_to_delete.push pending
check_updates.push id
for pd in pending_to_delete
@data.store[pd['id']]['change'] = null
@_save_entity pd['id']
@data.send_queue = (p for p in @data.send_queue when p isnt pd)
if pending_to_delete.length > 0
@_check_pending()
# console.log "#{@name}: send queue: #{JSON.stringify(@data.send_queue)}"
if 'error' of change
switch change['error']
when 412
console.log "#{@name}: on_changes(): empty change, dont check"
idx = check_updates.indexOf(change['id'])
if idx > -1
check_updates.splice(idx, 1)
when 409
console.log "#{@name}: on_changes(): duplicate change, ignoring"
when 405
console.log "#{@name}: on_changes(): bad version"
if change['id'] of @data.store
@data.store[change['id']]['version'] = null
reload_needed = true
when 440
console.log "#{@name}: on_change(): bad diff, sending full object"
@data.store[id]['sendfull'] = true
else
console.log "#{@name}: error for last change, reloading"
if change['id'] of @data.store
@data.store[change['id']]['version'] = null
reload_needed = true
else
op = change['o']
if op is '-'
delete @data.store[id]
@_remove_entity id
if not ('local' of change)
# setTimeout do(change) =>
# => @_notify_client change['id'], null, null, null
@_notify_client change['id'], null, null, null, null
else if op is 'M'
s_data = @data.store[id]
if ('sv' of change and s_data? and s_data['version']? and s_data['version'] == change['sv']) or !('sv' of change) or (change['ev'] == 1)
if not s_data?
@data.store[id] =
'id' : id
'object' : {}
'version' : null
'change' : null
'check' : null
s_data = @data.store[id]
# console.log "#{@name}: processing modify for #{JSON.stringify(s_data)}"
orig_object = @jd.deepCopy s_data['object']
s_data['object'] = @jd.apply_object_diff s_data['object'], change['v']
s_data['version'] = change['ev']
new_object = @jd.deepCopy s_data['object']
if not ('local' of change)
# setTimeout do(change, new_object, orig_object) =>
# => @_notify_client change['id'], new_object, orig_object, change['v']
@_notify_client change['id'], new_object, orig_object, change['v'], change['ev']
else if s_data? and s_data['version']? and change['ev'] <= s_data['version']
console.log "#{@name}: old or duplicate change received, ignoring, change.ev=#{change['ev']}, s_data.version:#{s_data['version']}"
else
if s_data?
console.log "#{@name}: version mismatch couldnt apply change, change.ev:#{change['ev']}, s_data.version:#{s_data['version']}"
else
console.log "#{@name}: version mismatch couldnt apply change, change.ev:#{change['ev']}, s_data null"
if s_data?
@data.store[id]['version'] = null
reload_needed = true
else
console.log "#{@name}: no operation found for change"
if not reload_needed
@data.last_cv = change['cv']
@_save_meta()
console.log "#{@name}: checkpoint cv=#{@data.last_cv} ccid=#{@data.ccid}"
if reload_needed
console.log "#{@name}: reload needed, refreshing store"
setTimeout => @_refresh_store()
else
for id in check_updates
do (id) => setTimeout (=> @_check_update id), @options['update_delay']
return
pending: =>
x = (change['id'] for change in @data.send_queue)
console.log "#{@name}: pending: #{JSON.stringify(x)}"
(change['id'] for change in @data.send_queue)
_check_pending: =>
if @cb_np?
curr_pending = @pending()
diff = true
if @_last_pending
diff = false
if @_last_pending.length is curr_pending.length
for x in @_last_pending
if curr_pending.indexOf(x) is -1
diff = true
else
diff = true
if diff
@_last_pending = curr_pending
@cb_np curr_pending
class simperium
lowerstrip: (s) ->
s = s.toLowerCase()
if String::trim? then s.trim() else s.replace /^\s+|\s+$/g, ""
_time: ->
d = new Date()
Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate(), d.getUTCHours(), d.getUTCMinutes(), d.getUTCSeconds(), d.getUTCMilliseconds())/1000
supports_html5_storage: ->
try
return 'localStorage' of window && window['localStorage'] != null
catch error
return false
constructor: (@app_id, @options) ->
@bversion = 2014030401
@jd = new jsondiff()
@dmp = @jd.dmp
@auth_token = null
@options = @options || {}
@options['app_id'] = @app_id
@sock_opts = { 'debug' : false }
if 'sockjs' of @options
for own name, val of @options['sockjs']
@sock_opts[name] = val
if not ('host' of @options) then @options['host'] = 'api.simperium.com'
if not ('port' of @options) then @options['port'] = 80
if 'token' of @options then @auth_token = @options['token']
if @options['host'].indexOf("simperium.com") != -1
scheme = "https"
else
scheme = "http"
@buckets = {}
@channels = 0
if not ('update_delay' of @options)
@options['update_delay'] = 0
if not ('page_delay' of @options)
@options['page_delay'] = 0
@options['prefix'] = "sock/1/#{@app_id}"
@options['port'] = parseInt(@options['port'])
if @options['port'] != 80 and @options['port'] != 443
@sock_url = "#{scheme}://#{@options['host']}:#{@options['port']}/#{@options['prefix']}"
else
@sock_url = "#{scheme}://#{@options['host']}/#{@options['prefix']}"
@stopped = false
@_sock_backoff = 3000
@_sock_hb = 1
@_sock_connect()
bucket: (name, b_opts) =>
name = @lowerstrip(name)
b_opts = b_opts || {}
b_opts['n'] = @channels++
b = new bucket(@, name, b_opts)
@buckets[b_opts['n']] = b
return b
on: (bucket, event, callback) =>
@buckets[bucket].on(event, callback)
start: =>
@stopped = false
for own name, bucket of @buckets
bucket.start()
stop: =>
@stopped = true
if @sock?
@sock.close()
send: (data) =>
@sock.send(data)
synced: =>
for own name, bucket of @buckets
if bucket.pending().length > 0
return false
return true
_sock_connect: =>
console.log "simperium: connecting to #{@sock_url}"
@connected = false
@authorized = false
@sock = new SockJS(@sock_url, undefined, @sock_opts)
@sock.onopen = @_sock_opened
@sock.onmessage = @_sock_message
@sock.onclose = @_sock_closed
_sock_opened: =>
@_sock_backoff = 3000
@connected = true
@_sock_hb_timer = setTimeout @_sock_hb_check, 20000
for own name, bucket of @buckets
if bucket.started
bucket.start()
_sock_closed: =>
@connected = false
for own name, bucket of @buckets
bucket.authorized = false
console.log "simperium: sock js closed"
if @_sock_backoff < 4000
@_sock_backoff = @_sock_backoff + 1
else
@_sock_backoff = 15000
if @_sock_hb_timer
clearTimeout @_sock_hb_timer
@_sock_hb_timer = null
if not @stopped
setTimeout @_sock_connect, @_sock_backoff
_sock_hb_check: =>
delay = new Date().getTime() - @_sock_msg_time
if @connected is false
return
if delay > 40000
console.log "simperium: force conn close"
@sock.close()
else if delay > 15000
console.log "simperium: send hb #{@_sock_hb}"
@sock.send("h:#{@_sock_hb}")
@_sock_hb_timer = setTimeout @_sock_hb_check, 20000
_sock_message: (e) =>
@_sock_msg_time = new Date().getTime()
data = e.data
sep = data.indexOf(":")
chan = null
if sep is 1 and data.charAt(0) is 'h'
@_sock_hb = data.substr(2)
return
try
chan = parseInt(data.substr(0, sep))
data = data.substr(sep+1)
catch error
chan = null
if chan is null
return
if not (chan of @buckets)
return
@buckets[chan].on_data(data)
_captureCursor: (element) =>
return
_captureCursor: `function(element) {
if ('activeElement' in element && !element.activeElement) {
// Safari specific code.
// Restoring a cursor in an unfocused element causes the focus to jump.
return null;
}
var padLength = this.dmp.Match_MaxBits / 2; // Normally 16.
var text = element.value;
var cursor = {};
if ('selectionStart' in element) { // W3
try {
var selectionStart = element.selectionStart;
var selectionEnd = element.selectionEnd;
} catch (e) {
// No cursor; the element may be "display:none".
return null;
}
cursor.startPrefix = text.substring(selectionStart - padLength, selectionStart);
cursor.startSuffix = text.substring(selectionStart, selectionStart + padLength);
cursor.startOffset = selectionStart;
cursor.collapsed = (selectionStart == selectionEnd);
if (!cursor.collapsed) {
cursor.endPrefix = text.substring(selectionEnd - padLength, selectionEnd);
cursor.endSuffix = text.substring(selectionEnd, selectionEnd + padLength);
cursor.endOffset = selectionEnd;
}
} else { // IE
// Walk up the tree looking for this textarea's document node.
var doc = element;
while (doc.parentNode) {
doc = doc.parentNode;
}
if (!doc.selection || !doc.selection.createRange) {
// Not IE?
return null;
}
var range = doc.selection.createRange();
if (range.parentElement() != element) {
// Cursor not in this textarea.
return null;
}
var newRange = doc.body.createTextRange();
cursor.collapsed = (range.text == '');
newRange.moveToElementText(element);
if (!cursor.collapsed) {
newRange.setEndPoint('EndToEnd', range);
cursor.endPrefix = newRange.text;
cursor.endOffset = cursor.endPrefix.length;
cursor.endPrefix = cursor.endPrefix.substring(cursor.endPrefix.length - padLength);
}
newRange.setEndPoint('EndToStart', range);
cursor.startPrefix = newRange.text;
cursor.startOffset = cursor.startPrefix.length;
cursor.startPrefix = cursor.startPrefix.substring(cursor.startPrefix.length - padLength);
newRange.moveToElementText(element);
newRange.setEndPoint('StartToStart', range);
cursor.startSuffix = newRange.text.substring(0, padLength);
if (!cursor.collapsed) {
newRange.setEndPoint('StartToEnd', range);
cursor.endSuffix = newRange.text.substring(0, padLength);
}
}
// Record scrollbar locations
if ('scrollTop' in element) {
cursor.scrollTop = element.scrollTop / element.scrollHeight;
cursor.scrollLeft = element.scrollLeft / element.scrollWidth;
}
// alert(cursor.startPrefix + '|' + cursor.startSuffix + ' ' +
// cursor.startOffset + '\n' + cursor.endPrefix + '|' +
// cursor.endSuffix + ' ' + cursor.endOffset + '\n' +
// cursor.scrollTop + ' x ' + cursor.scrollLeft);
return cursor;
}`
_restoreCursor: (element, cursor) =>
return
_restoreCursor: `function(element, cursor) {
// Set some constants which tweak the matching behaviour.
// Maximum distance to search from expected location.
this.dmp.Match_Distance = 1000;
// At what point is no match declared (0.0 = perfection, 1.0 = very loose)
this.dmp.Match_Threshold = 0.9;
var padLength = this.dmp.Match_MaxBits / 2; // Normally 16.
var newText = element.value;
// Find the start of the selection in the new text.
var pattern1 = cursor.startPrefix + cursor.startSuffix;
var pattern2, diff;
var cursorStartPoint = this.dmp.match_main(newText, pattern1,
cursor.startOffset - padLength);
if (cursorStartPoint !== null) {
pattern2 = newText.substring(cursorStartPoint,
cursorStartPoint + pattern1.length);
//alert(pattern1 + '\nvs\n' + pattern2);
// Run a diff to get a framework of equivalent indicies.
diff = this.dmp.diff_main(pattern1, pattern2, false);
cursorStartPoint += this.dmp.diff_xIndex(diff, cursor.startPrefix.length);
}
var cursorEndPoint = null;
if (!cursor.collapsed) {