Skip to content

Commit

Permalink
MOBILE-4653 chore: Fix singleton constructors and compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
crazyserver committed Nov 11, 2024
1 parent 304058f commit d413beb
Show file tree
Hide file tree
Showing 23 changed files with 135 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ jobs:
cat circular-dependencies
lines=$(cat circular-dependencies | wc -l)
echo "Total circular dependencies: $lines"
test $lines -eq 130
test $lines -eq 90
- name: JavaScript code compatibility
run: |
npx check-es-compat www/*.js --polyfills="\{Array,String,TypedArray\}.prototype.at,Object.hasOwn"
Expand Down
31 changes: 25 additions & 6 deletions src/core/features/compile/services/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,24 +79,33 @@ import { Md5 } from 'ts-md5/dist/md5';

// Import core classes that can be useful for site plugins.
import { CoreSyncBaseProvider } from '@classes/base-sync';
import { CoreApp } from '@singletons/app';
import { CoreArray } from '@singletons/array';
import { CoreCache } from '@classes/cache';
import { CoreColors } from '@singletons/colors';
import { CoreCountries } from '@singletons/countries';
import { CoreDelegate } from '@classes/delegate';
import { CoreDirectivesRegistry } from '@singletons/directives-registry';
import { CoreDom } from '@singletons/dom';
import { CoreFile } from '@singletons/file';
import { CoreForms } from '@singletons/form';
import { CoreGeolocationError, CoreGeolocationErrorReason } from '@services/geolocation';
import { CoreInAppBrowser } from '@singletons/iab';
import { CoreKeyboard } from '@singletons/keyboard';
import { CoreNetwork } from '@services/network';
import { CoreObject } from '@singletons/object';
import { CoreOpener } from '@singletons/opener';
import { CorePath } from '@singletons/path';
import { CorePromiseUtils } from '@singletons/promise-utils';
import { CoreRedirects } from '@singletons/redirects';
import { CoreSSO } from '@singletons/sso';
import { CoreText } from '@singletons/text';
import { CoreTime } from '@singletons/time';
import { CoreUrl } from '@singletons/url';
import { CoreUtils } from '@singletons/utils';
import { CoreWait } from '@singletons/wait';
import { CoreWindow } from '@singletons/window';
import { CoreCache } from '@classes/cache';
import { CoreDelegate } from '@classes/delegate';
import { CoreGeolocationError, CoreGeolocationErrorReason } from '@services/geolocation';
import { getCoreErrorsExportedObjects } from '@classes/errors/errors';
import { CoreNetwork } from '@services/network';

// Import all core modules that define components, directives and pipes.
import { CoreSharedModule } from '@/core/shared.module';
Expand Down Expand Up @@ -316,19 +325,29 @@ export class CoreCompileProvider {
*/
instance['Network'] = CoreNetwork.instance;
instance['CoreNetwork'] = CoreNetwork.instance;
instance['CorePlatform'] = CorePlatform.instance;
instance['CoreSyncBaseProvider'] = CoreSyncBaseProvider;
instance['CoreApp'] = CoreApp;
instance['CoreArray'] = CoreArray;
instance['CoreArray'] = CoreArray;
instance['CoreColors'] = CoreColors;
instance['CoreCountries'] = CoreCountries;
instance['CoreDirectivesRegistry'] = CoreDirectivesRegistry;
instance['CoreDom'] = CoreDom;
instance['CoreFile'] = CoreFile;
instance['CoreForms'] = CoreForms;
instance['CoreInAppBrowser'] = CoreInAppBrowser;
instance['CoreKeyboard'] = CoreKeyboard;
instance['CoreObject'] = CoreObject;
instance['CoreOpener'] = CoreOpener;
instance['CorePath'] = CorePath;
instance['CorePlatform'] = CorePlatform.instance;
instance['CorePromiseUtils'] = CorePromiseUtils;
instance['CoreRedirects'] = CoreRedirects;
instance['CoreSSO'] = CoreSSO;
instance['CoreSyncBaseProvider'] = CoreSyncBaseProvider;
instance['CoreText'] = CoreText;
instance['CoreTime'] = CoreTime;
instance['CoreUrl'] = CoreUrl;
instance['CoreUtils'] = CoreUtils;
instance['CoreWait'] = CoreWait;
instance['CoreWindow'] = CoreWindow;
instance['CoreCache'] = CoreCache; // @deprecated since 4.4, plugins should use plain objects instead.
Expand Down
5 changes: 5 additions & 0 deletions src/core/singletons/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
*/
export class CoreBrowser {

// Avoid creating singleton instances.
private constructor() {
// Nothing to do.
}

/**
* Check whether the given cookie is set.
*
Expand Down
5 changes: 5 additions & 0 deletions src/core/singletons/colors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ export enum CoreIonicColorNames {
*/
export class CoreColors {

// Avoid creating singleton instances.
private constructor() {
// Nothing to do.
}

/**
* Returns better contrast color.
*
Expand Down
5 changes: 5 additions & 0 deletions src/core/singletons/countries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ import { Translate } from '@singletons';
*/
export class CoreCountries {

// Avoid creating singleton instances.
private constructor() {
// Nothing to do.
}

/**
* Get country name based on country code.
*
Expand Down
18 changes: 9 additions & 9 deletions src/core/singletons/error-logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,25 @@
// 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 { Injectable } from '@angular/core';
import { makeSingleton } from '@singletons';

/**
* Service that stores error logs in memory.
*/
@Injectable({ providedIn: 'root' })
export class CoreErrorLogsService {
export class CoreErrorLogs {

protected errorLogs: CoreSettingsErrorLog[] = [];
protected static errorLogs: CoreSettingsErrorLog[] = [];

// Avoid creating singleton instances.
private constructor() {
// Nothing to do.
}

/**
* Retrieve error logs displayed in the DOM.
*
* @returns Error logs
*/
getErrorLogs(): CoreSettingsErrorLog[] {
static getErrorLogs(): CoreSettingsErrorLog[] {
return this.errorLogs;
}

Expand All @@ -36,14 +38,12 @@ export class CoreErrorLogsService {
*
* @param error Error.
*/
addErrorLog(error: CoreSettingsErrorLog): void {
static addErrorLog(error: CoreSettingsErrorLog): void {
this.errorLogs.push(error);
}

}

export const CoreErrorLogs = makeSingleton(CoreErrorLogsService);

export type CoreSettingsErrorLog = {
data?: unknown;
message: string;
Expand Down
5 changes: 5 additions & 0 deletions src/core/singletons/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ export class CoreEvents {
protected static observables: { [eventName: string]: Subject<unknown> } = {};
protected static uniqueEvents: { [eventName: string]: {data: unknown} } = {};

// Avoid creating singleton instances.
private constructor() {
// Nothing to do.
}

/**
* Listen for a certain event. To stop listening to the event:
* let observer = eventsProvider.on('something', myCallBack);
Expand Down
5 changes: 5 additions & 0 deletions src/core/singletons/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ import { Translate } from '@singletons';
*/
export class CoreFile {

// Avoid creating singleton instances.
private constructor() {
// Nothing to do.
}

/**
* Check if a file is a FileEntry
*
Expand Down
5 changes: 5 additions & 0 deletions src/core/singletons/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ export class CoreForms {

private static formIds: Record<string, number> = {};

// Avoid creating singleton instances.
private constructor() {
// Nothing to do.
}

/**
* Get the data from a form. It will only collect elements that have a name.
*
Expand Down
5 changes: 5 additions & 0 deletions src/core/singletons/html-classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ export class CoreHTMLClasses {
protected static readonly MOODLEAPP_VERSION_PREFIX = 'moodleapp-';
protected static readonly MOODLE_SITE_THEME_PREFIX = 'theme-site-';

// Avoid creating singleton instances.
private constructor() {
// Nothing to do.
}

/**
* Initialize HTML classes.
*/
Expand Down
5 changes: 5 additions & 0 deletions src/core/singletons/iab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ export class CoreInAppBrowser {

private static iabInstance?: InAppBrowserObject;

// Avoid creating singleton instances.
private constructor() {
// Nothing to do.
}

/**
* Close the InAppBrowser window.
*/
Expand Down
5 changes: 5 additions & 0 deletions src/core/singletons/icons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ export class CoreIcons {

protected static logger = CoreLogger.getInstance('CoreIcons');

// Avoid creating singleton instances.
private constructor() {
// Nothing to do.
}

/**
* Add custom icons to Ionicons.
*/
Expand Down
5 changes: 5 additions & 0 deletions src/core/singletons/keyboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ export class CoreKeyboard {
protected static keyboardOpening = false;
protected static keyboardClosing = false;

// Avoid creating singleton instances.
private constructor() {
// Nothing to do.
}

/**
* Closes the keyboard.
*/
Expand Down
5 changes: 5 additions & 0 deletions src/core/singletons/math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
*/
export class CoreMath {

// Avoid creating singleton instances.
private constructor() {
// Nothing to do.
}

/**
* Clamp a value between a minimum and a maximum.
*
Expand Down
5 changes: 5 additions & 0 deletions src/core/singletons/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ export type CoreObjectWithoutUndefined<T> = Pretty<{
*/
export class CoreObject {

// Avoid creating singleton instances.
private constructor() {
// Nothing to do.
}

/**
* Returns a value of an object and deletes it from the object.
*
Expand Down
5 changes: 5 additions & 0 deletions src/core/singletons/opener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ export class CoreOpener {

protected static logger = CoreLogger.getInstance('CoreOpener');

// Avoid creating singleton instances.
private constructor() {
// Nothing to do.
}

/**
* Show a confirm before opening a link in browser, unless the user previously marked to not show again.
*
Expand Down
5 changes: 5 additions & 0 deletions src/core/singletons/promise-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ export class CorePromiseUtils {

protected static logger = CoreLogger.getInstance('CorePromiseUtils');

// Avoid creating singleton instances.
private constructor() {
// Nothing to do.
}

/**
* Similar to Promise.all, but if a promise fails this function's promise won't be rejected until ALL promises have finished.
*
Expand Down
5 changes: 5 additions & 0 deletions src/core/singletons/redirects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ export class CoreRedirects {
private static redirect?: CoreRedirectData;
protected static logger = CoreLogger.getInstance('CoreRedirects');

// Avoid creating singleton instances.
private constructor() {
// Nothing to do.
}

/**
* Read redirect data from local storage and clear it if it existed.
*/
Expand Down
5 changes: 5 additions & 0 deletions src/core/singletons/sso.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ export class CoreSSO {

private static ssoAuthenticationDeferred?: CorePromisedValue<void>;

// Avoid creating singleton instances.
private constructor() {
// Nothing to do.
}

/**
* Start an SSO authentication process.
* Please notice that this function should be called when the app receives the new token from the browser,
Expand Down
5 changes: 5 additions & 0 deletions src/core/singletons/subscriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ type Subscribable<T> = EventEmitter<T> | Observable<T>;
*/
export class CoreSubscriptions {

// Avoid creating singleton instances.
private constructor() {
// Nothing to do.
}

/**
* Listen once to a subscribable object.
*
Expand Down
5 changes: 5 additions & 0 deletions src/core/singletons/swiper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ import { SwiperOptions } from 'swiper/types';
*/
export class CoreSwiper {

// Avoid creating singleton instances.
private constructor() {
// Nothing to do.
}

/**
* Initialize a Swiper instance.
* It will return swiper instance if current is not set or destroyed and new is set and not destroyed.
Expand Down
5 changes: 5 additions & 0 deletions src/core/singletons/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ export class CoreTime {
'13.0': 'Etc/GMT-13',
};

// Avoid creating singleton instances.
private constructor() {
// Nothing to do.
}

/**
* Returns years, months, days, hours, minutes and seconds in a human readable format.
*
Expand Down
5 changes: 5 additions & 0 deletions src/core/singletons/wait.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ import { CorePlatform } from '@services/platform';
*/
export class CoreWait {

// Avoid creating singleton instances.
private constructor() {
// Nothing to do.
}

/**
* Wait until the next tick.
*
Expand Down

0 comments on commit d413beb

Please sign in to comment.