MOBILE-3101 core: Create singletons of all core providers

main
Dani Palou 2020-03-24 12:18:32 +01:00
parent e109f42d4e
commit 3a3d45db92
36 changed files with 222 additions and 7 deletions

View File

@ -12,7 +12,8 @@ const customConfig = {
'@providers': resolve('./src/providers'),
'@components': resolve('./src/components'),
'@directives': resolve('./src/directives'),
'@pipes': resolve('./src/pipes')
'@pipes': resolve('./src/pipes'),
'@singletons': resolve('./src/singletons'),
}
},
externals: [

View File

@ -14,7 +14,7 @@
import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { NgModule, COMPILER_OPTIONS } from '@angular/core';
import { NgModule, COMPILER_OPTIONS, Injector } from '@angular/core';
import { IonicApp, IonicModule, Platform, Content, ScrollEvent, Config, Refresher } from 'ionic-angular';
import { assert } from 'ionic-angular/util/util';
import { HttpClient, HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';
@ -155,6 +155,8 @@ import { AddonQtypeModule } from '@addon/qtype/qtype.module';
import { AddonStorageManagerModule } from '@addon/storagemanager/storagemanager.module';
import { AddonFilterModule } from '@addon/filter/filter.module';
import { setSingletonsInjector } from '@singletons/core.singletons';
// For translate loader. AoT requires an exported function for factories.
export function createTranslateLoader(http: HttpClient): TranslateHttpLoader {
return new TranslateHttpLoader(http, './assets/lang/', '.json');
@ -357,6 +359,7 @@ export class AppModule {
private eventsProvider: CoreEventsProvider,
cronDelegate: CoreCronDelegate,
siteInfoCronHandler: CoreSiteInfoCronHandler,
injector: Injector,
) {
// Register a handler for platform ready.
initDelegate.registerProcess({
@ -391,6 +394,9 @@ export class AppModule {
// Register handlers.
cronDelegate.register(siteInfoCronHandler);
// Set the injector.
setSingletonsInjector(injector);
// Set transition animation.
config.setTransition('core-page-transition', CorePageTransition);
config.setTransition('core-modal-lateral-transition', CoreModalLateralTransition);

View File

@ -0,0 +1,76 @@
// (C) Copyright 2015 Moodle Pty Ltd.
//
// 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 { Injector, Type } from '@angular/core';
/**
* Stub class used to type anonymous classes created in CoreSingletonsFactory#makeSingleton method.
*/
class CoreSingleton {}
/**
* Token that can be used to resolve instances from the injector.
*/
export type CoreInjectionToken<Service> = Type<Service> | Type<any> | string;
/**
* Singleton class created using the factory.
*/
export type CoreSingletonClass<Service> = typeof CoreSingleton & { instance: Service };
/**
* Factory used to create CoreSingleton classes that get instances from an injector.
*/
export class CoreSingletonsFactory {
/**
* Angular injector used to resolve singleton instances.
*/
private injector: Injector;
/**
* Set the injector that will be used to resolve instances in the singletons created with this factory.
*
* @param injector Injector.
*/
setInjector(injector: Injector): void {
this.injector = injector;
}
/**
* Make a singleton that will hold an instance resolved from the factory injector.
*
* @param injectionToken Injection token used to resolve the singleton instance. This is usually the service class if the
* provider was defined using a class or the string used in the `provide` key if it was defined using an object.
*/
makeSingleton<Service>(injectionToken: CoreInjectionToken<Service>): CoreSingletonClass<Service> {
// tslint:disable: no-this-assignment
const factory = this;
return class {
private static _instance: Service;
static get instance(): Service {
// Initialize instances lazily.
if (!this._instance) {
this._instance = factory.injector.get(injectionToken);
}
return this._instance;
}
};
}
}

View File

@ -56,7 +56,7 @@ import { Md5 } from 'ts-md5/dist/md5';
// Import core classes that can be useful for site plugins.
import { CoreSyncBaseProvider } from '@classes/base-sync';
import { CoreUrl } from '@classes/utils/url';
import { CoreUrl } from '@singletons/url';
import { CoreCache } from '@classes/cache';
import { CoreDelegate } from '@classes/delegate';
import { CoreContentLinksHandlerBase } from '@core/contentlinks/classes/base-handler';

View File

@ -22,7 +22,7 @@ import { CoreUrlUtilsProvider } from '@providers/utils/url';
import { CoreConfigConstants } from '../../../../configconstants';
import { CoreLoginHelperProvider } from '../../providers/helper';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { CoreUrl } from '@classes/utils/url';
import { CoreUrl } from '@singletons/url';
import { TranslateService } from '@ngx-translate/core';
/**

View File

@ -23,6 +23,7 @@ import { CoreLoggerProvider } from './logger';
import { CoreEventsProvider } from './events';
import { SQLiteDB, SQLiteDBTableSchema } from '@classes/sqlitedb';
import { CoreConfigConstants } from '../configconstants';
import { makeSingleton } from '@singletons/core.singletons';
/**
* Data stored for a redirect to another page/site.
@ -703,3 +704,5 @@ export class CoreAppProvider {
this.forceOffline = !!value;
}
}
export class CoreApp extends makeSingleton(CoreAppProvider) {}

View File

@ -15,6 +15,7 @@
import { Injectable } from '@angular/core';
import { CoreAppProvider, CoreAppSchema } from './app';
import { SQLiteDB } from '@classes/sqlitedb';
import { makeSingleton } from '@singletons/core.singletons';
/**
* Factory to provide access to dynamic and permanent config and settings.
@ -102,3 +103,5 @@ export class CoreConfigProvider {
return this.appDB.insertRecord(this.TABLE_NAME, { name: name, value: value });
}
}
export class CoreConfig extends makeSingleton(CoreConfigProvider) {}

View File

@ -20,6 +20,7 @@ import { CoreLoggerProvider } from './logger';
import { CoreUtilsProvider } from './utils/utils';
import { CoreConstants } from '@core/constants';
import { SQLiteDB } from '@classes/sqlitedb';
import { makeSingleton } from '@singletons/core.singletons';
/**
* Interface that all cron handlers must implement.
@ -554,3 +555,5 @@ export class CoreCronDelegate {
delete this.handlers[name].timeout;
}
}
export class CoreCron extends makeSingleton(CoreCronDelegate) {}

View File

@ -17,6 +17,7 @@ import { SQLite } from '@ionic-native/sqlite';
import { Platform } from 'ionic-angular';
import { SQLiteDB } from '@classes/sqlitedb';
import { SQLiteDBMock } from '@core/emulator/classes/sqlitedb';
import { makeSingleton } from '@singletons/core.singletons';
/**
* This service allows interacting with the local database to store and retrieve data.
@ -81,3 +82,5 @@ export class CoreDbProvider {
});
}
}
export class CoreDB extends makeSingleton(CoreDbProvider) {}

View File

@ -15,6 +15,7 @@
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';
import { CoreLoggerProvider } from '@providers/logger';
import { makeSingleton } from '@singletons/core.singletons';
/**
* Observer instance to stop listening to an event.
@ -173,3 +174,5 @@ export class CoreEventsProvider {
}
}
}
export class CoreEvents extends makeSingleton(CoreEventsProvider) {}

View File

@ -21,6 +21,7 @@ import { CoreSitesProvider } from './sites';
import { CoreWSProvider } from './ws';
import { CoreUtilsProvider } from './utils/utils';
import { CoreConstants } from '@core/constants';
import { makeSingleton } from '@singletons/core.singletons';
/**
* Provider to provide some helper functions regarding files and packages.
@ -333,5 +334,6 @@ export class CoreFileHelperProvider {
throw new Error('Couldn\'t determine file size: ' + file.fileurl);
}
}
export class CoreFileHelper extends makeSingleton(CoreFileHelperProvider) {}

View File

@ -14,6 +14,7 @@
import { Injectable } from '@angular/core';
import { CoreSitesProvider } from './sites';
import { makeSingleton } from '@singletons/core.singletons';
/**
* Helper to store some temporary data for file submission.
@ -146,3 +147,5 @@ export class CoreFileSessionProvider {
this.files[siteId][component][id] = newFiles;
}
}
export class CoreFileSession extends makeSingleton(CoreFileSessionProvider) {}

View File

@ -21,6 +21,7 @@ import { CoreLoggerProvider } from './logger';
import { CoreMimetypeUtilsProvider } from './utils/mimetype';
import { CoreTextUtilsProvider } from './utils/text';
import { Zip } from '@ionic-native/zip';
import { makeSingleton } from '@singletons/core.singletons';
/**
* Progress event used when writing a file data into a file.
@ -1270,3 +1271,5 @@ export class CoreFileProvider {
return window.location.href;
}
}
export class CoreFile extends makeSingleton(CoreFileProvider) {}

View File

@ -31,6 +31,7 @@ import { CoreUtilsProvider } from './utils/utils';
import { SQLiteDB } from '@classes/sqlitedb';
import { CoreConstants } from '@core/constants';
import { Md5 } from 'ts-md5/dist/md5';
import { makeSingleton } from '@singletons/core.singletons';
/**
* Entry from filepool.
@ -3069,3 +3070,5 @@ export class CoreFilepoolProvider {
});
}
}
export class CoreFilepool extends makeSingleton(CoreFilepoolProvider) {}

View File

@ -17,6 +17,7 @@ import { TranslateService } from '@ngx-translate/core';
import { CoreSitesProvider } from './sites';
import { CoreCoursesProvider } from '@core/courses/providers/courses';
import { CoreSite, CoreSiteWSPreSets } from '@classes/site';
import { makeSingleton } from '@singletons/core.singletons';
/**
* Group info for an activity.
@ -449,3 +450,5 @@ export class CoreGroupsProvider {
return groupInfo.defaultGroupId;
}
}
export class CoreGroups extends makeSingleton(CoreGroupsProvider) {}

View File

@ -16,6 +16,7 @@ import { Injectable } from '@angular/core';
import { Platform } from 'ionic-angular';
import { CoreLoggerProvider } from './logger';
import { CoreUtilsProvider } from './utils/utils';
import { makeSingleton } from '@singletons/core.singletons';
/**
* Interface that all init handlers must implement.
@ -175,3 +176,5 @@ export class CoreInitDelegate {
this.initProcesses[handler.name] = handler;
}
}
export class CoreInit extends makeSingleton(CoreInitDelegate) {}

View File

@ -20,6 +20,7 @@ import { Platform, Config } from 'ionic-angular';
import { CoreAppProvider } from '@providers/app';
import { CoreConfigProvider } from './config';
import { CoreConfigConstants } from '../configconstants';
import { makeSingleton } from '@singletons/core.singletons';
/*
* Service to handle language features, like changing the current language.
@ -453,3 +454,5 @@ export class CoreLangProvider {
}
}
}
export class CoreLang extends makeSingleton(CoreLangProvider) {}

View File

@ -27,6 +27,7 @@ import { SQLiteDB } from '@classes/sqlitedb';
import { CoreConstants } from '@core/constants';
import { CoreConfigConstants } from '../configconstants';
import { Subject, Subscription } from 'rxjs';
import { makeSingleton } from '@singletons/core.singletons';
/*
* Generated class for the LocalNotificationsProvider provider.
@ -754,3 +755,5 @@ export class CoreLocalNotificationsProvider {
return this.appDB.updateRecords(this.COMPONENTS_TABLE, {id: newId}, {id: oldId});
}
}
export class CoreLocalNotifications extends makeSingleton(CoreLocalNotificationsProvider) {}

View File

@ -14,6 +14,7 @@
import { Injectable } from '@angular/core';
import * as moment from 'moment';
import { makeSingleton } from '@singletons/core.singletons';
/**
* Helper service to display messages in the console.
@ -72,3 +73,5 @@ export class CoreLoggerProvider {
};
}
}
export class CoreLogger extends makeSingleton(CoreLoggerProvider) {}

View File

@ -19,6 +19,7 @@ import { CoreSitesProvider } from './sites';
import { CoreWSExternalFile } from '@providers/ws';
import { FileEntry } from '@ionic-native/file';
import { CoreDelegate, CoreDelegateHandler } from '@classes/delegate';
import { makeSingleton } from '@singletons/core.singletons';
/**
* Interface that all plugin file handlers must implement.
@ -371,3 +372,5 @@ export class CorePluginFileDelegate extends CoreDelegate {
return Promise.resolve();
}
}
export class CorePluginFile extends makeSingleton(CorePluginFileDelegate) {}

View File

@ -14,6 +14,7 @@
import { Injectable, Injector } from '@angular/core';
import { CoreSite } from '@classes/site';
import { makeSingleton } from '@singletons/core.singletons';
/*
* Provider to create sites instances.
@ -56,3 +57,5 @@ export class CoreSitesFactoryProvider {
return methods;
}
}
export class CoreSitesFactory extends makeSingleton(CoreSitesFactoryProvider) {}

View File

@ -30,6 +30,7 @@ import { CoreSite, CoreSiteWSPreSets } from '@classes/site';
import { SQLiteDB, SQLiteDBTableSchema } from '@classes/sqlitedb';
import { Md5 } from 'ts-md5/dist/md5';
import { WP_PROVIDER } from '@app/app.module';
import { makeSingleton } from '@singletons/core.singletons';
/**
* Response of checking if a site exists and its configuration.
@ -1925,5 +1926,6 @@ export class CoreSitesProvider {
return {};
}
}
}
export class CoreSites extends makeSingleton(CoreSitesProvider) {}

View File

@ -15,6 +15,7 @@
import { Injectable } from '@angular/core';
import { CoreEventsProvider } from './events';
import { CoreSitesProvider, CoreSiteSchema } from './sites';
import { makeSingleton } from '@singletons/core.singletons';
/*
* Service that provides some features regarding synchronization.
@ -206,3 +207,5 @@ export class CoreSyncProvider {
}
}
}
export class CoreSync extends makeSingleton(CoreSyncProvider) {}

View File

@ -17,6 +17,7 @@ import { CoreConfigProvider } from './config';
import { CoreInitHandler, CoreInitDelegate } from './init';
import { CoreLoggerProvider } from './logger';
import { CoreConfigConstants } from '../configconstants';
import { makeSingleton } from '@singletons/core.singletons';
/**
* Factory to handle app updates. This factory shouldn't be used outside of core.
@ -59,3 +60,5 @@ export class CoreUpdateManagerProvider implements CoreInitHandler {
});
}
}
export class CoreUpdateManager extends makeSingleton<CoreUpdateManagerProvider>(CoreUpdateManagerProvider) {}

View File

@ -28,6 +28,7 @@ import { CoreContentLinksDelegate } from '@core/contentlinks/providers/delegate'
import { CoreSitePluginsProvider } from '@core/siteplugins/providers/siteplugins';
import { CoreConfigConstants } from '../configconstants';
import { CoreConstants } from '@core/constants';
import { makeSingleton } from '@singletons/core.singletons';
/**
* All params that can be in a custom URL scheme.
@ -488,3 +489,5 @@ export class CoreCustomURLSchemesProvider {
return url.indexOf(CoreConfigConstants.customurlscheme + '://token=') != -1;
}
}
export class CoreCustomURLSchemes extends makeSingleton(CoreCustomURLSchemesProvider) {}

View File

@ -30,6 +30,7 @@ import { CoreConstants } from '@core/constants';
import { CoreBSTooltipComponent } from '@components/bs-tooltip/bs-tooltip';
import { Md5 } from 'ts-md5/dist/md5';
import { Subject } from 'rxjs';
import { makeSingleton } from '@singletons/core.singletons';
/**
* Interface that defines an extension of the Ionic Alert class, to support multiple listeners.
@ -1666,3 +1667,5 @@ export class CoreDomUtilsProvider {
}, siteId);
}
}
export class CoreDomUtils extends makeSingleton(CoreDomUtilsProvider) {}

View File

@ -25,6 +25,7 @@ import { CoreTextUtilsProvider } from './text';
import { CoreUrlUtilsProvider } from './url';
import { CoreUtilsProvider } from './utils';
import { CoreContentLinksHelperProvider } from '@core/contentlinks/providers/helper';
import { makeSingleton } from '@singletons/core.singletons';
/*
* "Utils" service with helper functions for iframes, embed and similar.
@ -396,6 +397,8 @@ export class CoreIframeUtilsProvider {
}
}
export class CoreIframeUtils extends makeSingleton(CoreIframeUtilsProvider) {}
/**
* Subtype of HTMLAnchorElement, with some calculated data.
*/

View File

@ -17,6 +17,7 @@ import { HttpClient } from '@angular/common/http';
import { CoreLoggerProvider } from '../logger';
import { TranslateService } from '@ngx-translate/core';
import { CoreTextUtilsProvider } from './text';
import { makeSingleton } from '@singletons/core.singletons';
/*
* "Utils" service with helper functions for mimetypes and extensions.
@ -549,3 +550,5 @@ export class CoreMimetypeUtilsProvider {
return path;
}
}
export class CoreMimetypeUtils extends makeSingleton(CoreMimetypeUtilsProvider) {}

View File

@ -17,6 +17,7 @@ import { DomSanitizer, SafeUrl } from '@angular/platform-browser';
import { ModalController, Platform } from 'ionic-angular';
import { TranslateService } from '@ngx-translate/core';
import { CoreLangProvider } from '../lang';
import { makeSingleton } from '@singletons/core.singletons';
/**
* Different type of errors the app can treat.
@ -1106,3 +1107,5 @@ export class CoreTextUtilsProvider {
return _unserialize((data + ''), 0)[2];
}
}
export class CoreTextUtils extends makeSingleton(CoreTextUtilsProvider) {}

View File

@ -16,6 +16,7 @@ import { Injectable } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
import * as moment from 'moment';
import { CoreConstants } from '@core/constants';
import { makeSingleton } from '@singletons/core.singletons';
/*
* "Utils" service with helper functions for date and time.
@ -353,3 +354,5 @@ export class CoreTimeUtilsProvider {
}
}
}
export class CoreTimeUtils extends makeSingleton(CoreTimeUtilsProvider) {}

View File

@ -15,6 +15,7 @@
import { Injectable } from '@angular/core';
import { CoreLangProvider } from '../lang';
import { CoreTextUtilsProvider } from './text';
import { makeSingleton } from '@singletons/core.singletons';
/*
* "Utils" service with helper functions for URLs.
@ -498,3 +499,5 @@ export class CoreUrlUtilsProvider {
return url;
}
}
export class CoreUrlUtils extends makeSingleton(CoreUrlUtilsProvider) {}

View File

@ -27,6 +27,7 @@ import { CoreLoggerProvider } from '../logger';
import { TranslateService } from '@ngx-translate/core';
import { CoreLangProvider } from '../lang';
import { CoreWSProvider, CoreWSError } from '../ws';
import { makeSingleton } from '@singletons/core.singletons';
/**
* Deferred promise. It's similar to the result of $q.defer() in AngularJS.
@ -1393,3 +1394,5 @@ export class CoreUtilsProvider {
return filtered;
}
}
export class CoreUtils extends makeSingleton(CoreUtilsProvider) {}

View File

@ -24,6 +24,7 @@ import { CoreTextUtilsProvider } from './utils/text';
import { CoreConstants } from '@core/constants';
import { Md5 } from 'ts-md5/dist/md5';
import { CoreInterceptor } from '@classes/interceptor';
import { makeSingleton } from '@singletons/core.singletons';
/**
* PreSets accepted by the WS call.
@ -879,6 +880,8 @@ export class CoreWSProvider {
}
}
export class CoreWS extends makeSingleton(CoreWSProvider) {}
/**
* Error returned by a WS call.
*/

View File

@ -0,0 +1,49 @@
// (C) Copyright 2015 Moodle Pty Ltd.
//
// 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 { AlertController, App } from 'ionic-angular';
import { Injector } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
import { HttpClient } from '@angular/common/http';
import { CoreSingletonsFactory, CoreInjectionToken, CoreSingletonClass } from '@classes/singletons-factory';
const factory = new CoreSingletonsFactory();
/**
* Set the injector that will be used to resolve instances in the singletons of this module.
*
* @param injector Module injector.
*/
export function setSingletonsInjector(injector: Injector): void {
factory.setInjector(injector);
}
/**
* Make a singleton for this module.
*
* @param injectionToken Injection token used to resolve the singleton instance. This is usually the service class if the
* provider was defined using a class or the string used in the `provide` key if it was defined using an object.
*/
export function makeSingleton<Service>(injectionToken: CoreInjectionToken<Service>): CoreSingletonClass<Service> {
return factory.makeSingleton(injectionToken);
}
export class Translate extends makeSingleton(TranslateService) {}
export class Alerts extends makeSingleton(AlertController) {}
export class Ionic extends makeSingleton(App) {}
export class Http extends makeSingleton(HttpClient) {}

View File

@ -21,7 +21,8 @@
"@providers/*": ["providers/*"],
"@components/*": ["components/*"],
"@directives/*": ["directives/*"],
"@pipes/*": ["pipes/*"]
"@pipes/*": ["pipes/*"],
"@singletons/*": ["singletons/*"]
},
"typeRoots": [
"node_modules/@types"