refactor: move extension interface to its own service

This commit is contained in:
Johannes Millan 2018-03-22 22:06:16 +01:00
parent 193198a30c
commit c6038ce319
4 changed files with 75 additions and 17 deletions

View file

@ -131,6 +131,7 @@ TODO configure more restrictive Content-Security-Policy
<script src="scripts/main/global-filters/number-to-month-filter.js"></script>
<script src="scripts/main/global-services/app-storage-s.js"></script>
<script src="scripts/main/global-services/check-shortcut-key-combo-s.js"></script>
<script src="scripts/main/global-services/extension-interface-s.js"></script>
<script src="scripts/main/global-services/git-log-s.js"></script>
<script src="scripts/main/global-services/git-s.js"></script>
<script src="scripts/main/global-services/google-api-s.js"></script>

View file

@ -0,0 +1,51 @@
/**
* @ngdoc service
* @name superProductivity.ExtensionInterface
* @description
* # ExtensionInterface
* Service in the superProductivity.
*/
(() => {
'use strict';
const interfaceEl = window;
class ExtensionInterface {
/* @ngInject */
constructor() {
this.isInterfaceReady = false;
interfaceEl.addEventListener('SP_EXTENSION_READY', () => {
this.isInterfaceReady = true;
});
}
addEventListener(ev, cb) {
interfaceEl.addEventListener(ev, (ev) => {
cb(ev, ev.detail);
})
}
dispatchEvent(evName, data) {
const ev = new CustomEvent(evName, {
detail: data,
});
if (this.isInterfaceReady) {
interfaceEl.dispatchEvent(ev);
} else {
setTimeout(() => {
interfaceEl.dispatchEvent(ev);
}, 2000);
}
}
}
angular
.module('superProductivity')
.service('ExtensionInterface', ExtensionInterface);
// hacky fix for ff
ExtensionInterface.$$ngIsClass = true;
})();

View file

@ -0,0 +1,17 @@
'use strict';
describe('Service: ExtensionInterface', () => {
// load the service's module
beforeEach(module('superProductivity'));
// instantiate service
let ExtensionInterface;
beforeEach(inject((_ExtensionInterface_) => {
ExtensionInterface = _ExtensionInterface_;
}));
it('should be defined', () => {
expect(true).toBe(true);
});
});

View file

@ -31,7 +31,7 @@
/* @ngInject */
class Jira {
constructor(IS_ELECTRON, IS_EXTENSION, SimpleToast, Uid, $q, $rootScope, Dialogs, Notifier, $injector, $timeout, REQUEST_TIMEOUT, $log, $window) {
constructor(IS_ELECTRON, IS_EXTENSION, SimpleToast, Uid, $q, $rootScope, Dialogs, Notifier, $injector, $timeout, REQUEST_TIMEOUT, $log, $window, ExtensionInterface) {
this.requestsLog = {};
this.IS_ELECTRON = IS_ELECTRON;
@ -47,6 +47,7 @@
this.$log = $log;
this.SimpleToast = SimpleToast;
this.$window = $window;
this.ExtensionInterface = ExtensionInterface;
this.isExtensionReady = false;
const that = this;
@ -80,12 +81,10 @@
handleResponse(res);
});
} else if (IS_EXTENSION) {
window.addEventListener('SP_JIRA_RESPONSE', (ev) => {
handleResponse(ev.detail);
});
window.addEventListener('SP_EXTENSION_READY', () => {
this.isExtensionReady = true;
this.ExtensionInterface.addEventListener('SP_JIRA_RESPONSE', (ev, data) => {
handleResponse(data);
});
}
}
@ -113,17 +112,7 @@
if (this.IS_ELECTRON) {
window.ipcRenderer.send(IPC_JIRA_MAKE_REQUEST_EVENT, request);
} else if (this.IS_EXTENSION) {
const ev = new CustomEvent('SP_JIRA_REQUEST', {
detail: request,
});
if (this.isExtensionReady) {
window.dispatchEvent(ev);
} else {
setTimeout(() => {
window.dispatchEvent(ev);
}, 2000);
}
this.ExtensionInterface.dispatchEvent('SP_JIRA_REQUEST', request);
}
return defer.promise;