1// Copyright (C) 2019 The Android Open Source Project 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15import {LoadingTracker} from '../common/engine'; 16import {globals} from './globals'; 17 18// Used to keep track of whether the engine is currently querying. 19export class LoadingManager implements LoadingTracker { 20 private static _instance: LoadingManager; 21 private numQueuedQueries = 0; 22 private numLastUpdate = 0; 23 24 static get getInstance(): LoadingManager { 25 return this._instance || (this._instance = new this()); 26 } 27 28 beginLoading() { 29 this.update(1); 30 } 31 32 endLoading() { 33 this.update(-1); 34 } 35 36 private update(change: number) { 37 this.numQueuedQueries += change; 38 if (this.numQueuedQueries === 0 || 39 Math.abs(this.numLastUpdate - this.numQueuedQueries) > 2) { 40 this.numLastUpdate = this.numQueuedQueries; 41 globals.publish('Loading', this.numQueuedQueries); 42 } 43 } 44} 45