From f6bd83ae6cd08286afee8376afcb2d25cc7e5329 Mon Sep 17 00:00:00 2001 From: Noel De Martin Date: Thu, 4 Jul 2024 16:05:35 +0200 Subject: [PATCH] MOBILE-4596 data: Decouple handlers --- src/addons/mod/data/components/index/index.ts | 2 +- src/addons/mod/data/constants.ts | 5 + src/addons/mod/data/data.module.ts | 25 +- .../services/handlers/approve-link-lazy.ts | 52 +++ .../data/services/handlers/approve-link.ts | 60 ++-- .../services/handlers/delete-link-lazy.ts | 50 +++ .../mod/data/services/handlers/delete-link.ts | 59 ++-- .../data/services/handlers/edit-link-lazy.ts | 77 +++++ .../mod/data/services/handlers/edit-link.ts | 80 ++--- .../data/services/handlers/prefetch-lazy.ts | 291 ++++++++++++++++ .../mod/data/services/handlers/prefetch.ts | 316 +++--------------- .../data/services/handlers/show-link-lazy.ts | 87 +++++ .../mod/data/services/handlers/show-link.ts | 94 ++---- .../data/services/handlers/sync-cron-lazy.ts | 43 +++ .../mod/data/services/handlers/sync-cron.ts | 47 +-- src/core/services/db.ts | 2 +- src/core/utils/async-instance.ts | 26 +- src/core/utils/tests/async-instance.test.ts | 18 + 18 files changed, 860 insertions(+), 474 deletions(-) create mode 100644 src/addons/mod/data/services/handlers/approve-link-lazy.ts create mode 100644 src/addons/mod/data/services/handlers/delete-link-lazy.ts create mode 100644 src/addons/mod/data/services/handlers/edit-link-lazy.ts create mode 100644 src/addons/mod/data/services/handlers/prefetch-lazy.ts create mode 100644 src/addons/mod/data/services/handlers/show-link-lazy.ts create mode 100644 src/addons/mod/data/services/handlers/sync-cron-lazy.ts diff --git a/src/addons/mod/data/components/index/index.ts b/src/addons/mod/data/components/index/index.ts index 71293910a..6667397e0 100644 --- a/src/addons/mod/data/components/index/index.ts +++ b/src/addons/mod/data/components/index/index.ts @@ -40,7 +40,7 @@ import { } from '../../services/data'; import { AddonModDataHelper, AddonModDatDisplayFieldsOptions } from '../../services/data-helper'; import { AddonModDataAutoSyncData, AddonModDataSyncResult } from '../../services/data-sync'; -import { AddonModDataPrefetchHandler } from '../../services/handlers/prefetch'; +import { AddonModDataPrefetchHandler } from '../../services/handlers/prefetch-lazy'; import { AddonModDataComponentsCompileModule } from '../components-compile.module'; import { AddonModDataSearchComponent } from '../search/search'; import { CoreUrlUtils } from '@services/utils/url'; diff --git a/src/addons/mod/data/constants.ts b/src/addons/mod/data/constants.ts index 97bde3afb..9f8bcfae1 100644 --- a/src/addons/mod/data/constants.ts +++ b/src/addons/mod/data/constants.ts @@ -21,3 +21,8 @@ export const ADDON_MOD_DATA_ENTRIES_PER_PAGE = 25; export const ADDON_MOD_DATA_ENTRY_CHANGED = 'addon_mod_data_entry_changed'; export const ADDON_MOD_DATA_AUTO_SYNCED = 'addon_mod_data_autom_synced'; + +// Handlers. +export const ADDON_MOD_DATA_PREFETCH_NAME = 'AddonModData'; +export const ADDON_MOD_DATA_PREFETCH_MODNAME = 'data'; +export const ADDON_MOD_DATA_PREFETCH_COMPONENT = ADDON_MOD_DATA_COMPONENT; diff --git a/src/addons/mod/data/data.module.ts b/src/addons/mod/data/data.module.ts index 1388c9771..438caf22b 100644 --- a/src/addons/mod/data/data.module.ts +++ b/src/addons/mod/data/data.module.ts @@ -22,15 +22,15 @@ import { CoreTagAreaDelegate } from '@features/tag/services/tag-area-delegate'; import { CoreCronDelegate } from '@services/cron'; import { CORE_SITE_SCHEMAS } from '@services/sites'; import { ADDON_MOD_DATA_OFFLINE_SITE_SCHEMA } from './services/database/data'; -import { AddonModDataApproveLinkHandler } from './services/handlers/approve-link'; -import { AddonModDataDeleteLinkHandler } from './services/handlers/delete-link'; -import { AddonModDataEditLinkHandler } from './services/handlers/edit-link'; +import { getApproveLinkHandlerInstance } from './services/handlers/approve-link'; +import { getDeleteLinkHandlerInstance } from './services/handlers/delete-link'; +import { getEditLinkHandlerInstance } from './services/handlers/edit-link'; import { AddonModDataIndexLinkHandler } from './services/handlers/index-link'; import { AddonModDataListLinkHandler } from './services/handlers/list-link'; import { AddonModDataModuleHandler } from './services/handlers/module'; -import { AddonModDataPrefetchHandler } from './services/handlers/prefetch'; -import { AddonModDataShowLinkHandler } from './services/handlers/show-link'; -import { AddonModDataSyncCronHandler } from './services/handlers/sync-cron'; +import { getPrefetchHandlerInstance } from './services/handlers/prefetch'; +import { getShowLinkHandlerInstance } from './services/handlers/show-link'; +import { getCronHandlerInstance } from './services/handlers/sync-cron'; import { AddonModDataTagAreaHandler } from './services/handlers/tag-area'; import { AddonModDataFieldModule } from './fields/field.module'; import { CoreCourseHelper } from '@features/course/services/course-helper'; @@ -58,15 +58,16 @@ const routes: Routes = [ provide: APP_INITIALIZER, multi: true, useValue: () => { + CoreCourseModulePrefetchDelegate.registerHandler(getPrefetchHandlerInstance()); + CoreCronDelegate.register(getCronHandlerInstance()); + CoreContentLinksDelegate.registerHandler(getApproveLinkHandlerInstance()); + CoreContentLinksDelegate.registerHandler(getDeleteLinkHandlerInstance()); + CoreContentLinksDelegate.registerHandler(getShowLinkHandlerInstance()); + CoreContentLinksDelegate.registerHandler(getEditLinkHandlerInstance()); + CoreCourseModuleDelegate.registerHandler(AddonModDataModuleHandler.instance); - CoreCourseModulePrefetchDelegate.registerHandler(AddonModDataPrefetchHandler.instance); - CoreCronDelegate.register(AddonModDataSyncCronHandler.instance); CoreContentLinksDelegate.registerHandler(AddonModDataIndexLinkHandler.instance); CoreContentLinksDelegate.registerHandler(AddonModDataListLinkHandler.instance); - CoreContentLinksDelegate.registerHandler(AddonModDataApproveLinkHandler.instance); - CoreContentLinksDelegate.registerHandler(AddonModDataDeleteLinkHandler.instance); - CoreContentLinksDelegate.registerHandler(AddonModDataShowLinkHandler.instance); - CoreContentLinksDelegate.registerHandler(AddonModDataEditLinkHandler.instance); CoreTagAreaDelegate.registerHandler(AddonModDataTagAreaHandler.instance); CoreCourseHelper.registerModuleReminderClick(ADDON_MOD_DATA_COMPONENT); diff --git a/src/addons/mod/data/services/handlers/approve-link-lazy.ts b/src/addons/mod/data/services/handlers/approve-link-lazy.ts new file mode 100644 index 000000000..df48270a6 --- /dev/null +++ b/src/addons/mod/data/services/handlers/approve-link-lazy.ts @@ -0,0 +1,52 @@ +// (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 { Injectable } from '@angular/core'; +import { makeSingleton } from '@singletons'; +import { AddonModDataHelper } from '../data-helper'; +import { AddonModDataApproveLinkHandlerService } from '@addons/mod/data/services/handlers/approve-link'; + +/** + * Content links handler for database approve/disapprove entry. + * Match mod/data/view.php?d=6&approve=5 with a valid data id and entryid. + */ +@Injectable({ providedIn: 'root' }) +export class AddonModDataApproveLinkHandlerLazyService extends AddonModDataApproveLinkHandlerService { + + /** + * @inheritdoc + */ + async handleAction(siteId: string, params: Record, courseId?: number): Promise { + const dataId = parseInt(params.d, 10); + const entryId = parseInt(params.approve, 10) || parseInt(params.disapprove, 10); + const approve = parseInt(params.approve, 10) ? true : false; + + await AddonModDataHelper.approveOrDisapproveEntry(dataId, entryId, approve, courseId, siteId); + } + + /** + * @inheritdoc + */ + async isEnabled(siteId: string, url: string, params: Record): Promise { + if (params.d === undefined || (params.approve === undefined && params.disapprove === undefined)) { + // Required fields not defined. Cannot treat the URL. + return false; + } + + return true; + } + +} + +export const AddonModDataApproveLinkHandler = makeSingleton(AddonModDataApproveLinkHandlerLazyService); diff --git a/src/addons/mod/data/services/handlers/approve-link.ts b/src/addons/mod/data/services/handlers/approve-link.ts index 721a559f9..ad133b362 100644 --- a/src/addons/mod/data/services/handlers/approve-link.ts +++ b/src/addons/mod/data/services/handlers/approve-link.ts @@ -12,18 +12,12 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { Injectable } from '@angular/core'; +import { asyncInstance } from '@/core/utils/async-instance'; +import { ADDON_MOD_DATA_FEATURE_NAME } from '@addons/mod/data/constants'; import { CoreContentLinksHandlerBase } from '@features/contentlinks/classes/base-handler'; -import { CoreContentLinksAction } from '@features/contentlinks/services/contentlinks-delegate'; -import { makeSingleton } from '@singletons'; -import { AddonModDataHelper } from '../data-helper'; -import { ADDON_MOD_DATA_FEATURE_NAME } from '../../constants'; +import { CoreContentLinksAction, CoreContentLinksHandler } from '@features/contentlinks/services/contentlinks-delegate'; +import type { AddonModDataApproveLinkHandlerLazyService } from '@addons/mod/data/services/handlers/approve-link-lazy'; -/** - * Content links handler for database approve/disapprove entry. - * Match mod/data/view.php?d=6&approve=5 with a valid data id and entryid. - */ -@Injectable({ providedIn: 'root' }) export class AddonModDataApproveLinkHandlerService extends CoreContentLinksHandlerBase { name = 'AddonModDataApproveLinkHandler'; @@ -36,27 +30,41 @@ export class AddonModDataApproveLinkHandlerService extends CoreContentLinksHandl */ getActions(siteIds: string[], url: string, params: Record, courseId?: number): CoreContentLinksAction[] { return [{ - action: async (siteId): Promise => { - const dataId = parseInt(params.d, 10); - const entryId = parseInt(params.approve, 10) || parseInt(params.disapprove, 10); - const approve = parseInt(params.approve, 10) ? true : false; - - await AddonModDataHelper.approveOrDisapproveEntry(dataId, entryId, approve, courseId, siteId); - }, + action: (siteId) => this.handleAction(siteId, params, courseId), }]; } /** - * @inheritdoc + * Handle link action. + * + * @param siteId Site id. + * @param params Params. + * @param courseId Course id. */ - async isEnabled(siteId: string, url: string, params: Record): Promise { - if (params.d === undefined || (params.approve === undefined && params.disapprove === undefined)) { - // Required fields not defined. Cannot treat the URL. - return false; - } - - return true; + // eslint-disable-next-line @typescript-eslint/no-unused-vars + async handleAction(siteId: string, params: Record, courseId?: number): Promise { + // Stub to override. } } -export const AddonModDataApproveLinkHandler = makeSingleton(AddonModDataApproveLinkHandlerService); + +/** + * Get approve link handler instance. + * + * @returns Link handler. + */ +export function getApproveLinkHandlerInstance(): CoreContentLinksHandler { + const lazyHandler = asyncInstance< + AddonModDataApproveLinkHandlerLazyService, + AddonModDataApproveLinkHandlerService + >(async () => { + const { AddonModDataApproveLinkHandler } = await import('./approve-link-lazy'); + + return AddonModDataApproveLinkHandler.instance; + }); + + lazyHandler.setEagerInstance(new AddonModDataApproveLinkHandlerService()); + lazyHandler.setLazyOverrides(['isEnabled', 'handleAction']); + + return lazyHandler; +} diff --git a/src/addons/mod/data/services/handlers/delete-link-lazy.ts b/src/addons/mod/data/services/handlers/delete-link-lazy.ts new file mode 100644 index 000000000..f275630ee --- /dev/null +++ b/src/addons/mod/data/services/handlers/delete-link-lazy.ts @@ -0,0 +1,50 @@ +// (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 { Injectable } from '@angular/core'; +import { makeSingleton } from '@singletons'; +import { AddonModDataHelper } from '../data-helper'; +import { AddonModDataDeleteLinkHandlerService } from '@addons/mod/data/services/handlers/delete-link'; + +/** + * Content links handler for database delete entry. + * Match mod/data/view.php?d=6&delete=5 with a valid data id and entryid. + */ +@Injectable({ providedIn: 'root' }) +export class AddonModDataDeleteLinkHandlerLazyService extends AddonModDataDeleteLinkHandlerService { + + /** + * @inheritdoc + */ + async handleAction(siteId: string, params: Record, courseId?: number): Promise { + const dataId = parseInt(params.d, 10); + const entryId = parseInt(params.delete, 10); + + await AddonModDataHelper.showDeleteEntryModal(dataId, entryId, courseId, siteId); + } + + /** + * @inheritdoc + */ + async isEnabled(siteId: string, url: string, params: Record): Promise { + if (params.d === undefined || params.delete === undefined) { + // Required fields not defined. Cannot treat the URL. + return false; + } + + return true; + } + +} +export const AddonModDataDeleteLinkHandler = makeSingleton(AddonModDataDeleteLinkHandlerLazyService); diff --git a/src/addons/mod/data/services/handlers/delete-link.ts b/src/addons/mod/data/services/handlers/delete-link.ts index 24f042ac6..076c40bb8 100644 --- a/src/addons/mod/data/services/handlers/delete-link.ts +++ b/src/addons/mod/data/services/handlers/delete-link.ts @@ -12,18 +12,12 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { Injectable } from '@angular/core'; +import { asyncInstance } from '@/core/utils/async-instance'; +import { ADDON_MOD_DATA_FEATURE_NAME } from '@addons/mod/data/constants'; import { CoreContentLinksHandlerBase } from '@features/contentlinks/classes/base-handler'; -import { CoreContentLinksAction } from '@features/contentlinks/services/contentlinks-delegate'; -import { makeSingleton } from '@singletons'; -import { AddonModDataHelper } from '../data-helper'; -import { ADDON_MOD_DATA_FEATURE_NAME } from '../../constants'; +import { CoreContentLinksAction, CoreContentLinksHandler } from '@features/contentlinks/services/contentlinks-delegate'; +import type { AddonModDataDeleteLinkHandlerLazyService } from '@addons/mod/data/services/handlers/delete-link-lazy'; -/** - * Content links handler for database delete entry. - * Match mod/data/view.php?d=6&delete=5 with a valid data id and entryid. - */ -@Injectable({ providedIn: 'root' }) export class AddonModDataDeleteLinkHandlerService extends CoreContentLinksHandlerBase { name = 'AddonModDataDeleteLinkHandler'; @@ -35,26 +29,41 @@ export class AddonModDataDeleteLinkHandlerService extends CoreContentLinksHandle */ getActions(siteIds: string[], url: string, params: Record, courseId?: number): CoreContentLinksAction[] { return [{ - action: async (siteId): Promise => { - const dataId = parseInt(params.d, 10); - const entryId = parseInt(params.delete, 10); - - await AddonModDataHelper.showDeleteEntryModal(dataId, entryId, courseId, siteId); - }, + action: (siteId) => this.handleAction(siteId, params, courseId), }]; } /** - * @inheritdoc + * Handle link action. + * + * @param siteId Site id. + * @param params Params. + * @param courseId Course id. */ - async isEnabled(siteId: string, url: string, params: Record): Promise { - if (params.d === undefined || params.delete === undefined) { - // Required fields not defined. Cannot treat the URL. - return false; - } - - return true; + // eslint-disable-next-line @typescript-eslint/no-unused-vars + async handleAction(siteId: string, params: Record, courseId?: number): Promise { + // Stub to override. } } -export const AddonModDataDeleteLinkHandler = makeSingleton(AddonModDataDeleteLinkHandlerService); + +/** + * Get delete link handler instance. + * + * @returns Link handler. + */ +export function getDeleteLinkHandlerInstance(): CoreContentLinksHandler { + const lazyHandler = asyncInstance< + AddonModDataDeleteLinkHandlerLazyService, + AddonModDataDeleteLinkHandlerService + >(async () => { + const { AddonModDataDeleteLinkHandler } = await import('./delete-link-lazy'); + + return AddonModDataDeleteLinkHandler.instance; + }); + + lazyHandler.setEagerInstance(new AddonModDataDeleteLinkHandlerService()); + lazyHandler.setLazyOverrides(['isEnabled', 'handleAction']); + + return lazyHandler; +} diff --git a/src/addons/mod/data/services/handlers/edit-link-lazy.ts b/src/addons/mod/data/services/handlers/edit-link-lazy.ts new file mode 100644 index 000000000..b5b8c2894 --- /dev/null +++ b/src/addons/mod/data/services/handlers/edit-link-lazy.ts @@ -0,0 +1,77 @@ +// (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 { Injectable } from '@angular/core'; +import { Params } from '@angular/router'; +import { CoreCourse } from '@features/course/services/course'; +import { CoreNavigator } from '@services/navigator'; +import { CoreSitesReadingStrategy } from '@services/sites'; +import { CoreDomUtils } from '@services/utils/dom'; +import { makeSingleton } from '@singletons'; +import { ADDON_MOD_DATA_FEATURE_NAME, ADDON_MOD_DATA_PAGE_NAME } from '../../constants'; +import { AddonModDataEditLinkHandlerService } from '@addons/mod/data/services/handlers/edit-link'; + +/** + * Content links handler for database add or edit entry. + * Match mod/data/edit.php?d=6&rid=6 with a valid data and optional record id. + */ +@Injectable({ providedIn: 'root' }) +export class AddonModDataEditLinkHandlerLazyService extends AddonModDataEditLinkHandlerService { + + name = 'AddonModDataEditLinkHandler'; + featureName = ADDON_MOD_DATA_FEATURE_NAME; + pattern = /\/mod\/data\/edit\.php.*([?&](d|rid)=\d+)/; + + /** + * @inheritdoc + */ + async handleAction(siteId: string, params: Record): Promise { + const modal = await CoreDomUtils.showModalLoading(); + const dataId = parseInt(params.d, 10); + const rId = params.rid || ''; + + try { + const module = await CoreCourse.getModuleBasicInfoByInstance( + dataId, + 'data', + { siteId, readingStrategy: CoreSitesReadingStrategy.PREFER_CACHE }, + ); + const pageParams: Params = { + title: module.name, + }; + + await CoreNavigator.navigateToSitePath( + `${ADDON_MOD_DATA_PAGE_NAME}/${module.course}/${module.id}/edit/${rId}`, + { siteId, params: pageParams }, + ); + } finally { + // Just in case. In fact we need to dismiss the modal before showing a toast or error message. + modal.dismiss(); + } + } + + /** + * @inheritdoc + */ + async isEnabled(siteId: string, url: string, params: Record): Promise { + if (params.d === undefined) { + // Id not defined. Cannot treat the URL. + return false; + } + + return true; + } + +} +export const AddonModDataEditLinkHandler = makeSingleton(AddonModDataEditLinkHandlerLazyService); diff --git a/src/addons/mod/data/services/handlers/edit-link.ts b/src/addons/mod/data/services/handlers/edit-link.ts index ce57e83e1..8511dfc08 100644 --- a/src/addons/mod/data/services/handlers/edit-link.ts +++ b/src/addons/mod/data/services/handlers/edit-link.ts @@ -12,22 +12,12 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { Injectable } from '@angular/core'; -import { Params } from '@angular/router'; +import { asyncInstance } from '@/core/utils/async-instance'; +import { ADDON_MOD_DATA_FEATURE_NAME } from '@addons/mod/data/constants'; import { CoreContentLinksHandlerBase } from '@features/contentlinks/classes/base-handler'; -import { CoreContentLinksAction } from '@features/contentlinks/services/contentlinks-delegate'; -import { CoreCourse } from '@features/course/services/course'; -import { CoreNavigator } from '@services/navigator'; -import { CoreSitesReadingStrategy } from '@services/sites'; -import { CoreDomUtils } from '@services/utils/dom'; -import { makeSingleton } from '@singletons'; -import { ADDON_MOD_DATA_FEATURE_NAME, ADDON_MOD_DATA_PAGE_NAME } from '../../constants'; +import { CoreContentLinksAction, CoreContentLinksHandler } from '@features/contentlinks/services/contentlinks-delegate'; +import type { AddonModDataEditLinkHandlerLazyService } from '@addons/mod/data/services/handlers/edit-link-lazy'; -/** - * Content links handler for database add or edit entry. - * Match mod/data/edit.php?d=6&rid=6 with a valid data and optional record id. - */ -@Injectable({ providedIn: 'root' }) export class AddonModDataEditLinkHandlerService extends CoreContentLinksHandlerBase { name = 'AddonModDataEditLinkHandler'; @@ -39,44 +29,40 @@ export class AddonModDataEditLinkHandlerService extends CoreContentLinksHandlerB */ getActions(siteIds: string[], url: string, params: Record): CoreContentLinksAction[] { return [{ - action: async (siteId): Promise => { - const modal = await CoreDomUtils.showModalLoading(); - const dataId = parseInt(params.d, 10); - const rId = params.rid || ''; - - try { - const module = await CoreCourse.getModuleBasicInfoByInstance( - dataId, - 'data', - { siteId, readingStrategy: CoreSitesReadingStrategy.PREFER_CACHE }, - ); - const pageParams: Params = { - title: module.name, - }; - - await CoreNavigator.navigateToSitePath( - `${ADDON_MOD_DATA_PAGE_NAME}/${module.course}/${module.id}/edit/${rId}`, - { siteId, params: pageParams }, - ); - } finally { - // Just in case. In fact we need to dismiss the modal before showing a toast or error message. - modal.dismiss(); - } - }, + action: (siteId) => this.handleAction(siteId, params), }]; } /** - * @inheritdoc + * Handle link action. + * + * @param siteId Site id. + * @param params Params. */ - async isEnabled(siteId: string, url: string, params: Record): Promise { - if (params.d === undefined) { - // Id not defined. Cannot treat the URL. - return false; - } - - return true; + // eslint-disable-next-line @typescript-eslint/no-unused-vars + async handleAction(siteId: string, params: Record): Promise { + // Stub to override. } } -export const AddonModDataEditLinkHandler = makeSingleton(AddonModDataEditLinkHandlerService); + +/** + * Get edit link handler instance. + * + * @returns Link handler. + */ +export function getEditLinkHandlerInstance(): CoreContentLinksHandler { + const lazyHandler = asyncInstance< + AddonModDataEditLinkHandlerLazyService, + AddonModDataEditLinkHandlerService + >(async () => { + const { AddonModDataEditLinkHandler } = await import('./edit-link-lazy'); + + return AddonModDataEditLinkHandler.instance; + }); + + lazyHandler.setEagerInstance(new AddonModDataEditLinkHandlerService()); + lazyHandler.setLazyOverrides(['isEnabled', 'handleAction']); + + return lazyHandler; +} diff --git a/src/addons/mod/data/services/handlers/prefetch-lazy.ts b/src/addons/mod/data/services/handlers/prefetch-lazy.ts new file mode 100644 index 000000000..d79159dbd --- /dev/null +++ b/src/addons/mod/data/services/handlers/prefetch-lazy.ts @@ -0,0 +1,291 @@ +// (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 { Injectable } from '@angular/core'; +import { CoreComments } from '@features/comments/services/comments'; +import { CoreCourseCommonModWSOptions, CoreCourse, CoreCourseAnyModuleData } from '@features/course/services/course'; +import { CoreCourses } from '@features/courses/services/courses'; +import { CoreFilepool } from '@services/filepool'; +import { CoreGroup, CoreGroups } from '@services/groups'; +import { CoreSitesCommonWSOptions, CoreSites, CoreSitesReadingStrategy } from '@services/sites'; +import { CoreTimeUtils } from '@services/utils/time'; +import { CoreUtils } from '@services/utils/utils'; +import { CoreWSFile } from '@services/ws'; +import { makeSingleton } from '@singletons'; +import { AddonModDataEntry, AddonModData, AddonModDataData } from '../data'; +import { AddonModDataSync, AddonModDataSyncResult } from '../data-sync'; +import { ContextLevel } from '@/core/constants'; +import { AddonModDataPrefetchHandlerService } from '@addons/mod/data/services/handlers/prefetch'; + +/** + * Handler to prefetch databases. + */ +@Injectable({ providedIn: 'root' }) +export class AddonModDataPrefetchHandlerLazyService extends AddonModDataPrefetchHandlerService { + + /** + * Retrieves all the entries for all the groups and then returns only unique entries. + * + * @param dataId Database Id. + * @param groups Array of groups in the activity. + * @param options Other options. + * @returns All unique entries. + */ + protected async getAllUniqueEntries( + dataId: number, + groups: CoreGroup[], + options: CoreSitesCommonWSOptions = {}, + ): Promise { + + const promises = groups.map((group) => AddonModData.fetchAllEntries(dataId, { + groupId: group.id, + ...options, // Include all options. + })); + + const responses = await Promise.all(promises); + + const uniqueEntries: Record = {}; + responses.forEach((groupEntries) => { + groupEntries.forEach((entry) => { + uniqueEntries[entry.id] = entry; + }); + }); + + return CoreUtils.objectToArray(uniqueEntries); + } + + /** + * Helper function to get all database info just once. + * + * @param module Module to get the files. + * @param courseId Course ID the module belongs to. + * @param omitFail True to always return even if fails. Default false. + * @param options Other options. + * @returns Promise resolved with the info fetched. + */ + protected async getDatabaseInfoHelper( + module: CoreCourseAnyModuleData, + courseId: number, + omitFail: boolean, + options: CoreCourseCommonModWSOptions = {}, + ): Promise<{ database: AddonModDataData; groups: CoreGroup[]; entries: AddonModDataEntry[]; files: CoreWSFile[]}> { + let groups: CoreGroup[] = []; + let entries: AddonModDataEntry[] = []; + let files: CoreWSFile[] = []; + + options.cmId = options.cmId || module.id; + options.siteId = options.siteId || CoreSites.getCurrentSiteId(); + + const database = await AddonModData.getDatabase(courseId, module.id, options); + + try { + files = this.getIntroFilesFromInstance(module, database); + + const groupInfo = await CoreGroups.getActivityGroupInfo(module.id, false, undefined, options.siteId); + if (!groupInfo.groups || groupInfo.groups.length == 0) { + groupInfo.groups = [{ id: 0, name: '' }]; + } + groups = groupInfo.groups || []; + + entries = await this.getAllUniqueEntries(database.id, groups, options); + files = files.concat(this.getEntriesFiles(entries)); + + return { + database, + groups, + entries, + files, + }; + } catch (error) { + if (omitFail) { + // Any error, return the info we have. + return { + database, + groups, + entries, + files, + }; + } + + throw error; + } + } + + /** + * Returns the file contained in the entries. + * + * @param entries List of entries to get files from. + * @returns List of files. + */ + protected getEntriesFiles(entries: AddonModDataEntry[]): CoreWSFile[] { + let files: CoreWSFile[] = []; + + entries.forEach((entry) => { + CoreUtils.objectToArray(entry.contents).forEach((content) => { + files = files.concat(content.files); + }); + }); + + return files; + } + + /** + * @inheritdoc + */ + async getFiles(module: CoreCourseAnyModuleData, courseId: number): Promise { + return this.getDatabaseInfoHelper(module, courseId, true).then((info) => info.files); + } + + /** + * @inheritdoc + */ + async getIntroFiles(module: CoreCourseAnyModuleData, courseId: number): Promise { + const data = await CoreUtils.ignoreErrors(AddonModData.getDatabase(courseId, module.id)); + + return this.getIntroFilesFromInstance(module, data); + } + + /** + * @inheritdoc + */ + async invalidateContent(moduleId: number, courseId: number): Promise { + await AddonModData.invalidateContent(moduleId, courseId); + } + + /** + * @inheritdoc + */ + async invalidateModule(module: CoreCourseAnyModuleData, courseId: number): Promise { + const promises: Promise[] = []; + promises.push(AddonModData.invalidateDatabaseData(courseId)); + promises.push(AddonModData.invalidateDatabaseAccessInformationData(module.instance)); + + await Promise.all(promises); + } + + /** + * @inheritdoc + */ + async isDownloadable(module: CoreCourseAnyModuleData, courseId: number): Promise { + const database = await AddonModData.getDatabase(courseId, module.id, { + readingStrategy: CoreSitesReadingStrategy.PREFER_CACHE, + }); + + const accessData = await AddonModData.getDatabaseAccessInformation(database.id, { cmId: module.id }); + // Check if database is restricted by time. + if (!accessData.timeavailable) { + const time = CoreTimeUtils.timestamp(); + + // It is restricted, checking times. + if (database.timeavailablefrom && time < database.timeavailablefrom) { + return false; + } + if (database.timeavailableto && time > database.timeavailableto) { + return false; + } + } + + return true; + } + + /** + * @inheritdoc + */ + prefetch(module: CoreCourseAnyModuleData, courseId: number): Promise { + return this.prefetchPackage(module, courseId, (siteId) => this.prefetchDatabase(module, courseId, siteId)); + } + + /** + * Prefetch a database. + * + * @param module Module. + * @param courseId Course ID the module belongs to. + * @param siteId Site ID. + * @returns Promise resolved when done. + */ + protected async prefetchDatabase(module: CoreCourseAnyModuleData, courseId: number, siteId: string): Promise { + const options = { + cmId: module.id, + readingStrategy: CoreSitesReadingStrategy.ONLY_NETWORK, + siteId, + }; + + const info = await this.getDatabaseInfoHelper(module, courseId, false, options); + + // Prefetch the database data. + const database = info.database; + + const commentsEnabled = CoreComments.areCommentsEnabledInSite(); + + const promises: Promise[] = []; + + promises.push(AddonModData.getFields(database.id, options)); + promises.push(CoreFilepool.addFilesToQueue(siteId, info.files, this.component, module.id)); + + info.groups.forEach((group) => { + promises.push(AddonModData.getDatabaseAccessInformation(database.id, { + groupId: group.id, + ...options, // Include all options. + })); + }); + + info.entries.forEach((entry) => { + promises.push(AddonModData.getEntry(database.id, entry.id, options)); + + if (commentsEnabled && database.comments) { + promises.push(CoreComments.getComments( + ContextLevel.MODULE, + database.coursemodule, + 'mod_data', + entry.id, + 'database_entry', + 0, + siteId, + )); + } + }); + + // Add Basic Info to manage links. + promises.push(CoreCourse.getModuleBasicInfoByInstance(database.id, 'data', { siteId })); + + // Get course data, needed to determine upload max size if it's configured to be course limit. + promises.push(CoreUtils.ignoreErrors(CoreCourses.getCourseByField('id', courseId, siteId))); + + await Promise.all(promises); + } + + /** + * Sync a module. + * + * @param module Module. + * @param courseId Course ID the module belongs to + * @param siteId Site ID. If not defined, current site. + * @returns Promise resolved when done. + */ + async sync(module: CoreCourseAnyModuleData, courseId: number, siteId?: string): Promise { + const promises = [ + AddonModDataSync.syncDatabase(module.instance, siteId), + AddonModDataSync.syncRatings(module.id, true, siteId), + ]; + + const results = await Promise.all(promises); + + return results.reduce((a, b) => ({ + updated: a.updated || b.updated, + warnings: (a.warnings || []).concat(b.warnings || []), + }), { updated: false , warnings: [] }); + } + +} +export const AddonModDataPrefetchHandler = makeSingleton(AddonModDataPrefetchHandlerLazyService); diff --git a/src/addons/mod/data/services/handlers/prefetch.ts b/src/addons/mod/data/services/handlers/prefetch.ts index c863cc25a..16605c950 100644 --- a/src/addons/mod/data/services/handlers/prefetch.ts +++ b/src/addons/mod/data/services/handlers/prefetch.ts @@ -12,286 +12,50 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { Injectable } from '@angular/core'; -import { CoreComments } from '@features/comments/services/comments'; +import { asyncInstance } from '@/core/utils/async-instance'; +import { + ADDON_MOD_DATA_PREFETCH_COMPONENT, + ADDON_MOD_DATA_PREFETCH_MODNAME, + ADDON_MOD_DATA_PREFETCH_NAME, +} from '@addons/mod/data/constants'; import { CoreCourseActivityPrefetchHandlerBase } from '@features/course/classes/activity-prefetch-handler'; -import { CoreCourseCommonModWSOptions, CoreCourse, CoreCourseAnyModuleData } from '@features/course/services/course'; -import { CoreCourses } from '@features/courses/services/courses'; -import { CoreFilepool } from '@services/filepool'; -import { CoreGroup, CoreGroups } from '@services/groups'; -import { CoreSitesCommonWSOptions, CoreSites, CoreSitesReadingStrategy } from '@services/sites'; -import { CoreTimeUtils } from '@services/utils/time'; -import { CoreUtils } from '@services/utils/utils'; -import { CoreWSFile } from '@services/ws'; -import { makeSingleton } from '@singletons'; -import { AddonModDataEntry, AddonModData, AddonModDataData } from '../data'; -import { AddonModDataSync, AddonModDataSyncResult } from '../data-sync'; -import { ContextLevel } from '@/core/constants'; -import { ADDON_MOD_DATA_COMPONENT } from '../../constants'; +import { CoreCourseModulePrefetchHandler } from '@features/course/services/module-prefetch-delegate'; +import type { AddonModDataPrefetchHandlerLazyService } from '@addons/mod/data/services/handlers/prefetch-lazy'; -/** - * Handler to prefetch databases. - */ -@Injectable({ providedIn: 'root' }) export class AddonModDataPrefetchHandlerService extends CoreCourseActivityPrefetchHandlerBase { - name = 'AddonModData'; - modName = 'data'; - component = ADDON_MOD_DATA_COMPONENT; + name = ADDON_MOD_DATA_PREFETCH_NAME; + modName = ADDON_MOD_DATA_PREFETCH_MODNAME; + component = ADDON_MOD_DATA_PREFETCH_COMPONENT; updatesNames = /^configuration$|^.*files$|^entries$|^gradeitems$|^outcomes$|^comments$|^ratings/; - /** - * Retrieves all the entries for all the groups and then returns only unique entries. - * - * @param dataId Database Id. - * @param groups Array of groups in the activity. - * @param options Other options. - * @returns All unique entries. - */ - protected async getAllUniqueEntries( - dataId: number, - groups: CoreGroup[], - options: CoreSitesCommonWSOptions = {}, - ): Promise { - - const promises = groups.map((group) => AddonModData.fetchAllEntries(dataId, { - groupId: group.id, - ...options, // Include all options. - })); - - const responses = await Promise.all(promises); - - const uniqueEntries: Record = {}; - responses.forEach((groupEntries) => { - groupEntries.forEach((entry) => { - uniqueEntries[entry.id] = entry; - }); - }); - - return CoreUtils.objectToArray(uniqueEntries); - } - - /** - * Helper function to get all database info just once. - * - * @param module Module to get the files. - * @param courseId Course ID the module belongs to. - * @param omitFail True to always return even if fails. Default false. - * @param options Other options. - * @returns Promise resolved with the info fetched. - */ - protected async getDatabaseInfoHelper( - module: CoreCourseAnyModuleData, - courseId: number, - omitFail: boolean, - options: CoreCourseCommonModWSOptions = {}, - ): Promise<{ database: AddonModDataData; groups: CoreGroup[]; entries: AddonModDataEntry[]; files: CoreWSFile[]}> { - let groups: CoreGroup[] = []; - let entries: AddonModDataEntry[] = []; - let files: CoreWSFile[] = []; - - options.cmId = options.cmId || module.id; - options.siteId = options.siteId || CoreSites.getCurrentSiteId(); - - const database = await AddonModData.getDatabase(courseId, module.id, options); - - try { - files = this.getIntroFilesFromInstance(module, database); - - const groupInfo = await CoreGroups.getActivityGroupInfo(module.id, false, undefined, options.siteId); - if (!groupInfo.groups || groupInfo.groups.length == 0) { - groupInfo.groups = [{ id: 0, name: '' }]; - } - groups = groupInfo.groups || []; - - entries = await this.getAllUniqueEntries(database.id, groups, options); - files = files.concat(this.getEntriesFiles(entries)); - - return { - database, - groups, - entries, - files, - }; - } catch (error) { - if (omitFail) { - // Any error, return the info we have. - return { - database, - groups, - entries, - files, - }; - } - - throw error; - } - } - - /** - * Returns the file contained in the entries. - * - * @param entries List of entries to get files from. - * @returns List of files. - */ - protected getEntriesFiles(entries: AddonModDataEntry[]): CoreWSFile[] { - let files: CoreWSFile[] = []; - - entries.forEach((entry) => { - CoreUtils.objectToArray(entry.contents).forEach((content) => { - files = files.concat(content.files); - }); - }); - - return files; - } - - /** - * @inheritdoc - */ - async getFiles(module: CoreCourseAnyModuleData, courseId: number): Promise { - return this.getDatabaseInfoHelper(module, courseId, true).then((info) => info.files); - } - - /** - * @inheritdoc - */ - async getIntroFiles(module: CoreCourseAnyModuleData, courseId: number): Promise { - const data = await CoreUtils.ignoreErrors(AddonModData.getDatabase(courseId, module.id)); - - return this.getIntroFilesFromInstance(module, data); - } - - /** - * @inheritdoc - */ - async invalidateContent(moduleId: number, courseId: number): Promise { - await AddonModData.invalidateContent(moduleId, courseId); - } - - /** - * @inheritdoc - */ - async invalidateModule(module: CoreCourseAnyModuleData, courseId: number): Promise { - const promises: Promise[] = []; - promises.push(AddonModData.invalidateDatabaseData(courseId)); - promises.push(AddonModData.invalidateDatabaseAccessInformationData(module.instance)); - - await Promise.all(promises); - } - - /** - * @inheritdoc - */ - async isDownloadable(module: CoreCourseAnyModuleData, courseId: number): Promise { - const database = await AddonModData.getDatabase(courseId, module.id, { - readingStrategy: CoreSitesReadingStrategy.PREFER_CACHE, - }); - - const accessData = await AddonModData.getDatabaseAccessInformation(database.id, { cmId: module.id }); - // Check if database is restricted by time. - if (!accessData.timeavailable) { - const time = CoreTimeUtils.timestamp(); - - // It is restricted, checking times. - if (database.timeavailablefrom && time < database.timeavailablefrom) { - return false; - } - if (database.timeavailableto && time > database.timeavailableto) { - return false; - } - } - - return true; - } - - /** - * @inheritdoc - */ - prefetch(module: CoreCourseAnyModuleData, courseId: number): Promise { - return this.prefetchPackage(module, courseId, (siteId) => this.prefetchDatabase(module, courseId, siteId)); - } - - /** - * Prefetch a database. - * - * @param module Module. - * @param courseId Course ID the module belongs to. - * @param siteId Site ID. - * @returns Promise resolved when done. - */ - protected async prefetchDatabase(module: CoreCourseAnyModuleData, courseId: number, siteId: string): Promise { - const options = { - cmId: module.id, - readingStrategy: CoreSitesReadingStrategy.ONLY_NETWORK, - siteId, - }; - - const info = await this.getDatabaseInfoHelper(module, courseId, false, options); - - // Prefetch the database data. - const database = info.database; - - const commentsEnabled = CoreComments.areCommentsEnabledInSite(); - - const promises: Promise[] = []; - - promises.push(AddonModData.getFields(database.id, options)); - promises.push(CoreFilepool.addFilesToQueue(siteId, info.files, this.component, module.id)); - - info.groups.forEach((group) => { - promises.push(AddonModData.getDatabaseAccessInformation(database.id, { - groupId: group.id, - ...options, // Include all options. - })); - }); - - info.entries.forEach((entry) => { - promises.push(AddonModData.getEntry(database.id, entry.id, options)); - - if (commentsEnabled && database.comments) { - promises.push(CoreComments.getComments( - ContextLevel.MODULE, - database.coursemodule, - 'mod_data', - entry.id, - 'database_entry', - 0, - siteId, - )); - } - }); - - // Add Basic Info to manage links. - promises.push(CoreCourse.getModuleBasicInfoByInstance(database.id, 'data', { siteId })); - - // Get course data, needed to determine upload max size if it's configured to be course limit. - promises.push(CoreUtils.ignoreErrors(CoreCourses.getCourseByField('id', courseId, siteId))); - - await Promise.all(promises); - } - - /** - * Sync a module. - * - * @param module Module. - * @param courseId Course ID the module belongs to - * @param siteId Site ID. If not defined, current site. - * @returns Promise resolved when done. - */ - async sync(module: CoreCourseAnyModuleData, courseId: number, siteId?: string): Promise { - const promises = [ - AddonModDataSync.syncDatabase(module.instance, siteId), - AddonModDataSync.syncRatings(module.id, true, siteId), - ]; - - const results = await Promise.all(promises); - - return results.reduce((a, b) => ({ - updated: a.updated || b.updated, - warnings: (a.warnings || []).concat(b.warnings || []), - }), { updated: false , warnings: [] }); - } - } -export const AddonModDataPrefetchHandler = makeSingleton(AddonModDataPrefetchHandlerService); + +/** + * Get prefetch handler instance. + * + * @returns Prefetch handler. + */ +export function getPrefetchHandlerInstance(): CoreCourseModulePrefetchHandler { + const lazyHandler = asyncInstance< + AddonModDataPrefetchHandlerLazyService, + AddonModDataPrefetchHandlerService + >(async () => { + const { AddonModDataPrefetchHandler } = await import('./prefetch-lazy'); + + return AddonModDataPrefetchHandler.instance; + }); + + lazyHandler.setEagerInstance(new AddonModDataPrefetchHandlerService()); + lazyHandler.setLazyMethods(['sync']); + lazyHandler.setLazyOverrides([ + 'getFiles', + 'getIntroFiles', + 'invalidateContent', + 'invalidateModule', + 'isDownloadable', + 'prefetch', + ]); + + return lazyHandler; +} diff --git a/src/addons/mod/data/services/handlers/show-link-lazy.ts b/src/addons/mod/data/services/handlers/show-link-lazy.ts new file mode 100644 index 000000000..652ba7f3a --- /dev/null +++ b/src/addons/mod/data/services/handlers/show-link-lazy.ts @@ -0,0 +1,87 @@ +// (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 { Injectable } from '@angular/core'; +import { Params } from '@angular/router'; +import { CoreCourse } from '@features/course/services/course'; +import { CoreNavigator } from '@services/navigator'; +import { CoreSitesReadingStrategy } from '@services/sites'; +import { CoreDomUtils } from '@services/utils/dom'; +import { makeSingleton } from '@singletons'; +import { ADDON_MOD_DATA_PAGE_NAME } from '../../constants'; +import { AddonModDataShowLinkHandlerService } from '@addons/mod/data/services/handlers/show-link'; + +/** + * Content links handler for database show entry. + * Match mod/data/view.php?d=6&rid=5 with a valid data id and entryid. + */ +@Injectable({ providedIn: 'root' }) +export class AddonModDataShowLinkHandlerLazyService extends AddonModDataShowLinkHandlerService { + + /** + * @inheritdoc + */ + async handleAction(siteId: string, params: Record): Promise { + const modal = await CoreDomUtils.showModalLoading(); + const dataId = parseInt(params.d, 10); + const rId = params.rid || ''; + const group = parseInt(params.group, 10) || false; + const page = parseInt(params.page, 10) || false; + + try { + const module = await CoreCourse.getModuleBasicInfoByInstance( + dataId, + 'data', + { siteId, readingStrategy: CoreSitesReadingStrategy.PREFER_CACHE }, + ); + const pageParams: Params = { + title: module.name, + }; + + if (group) { + pageParams.group = group; + } + + if (params.mode && params.mode == 'single') { + pageParams.offset = page || 0; + } + + await CoreNavigator.navigateToSitePath( + `${ADDON_MOD_DATA_PAGE_NAME}/${module.course}/${module.id}/${rId}`, + { siteId, params: pageParams }, + ); + } finally { + // Just in case. In fact we need to dismiss the modal before showing a toast or error message. + modal.dismiss(); + } + } + + /** + * @inheritdoc + */ + async isEnabled(siteId: string, url: string, params: Record): Promise { + if (params.d === undefined) { + // Id not defined. Cannot treat the URL. + return false; + } + + if ((!params.mode || params.mode != 'single') && params.rid === undefined) { + return false; + } + + return true; + } + +} +export const AddonModDataShowLinkHandler = makeSingleton(AddonModDataShowLinkHandlerLazyService); diff --git a/src/addons/mod/data/services/handlers/show-link.ts b/src/addons/mod/data/services/handlers/show-link.ts index 52b47de9d..5f436f16e 100644 --- a/src/addons/mod/data/services/handlers/show-link.ts +++ b/src/addons/mod/data/services/handlers/show-link.ts @@ -12,22 +12,12 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { Injectable } from '@angular/core'; -import { Params } from '@angular/router'; +import { asyncInstance } from '@/core/utils/async-instance'; +import { ADDON_MOD_DATA_FEATURE_NAME } from '@addons/mod/data/constants'; import { CoreContentLinksHandlerBase } from '@features/contentlinks/classes/base-handler'; -import { CoreContentLinksAction } from '@features/contentlinks/services/contentlinks-delegate'; -import { CoreCourse } from '@features/course/services/course'; -import { CoreNavigator } from '@services/navigator'; -import { CoreSitesReadingStrategy } from '@services/sites'; -import { CoreDomUtils } from '@services/utils/dom'; -import { makeSingleton } from '@singletons'; -import { ADDON_MOD_DATA_FEATURE_NAME, ADDON_MOD_DATA_PAGE_NAME } from '../../constants'; +import { CoreContentLinksAction, CoreContentLinksHandler } from '@features/contentlinks/services/contentlinks-delegate'; +import type { AddonModDataShowLinkHandlerLazyService } from '@addons/mod/data/services/handlers/show-link-lazy'; -/** - * Content links handler for database show entry. - * Match mod/data/view.php?d=6&rid=5 with a valid data id and entryid. - */ -@Injectable({ providedIn: 'root' }) export class AddonModDataShowLinkHandlerService extends CoreContentLinksHandlerBase { name = 'AddonModDataShowLinkHandler'; @@ -40,58 +30,40 @@ export class AddonModDataShowLinkHandlerService extends CoreContentLinksHandlerB */ getActions(siteIds: string[], url: string, params: Record): CoreContentLinksAction[] { return [{ - action: async (siteId): Promise => { - const modal = await CoreDomUtils.showModalLoading(); - const dataId = parseInt(params.d, 10); - const rId = params.rid || ''; - const group = parseInt(params.group, 10) || false; - const page = parseInt(params.page, 10) || false; - - try { - const module = await CoreCourse.getModuleBasicInfoByInstance( - dataId, - 'data', - { siteId, readingStrategy: CoreSitesReadingStrategy.PREFER_CACHE }, - ); - const pageParams: Params = { - title: module.name, - }; - - if (group) { - pageParams.group = group; - } - - if (params.mode && params.mode == 'single') { - pageParams.offset = page || 0; - } - - await CoreNavigator.navigateToSitePath( - `${ADDON_MOD_DATA_PAGE_NAME}/${module.course}/${module.id}/${rId}`, - { siteId, params: pageParams }, - ); - } finally { - // Just in case. In fact we need to dismiss the modal before showing a toast or error message. - modal.dismiss(); - } - }, + action: (siteId) => this.handleAction(siteId, params), }]; } /** - * @inheritdoc + * Handle link action. + * + * @param siteId Site id. + * @param params Params. */ - async isEnabled(siteId: string, url: string, params: Record): Promise { - if (params.d === undefined) { - // Id not defined. Cannot treat the URL. - return false; - } - - if ((!params.mode || params.mode != 'single') && params.rid === undefined) { - return false; - } - - return true; + // eslint-disable-next-line @typescript-eslint/no-unused-vars + async handleAction(siteId: string, params: Record): Promise { + // Stub to override. } } -export const AddonModDataShowLinkHandler = makeSingleton(AddonModDataShowLinkHandlerService); + +/** + * Get show link handler instance. + * + * @returns Link handler. + */ +export function getShowLinkHandlerInstance(): CoreContentLinksHandler { + const lazyHandler = asyncInstance< + AddonModDataShowLinkHandlerLazyService, + AddonModDataShowLinkHandlerService + >(async () => { + const { AddonModDataShowLinkHandler } = await import('./show-link-lazy'); + + return AddonModDataShowLinkHandler.instance; + }); + + lazyHandler.setEagerInstance(new AddonModDataShowLinkHandlerService()); + lazyHandler.setLazyOverrides(['isEnabled', 'handleAction']); + + return lazyHandler; +} diff --git a/src/addons/mod/data/services/handlers/sync-cron-lazy.ts b/src/addons/mod/data/services/handlers/sync-cron-lazy.ts new file mode 100644 index 000000000..fa467d7af --- /dev/null +++ b/src/addons/mod/data/services/handlers/sync-cron-lazy.ts @@ -0,0 +1,43 @@ +// (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 { Injectable } from '@angular/core'; +import { CoreCronHandler } from '@services/cron'; +import { makeSingleton } from '@singletons'; +import { AddonModDataSync } from '../data-sync'; +import { AddonModDataSyncCronHandlerService } from '@addons/mod/data/services/handlers/sync-cron'; + +/** + * Synchronization cron handler. + */ +@Injectable({ providedIn: 'root' }) +export class AddonModDataSyncCronHandlerLazyService extends AddonModDataSyncCronHandlerService implements CoreCronHandler { + + /** + * @inheritdoc + */ + execute(siteId?: string, force?: boolean): Promise { + return AddonModDataSync.syncAllDatabases(siteId, force); + } + + /** + * @inheritdoc + */ + getInterval(): number { + return AddonModDataSync.syncInterval; + } + +} + +export const AddonModDataSyncCronHandler = makeSingleton(AddonModDataSyncCronHandlerLazyService); diff --git a/src/addons/mod/data/services/handlers/sync-cron.ts b/src/addons/mod/data/services/handlers/sync-cron.ts index daab609e2..0ddeb95e4 100644 --- a/src/addons/mod/data/services/handlers/sync-cron.ts +++ b/src/addons/mod/data/services/handlers/sync-cron.ts @@ -12,32 +12,33 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { Injectable } from '@angular/core'; +import { asyncInstance } from '@/core/utils/async-instance'; import { CoreCronHandler } from '@services/cron'; -import { makeSingleton } from '@singletons'; -import { AddonModDataSync } from '../data-sync'; +import type { AddonModDataSyncCronHandlerLazyService } from '@addons/mod/data/services/handlers/sync-cron-lazy'; -/** - * Synchronization cron handler. - */ -@Injectable({ providedIn: 'root' }) -export class AddonModDataSyncCronHandlerService implements CoreCronHandler { +export class AddonModDataSyncCronHandlerService { name = 'AddonModDataSyncCronHandler'; - /** - * @inheritdoc - */ - execute(siteId?: string, force?: boolean): Promise { - return AddonModDataSync.syncAllDatabases(siteId, force); - } - - /** - * @inheritdoc - */ - getInterval(): number { - return AddonModDataSync.syncInterval; - } - } -export const AddonModDataSyncCronHandler = makeSingleton(AddonModDataSyncCronHandlerService); + +/** + * Get cron handler instance. + * + * @returns Cron handler. + */ +export function getCronHandlerInstance(): CoreCronHandler { + const lazyHandler = asyncInstance< + AddonModDataSyncCronHandlerLazyService, + AddonModDataSyncCronHandlerService + >(async () => { + const { AddonModDataSyncCronHandler } = await import('./sync-cron-lazy'); + + return AddonModDataSyncCronHandler.instance; + }); + + lazyHandler.setEagerInstance(new AddonModDataSyncCronHandlerService()); + lazyHandler.setLazyMethods(['execute', 'getInterval']); + + return lazyHandler; +} diff --git a/src/core/services/db.ts b/src/core/services/db.ts index 80df9a09f..5a5d67186 100644 --- a/src/core/services/db.ts +++ b/src/core/services/db.ts @@ -239,7 +239,7 @@ export class CoreDbProvider { await CorePlatform.ready(); return SQLite.create({ name, location: 'default' }); - }); + }, {}); } /** diff --git a/src/core/utils/async-instance.ts b/src/core/utils/async-instance.ts index 1517837cf..0482e1c17 100644 --- a/src/core/utils/async-instance.ts +++ b/src/core/utils/async-instance.ts @@ -151,15 +151,32 @@ export type AsyncMethod = : (...args: Params) => Promise : never; +/** + * Get instance methods that don't return a promise. + */ +export type GetEagerMethods = { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + [k in keyof TEagerInstance]: TEagerInstance[k] extends (...args: any[]) => infer TReturn + // eslint-disable-next-line @typescript-eslint/no-explicit-any + ? (TReturn extends Promise ? never : k) + : never +}[keyof TEagerInstance]; + /** * Asynchronous instance. * * All methods are converted to their asynchronous version, and properties are available asynchronously using * the getProperty method. */ -export type AsyncInstance> = - AsyncInstanceWrapper & { +export type AsyncInstance< + TLazyInstance extends TEagerInstance, + TEagerInstance extends AsyncObject = Partial, + TEagerMethods extends keyof TEagerInstance = GetEagerMethods +> = + AsyncInstanceWrapper & Omit<{ [k in keyof TLazyInstance]: AsyncMethod; + }, TEagerMethods> & { + [k in TEagerMethods]: TEagerInstance[k]; }; /** @@ -177,9 +194,14 @@ export type LazyMethodsGuard, T */ export function asyncInstance>( lazyConstructor?: () => TLazyInstance | Promise, + eagerInstance?: TEagerInstance, ): AsyncInstance { const wrapper = createAsyncInstanceWrapper(lazyConstructor); + if (eagerInstance) { + wrapper.setEagerInstance(eagerInstance); + } + return new Proxy(wrapper, { get: (target, p, receiver) => { const property = p as keyof TEagerInstance; diff --git a/src/core/utils/tests/async-instance.test.ts b/src/core/utils/tests/async-instance.test.ts index 6a7d82ac9..f6591ceac 100644 --- a/src/core/utils/tests/async-instance.test.ts +++ b/src/core/utils/tests/async-instance.test.ts @@ -113,6 +113,20 @@ describe('AsyncInstance', () => { expectSameTypes['goodbye'], () => Promise>(true); }); + it('keeps eager methods synchronous', () => { + // Arrange. + const asyncService = asyncInstance(() => new LazyService()); + + asyncService.setEagerInstance(new EagerService()); + + // Act. + const message = asyncService.eagerHello(); + + // Assert. + expect(message).toEqual('hello'); + expectSameTypes(true); + }); + }); class EagerService { @@ -121,6 +135,10 @@ class EagerService { notImplemented?(): void; + eagerHello(): string { + return 'hello'; + } + async isEager(): Promise { return true; }