2020-10-06 10:48:26 +02:00
|
|
|
// (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.
|
|
|
|
*/
|
2020-10-07 10:11:20 +02:00
|
|
|
export type CoreInjectionToken<Service> = Type<Service> | Type<unknown> | string;
|
2020-10-06 10:48:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Singleton class created using the factory.
|
|
|
|
*/
|
2020-10-07 11:13:54 +02:00
|
|
|
export type CoreSingletonClass<Service> = typeof CoreSingleton & {
|
|
|
|
instance: Service;
|
|
|
|
setInstance(instance: Service): void;
|
|
|
|
};
|
2020-10-06 10:48:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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> {
|
2020-10-07 10:11:20 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
2020-10-06 10:48:26 +02:00
|
|
|
const factory = this;
|
|
|
|
|
|
|
|
return class {
|
|
|
|
|
2020-10-07 10:11:20 +02:00
|
|
|
private static serviceInstance: Service;
|
2020-10-06 10:48:26 +02:00
|
|
|
|
|
|
|
static get instance(): Service {
|
|
|
|
// Initialize instances lazily.
|
2020-10-07 10:11:20 +02:00
|
|
|
if (!this.serviceInstance) {
|
|
|
|
this.serviceInstance = factory.injector.get(injectionToken);
|
2020-10-06 10:48:26 +02:00
|
|
|
}
|
|
|
|
|
2020-10-07 10:11:20 +02:00
|
|
|
return this.serviceInstance;
|
2020-10-06 10:48:26 +02:00
|
|
|
}
|
|
|
|
|
2020-10-07 11:13:54 +02:00
|
|
|
static setInstance(instance: Service): void {
|
|
|
|
this.serviceInstance = instance;
|
|
|
|
}
|
|
|
|
|
2020-10-06 10:48:26 +02:00
|
|
|
};
|
|
|
|
}
|
2020-10-09 12:41:41 +02:00
|
|
|
|
2020-10-06 10:48:26 +02:00
|
|
|
}
|