1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- const EventEmitter = require('events');
- module.exports = function (dependencies) {
- const Client = dependencies.Client;
- function Provider(options) {
- if (false === this instanceof Provider) {
- return new Provider(options);
- }
- this.client = new Client(options);
- EventEmitter.call(this);
- }
- Provider.prototype = Object.create(EventEmitter.prototype);
- Provider.prototype.send = function send(notification, recipients) {
- const builtNotification = {
- headers: notification.headers(),
- body: notification.compile(),
- };
- if (!Array.isArray(recipients)) {
- recipients = [recipients];
- }
- return Promise.all(recipients.map(token => this.client.write(builtNotification, token))).then(
- responses => {
- const sent = [];
- const failed = [];
- responses.forEach(response => {
- if (response.status || response.error) {
- failed.push(response);
- } else {
- sent.push(response);
- }
- });
- return { sent, failed };
- }
- );
- };
- Provider.prototype.shutdown = function shutdown(callback) {
- this.client.shutdown(callback);
- };
- return Provider;
- };
|