mango-cms 0.2.27 → 0.2.30
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
const subscribe = ({ io, collection, document, request, individual }) => {
|
|
2
|
+
let method = request.method
|
|
3
|
+
if (collection.subscribe && method != 'read' && individual) {
|
|
4
|
+
let roomId = request.member?.id
|
|
5
|
+
console.log('attempting to emit to', collection.name, roomId, `${collection.name}:${method}d`)
|
|
6
|
+
const subscription = io.of(collection.name);
|
|
7
|
+
subscription.to(roomId).emit(`${collection.name}:${method}d`, document);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export { subscribe }
|
package/default/package.json
CHANGED
|
@@ -226,13 +226,49 @@ const Mango = collections.reduce((a, c) => {
|
|
|
226
226
|
|
|
227
227
|
}
|
|
228
228
|
|
|
229
|
-
let subscribe = (
|
|
230
|
-
|
|
229
|
+
let subscribe = ({target, triggers, room} = {}) => {
|
|
230
|
+
|
|
231
231
|
let socket = io(`${api}/${c.name}`, { transports: ['websocket'] })
|
|
232
|
-
let userId = window.localStorage.getItem('
|
|
232
|
+
let userId = window.localStorage.getItem('token').split(':')[1]
|
|
233
|
+
|
|
233
234
|
room = room || userId
|
|
234
235
|
socket.emit('subscribeToThread', room)
|
|
235
|
-
|
|
236
|
+
|
|
237
|
+
triggers = triggers || {}
|
|
238
|
+
let defaultTriggers = {
|
|
239
|
+
created: (data) => {
|
|
240
|
+
if (Array.isArray(target)) target.push(data)
|
|
241
|
+
else Object.assign(target, data)
|
|
242
|
+
},
|
|
243
|
+
updated: (data) => {
|
|
244
|
+
if (Array.isArray(target)) {
|
|
245
|
+
const index = target.findIndex(t => t.id == data.id)
|
|
246
|
+
if (index !== -1) {
|
|
247
|
+
Object.assign(target[index], data)
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
else if (target.id == data.id) Object.assign(target, data)
|
|
251
|
+
},
|
|
252
|
+
deleted: (data) => {
|
|
253
|
+
if (Array.isArray(target)) {
|
|
254
|
+
const index = target.findIndex(t => t.id == data.id)
|
|
255
|
+
if (index !== -1) {
|
|
256
|
+
target.splice(index, 1) // Mutates the existing array
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
else if (target.id == data.id) {
|
|
260
|
+
// Clear the object properties while maintaining reactivity
|
|
261
|
+
Object.keys(target).forEach(key => delete target[key])
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
let combinedTriggers = { ...defaultTriggers, ...triggers }
|
|
267
|
+
|
|
268
|
+
for (let trigger of Object.keys(combinedTriggers)) {
|
|
269
|
+
socket.on(`${c.name}:${trigger}`, combinedTriggers[trigger])
|
|
270
|
+
}
|
|
271
|
+
|
|
236
272
|
}
|
|
237
273
|
|
|
238
274
|
a[c.name] = runQuery
|