/ src / notification.js
notification.js
  1  const { Emitter } = require('event-kit');
  2  const _ = require('underscore-plus');
  3  
  4  // Public: A notification to the user containing a message and type.
  5  module.exports = class Notification {
  6    constructor(type, message, options = {}) {
  7      this.type = type;
  8      this.message = message;
  9      this.options = options;
 10      this.emitter = new Emitter();
 11      this.timestamp = new Date();
 12      this.dismissed = true;
 13      if (this.isDismissable()) this.dismissed = false;
 14      this.displayed = false;
 15      this.validate();
 16    }
 17  
 18    validate() {
 19      if (typeof this.message !== 'string') {
 20        throw new Error(
 21          `Notification must be created with string message: ${this.message}`
 22        );
 23      }
 24  
 25      if (!_.isObject(this.options) || Array.isArray(this.options)) {
 26        throw new Error(
 27          `Notification must be created with an options object: ${this.options}`
 28        );
 29      }
 30    }
 31  
 32    /*
 33    Section: Event Subscription
 34    */
 35  
 36    // Public: Invoke the given callback when the notification is dismissed.
 37    //
 38    // * `callback` {Function} to be called when the notification is dismissed.
 39    //
 40    // Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
 41    onDidDismiss(callback) {
 42      return this.emitter.on('did-dismiss', callback);
 43    }
 44  
 45    // Public: Invoke the given callback when the notification is displayed.
 46    //
 47    // * `callback` {Function} to be called when the notification is displayed.
 48    //
 49    // Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
 50    onDidDisplay(callback) {
 51      return this.emitter.on('did-display', callback);
 52    }
 53  
 54    getOptions() {
 55      return this.options;
 56    }
 57  
 58    /*
 59    Section: Methods
 60    */
 61  
 62    // Public: Returns the {String} type.
 63    getType() {
 64      return this.type;
 65    }
 66  
 67    // Public: Returns the {String} message.
 68    getMessage() {
 69      return this.message;
 70    }
 71  
 72    getTimestamp() {
 73      return this.timestamp;
 74    }
 75  
 76    getDetail() {
 77      return this.options.detail;
 78    }
 79  
 80    isEqual(other) {
 81      return (
 82        this.getMessage() === other.getMessage() &&
 83        this.getType() === other.getType() &&
 84        this.getDetail() === other.getDetail()
 85      );
 86    }
 87  
 88    // Extended: Dismisses the notification, removing it from the UI. Calling this
 89    // programmatically will call all callbacks added via `onDidDismiss`.
 90    dismiss() {
 91      if (!this.isDismissable() || this.isDismissed()) return;
 92      this.dismissed = true;
 93      this.emitter.emit('did-dismiss', this);
 94    }
 95  
 96    isDismissed() {
 97      return this.dismissed;
 98    }
 99  
100    isDismissable() {
101      return !!this.options.dismissable;
102    }
103  
104    wasDisplayed() {
105      return this.displayed;
106    }
107  
108    setDisplayed(displayed) {
109      this.displayed = displayed;
110      this.emitter.emit('did-display', this);
111    }
112  
113    getIcon() {
114      if (this.options.icon != null) return this.options.icon;
115      switch (this.type) {
116        case 'fatal':
117          return 'bug';
118        case 'error':
119          return 'flame';
120        case 'warning':
121          return 'alert';
122        case 'info':
123          return 'info';
124        case 'success':
125          return 'check';
126      }
127    }
128  };