Merge "Move gerrit event emitter to services"

This commit is contained in:
Milutin Kristofic
2020-06-19 12:30:03 +00:00
committed by Gerrit Code Review
13 changed files with 37 additions and 52 deletions

View File

@@ -28,7 +28,6 @@ import {PolymerElement} from '@polymer/polymer/polymer-element.js';
import {htmlTemplate} from './gr-error-manager_html.js';
import {BaseUrlBehavior} from '../../../behaviors/base-url-behavior/base-url-behavior.js';
import {authService} from '../../shared/gr-rest-api-interface/gr-auth.js';
import {gerritEventEmitter} from '../../shared/gr-event-emitter/gr-event-emitter.js';
import {appContext} from '../../../services/app-context.js';
const HIDE_ALERT_TIMEOUT_MS = 5000;
@@ -93,6 +92,7 @@ class GrErrorManager extends mixinBehaviors( [
this._authErrorHandlerDeregistrationHook;
this.reporting = appContext.reportingService;
this.eventEmitter = appContext.eventEmitter;
}
/** @override */
@@ -106,7 +106,7 @@ class GrErrorManager extends mixinBehaviors( [
this.listen(document, 'show-auth-required', '_handleAuthRequired');
this._authErrorHandlerDeregistrationHook =
gerritEventEmitter.on('auth-error',
this.eventEmitter.on('auth-error',
event => {
this._handleAuthError(event.message, event.action);
});

View File

@@ -50,7 +50,7 @@ import {GrReviewerSuggestionsProvider, SUGGESTIONS_PROVIDERS_USERS_TYPES} from '
import {util} from '../scripts/util.js';
import page from 'page/page.mjs';
import {Auth} from './shared/gr-rest-api-interface/gr-auth.js';
import {EventEmitter} from './shared/gr-event-interface/gr-event-interface.js';
import {appContext} from '../services/app-context.js';
import {GrAdminApi} from './plugins/gr-admin-api/gr-admin-api.js';
import {GrAnnotationActionsContext} from './shared/gr-js-api-interface/gr-annotation-actions-context.js';
import {GrAnnotationActionsInterface} from './shared/gr-js-api-interface/gr-annotation-actions-js-api.js';
@@ -104,7 +104,7 @@ export function initGlobalVariables() {
window.util = util;
window.page = page;
window.Auth = Auth;
window.EventEmitter = EventEmitter;
window.EventEmitter = appContext.eventEmitter;
window.GrAdminApi = GrAdminApi;
window.GrAnnotationActionsContext = GrAnnotationActionsContext;
window.GrAnnotationActionsInterface = GrAnnotationActionsInterface;

View File

@@ -38,6 +38,7 @@ import {PolymerElement} from '@polymer/polymer/polymer-element.js';
import {htmlTemplate} from './gr-app_html.js';
import {SafeTypes} from '../behaviors/safe-types-behavior/safe-types-behavior.js';
import {initGerritPluginApi} from './shared/gr-js-api-interface/gr-gerrit.js';
import {appContext} from '../services/app-context.js';
security.polymer_resin.install({
allowedIdentifierPrefixes: [''],
@@ -57,4 +58,4 @@ class GrApp extends GestureEventListeners(
customElements.define(GrApp.is, GrApp);
initGlobalVariables();
initGerritPluginApi();
initGerritPluginApi(appContext);

View File

@@ -1,21 +0,0 @@
/**
* @license
* Copyright (C) 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {EventEmitter} from '../gr-event-interface/gr-event-interface.js';
// TODO(dmfilippov): move to appContext
export const gerritEventEmitter = new EventEmitter();

View File

@@ -21,8 +21,8 @@
*/
import {pluginLoader} from './gr-plugin-loader.js';
import {gerritEventEmitter} from '../gr-event-emitter/gr-event-emitter.js';
import {getRestAPI, send} from './gr-api-utils.js';
import {appContext} from '../../../services/app-context.js';
/**
* Trigger the preinstalls for bundled plugins.
@@ -146,9 +146,11 @@ function initGerritPluginsMethods(globalGerritObj) {
return pluginLoader.isPluginLoaded(pathOrUrl);
};
const eventEmitter = appContext.eventEmitter;
// TODO(taoalpha): List all internal supported event names.
// Also convert this to inherited class once we move Gerrit to class.
globalGerritObj._eventEmitter = gerritEventEmitter;
globalGerritObj._eventEmitter = eventEmitter;
['addListener',
'dispatch',
'emit',
@@ -180,7 +182,7 @@ function initGerritPluginsMethods(globalGerritObj) {
* });
* });
*/
globalGerritObj[method] = gerritEventEmitter[method]
.bind(gerritEventEmitter);
globalGerritObj[method] = eventEmitter[method]
.bind(eventEmitter);
});
}

View File

@@ -15,7 +15,7 @@
* limitations under the License.
*/
import {BaseUrlBehavior} from '../../../behaviors/base-url-behavior/base-url-behavior.js';
import {gerritEventEmitter} from '../gr-event-emitter/gr-event-emitter.js';
import {appContext} from '../../../services/app-context.js';
const MAX_AUTH_CHECK_WAIT_TIME_MS = 1000 * 30; // 30s
const MAX_GET_TOKEN_RETRIES = 2;
@@ -34,6 +34,7 @@ export class Auth {
this._status = Auth.STATUS.UNDETERMINED;
this._authCheckPromise = null;
this._last_auth_check_time = Date.now();
this.eventEmitter = appContext.eventEmitter;
}
get baseUrl() {
@@ -83,7 +84,7 @@ export class Auth {
if (this._status === status) return;
if (this._status === Auth.STATUS.AUTHED) {
gerritEventEmitter.emit('auth-error', {
this.eventEmitter.emit('auth-error', {
message: Auth.CREDS_EXPIRED_MSG, action: 'Refresh credentials',
});
}

View File

@@ -28,7 +28,7 @@ limitations under the License.
import '../../../test/common-test-setup.js';
import {BaseUrlBehavior} from '../../../behaviors/base-url-behavior/base-url-behavior.js';
import {Auth, authService} from './gr-auth.js';
import {gerritEventEmitter} from '../gr-event-emitter/gr-event-emitter.js';
import {appContext} from '../../../services/app-context.js';
suite('gr-auth', () => {
let auth;
@@ -161,7 +161,7 @@ suite('gr-auth', () => {
clock.tick(1000 * 10000);
fakeFetch.returns(Promise.resolve({status: 403}));
const emitStub = sinon.stub();
gerritEventEmitter.emit = emitStub;
appContext.eventEmitter.emit = emitStub;
auth.authCheck().then(authed2 => {
assert.isFalse(authed2);
assert.equal(auth.status, Auth.STATUS.NOT_AUTHED);
@@ -179,7 +179,7 @@ suite('gr-auth', () => {
clock.tick(1000 * 10000);
fakeFetch.returns(Promise.reject(new Error('random error')));
const emitStub = sinon.stub();
gerritEventEmitter.emit = emitStub;
appContext.eventEmitter.emit = emitStub;
auth.authCheck().then(authed2 => {
assert.isFalse(authed2);
assert.isTrue(emitStub.called);
@@ -197,7 +197,7 @@ suite('gr-auth', () => {
clock.tick(1000 * 10000);
fakeFetch.returns(Promise.resolve({status: 204}));
const emitStub = sinon.stub();
gerritEventEmitter.emit = emitStub;
appContext.eventEmitter.emit = emitStub;
auth.authCheck().then(authed2 => {
assert.isTrue(authed2);
assert.isFalse(emitStub.called);
@@ -215,7 +215,7 @@ suite('gr-auth', () => {
clock.tick(1000 * 10000);
fakeFetch.returns(Promise.reject(new Error('random error')));
const emitStub = sinon.stub();
gerritEventEmitter.emit = emitStub;
appContext.eventEmitter.emit = emitStub;
auth.authCheck().then(authed2 => {
assert.isFalse(authed2);
assert.isFalse(emitStub.called);

View File

@@ -17,6 +17,7 @@
import {appContext} from './app-context.js';
import {FlagsService} from './flags.js';
import {GrReporting} from './gr-reporting/gr-reporting.js';
import {EventEmitter} from './gr-event-interface/gr-event-interface.js';
const initializedServices = new Map();
@@ -46,6 +47,6 @@ export function initAppContext() {
addService('flagsService', () => new FlagsService());
addService('reportingService',
() => new GrReporting(appContext.flagsService));
addService('eventEmitter', () => new EventEmitter());
Object.defineProperties(appContext, registeredServices);
}

View File

@@ -24,4 +24,5 @@
export const appContext = {
flagsService: null,
reportingService: null,
eventEmitter: null,
};

View File

@@ -30,10 +30,10 @@ limitations under the License.
</test-fixture>
<script type="module">
import '../../../test/common-test-setup.js';
import '../gr-js-api-interface/gr-js-api-interface.js';
import '../../test/common-test-setup.js';
import '../../elements/shared/gr-js-api-interface/gr-js-api-interface.js';
import {EventEmitter} from './gr-event-interface.js';
import {_testOnly_initGerritPluginApi} from '../gr-js-api-interface/gr-gerrit.js';
import {_testOnly_initGerritPluginApi} from '../../elements/shared/gr-js-api-interface/gr-gerrit.js';
const pluginApi = _testOnly_initGerritPluginApi();

View File

@@ -91,16 +91,6 @@ setup(() => {
assert.equal(cleanups.length, 0);
_testOnly_resetPluginLoader();
initAppContext();
function setMock(serviceName, setupMock) {
Object.defineProperty(appContext, serviceName, {
get() {
return setupMock;
},
});
}
setMock('reportingService', grReportingMock);
});
if (isKarmaTest() || window.stub) {
@@ -125,6 +115,16 @@ if (isKarmaTest() || window.stub) {
throw new Error('window.stub must be set after wct sets it');
}
initAppContext();
function setMock(serviceName, setupMock) {
Object.defineProperty(appContext, serviceName, {
get() {
return setupMock;
},
});
}
setMock('reportingService', grReportingMock);
teardown(() => {
// WCT incorrectly uses teardown method in the 'fixture' and 'stub'
// implementations. This leads to slowdown WCT tests after each tests.

View File

@@ -146,7 +146,6 @@ const elements = [
'settings/gr-settings-view/gr-settings-view_test.html',
'settings/gr-ssh-editor/gr-ssh-editor_test.html',
'settings/gr-watched-projects-editor/gr-watched-projects-editor_test.html',
'shared/gr-event-interface/gr-event-interface_test.html',
'shared/gr-account-entry/gr-account-entry_test.html',
'shared/gr-account-label/gr-account-label_test.html',
'shared/gr-account-list/gr-account-list_test.html',
@@ -256,6 +255,7 @@ const services = [
'flags_test.html',
'gr-reporting/gr-reporting_test.html',
'gr-reporting/gr-reporting_mock_test.html',
'gr-event-interface/gr-event-interface_test.html',
];
for (let file of services) {
file = servicesPath + file;