diff --git a/config/webpack.config.js b/config/webpack.config.js index 3f622bcd3..1c6ae7ca5 100644 --- a/config/webpack.config.js +++ b/config/webpack.config.js @@ -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: [ diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 81fdb84a7..c36f76078 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -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); diff --git a/src/classes/singletons-factory.ts b/src/classes/singletons-factory.ts new file mode 100644 index 000000000..791b192f0 --- /dev/null +++ b/src/classes/singletons-factory.ts @@ -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 = Type | Type | string; + +/** + * Singleton class created using the factory. + */ +export type CoreSingletonClass = 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(injectionToken: CoreInjectionToken): CoreSingletonClass { + // 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; + } + + }; + } +} diff --git a/src/core/compile/providers/compile.ts b/src/core/compile/providers/compile.ts index 51cbc8afd..fc8268f61 100644 --- a/src/core/compile/providers/compile.ts +++ b/src/core/compile/providers/compile.ts @@ -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'; diff --git a/src/core/login/pages/site/site.ts b/src/core/login/pages/site/site.ts index ebf6cfcaf..400491069 100644 --- a/src/core/login/pages/site/site.ts +++ b/src/core/login/pages/site/site.ts @@ -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'; /** diff --git a/src/providers/app.ts b/src/providers/app.ts index 7a50ff553..4d29185e5 100644 --- a/src/providers/app.ts +++ b/src/providers/app.ts @@ -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) {} diff --git a/src/providers/config.ts b/src/providers/config.ts index 709b5ce8e..cf62a9012 100644 --- a/src/providers/config.ts +++ b/src/providers/config.ts @@ -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) {} diff --git a/src/providers/cron.ts b/src/providers/cron.ts index f0b130e9f..0cdf5e7e2 100644 --- a/src/providers/cron.ts +++ b/src/providers/cron.ts @@ -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) {} diff --git a/src/providers/db.ts b/src/providers/db.ts index 6805c82e1..8e6c03299 100644 --- a/src/providers/db.ts +++ b/src/providers/db.ts @@ -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) {} diff --git a/src/providers/events.ts b/src/providers/events.ts index abed87a95..16f9baf3c 100644 --- a/src/providers/events.ts +++ b/src/providers/events.ts @@ -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) {} diff --git a/src/providers/file-helper.ts b/src/providers/file-helper.ts index 73daa59b3..00b4b4150 100644 --- a/src/providers/file-helper.ts +++ b/src/providers/file-helper.ts @@ -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) {} diff --git a/src/providers/file-session.ts b/src/providers/file-session.ts index 79798ae0f..552284a86 100644 --- a/src/providers/file-session.ts +++ b/src/providers/file-session.ts @@ -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) {} diff --git a/src/providers/file.ts b/src/providers/file.ts index 07d1628f5..1af76887e 100644 --- a/src/providers/file.ts +++ b/src/providers/file.ts @@ -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) {} diff --git a/src/providers/filepool.ts b/src/providers/filepool.ts index a4faa08f3..40a5c0d56 100644 --- a/src/providers/filepool.ts +++ b/src/providers/filepool.ts @@ -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) {} diff --git a/src/providers/groups.ts b/src/providers/groups.ts index d0c96e8d2..e05335501 100644 --- a/src/providers/groups.ts +++ b/src/providers/groups.ts @@ -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) {} diff --git a/src/providers/init.ts b/src/providers/init.ts index f44e6f79e..3ac620352 100644 --- a/src/providers/init.ts +++ b/src/providers/init.ts @@ -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) {} diff --git a/src/providers/lang.ts b/src/providers/lang.ts index df16004e3..b598be1b7 100644 --- a/src/providers/lang.ts +++ b/src/providers/lang.ts @@ -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) {} diff --git a/src/providers/local-notifications.ts b/src/providers/local-notifications.ts index 78f2ccb08..2ac4dbdfb 100644 --- a/src/providers/local-notifications.ts +++ b/src/providers/local-notifications.ts @@ -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) {} diff --git a/src/providers/logger.ts b/src/providers/logger.ts index 1453f8cbf..12ad8a79f 100644 --- a/src/providers/logger.ts +++ b/src/providers/logger.ts @@ -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) {} diff --git a/src/providers/plugin-file-delegate.ts b/src/providers/plugin-file-delegate.ts index 42ee781a7..b08989dc2 100644 --- a/src/providers/plugin-file-delegate.ts +++ b/src/providers/plugin-file-delegate.ts @@ -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) {} diff --git a/src/providers/sites-factory.ts b/src/providers/sites-factory.ts index f97d334cb..2ce61382b 100644 --- a/src/providers/sites-factory.ts +++ b/src/providers/sites-factory.ts @@ -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) {} diff --git a/src/providers/sites.ts b/src/providers/sites.ts index dcf33d0c4..f0f85df99 100644 --- a/src/providers/sites.ts +++ b/src/providers/sites.ts @@ -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) {} diff --git a/src/providers/sync.ts b/src/providers/sync.ts index 739d1b59f..c68514165 100644 --- a/src/providers/sync.ts +++ b/src/providers/sync.ts @@ -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) {} diff --git a/src/providers/update-manager.ts b/src/providers/update-manager.ts index b32ef3499..cbf493127 100644 --- a/src/providers/update-manager.ts +++ b/src/providers/update-manager.ts @@ -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) {} diff --git a/src/providers/urlschemes.ts b/src/providers/urlschemes.ts index b7ed851c3..5c22508d2 100644 --- a/src/providers/urlschemes.ts +++ b/src/providers/urlschemes.ts @@ -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) {} diff --git a/src/providers/utils/dom.ts b/src/providers/utils/dom.ts index c692faf65..743a333ec 100644 --- a/src/providers/utils/dom.ts +++ b/src/providers/utils/dom.ts @@ -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) {} diff --git a/src/providers/utils/iframe.ts b/src/providers/utils/iframe.ts index e60c19b58..5b82ebab2 100644 --- a/src/providers/utils/iframe.ts +++ b/src/providers/utils/iframe.ts @@ -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. */ diff --git a/src/providers/utils/mimetype.ts b/src/providers/utils/mimetype.ts index c162f3dc7..4b256196c 100644 --- a/src/providers/utils/mimetype.ts +++ b/src/providers/utils/mimetype.ts @@ -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) {} diff --git a/src/providers/utils/text.ts b/src/providers/utils/text.ts index 8565b6dfc..66e617091 100644 --- a/src/providers/utils/text.ts +++ b/src/providers/utils/text.ts @@ -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) {} diff --git a/src/providers/utils/time.ts b/src/providers/utils/time.ts index ca0d464fe..470c30f44 100644 --- a/src/providers/utils/time.ts +++ b/src/providers/utils/time.ts @@ -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) {} diff --git a/src/providers/utils/url.ts b/src/providers/utils/url.ts index 5c8c0e35a..26e2bb691 100644 --- a/src/providers/utils/url.ts +++ b/src/providers/utils/url.ts @@ -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) {} diff --git a/src/providers/utils/utils.ts b/src/providers/utils/utils.ts index bedd0e4b0..f1235e375 100644 --- a/src/providers/utils/utils.ts +++ b/src/providers/utils/utils.ts @@ -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) {} diff --git a/src/providers/ws.ts b/src/providers/ws.ts index 8440316ae..9f8743a86 100644 --- a/src/providers/ws.ts +++ b/src/providers/ws.ts @@ -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. */ diff --git a/src/singletons/core.singletons.ts b/src/singletons/core.singletons.ts new file mode 100644 index 000000000..7eacfe6bc --- /dev/null +++ b/src/singletons/core.singletons.ts @@ -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(injectionToken: CoreInjectionToken): CoreSingletonClass { + 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) {} diff --git a/src/classes/utils/url.ts b/src/singletons/url.ts similarity index 100% rename from src/classes/utils/url.ts rename to src/singletons/url.ts diff --git a/tsconfig.json b/tsconfig.json index 84a65ecd5..22454c549 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -21,7 +21,8 @@ "@providers/*": ["providers/*"], "@components/*": ["components/*"], "@directives/*": ["directives/*"], - "@pipes/*": ["pipes/*"] + "@pipes/*": ["pipes/*"], + "@singletons/*": ["singletons/*"] }, "typeRoots": [ "node_modules/@types"