feature/IO-3701-Harness-Replacement - Implement
This commit is contained in:
40
server/feature-flags/socket-events.js
Normal file
40
server/feature-flags/socket-events.js
Normal file
@@ -0,0 +1,40 @@
|
||||
const FEATURE_FLAGS_CHANGED_EVENT = "feature-flags-changed";
|
||||
|
||||
/**
|
||||
* Creates the Socket.IO payload used to tell browsers that feature flags changed.
|
||||
*/
|
||||
const createFeatureFlagsChangedPayload = ({ bodyshopId = null, source = "unknown", table = null, name = null } = {}) => ({
|
||||
bodyshopId,
|
||||
changedAt: new Date().toISOString(),
|
||||
name,
|
||||
scope: bodyshopId ? "bodyshop" : "global",
|
||||
source,
|
||||
table
|
||||
});
|
||||
|
||||
/**
|
||||
* Emits a feature-flag change event globally or to one bodyshop room.
|
||||
*/
|
||||
const emitFeatureFlagsChanged = ({ req, bodyshopId = null, source = "unknown", table = null, name = null } = {}) => {
|
||||
const io = req?.ioRedis;
|
||||
if (!io) return null;
|
||||
|
||||
const payload = createFeatureFlagsChangedPayload({ bodyshopId, source, table, name });
|
||||
|
||||
if (bodyshopId) {
|
||||
const room = req?.ioHelpers?.getBodyshopRoom
|
||||
? req.ioHelpers.getBodyshopRoom(bodyshopId)
|
||||
: `bodyshop-broadcast-room:${bodyshopId}`;
|
||||
io.to(room).emit(FEATURE_FLAGS_CHANGED_EVENT, payload);
|
||||
return payload;
|
||||
}
|
||||
|
||||
io.emit(FEATURE_FLAGS_CHANGED_EVENT, payload);
|
||||
return payload;
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
FEATURE_FLAGS_CHANGED_EVENT,
|
||||
createFeatureFlagsChangedPayload,
|
||||
emitFeatureFlagsChanged
|
||||
};
|
||||
Reference in New Issue
Block a user