-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
API variant this.notify/emit with event = type
- Loading branch information
Showing
3 changed files
with
39 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |