Feat: expose reporting complete notifications (#19283)

This PR is a pretty small change to the Reporting job complete notification service. It converts the actual code to a simple object, instead of a class that needs to be instantiated. This makes the notification service a singleton, and also exports it so it can be used in non-Angular applications.

- Converts `reportingJobCompletionNotifications` factory to a singleton
- Exports the underlying jobCompletionNotifications
  - Allow use in non-angular plugins

Example use:

```js
import { jobCompletionNotifications } from 'plugins/reporting/services/job_completion_notifications';

createReportingJob() // pseudo code function that returns a reporting job id
.then(jobId => jobCompletionNotifications.add(jobId));
```
This commit is contained in:
Joe Fleming 2018-05-22 12:47:49 -07:00 committed by GitHub
parent cc707eabe2
commit c6bf945f2f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -7,18 +7,19 @@
import { uiModules } from 'ui/modules';
import { JOB_COMPLETION_NOTIFICATIONS_SESSION_KEY } from '../../common/constants';
class JobCompletionNotifications {
export const jobCompletionNotifications = {
add(jobId) {
const jobs = this.getAll();
jobs.push(jobId);
this._set(jobs);
}
},
getAll() {
const sessionValue = sessionStorage.getItem(JOB_COMPLETION_NOTIFICATIONS_SESSION_KEY);
const sessionValue = sessionStorage.getItem(
JOB_COMPLETION_NOTIFICATIONS_SESSION_KEY
);
return sessionValue ? JSON.parse(sessionValue) : [];
}
},
remove(jobId) {
const jobs = this.getAll();
@ -29,14 +30,16 @@ class JobCompletionNotifications {
jobs.splice(index, 1);
this._set(jobs);
}
},
_set(jobs) {
sessionStorage.setItem(JOB_COMPLETION_NOTIFICATIONS_SESSION_KEY, JSON.stringify(jobs));
}
}
sessionStorage.setItem(
JOB_COMPLETION_NOTIFICATIONS_SESSION_KEY,
JSON.stringify(jobs)
);
},
};
uiModules.get('xpack/reporting')
.factory('reportingJobCompletionNotifications', function () {
return new JobCompletionNotifications();
});
uiModules
.get('xpack/reporting')
.factory('reportingJobCompletionNotifications', () => jobCompletionNotifications);