-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.coffee
135 lines (101 loc) · 5.11 KB
/
server.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
guardObject = {}
extendPublish (name, publishFunction, options) ->
newPublishFunction = (args...) ->
publish = @
disabled = false
publish.disableMergebox = ->
disabled = true
originalAdded = publish.added
publish.added = (collectionName, id, fields) ->
return originalAdded.call @, collectionName, id, fields unless disabled
stringId = @_idFilter.idStringify id
FiberUtils.synchronize guardObject, "#{collectionName}$#{stringId}", =>
collectionView = @_session.getCollectionView collectionName
if collectionView.documents instanceof Map
originalSessionDocumentView = collectionView.documents.get(stringId)
try
# Make sure we start with a clean slate for this document ID.
collectionView.documents.delete(stringId)
originalAdded.call @, collectionName, id, fields
finally
if originalSessionDocumentView
collectionView.documents.set(stringId, originalSessionDocumentView)
else
collectionView.documents.delete(stringId)
else
originalSessionDocumentView = collectionView.documents[stringId]
try
# Make sure we start with a clean slate for this document ID.
delete collectionView.documents[stringId]
originalAdded.call @, collectionName, id, fields
finally
if originalSessionDocumentView
collectionView.documents[stringId] = originalSessionDocumentView
else
delete collectionView.documents[stringId]
originalChanged = publish.changed
publish.changed = (collectionName, id, fields) ->
return originalChanged.call @, collectionName, id, fields unless disabled
stringId = @_idFilter.idStringify id
FiberUtils.synchronize guardObject, "#{collectionName}$#{stringId}", =>
collectionView = @_session.getCollectionView collectionName
if collectionView.documents instanceof Map
originalSessionDocumentView = collectionView.documents.get(stringId)
try
# Create an empty session document for this id.
collectionView.documents.set(id, new DDPServer._SessionDocumentView())
# For fields which are being cleared we have to mock some existing
# value otherwise change will not be send to the client.
for field, value of fields when value is undefined
collectionView.documents.get(id).dataByKey.set(field, [subscriptionHandle: @_subscriptionHandle, value: null])
originalChanged.call @, collectionName, id, fields
finally
if originalSessionDocumentView
collectionView.documents.set(stringId, originalSessionDocumentView)
else
collectionView.documents.delete(stringId)
else
originalSessionDocumentView = collectionView.documents[stringId]
try
# Create an empty session document for this id.
collectionView.documents[id] = new DDPServer._SessionDocumentView()
# For fields which are being cleared we have to mock some existing
# value otherwise change will not be send to the client.
for field, value of fields when value is undefined
collectionView.documents[id].dataByKey[field] = [subscriptionHandle: @_subscriptionHandle, value: null]
originalChanged.call @, collectionName, id, fields
finally
if originalSessionDocumentView
collectionView.documents[stringId] = originalSessionDocumentView
else
delete collectionView.documents[stringId]
originalRemoved = publish.removed
publish.removed = (collectionName, id) ->
return originalRemoved.call @, collectionName, id unless disabled
stringId = @_idFilter.idStringify id
FiberUtils.synchronize guardObject, "#{collectionName}$#{stringId}", =>
collectionView = @_session.getCollectionView collectionName
if collectionView.documents instanceof Map
originalSessionDocumentView = collectionView.documents.get(stringId)
try
# Create an empty session document for this id.
collectionView.documents.set(id, new DDPServer._SessionDocumentView())
originalRemoved.call @, collectionName, id
finally
if originalSessionDocumentView
collectionView.documents.set(stringId, originalSessionDocumentView)
else
collectionView.documents.delete(stringId)
else
originalSessionDocumentView = collectionView.documents[stringId]
try
# Create an empty session document for this id.
collectionView.documents[id] = new DDPServer._SessionDocumentView()
originalRemoved.call @, collectionName, id
finally
if originalSessionDocumentView
collectionView.documents[stringId] = originalSessionDocumentView
else
delete collectionView.documents[stringId]
publishFunction.apply publish, args
[name, newPublishFunction, options]