Skip to content

Commit

Permalink
API variant this.notify/emit with event = type
Browse files Browse the repository at this point in the history
  • Loading branch information
danjoa committed Oct 27, 2023
1 parent 0e557e6 commit 601100a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 deletions.
3 changes: 2 additions & 1 deletion srv/notifyToConsole.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ const LOG = cds.log('notifications');
module.exports = class NotifyToConsole extends NotificationService {
async init() {

this.on("notification", req => {
this.on("*", req => {
LOG._debug && LOG.debug('Handling notification event:', req.event)
const notification = req.data; if (!notification) return
console.log (
'\n---------------------------------------------------------------\n' +
Expand Down
2 changes: 1 addition & 1 deletion srv/notifyToRest.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const NOTIFICATIONS_API_ENDPOINT = "v2/Notification.svc";

module.exports = exports = class NotifyToRest extends NotificationService {
async init() {
this.on("notification", req => this.postNotification(req.data))
this.on("*", req => this.postNotification(req.data))
return super.init()
}

Expand Down
44 changes: 36 additions & 8 deletions srv/service.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,40 @@
const { buildNotification } = require("./../lib/utils");
const { buildNotification } = require("./../lib/utils")
const cds = require('@sap/cds')

const cds = require('@sap/cds');
const Base = cds.outboxed ? cds.Service : require('@sap/cds/libx/_runtime/messaging/Outbox');
class NotificationService extends cds.Service {

module.exports = class NotificationService extends Base {
notify (message) {
// Subclasses simply register handlers for 'notification' events
// App code could plugin own before / after handlers
return this.emit('notification', buildNotification(message))
/**
* Emits a notification. Method notify can be used alternatively.
* @param {string} [event] - The notification type.
* @param {object} message - The message object
*/
emit (event, message) {
// Outbox calls us with a req object, e.g. { event, data, headers }
if (event.event) return super.emit (event)
// First argument is optional for convenience
if (!message) [ message, event ] = [ event ]
// CAP events translate to notification types and vice versa
if (event) message.type = event
else event = message.type || message.NotificationTypeKey || 'Default'
// Prepare and emit the notification
message = buildNotification(message)
return super.emit (event, message)
}

/**
* That's just a semantic alias for emit.
*/
notify (type, message) {
return this.emit (type,message)
}

}
module.exports = NotificationService

// Without Generic Outbox only alert.notify() can be used, not alert.emit()
// Remove that when @sap/cds with Generic Outbox is released
if (!cds.outboxed) {
class OutboxedNotificationService extends require('@sap/cds/libx/_runtime/messaging/Outbox') {}
OutboxedNotificationService.prototype.notify = NotificationService.prototype.emit
module.exports = OutboxedNotificationService
}

0 comments on commit 601100a

Please sign in to comment.