From f6bd83ae6cd08286afee8376afcb2d25cc7e5329 Mon Sep 17 00:00:00 2001 From: Noel De Martin Date: Thu, 4 Jul 2024 16:05:35 +0200 Subject: [PATCH 1/3] 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; } From 9943784c307d267f1939012616b2830826dae3de Mon Sep 17 00:00:00 2001 From: Noel De Martin Date: Tue, 23 Jul 2024 12:50:31 +0200 Subject: [PATCH 2/3] MOBILE-4596 data: Move enums to constants --- .../classes/base-field-plugin-component.ts | 3 +- .../mod/data/components/action/action.ts | 9 +++-- .../components/field-plugin/field-plugin.ts | 3 +- src/addons/mod/data/components/index/index.ts | 4 +- .../mod/data/components/search/search.ts | 2 +- src/addons/mod/data/constants.ts | 38 ++++++++++++++++++ src/addons/mod/data/pages/edit/edit.ts | 3 +- src/addons/mod/data/pages/entry/entry.ts | 4 +- src/addons/mod/data/services/data-helper.ts | 11 ++++-- src/addons/mod/data/services/data-offline.ts | 3 +- src/addons/mod/data/services/data-sync.ts | 4 +- src/addons/mod/data/services/data.ts | 39 +------------------ src/addons/mod/data/services/database/data.ts | 2 +- 13 files changed, 67 insertions(+), 58 deletions(-) diff --git a/src/addons/mod/data/classes/base-field-plugin-component.ts b/src/addons/mod/data/classes/base-field-plugin-component.ts index b045cca0b..93483a028 100644 --- a/src/addons/mod/data/classes/base-field-plugin-component.ts +++ b/src/addons/mod/data/classes/base-field-plugin-component.ts @@ -15,7 +15,8 @@ import { Input, Output, OnInit, OnChanges, SimpleChange, EventEmitter, Component } from '@angular/core'; import { FormGroup, FormBuilder, Validators } from '@angular/forms'; import { CoreFormFields } from '@singletons/form'; -import { AddonModDataData, AddonModDataEntryField, AddonModDataField, AddonModDataTemplateMode } from '../services/data'; +import { AddonModDataData, AddonModDataEntryField, AddonModDataField } from '../services/data'; +import { AddonModDataTemplateMode } from '../constants'; /** * Base class for component to render a field. diff --git a/src/addons/mod/data/components/action/action.ts b/src/addons/mod/data/components/action/action.ts index f328e90fc..10022f9f8 100644 --- a/src/addons/mod/data/components/action/action.ts +++ b/src/addons/mod/data/components/action/action.ts @@ -20,17 +20,20 @@ import { CoreNavigator } from '@services/navigator'; import { CoreSites } from '@services/sites'; import { CoreEvents } from '@singletons/events'; import { - AddonModDataAction, AddonModDataData, AddonModDataEntry, AddonModDataGetDataAccessInformationWSResponse, - AddonModDataTemplateMode, } from '../../services/data'; import { AddonModDataHelper } from '../../services/data-helper'; import { AddonModDataOffline } from '../../services/data-offline'; import { CoreDomUtils } from '@services/utils/dom'; import { AddonModDataActionsMenuComponent, AddonModDataActionsMenuItem } from '../actionsmenu/actionsmenu'; -import { ADDON_MOD_DATA_ENTRY_CHANGED, ADDON_MOD_DATA_PAGE_NAME } from '../../constants'; +import { + ADDON_MOD_DATA_ENTRY_CHANGED, + ADDON_MOD_DATA_PAGE_NAME, + AddonModDataAction, + AddonModDataTemplateMode, +} from '../../constants'; /** * Component that displays a database action. diff --git a/src/addons/mod/data/components/field-plugin/field-plugin.ts b/src/addons/mod/data/components/field-plugin/field-plugin.ts index e6a24f99a..3527ccee3 100644 --- a/src/addons/mod/data/components/field-plugin/field-plugin.ts +++ b/src/addons/mod/data/components/field-plugin/field-plugin.ts @@ -17,8 +17,9 @@ import { FormGroup } from '@angular/forms'; import { CoreDynamicComponent } from '@components/dynamic-component/dynamic-component'; import { CoreFormFields } from '@singletons/form'; import { AddonModDataEntryFieldInitialized, AddonModDataFieldPluginBaseComponent } from '../../classes/base-field-plugin-component'; -import { AddonModDataData, AddonModDataField, AddonModDataTemplateMode } from '../../services/data'; +import { AddonModDataData, AddonModDataField } from '../../services/data'; import { AddonModDataFieldsDelegate } from '../../services/data-fields-delegate'; +import { AddonModDataTemplateMode } from '../../constants'; /** * Component that displays a database field plugin. diff --git a/src/addons/mod/data/components/index/index.ts b/src/addons/mod/data/components/index/index.ts index 6667397e0..55abc74be 100644 --- a/src/addons/mod/data/components/index/index.ts +++ b/src/addons/mod/data/components/index/index.ts @@ -31,8 +31,6 @@ import { CoreEventObserver, CoreEvents } from '@singletons/events'; import { AddonModData, AddonModDataEntry, - AddonModDataTemplateType, - AddonModDataTemplateMode, AddonModDataField, AddonModDataGetDataAccessInformationWSResponse, AddonModDataData, @@ -51,6 +49,8 @@ import { ADDON_MOD_DATA_ENTRIES_PER_PAGE, ADDON_MOD_DATA_ENTRY_CHANGED, ADDON_MOD_DATA_PAGE_NAME, + AddonModDataTemplateType, + AddonModDataTemplateMode, } from '../../constants'; const contentToken = ''; diff --git a/src/addons/mod/data/components/search/search.ts b/src/addons/mod/data/components/search/search.ts index 22d000723..b51776d5f 100644 --- a/src/addons/mod/data/components/search/search.ts +++ b/src/addons/mod/data/components/search/search.ts @@ -23,13 +23,13 @@ import { ModalController } from '@singletons'; import { AddonModDataField, AddonModDataData, - AddonModDataTemplateType, AddonModDataSearchEntriesAdvancedField, } from '../../services/data'; import { AddonModDataFieldsDelegate } from '../../services/data-fields-delegate'; import { AddonModDataHelper } from '../../services/data-helper'; import { AddonModDataComponentsCompileModule } from '../components-compile.module'; import { AddonModDataSearchDataParams } from '../index'; +import { AddonModDataTemplateType } from '../../constants'; /** * Page that displays the search modal. diff --git a/src/addons/mod/data/constants.ts b/src/addons/mod/data/constants.ts index 9f8bcfae1..97a1384bd 100644 --- a/src/addons/mod/data/constants.ts +++ b/src/addons/mod/data/constants.ts @@ -26,3 +26,41 @@ export const ADDON_MOD_DATA_AUTO_SYNCED = 'addon_mod_data_autom_synced'; 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; + +export enum AddonModDataAction { + ADD = 'add', + EDIT = 'edit', + DELETE = 'delete', + APPROVE = 'approve', + DISAPPROVE = 'disapprove', + USER = 'user', + USERPICTURE = 'userpicture', + MORE = 'more', + MOREURL = 'moreurl', + COMMENTS = 'comments', + TIMEADDED = 'timeadded', + TIMEMODIFIED = 'timemodified', + TAGS = 'tags', + APPROVALSTATUS = 'approvalstatus', + APPROVALSTATUSCLASS = 'approvalstatusclass', + DELCHECK = 'delcheck', // Unused. + EXPORT = 'export', // Unused. + ACTIONSMENU = 'actionsmenu', + ID = 'id', +} + +export enum AddonModDataTemplateType { + LIST_HEADER = 'listtemplateheader', + LIST = 'listtemplate', + LIST_FOOTER = 'listtemplatefooter', + ADD = 'addtemplate', + SEARCH = 'asearchtemplate', + SINGLE = 'singletemplate', +} + +export enum AddonModDataTemplateMode { + LIST = 'list', + EDIT = 'edit', + SHOW = 'show', + SEARCH = 'search', +} diff --git a/src/addons/mod/data/pages/edit/edit.ts b/src/addons/mod/data/pages/edit/edit.ts index c48a102dc..1c1f43cf0 100644 --- a/src/addons/mod/data/pages/edit/edit.ts +++ b/src/addons/mod/data/pages/edit/edit.ts @@ -31,7 +31,6 @@ import { AddonModDataData, AddonModDataField, AddonModData, - AddonModDataTemplateType, AddonModDataEntry, AddonModDataEntryFields, AddonModDataEditEntryResult, @@ -44,7 +43,7 @@ import { AddonModDataEntryFieldInitialized } from '../../classes/base-field-plug import { CoreTextUtils } from '@services/utils/text'; import { CoreTime } from '@singletons/time'; import { CoreAnalytics, CoreAnalyticsEventType } from '@services/analytics'; -import { ADDON_MOD_DATA_COMPONENT, ADDON_MOD_DATA_ENTRY_CHANGED } from '../../constants'; +import { ADDON_MOD_DATA_COMPONENT, ADDON_MOD_DATA_ENTRY_CHANGED, AddonModDataTemplateType } from '../../constants'; /** * Page that displays the view edit page. diff --git a/src/addons/mod/data/pages/entry/entry.ts b/src/addons/mod/data/pages/entry/entry.ts index 32fc339d0..d76cdab95 100644 --- a/src/addons/mod/data/pages/entry/entry.ts +++ b/src/addons/mod/data/pages/entry/entry.ts @@ -30,8 +30,6 @@ import { AddonModDataData, AddonModDataGetDataAccessInformationWSResponse, AddonModDataField, - AddonModDataTemplateType, - AddonModDataTemplateMode, AddonModDataEntry, } from '../../services/data'; import { AddonModDataHelper } from '../../services/data-helper'; @@ -42,6 +40,8 @@ import { ADDON_MOD_DATA_COMPONENT, ADDON_MOD_DATA_ENTRIES_PER_PAGE, ADDON_MOD_DATA_ENTRY_CHANGED, + AddonModDataTemplateType, + AddonModDataTemplateMode, } from '../../constants'; /** diff --git a/src/addons/mod/data/services/data-helper.ts b/src/addons/mod/data/services/data-helper.ts index a3f788310..ee443d507 100644 --- a/src/addons/mod/data/services/data-helper.ts +++ b/src/addons/mod/data/services/data-helper.ts @@ -31,19 +31,22 @@ import { AddonModDataSearchEntriesOptions, AddonModDataEntries, AddonModDataEntryFields, - AddonModDataAction, AddonModDataGetEntryFormatted, AddonModDataData, - AddonModDataTemplateType, AddonModDataGetDataAccessInformationWSResponse, - AddonModDataTemplateMode, AddonModDataField, AddonModDataEntryWSField, } from './data'; import { AddonModDataFieldsDelegate } from './data-fields-delegate'; import { AddonModDataOffline, AddonModDataOfflineAction } from './data-offline'; import { CoreFileEntry } from '@services/file-helper'; -import { ADDON_MOD_DATA_COMPONENT, ADDON_MOD_DATA_ENTRY_CHANGED } from '../constants'; +import { + ADDON_MOD_DATA_COMPONENT, + ADDON_MOD_DATA_ENTRY_CHANGED, + AddonModDataAction, + AddonModDataTemplateType, + AddonModDataTemplateMode, +} from '../constants'; /** * Service that provides helper functions for datas. diff --git a/src/addons/mod/data/services/data-offline.ts b/src/addons/mod/data/services/data-offline.ts index f098a4f7b..c74d92633 100644 --- a/src/addons/mod/data/services/data-offline.ts +++ b/src/addons/mod/data/services/data-offline.ts @@ -20,8 +20,9 @@ import { CoreTextUtils } from '@services/utils/text'; import { CoreUtils } from '@services/utils/utils'; import { makeSingleton } from '@singletons'; import { CorePath } from '@singletons/path'; -import { AddonModDataAction, AddonModDataEntryWSField } from './data'; +import { AddonModDataEntryWSField } from './data'; import { AddonModDataEntryDBRecord, DATA_ENTRY_TABLE } from './database/data'; +import { AddonModDataAction } from '../constants'; /** * Service to handle Offline data. diff --git a/src/addons/mod/data/services/data-sync.ts b/src/addons/mod/data/services/data-sync.ts index fe5c4dbfc..973597caa 100644 --- a/src/addons/mod/data/services/data-sync.ts +++ b/src/addons/mod/data/services/data-sync.ts @@ -29,10 +29,10 @@ import { CoreTextUtils } from '@services/utils/text'; import { CoreUtils } from '@services/utils/utils'; import { Translate, makeSingleton } from '@singletons'; import { CoreEvents } from '@singletons/events'; -import { AddonModData, AddonModDataData, AddonModDataAction } from './data'; +import { AddonModData, AddonModDataData } from './data'; import { AddonModDataHelper } from './data-helper'; import { AddonModDataOffline, AddonModDataOfflineAction } from './data-offline'; -import { ADDON_MOD_DATA_AUTO_SYNCED, ADDON_MOD_DATA_COMPONENT } from '../constants'; +import { ADDON_MOD_DATA_AUTO_SYNCED, ADDON_MOD_DATA_COMPONENT, AddonModDataAction } from '../constants'; /** * Service to sync databases. diff --git a/src/addons/mod/data/services/data.ts b/src/addons/mod/data/services/data.ts index 3a35dd376..b7d1997b4 100644 --- a/src/addons/mod/data/services/data.ts +++ b/src/addons/mod/data/services/data.ts @@ -35,6 +35,7 @@ import { ADDON_MOD_DATA_COMPONENT, ADDON_MOD_DATA_ENTRIES_PER_PAGE, ADDON_MOD_DATA_ENTRY_CHANGED, + AddonModDataAction, } from '../constants'; declare module '@singletons/events' { @@ -50,44 +51,6 @@ declare module '@singletons/events' { } } -export enum AddonModDataAction { - ADD = 'add', - EDIT = 'edit', - DELETE = 'delete', - APPROVE = 'approve', - DISAPPROVE = 'disapprove', - USER = 'user', - USERPICTURE = 'userpicture', - MORE = 'more', - MOREURL = 'moreurl', - COMMENTS = 'comments', - TIMEADDED = 'timeadded', - TIMEMODIFIED = 'timemodified', - TAGS = 'tags', - APPROVALSTATUS = 'approvalstatus', - APPROVALSTATUSCLASS = 'approvalstatusclass', - DELCHECK = 'delcheck', // Unused. - EXPORT = 'export', // Unused. - ACTIONSMENU = 'actionsmenu', - ID = 'id', -} - -export enum AddonModDataTemplateType { - LIST_HEADER = 'listtemplateheader', - LIST = 'listtemplate', - LIST_FOOTER = 'listtemplatefooter', - ADD = 'addtemplate', - SEARCH = 'asearchtemplate', - SINGLE = 'singletemplate', -} - -export enum AddonModDataTemplateMode { - LIST = 'list', - EDIT = 'edit', - SHOW = 'show', - SEARCH = 'search', -} - /** * Service that provides some features for databases. */ diff --git a/src/addons/mod/data/services/database/data.ts b/src/addons/mod/data/services/database/data.ts index 8b4c8a150..b5caec8fe 100644 --- a/src/addons/mod/data/services/database/data.ts +++ b/src/addons/mod/data/services/database/data.ts @@ -13,7 +13,7 @@ // limitations under the License. import { CoreSiteSchema } from '@services/sites'; -import { AddonModDataAction } from '../data'; +import { AddonModDataAction } from '../../constants'; /** * Database variables for AddonModDataOfflineProvider. From c491b613c8b7fc7da7fae3cfac7295d693e894bc Mon Sep 17 00:00:00 2001 From: Noel De Martin Date: Tue, 23 Jul 2024 15:39:12 +0200 Subject: [PATCH 3/3] MOBILE-4596 wiki: Decouple handlers and components --- src/addons/mod/wiki/components/index/index.ts | 5 +- src/addons/mod/wiki/constants.ts | 6 + .../services/handlers/create-link-lazy.ts | 142 +++++++++++ .../mod/wiki/services/handlers/create-link.ts | 160 +++---------- .../wiki/services/handlers/edit-link-lazy.ts | 72 ++++++ .../mod/wiki/services/handlers/edit-link.ts | 94 +++----- .../handlers/page-or-map-link-lazy.ts | 97 ++++++++ .../services/handlers/page-or-map-link.ts | 112 +++------ .../wiki/services/handlers/prefetch-lazy.ts | 205 ++++++++++++++++ .../mod/wiki/services/handlers/prefetch.ts | 226 +++--------------- .../wiki/services/handlers/sync-cron-lazy.ts | 43 ++++ .../mod/wiki/services/handlers/sync-cron.ts | 46 ++-- src/addons/mod/wiki/wiki.module.ts | 21 +- 13 files changed, 751 insertions(+), 478 deletions(-) create mode 100644 src/addons/mod/wiki/services/handlers/create-link-lazy.ts create mode 100644 src/addons/mod/wiki/services/handlers/edit-link-lazy.ts create mode 100644 src/addons/mod/wiki/services/handlers/page-or-map-link-lazy.ts create mode 100644 src/addons/mod/wiki/services/handlers/prefetch-lazy.ts create mode 100644 src/addons/mod/wiki/services/handlers/sync-cron-lazy.ts diff --git a/src/addons/mod/wiki/components/index/index.ts b/src/addons/mod/wiki/components/index/index.ts index 57fdb29e2..c9940f548 100644 --- a/src/addons/mod/wiki/components/index/index.ts +++ b/src/addons/mod/wiki/components/index/index.ts @@ -50,8 +50,7 @@ import { AddonModWikiSyncWikiResult, AddonModWikiSyncWikiSubwiki, } from '../../services/wiki-sync'; -import { AddonModWikiMapModalComponent, AddonModWikiMapModalReturn } from '../map/map'; -import { AddonModWikiSubwikiPickerComponent } from '../subwiki-picker/subwiki-picker'; +import { AddonModWikiMapModalReturn } from '../map/map'; import { ADDON_MOD_WIKI_AUTO_SYNCED, ADDON_MOD_WIKI_COMPONENT, @@ -665,6 +664,7 @@ export class AddonModWikiIndexComponent extends CoreCourseModuleMainActivityComp */ async openMap(): Promise { // Create the toc modal. + const { AddonModWikiMapModalComponent } = await import('../map/map'); const modalData = await CoreDomUtils.openSideModal({ component: AddonModWikiMapModalComponent, componentProps: { @@ -883,6 +883,7 @@ export class AddonModWikiIndexComponent extends CoreCourseModuleMainActivityComp * @param event Event. */ async showSubwikiPicker(event: MouseEvent): Promise { + const { AddonModWikiSubwikiPickerComponent } = await import('../subwiki-picker/subwiki-picker'); const subwiki = await CoreDomUtils.openPopover({ component: AddonModWikiSubwikiPickerComponent, componentProps: { diff --git a/src/addons/mod/wiki/constants.ts b/src/addons/mod/wiki/constants.ts index 2272f0dcc..d2bb7120f 100644 --- a/src/addons/mod/wiki/constants.ts +++ b/src/addons/mod/wiki/constants.ts @@ -21,3 +21,9 @@ export const ADDON_MOD_WIKI_MANUAL_SYNCED = 'addon_mod_wiki_manual_synced'; export const ADDON_MOD_WIKI_PAGE_CREATED_EVENT = 'addon_mod_wiki_page_created'; export const ADDON_MOD_WIKI_RENEW_LOCK_TIME = 30000; // Milliseconds. + +export const ADDON_MOD_WIKI_FEATURE_NAME = 'CoreCourseModuleDelegate_AddonModWiki'; + +export const ADDON_MOD_WIKI_PREFETCH_NAME = 'AddonModWiki'; +export const ADDON_MOD_WIKI_PREFETCH_MODNAME = 'wiki'; +export const ADDON_MOD_WIKI_PREFETCH_COMPONENT = ADDON_MOD_WIKI_COMPONENT; diff --git a/src/addons/mod/wiki/services/handlers/create-link-lazy.ts b/src/addons/mod/wiki/services/handlers/create-link-lazy.ts new file mode 100644 index 000000000..0976c03fe --- /dev/null +++ b/src/addons/mod/wiki/services/handlers/create-link-lazy.ts @@ -0,0 +1,142 @@ +// (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 { ActivatedRoute } 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 { AddonModWiki } from '../wiki'; +import { ADDON_MOD_WIKI_PAGE_NAME } from '../../constants'; +import { AddonModWikiCreateLinkHandlerService } from '@addons/mod/wiki/services/handlers/create-link'; + +/** + * Handler to treat links to create a wiki page. + */ +@Injectable({ providedIn: 'root' }) +export class AddonModWikiCreateLinkHandlerLazyService extends AddonModWikiCreateLinkHandlerService { + + /** + * Check if the current view is a wiki page of the same wiki. + * + * @param route Activated route if current route is wiki index page, null otherwise. + * @param subwikiId Subwiki ID to check. + * @param siteId Site ID. + * @returns Promise resolved with boolean: whether current view belongs to the same wiki. + */ + protected async currentStateIsSameWiki(route: ActivatedRoute | null, subwikiId: number, siteId: string): Promise { + if (!route) { + // Current view isn't wiki index. + return false; + } + + const params = CoreNavigator.getRouteParams(route); + const queryParams = CoreNavigator.getRouteQueryParams(route); + + if (queryParams.subwikiId == subwikiId) { + // Same subwiki, so it's same wiki. + return true; + } + + const options = { + cmId: params.cmId, + readingStrategy: CoreSitesReadingStrategy.PREFER_CACHE, + siteId, + }; + + if (queryParams.pageId) { + // Get the page contents to check the subwiki. + try { + const page = await AddonModWiki.getPageContents(queryParams.pageId, options); + + return page.subwikiid == subwikiId; + } catch { + // Not found, check next case. + } + } + + try { + // Get the wiki. + const wiki = await AddonModWiki.getWiki(params.courseId, params.cmId, options); + + // Check if the subwiki belongs to this wiki. + return await AddonModWiki.wikiHasSubwiki(wiki.id, subwikiId, options); + } catch { + // Not found, return false. + return false; + } + } + + /** + * @inheritdoc + */ + async handleAction(siteId: string, courseId: number, params: Record): Promise { + const modal = await CoreDomUtils.showModalLoading(); + const { AddonModWikiIndexPage } = await import('../../pages/index'); + + try { + const route = CoreNavigator.getCurrentRoute({ pageComponent: AddonModWikiIndexPage }); + if (!route) { + // Current view isn't wiki index. + return; + } + const subwikiId = parseInt(params.swid, 10); + const wikiId = parseInt(params.wid, 10); + let path = ADDON_MOD_WIKI_PAGE_NAME; + + // Check if the link is inside the same wiki. + const isSameWiki = await this.currentStateIsSameWiki(route, subwikiId, siteId); + + if (isSameWiki) { + // User is seeing the wiki, we can get the module from the wiki params. + const routeParams = CoreNavigator.getRouteParams(route); + + path = path + `/${routeParams.courseId}/${routeParams.cmId}/edit`; + } else if (wikiId) { + // The URL specifies which wiki it belongs to. Get the module. + const module = await CoreCourse.getModuleBasicInfoByInstance( + wikiId, + 'wiki', + { siteId, readingStrategy: CoreSitesReadingStrategy.PREFER_CACHE }, + ); + + path = path + `/${module.course}/${module.id}/edit`; + } else { + // Cannot get module ID. + path = path + `/${courseId || 0}/0/edit`; + } + + // Open the page. + CoreNavigator.navigateToSitePath( + path, + { + params: { + pageTitle: params.title, + subwikiId: subwikiId, + }, + siteId, + }, + ); + } catch (error) { + CoreDomUtils.showErrorModalDefault(error, 'addon.mod_wiki.errorloadingpage', true); + } finally { + modal.dismiss(); + } + } + +} + +export const AddonModWikiCreateLinkHandler = makeSingleton(AddonModWikiCreateLinkHandlerLazyService); diff --git a/src/addons/mod/wiki/services/handlers/create-link.ts b/src/addons/mod/wiki/services/handlers/create-link.ts index baf13ab5b..b2050a905 100644 --- a/src/addons/mod/wiki/services/handlers/create-link.ts +++ b/src/addons/mod/wiki/services/handlers/create-link.ts @@ -12,80 +12,18 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { Injectable } from '@angular/core'; -import { ActivatedRoute } from '@angular/router'; +import { asyncInstance } from '@/core/utils/async-instance'; +import { ADDON_MOD_WIKI_FEATURE_NAME } from '@addons/mod/wiki/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 { AddonModWikiIndexPage } from '../../pages/index'; -import { AddonModWiki } from '../wiki'; -import { ADDON_MOD_WIKI_PAGE_NAME } from '../../constants'; +import { CoreContentLinksAction, CoreContentLinksHandler } from '@features/contentlinks/services/contentlinks-delegate'; +import type { AddonModWikiCreateLinkHandlerLazyService } from '@addons/mod/wiki/services/handlers/create-link-lazy'; -/** - * Handler to treat links to create a wiki page. - */ -@Injectable({ providedIn: 'root' }) export class AddonModWikiCreateLinkHandlerService extends CoreContentLinksHandlerBase { name = 'AddonModWikiCreateLinkHandler'; - featureName = 'CoreCourseModuleDelegate_AddonModWiki'; + featureName = ADDON_MOD_WIKI_FEATURE_NAME; pattern = /\/mod\/wiki\/create\.php.*([&?]swid=\d+)/; - /** - * Check if the current view is a wiki page of the same wiki. - * - * @param route Activated route if current route is wiki index page, null otherwise. - * @param subwikiId Subwiki ID to check. - * @param siteId Site ID. - * @returns Promise resolved with boolean: whether current view belongs to the same wiki. - */ - protected async currentStateIsSameWiki(route: ActivatedRoute | null, subwikiId: number, siteId: string): Promise { - if (!route) { - // Current view isn't wiki index. - return false; - } - - const params = CoreNavigator.getRouteParams(route); - const queryParams = CoreNavigator.getRouteQueryParams(route); - - if (queryParams.subwikiId == subwikiId) { - // Same subwiki, so it's same wiki. - return true; - } - - const options = { - cmId: params.cmId, - readingStrategy: CoreSitesReadingStrategy.PREFER_CACHE, - siteId, - }; - - if (queryParams.pageId) { - // Get the page contents to check the subwiki. - try { - const page = await AddonModWiki.getPageContents(queryParams.pageId, options); - - return page.subwikiid == subwikiId; - } catch { - // Not found, check next case. - } - } - - try { - // Get the wiki. - const wiki = await AddonModWiki.getWiki(params.courseId, params.cmId, options); - - // Check if the subwiki belongs to this wiki. - return await AddonModWiki.wikiHasSubwiki(wiki.id, subwikiId, options); - } catch { - // Not found, return false. - return false; - } - } - /** * @inheritdoc */ @@ -94,65 +32,45 @@ export class AddonModWikiCreateLinkHandlerService extends CoreContentLinksHandle url: string, params: Record, courseId?: number, - ): CoreContentLinksAction[] | Promise { + ): CoreContentLinksAction[] { courseId = Number(courseId || params.courseid || params.cid); return [{ - action: async (siteId: string) => { - const modal = await CoreDomUtils.showModalLoading(); - - try { - const route = CoreNavigator.getCurrentRoute({ pageComponent: AddonModWikiIndexPage }); - if (!route) { - // Current view isn't wiki index. - return; - } - const subwikiId = parseInt(params.swid, 10); - const wikiId = parseInt(params.wid, 10); - let path = ADDON_MOD_WIKI_PAGE_NAME; - - // Check if the link is inside the same wiki. - const isSameWiki = await this.currentStateIsSameWiki(route, subwikiId, siteId); - - if (isSameWiki) { - // User is seeing the wiki, we can get the module from the wiki params. - const routeParams = CoreNavigator.getRouteParams(route); - - path = path + `/${routeParams.courseId}/${routeParams.cmId}/edit`; - } else if (wikiId) { - // The URL specifies which wiki it belongs to. Get the module. - const module = await CoreCourse.getModuleBasicInfoByInstance( - wikiId, - 'wiki', - { siteId, readingStrategy: CoreSitesReadingStrategy.PREFER_CACHE }, - ); - - path = path + `/${module.course}/${module.id}/edit`; - } else { - // Cannot get module ID. - path = path + `/${courseId || 0}/0/edit`; - } - - // Open the page. - CoreNavigator.navigateToSitePath( - path, - { - params: { - pageTitle: params.title, - subwikiId: subwikiId, - }, - siteId, - }, - ); - } catch (error) { - CoreDomUtils.showErrorModalDefault(error, 'addon.mod_wiki.errorloadingpage', true); - } finally { - modal.dismiss(); - } - }, + action: (siteId) => this.handleAction(siteId, courseId, params), }]; } + /** + * Handle link action. + * + * @param siteId Site id. + * @param courseId Course id. + * @param params Params. + */ + // eslint-disable-next-line @typescript-eslint/no-unused-vars + async handleAction(siteId: string, courseId: number | undefined, params: Record): Promise { + // Stub to override. + } + } -export const AddonModWikiCreateLinkHandler = makeSingleton(AddonModWikiCreateLinkHandlerService); +/** + * Get create link handler instance. + * + * @returns Link handler. + */ +export function getCreateLinkHandlerInstance(): CoreContentLinksHandler { + const lazyHandler = asyncInstance< + AddonModWikiCreateLinkHandlerLazyService, + AddonModWikiCreateLinkHandlerService + >(async () => { + const { AddonModWikiCreateLinkHandler } = await import('./create-link-lazy'); + + return AddonModWikiCreateLinkHandler.instance; + }); + + lazyHandler.setEagerInstance(new AddonModWikiCreateLinkHandlerService()); + lazyHandler.setLazyOverrides(['handleAction']); + + return lazyHandler; +} diff --git a/src/addons/mod/wiki/services/handlers/edit-link-lazy.ts b/src/addons/mod/wiki/services/handlers/edit-link-lazy.ts new file mode 100644 index 000000000..f6e483ba6 --- /dev/null +++ b/src/addons/mod/wiki/services/handlers/edit-link-lazy.ts @@ -0,0 +1,72 @@ +// (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 { 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 { AddonModWiki } from '../wiki'; +import { ADDON_MOD_WIKI_PAGE_NAME } from '../../constants'; +import { AddonModWikiEditLinkHandlerService } from '@addons/mod/wiki/services/handlers/edit-link'; + +/** + * Handler to treat links to edit a wiki page. + */ +@Injectable({ providedIn: 'root' }) +export class AddonModWikiEditLinkHandlerLazyService extends AddonModWikiEditLinkHandlerService { + + /** + * @inheritdoc + */ + async handleAction(siteId: string, params: Record): Promise { + const modal = await CoreDomUtils.showModalLoading(); + + try { + const pageId = Number(params.pageid); + + const pageContents = await AddonModWiki.getPageContents(pageId, { siteId }); + + const module = await CoreCourse.getModuleBasicInfoByInstance( + pageContents.wikiid, + 'wiki', + { siteId, readingStrategy: CoreSitesReadingStrategy.PREFER_CACHE }, + ); + + let section = ''; + if (params.section !== undefined) { + section = params.section.replace(/\+/g, ' '); + } + + await CoreNavigator.navigateToSitePath( + `${ADDON_MOD_WIKI_PAGE_NAME}/${module.course}/${module.id}/edit`, + { + params: { + section: section, + pageId: pageId, + }, + siteId, + }, + ); + } catch (error) { + CoreDomUtils.showErrorModalDefault(error, 'addon.mod_wiki.errorloadingpage', true); + } finally { + modal.dismiss(); + } + } + +} + +export const AddonModWikiEditLinkHandler = makeSingleton(AddonModWikiEditLinkHandlerLazyService); diff --git a/src/addons/mod/wiki/services/handlers/edit-link.ts b/src/addons/mod/wiki/services/handlers/edit-link.ts index 16cd102c0..8f64ee08f 100644 --- a/src/addons/mod/wiki/services/handlers/edit-link.ts +++ b/src/addons/mod/wiki/services/handlers/edit-link.ts @@ -12,75 +12,57 @@ // 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_WIKI_FEATURE_NAME } from '@addons/mod/wiki/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 { AddonModWiki } from '../wiki'; -import { ADDON_MOD_WIKI_PAGE_NAME } from '../../constants'; +import { CoreContentLinksAction, CoreContentLinksHandler } from '@features/contentlinks/services/contentlinks-delegate'; +import type { AddonModWikiEditLinkHandlerLazyService } from '@addons/mod/wiki/services/handlers/edit-link-lazy'; -/** - * Handler to treat links to edit a wiki page. - */ -@Injectable({ providedIn: 'root' }) export class AddonModWikiEditLinkHandlerService extends CoreContentLinksHandlerBase { name = 'AddonModWikiEditLinkHandler'; - featureName = 'CoreCourseModuleDelegate_AddonModWiki'; + featureName = ADDON_MOD_WIKI_FEATURE_NAME; pattern = /\/mod\/wiki\/edit\.php.*([&?]pageid=\d+)/; /** * @inheritdoc */ - getActions( - siteIds: string[], - url: string, - params: Record, - ): CoreContentLinksAction[] | Promise { - + getActions(siteIds: string[], url: string, params: Record): CoreContentLinksAction[] { return [{ - action: async (siteId: string) => { - const modal = await CoreDomUtils.showModalLoading(); - - try { - const pageId = Number(params.pageid); - - const pageContents = await AddonModWiki.getPageContents(pageId, { siteId }); - - const module = await CoreCourse.getModuleBasicInfoByInstance( - pageContents.wikiid, - 'wiki', - { siteId, readingStrategy: CoreSitesReadingStrategy.PREFER_CACHE }, - ); - - let section = ''; - if (params.section !== undefined) { - section = params.section.replace(/\+/g, ' '); - } - - await CoreNavigator.navigateToSitePath( - `${ADDON_MOD_WIKI_PAGE_NAME}/${module.course}/${module.id}/edit`, - { - params: { - section: section, - pageId: pageId, - }, - siteId, - }, - ); - } catch (error) { - CoreDomUtils.showErrorModalDefault(error, 'addon.mod_wiki.errorloadingpage', true); - } finally { - modal.dismiss(); - } - }, + action: (siteId) => this.handleAction(siteId, params), }]; } + /** + * Handle link action. + * + * @param siteId Site id. + * @param params Params. + */ + // eslint-disable-next-line @typescript-eslint/no-unused-vars + async handleAction(siteId: string, params: Record): Promise { + // Stub to override. + } + } -export const AddonModWikiEditLinkHandler = makeSingleton(AddonModWikiEditLinkHandlerService); +/** + * Get edit link handler instance. + * + * @returns Link handler. + */ +export function getEditLinkHandlerInstance(): CoreContentLinksHandler { + const lazyHandler = asyncInstance< + AddonModWikiEditLinkHandlerLazyService, + AddonModWikiEditLinkHandlerService + >(async () => { + const { AddonModWikiEditLinkHandler } = await import('./edit-link-lazy'); + + return AddonModWikiEditLinkHandler.instance; + }); + + lazyHandler.setEagerInstance(new AddonModWikiEditLinkHandlerService()); + lazyHandler.setLazyOverrides(['handleAction']); + + return lazyHandler; +} diff --git a/src/addons/mod/wiki/services/handlers/page-or-map-link-lazy.ts b/src/addons/mod/wiki/services/handlers/page-or-map-link-lazy.ts new file mode 100644 index 000000000..d82efeab0 --- /dev/null +++ b/src/addons/mod/wiki/services/handlers/page-or-map-link-lazy.ts @@ -0,0 +1,97 @@ +// (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 { 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 { Md5 } from 'ts-md5'; +import { AddonModWiki } from '../wiki'; +import { ADDON_MOD_WIKI_PAGE_NAME } from '../../constants'; +import { AddonModWikiPageOrMapLinkHandlerService } from '@addons/mod/wiki/services/handlers/page-or-map-link'; + +/** + * Handler to treat links to a wiki page or the wiki map. + */ +@Injectable({ providedIn: 'root' }) +export class AddonModWikiPageOrMapLinkHandlerLazyService extends AddonModWikiPageOrMapLinkHandlerService { + + /** + * @inheritdoc + */ + async handleAction(url: string, siteId: string, params: Record): Promise { + const modal = await CoreDomUtils.showModalLoading(); + const pageId = parseInt(params.pageid, 10); + const action = url.indexOf('mod/wiki/map.php') != -1 ? 'map' : 'page'; + + try { + // Get the page data to obtain wikiId, subwikiId, etc. + const page = await AddonModWiki.getPageContents(pageId, { siteId }); + + const module = await CoreCourse.getModuleBasicInfoByInstance( + page.wikiid, + 'wiki', + { siteId, readingStrategy: CoreSitesReadingStrategy.PREFER_CACHE }, + ); + + const hash = Md5.hashAsciiStr(JSON.stringify({ + pageId: page.id, + pageTitle: page.title, + subwikiId: page.subwikiid, + action: action, + timestamp: Date.now(), + })); + + await CoreNavigator.navigateToSitePath( + `${ADDON_MOD_WIKI_PAGE_NAME}/${module.course}/${module.id}/page/${hash}`, + { + params: { + module, + pageId: page.id, + pageTitle: page.title, + subwikiId: page.subwikiid, + action: action, + }, + siteId, + }, + ); + } catch (error) { + CoreDomUtils.showErrorModalDefault(error, 'addon.mod_wiki.errorloadingpage', true); + } finally { + modal.dismiss(); + } + } + + /** + * @inheritdoc + */ + async isEnabled(siteId: string, url: string, params: Record): Promise { + const isMap = url.indexOf('mod/wiki/map.php') != -1; + + if (params.id && !isMap) { + // ID param is more prioritary than pageid in index page, it's a index URL. + return false; + } else if (isMap && params.option !== undefined && params.option != '5') { + // Map link but the option isn't "Page list", not supported. + return false; + } + + return true; + } + +} + +export const AddonModWikiPageOrMapLinkHandler = makeSingleton(AddonModWikiPageOrMapLinkHandlerLazyService); diff --git a/src/addons/mod/wiki/services/handlers/page-or-map-link.ts b/src/addons/mod/wiki/services/handlers/page-or-map-link.ts index d7f0e3dd4..da6a94345 100644 --- a/src/addons/mod/wiki/services/handlers/page-or-map-link.ts +++ b/src/addons/mod/wiki/services/handlers/page-or-map-link.ts @@ -12,100 +12,58 @@ // 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_WIKI_FEATURE_NAME } from '@addons/mod/wiki/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 { Md5 } from 'ts-md5'; -import { AddonModWiki } from '../wiki'; -import { ADDON_MOD_WIKI_PAGE_NAME } from '../../constants'; +import { CoreContentLinksAction, CoreContentLinksHandler } from '@features/contentlinks/services/contentlinks-delegate'; +import type { AddonModWikiPageOrMapLinkHandlerLazyService } from '@addons/mod/wiki/services/handlers/page-or-map-link-lazy'; -/** - * Handler to treat links to a wiki page or the wiki map. - */ -@Injectable({ providedIn: 'root' }) export class AddonModWikiPageOrMapLinkHandlerService extends CoreContentLinksHandlerBase { name = 'AddonModWikiPageOrMapLinkHandler'; - featureName = 'CoreCourseModuleDelegate_AddonModWiki'; + featureName = ADDON_MOD_WIKI_FEATURE_NAME; pattern = /\/mod\/wiki\/(view|map)\.php.*([&?]pageid=\d+)/; /** * @inheritdoc */ - getActions( - siteIds: string[], - url: string, - params: Record, - ): CoreContentLinksAction[] | Promise { - + getActions(siteIds: string[], url: string, params: Record): CoreContentLinksAction[] { return [{ - action: async (siteId: string) => { - const modal = await CoreDomUtils.showModalLoading(); - const pageId = parseInt(params.pageid, 10); - const action = url.indexOf('mod/wiki/map.php') != -1 ? 'map' : 'page'; - - try { - // Get the page data to obtain wikiId, subwikiId, etc. - const page = await AddonModWiki.getPageContents(pageId, { siteId }); - - const module = await CoreCourse.getModuleBasicInfoByInstance( - page.wikiid, - 'wiki', - { siteId, readingStrategy: CoreSitesReadingStrategy.PREFER_CACHE }, - ); - - const hash = Md5.hashAsciiStr(JSON.stringify({ - pageId: page.id, - pageTitle: page.title, - subwikiId: page.subwikiid, - action: action, - timestamp: Date.now(), - })); - - await CoreNavigator.navigateToSitePath( - `${ADDON_MOD_WIKI_PAGE_NAME}/${module.course}/${module.id}/page/${hash}`, - { - params: { - module, - pageId: page.id, - pageTitle: page.title, - subwikiId: page.subwikiid, - action: action, - }, - siteId, - }, - ); - } catch (error) { - CoreDomUtils.showErrorModalDefault(error, 'addon.mod_wiki.errorloadingpage', true); - } finally { - modal.dismiss(); - } - }, + action: (siteId) => this.handleAction(url, siteId, params), }]; } /** - * @inheritdoc + * Handle link action. + * + * @param url Url. + * @param siteId Site id. + * @param params Params. */ - async isEnabled(siteId: string, url: string, params: Record): Promise { - const isMap = url.indexOf('mod/wiki/map.php') != -1; - - if (params.id && !isMap) { - // ID param is more prioritary than pageid in index page, it's a index URL. - return false; - } else if (isMap && params.option !== undefined && params.option != '5') { - // Map link but the option isn't "Page list", not supported. - return false; - } - - return true; + // eslint-disable-next-line @typescript-eslint/no-unused-vars + async handleAction(url: string, siteId: string, params: Record): Promise { + // Stub to override. } } -export const AddonModWikiPageOrMapLinkHandler = makeSingleton(AddonModWikiPageOrMapLinkHandlerService); +/** + * Get page or map link handler instance. + * + * @returns Link handler. + */ +export function getPageOrMapLinkHandlerInstance(): CoreContentLinksHandler { + const lazyHandler = asyncInstance< + AddonModWikiPageOrMapLinkHandlerLazyService, + AddonModWikiPageOrMapLinkHandlerService + >(async () => { + const { AddonModWikiPageOrMapLinkHandler } = await import('./page-or-map-link-lazy'); + + return AddonModWikiPageOrMapLinkHandler.instance; + }); + + lazyHandler.setEagerInstance(new AddonModWikiPageOrMapLinkHandlerService()); + lazyHandler.setLazyOverrides(['isEnabled', 'handleAction']); + + return lazyHandler; +} diff --git a/src/addons/mod/wiki/services/handlers/prefetch-lazy.ts b/src/addons/mod/wiki/services/handlers/prefetch-lazy.ts new file mode 100644 index 000000000..d3629b3b9 --- /dev/null +++ b/src/addons/mod/wiki/services/handlers/prefetch-lazy.ts @@ -0,0 +1,205 @@ +// (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 { CoreCourse, CoreCourseAnyModuleData } from '@features/course/services/course'; +import { CoreFilepool } from '@services/filepool'; +import { CoreGroups } from '@services/groups'; +import { CoreFileSizeSum, CorePluginFileDelegate } from '@services/plugin-file-delegate'; +import { CoreSites, CoreSitesCommonWSOptions, CoreSitesReadingStrategy } from '@services/sites'; +import { CoreUtils } from '@services/utils/utils'; +import { CoreWSFile } from '@services/ws'; +import { makeSingleton } from '@singletons'; +import { AddonModWiki, AddonModWikiSubwikiPage } from '../wiki'; +import { AddonModWikiSync, AddonModWikiSyncWikiResult } from '../wiki-sync'; +import { AddonModWikiPrefetchHandlerService } from '@addons/mod/wiki/services/handlers/prefetch'; + +/** + * Handler to prefetch wikis. + */ +@Injectable({ providedIn: 'root' }) +export class AddonModWikiPrefetchHandlerLazyService extends AddonModWikiPrefetchHandlerService { + + /** + * Returns a list of pages that can be downloaded. + * + * @param module The module object returned by WS. + * @param courseId The course ID. + * @param options Other options. + * @returns List of pages. + */ + protected async getAllPages( + module: CoreCourseAnyModuleData, + courseId: number, + options: CoreSitesCommonWSOptions = {}, + ): Promise { + options.siteId = options.siteId || CoreSites.getCurrentSiteId(); + + try { + const wiki = await AddonModWiki.getWiki(courseId, module.id, options); + + return await AddonModWiki.getWikiPageList(wiki, options); + } catch { + // Wiki not found, return empty list. + return []; + } + } + + /** + * @inheritdoc + */ + async getDownloadSize(module: CoreCourseAnyModuleData, courseId: number, single?: boolean): Promise { + const promises: Promise[] = []; + const siteId = CoreSites.getCurrentSiteId(); + + promises.push(this.getFiles(module, courseId, single, siteId).then((files) => + CorePluginFileDelegate.getFilesDownloadSize(files))); + + promises.push(this.getAllPages(module, courseId, { + readingStrategy: CoreSitesReadingStrategy.ONLY_NETWORK, + siteId, + }).then((pages) => { + let size = 0; + + pages.forEach((page) => { + if (page.contentsize) { + size = size + page.contentsize; + } + }); + + return { size: size, total: true }; + })); + + const sizes = await Promise.all(promises); + + return { + size: sizes[0].size + sizes[1].size, + total: sizes[0].total && sizes[1].total, + }; + } + + /** + * @inheritdoc + */ + async getFiles( + module: CoreCourseAnyModuleData, + courseId: number, + single?: boolean, + siteId?: string, + ): Promise { + siteId = siteId || CoreSites.getCurrentSiteId(); + + try { + const wiki = await AddonModWiki.getWiki(courseId, module.id, { siteId }); + + const introFiles = this.getIntroFilesFromInstance(module, wiki); + + const files = await AddonModWiki.getWikiFileList(wiki, { siteId }); + + return introFiles.concat(files); + } catch { + // Wiki not found, return empty list. + return []; + } + } + + /** + * @inheritdoc + */ + invalidateContent(moduleId: number, courseId: number): Promise { + return AddonModWiki.invalidateContent(moduleId, courseId); + } + + /** + * @inheritdoc + */ + async prefetch(module: CoreCourseAnyModuleData, courseId: number, single?: boolean): Promise { + // Get the download time of the package before starting the download (otherwise we'd always get current time). + const siteId = CoreSites.getCurrentSiteId(); + + const data = await CoreUtils.ignoreErrors(CoreFilepool.getPackageData(siteId, this.component, module.id)); + + const downloadTime = data?.downloadTime || 0; + + return this.prefetchPackage( + module, + courseId, + (siteId) => this.prefetchWiki(module, courseId, !!single, downloadTime, siteId), + siteId, + ); + } + + /** + * Prefetch a wiki. + * + * @param module Module. + * @param courseId Course ID the module belongs to. + * @param single True if we're downloading a single module, false if we're downloading a whole section. + * @param downloadTime The previous download time, 0 if no previous download. + * @param siteId Site ID. + * @returns Promise resolved when done. + */ + protected async prefetchWiki( + module: CoreCourseAnyModuleData, + courseId: number, + single: boolean, + downloadTime: number, + siteId: string, + ): Promise { + const userId = CoreSites.getCurrentSiteUserId(); + + const commonOptions = { + readingStrategy: CoreSitesReadingStrategy.ONLY_NETWORK, + siteId, + }; + const modOptions = { + cmId: module.id, + ...commonOptions, // Include all common options. + }; + + // Get the list of pages. + const pages = await this.getAllPages(module, courseId, commonOptions); + const promises: Promise[] = []; + + pages.forEach((page) => { + // Fetch page contents if it needs to be fetched. + if (page.timemodified > downloadTime) { + promises.push(AddonModWiki.getPageContents(page.id, modOptions)); + } + }); + + // Fetch group data. + promises.push(CoreGroups.getActivityGroupInfo(module.id, false, userId, siteId)); + + // Fetch info to provide wiki links. + promises.push(AddonModWiki.getWiki(courseId, module.id, { siteId }).then((wiki) => + CoreCourse.getModuleBasicInfoByInstance(wiki.id, 'wiki', { siteId }))); + + // Get related page files and fetch them. + promises.push(this.getFiles(module, courseId, single, siteId).then((files) => + CoreFilepool.addFilesToQueue(siteId, files, this.component, module.id))); + + await Promise.all(promises); + } + + /** + * @inheritdoc + */ + sync(module: CoreCourseAnyModuleData, courseId: number, siteId?: string): Promise { + return AddonModWikiSync.syncWiki(module.instance, module.course, module.id, siteId); + } + +} + +export const AddonModWikiPrefetchHandler = makeSingleton(AddonModWikiPrefetchHandlerLazyService); diff --git a/src/addons/mod/wiki/services/handlers/prefetch.ts b/src/addons/mod/wiki/services/handlers/prefetch.ts index 69feb6614..148339ec2 100644 --- a/src/addons/mod/wiki/services/handlers/prefetch.ts +++ b/src/addons/mod/wiki/services/handlers/prefetch.ts @@ -12,200 +12,48 @@ // 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_WIKI_PREFETCH_COMPONENT, + ADDON_MOD_WIKI_PREFETCH_MODNAME, + ADDON_MOD_WIKI_PREFETCH_NAME, +} from '@addons/mod/wiki/constants'; import { CoreCourseActivityPrefetchHandlerBase } from '@features/course/classes/activity-prefetch-handler'; -import { CoreCourse, CoreCourseAnyModuleData } from '@features/course/services/course'; -import { CoreFilepool } from '@services/filepool'; -import { CoreGroups } from '@services/groups'; -import { CoreFileSizeSum, CorePluginFileDelegate } from '@services/plugin-file-delegate'; -import { CoreSites, CoreSitesCommonWSOptions, CoreSitesReadingStrategy } from '@services/sites'; -import { CoreUtils } from '@services/utils/utils'; -import { CoreWSFile } from '@services/ws'; -import { makeSingleton } from '@singletons'; -import { AddonModWiki, AddonModWikiSubwikiPage } from '../wiki'; -import { AddonModWikiSync, AddonModWikiSyncWikiResult } from '../wiki-sync'; -import { ADDON_MOD_WIKI_COMPONENT } from '../../constants'; +import { CoreCourseModulePrefetchHandler } from '@features/course/services/module-prefetch-delegate'; +import type { AddonModWikiPrefetchHandlerLazyService } from '@addons/mod/wiki/services/handlers/prefetch-lazy'; -/** - * Handler to prefetch wikis. - */ -@Injectable({ providedIn: 'root' }) export class AddonModWikiPrefetchHandlerService extends CoreCourseActivityPrefetchHandlerBase { - name = 'AddonModWiki'; - modName = 'wiki'; - component = ADDON_MOD_WIKI_COMPONENT; + name = ADDON_MOD_WIKI_PREFETCH_NAME; + modName = ADDON_MOD_WIKI_PREFETCH_MODNAME; + component = ADDON_MOD_WIKI_PREFETCH_COMPONENT; updatesNames = /^.*files$|^pages$/; - /** - * Returns a list of pages that can be downloaded. - * - * @param module The module object returned by WS. - * @param courseId The course ID. - * @param options Other options. - * @returns List of pages. - */ - protected async getAllPages( - module: CoreCourseAnyModuleData, - courseId: number, - options: CoreSitesCommonWSOptions = {}, - ): Promise { - options.siteId = options.siteId || CoreSites.getCurrentSiteId(); - - try { - const wiki = await AddonModWiki.getWiki(courseId, module.id, options); - - return await AddonModWiki.getWikiPageList(wiki, options); - } catch { - // Wiki not found, return empty list. - return []; - } - } - - /** - * @inheritdoc - */ - async getDownloadSize(module: CoreCourseAnyModuleData, courseId: number, single?: boolean): Promise { - const promises: Promise[] = []; - const siteId = CoreSites.getCurrentSiteId(); - - promises.push(this.getFiles(module, courseId, single, siteId).then((files) => - CorePluginFileDelegate.getFilesDownloadSize(files))); - - promises.push(this.getAllPages(module, courseId, { - readingStrategy: CoreSitesReadingStrategy.ONLY_NETWORK, - siteId, - }).then((pages) => { - let size = 0; - - pages.forEach((page) => { - if (page.contentsize) { - size = size + page.contentsize; - } - }); - - return { size: size, total: true }; - })); - - const sizes = await Promise.all(promises); - - return { - size: sizes[0].size + sizes[1].size, - total: sizes[0].total && sizes[1].total, - }; - } - - /** - * @inheritdoc - */ - async getFiles( - module: CoreCourseAnyModuleData, - courseId: number, - single?: boolean, - siteId?: string, - ): Promise { - siteId = siteId || CoreSites.getCurrentSiteId(); - - try { - const wiki = await AddonModWiki.getWiki(courseId, module.id, { siteId }); - - const introFiles = this.getIntroFilesFromInstance(module, wiki); - - const files = await AddonModWiki.getWikiFileList(wiki, { siteId }); - - return introFiles.concat(files); - } catch { - // Wiki not found, return empty list. - return []; - } - } - - /** - * @inheritdoc - */ - invalidateContent(moduleId: number, courseId: number): Promise { - return AddonModWiki.invalidateContent(moduleId, courseId); - } - - /** - * @inheritdoc - */ - async prefetch(module: CoreCourseAnyModuleData, courseId: number, single?: boolean): Promise { - // Get the download time of the package before starting the download (otherwise we'd always get current time). - const siteId = CoreSites.getCurrentSiteId(); - - const data = await CoreUtils.ignoreErrors(CoreFilepool.getPackageData(siteId, this.component, module.id)); - - const downloadTime = data?.downloadTime || 0; - - return this.prefetchPackage( - module, - courseId, - (siteId) => this.prefetchWiki(module, courseId, !!single, downloadTime, siteId), - siteId, - ); - } - - /** - * Prefetch a wiki. - * - * @param module Module. - * @param courseId Course ID the module belongs to. - * @param single True if we're downloading a single module, false if we're downloading a whole section. - * @param downloadTime The previous download time, 0 if no previous download. - * @param siteId Site ID. - * @returns Promise resolved when done. - */ - protected async prefetchWiki( - module: CoreCourseAnyModuleData, - courseId: number, - single: boolean, - downloadTime: number, - siteId: string, - ): Promise { - const userId = CoreSites.getCurrentSiteUserId(); - - const commonOptions = { - readingStrategy: CoreSitesReadingStrategy.ONLY_NETWORK, - siteId, - }; - const modOptions = { - cmId: module.id, - ...commonOptions, // Include all common options. - }; - - // Get the list of pages. - const pages = await this.getAllPages(module, courseId, commonOptions); - const promises: Promise[] = []; - - pages.forEach((page) => { - // Fetch page contents if it needs to be fetched. - if (page.timemodified > downloadTime) { - promises.push(AddonModWiki.getPageContents(page.id, modOptions)); - } - }); - - // Fetch group data. - promises.push(CoreGroups.getActivityGroupInfo(module.id, false, userId, siteId)); - - // Fetch info to provide wiki links. - promises.push(AddonModWiki.getWiki(courseId, module.id, { siteId }).then((wiki) => - CoreCourse.getModuleBasicInfoByInstance(wiki.id, 'wiki', { siteId }))); - - // Get related page files and fetch them. - promises.push(this.getFiles(module, courseId, single, siteId).then((files) => - CoreFilepool.addFilesToQueue(siteId, files, this.component, module.id))); - - await Promise.all(promises); - } - - /** - * @inheritdoc - */ - sync(module: CoreCourseAnyModuleData, courseId: number, siteId?: string): Promise { - return AddonModWikiSync.syncWiki(module.instance, module.course, module.id, siteId); - } - } -export const AddonModWikiPrefetchHandler = makeSingleton(AddonModWikiPrefetchHandlerService); +/** + * Get prefetch handler instance. + * + * @returns Prefetch handler. + */ +export function getPrefetchHandlerInstance(): CoreCourseModulePrefetchHandler { + const lazyHandler = asyncInstance< + AddonModWikiPrefetchHandlerLazyService, + AddonModWikiPrefetchHandlerService + >(async () => { + const { AddonModWikiPrefetchHandler } = await import('./prefetch-lazy'); + + return AddonModWikiPrefetchHandler.instance; + }); + + lazyHandler.setEagerInstance(new AddonModWikiPrefetchHandlerService()); + lazyHandler.setLazyMethods(['sync']); + lazyHandler.setLazyOverrides([ + 'getFiles', + 'getDownloadSize', + 'invalidateContent', + 'prefetch', + ]); + + return lazyHandler; +} diff --git a/src/addons/mod/wiki/services/handlers/sync-cron-lazy.ts b/src/addons/mod/wiki/services/handlers/sync-cron-lazy.ts new file mode 100644 index 000000000..ccf347bdf --- /dev/null +++ b/src/addons/mod/wiki/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 { AddonModWikiSync } from '../wiki-sync'; +import { AddonModWikiSyncCronHandlerService } from '@addons/mod/wiki/services/handlers/sync-cron'; + +/** + * Synchronization cron handler. + */ +@Injectable({ providedIn: 'root' }) +export class AddonModWikiSyncCronHandlerLazyService extends AddonModWikiSyncCronHandlerService implements CoreCronHandler { + + /** + * @inheritdoc + */ + execute(siteId?: string, force?: boolean): Promise { + return AddonModWikiSync.syncAllWikis(siteId, force); + } + + /** + * @inheritdoc + */ + getInterval(): number { + return AddonModWikiSync.syncInterval; + } + +} + +export const AddonModWikiSyncCronHandler = makeSingleton(AddonModWikiSyncCronHandlerLazyService); diff --git a/src/addons/mod/wiki/services/handlers/sync-cron.ts b/src/addons/mod/wiki/services/handlers/sync-cron.ts index 16848b150..7ad6ac0c4 100644 --- a/src/addons/mod/wiki/services/handlers/sync-cron.ts +++ b/src/addons/mod/wiki/services/handlers/sync-cron.ts @@ -12,33 +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 { AddonModWikiSync } from '../wiki-sync'; +import type { AddonModWikiSyncCronHandlerLazyService } from '@addons/mod/wiki/services/handlers/sync-cron-lazy'; -/** - * Synchronization cron handler. - */ -@Injectable({ providedIn: 'root' }) -export class AddonModWikiSyncCronHandlerService implements CoreCronHandler { +export class AddonModWikiSyncCronHandlerService { name = 'AddonModWikiSyncCronHandler'; - /** - * @inheritdoc - */ - execute(siteId?: string, force?: boolean): Promise { - return AddonModWikiSync.syncAllWikis(siteId, force); - } - - /** - * @inheritdoc - */ - getInterval(): number { - return AddonModWikiSync.syncInterval; - } - } -export const AddonModWikiSyncCronHandler = makeSingleton(AddonModWikiSyncCronHandlerService); +/** + * Get cron handler instance. + * + * @returns Cron handler. + */ +export function getCronHandlerInstance(): CoreCronHandler { + const lazyHandler = asyncInstance< + AddonModWikiSyncCronHandlerLazyService, + AddonModWikiSyncCronHandlerService + >(async () => { + const { AddonModWikiSyncCronHandler } = await import('./sync-cron-lazy'); + + return AddonModWikiSyncCronHandler.instance; + }); + + lazyHandler.setEagerInstance(new AddonModWikiSyncCronHandlerService()); + lazyHandler.setLazyMethods(['execute', 'getInterval']); + + return lazyHandler; +} diff --git a/src/addons/mod/wiki/wiki.module.ts b/src/addons/mod/wiki/wiki.module.ts index de4817400..db9f97f8f 100644 --- a/src/addons/mod/wiki/wiki.module.ts +++ b/src/addons/mod/wiki/wiki.module.ts @@ -23,14 +23,14 @@ import { CoreTagAreaDelegate } from '@features/tag/services/tag-area-delegate'; import { CoreCronDelegate } from '@services/cron'; import { CORE_SITE_SCHEMAS } from '@services/sites'; import { OFFLINE_SITE_SCHEMA } from './services/database/wiki'; -import { AddonModWikiCreateLinkHandler } from './services/handlers/create-link'; -import { AddonModWikiEditLinkHandler } from './services/handlers/edit-link'; +import { getCreateLinkHandlerInstance } from './services/handlers/create-link'; +import { getEditLinkHandlerInstance } from './services/handlers/edit-link'; import { AddonModWikiIndexLinkHandler } from './services/handlers/index-link'; import { AddonModWikiListLinkHandler } from './services/handlers/list-link'; import { AddonModWikiModuleHandler } from './services/handlers/module'; -import { AddonModWikiPageOrMapLinkHandler } from './services/handlers/page-or-map-link'; -import { AddonModWikiPrefetchHandler } from './services/handlers/prefetch'; -import { AddonModWikiSyncCronHandler } from './services/handlers/sync-cron'; +import { getPageOrMapLinkHandlerInstance } from './services/handlers/page-or-map-link'; +import { getPrefetchHandlerInstance } from './services/handlers/prefetch'; +import { getCronHandlerInstance } from './services/handlers/sync-cron'; import { AddonModWikiTagAreaHandler } from './services/handlers/tag-area'; import { ADDON_MOD_WIKI_COMPONENT, ADDON_MOD_WIKI_PAGE_NAME } from './constants'; @@ -55,14 +55,15 @@ const routes: Routes = [ provide: APP_INITIALIZER, multi: true, useValue: () => { + CoreContentLinksDelegate.registerHandler(getCreateLinkHandlerInstance()); + CoreContentLinksDelegate.registerHandler(getEditLinkHandlerInstance()); + CoreContentLinksDelegate.registerHandler(getPageOrMapLinkHandlerInstance()); + CoreCourseModulePrefetchDelegate.registerHandler(getPrefetchHandlerInstance()); + CoreCronDelegate.register(getCronHandlerInstance()); + CoreCourseModuleDelegate.registerHandler(AddonModWikiModuleHandler.instance); - CoreCourseModulePrefetchDelegate.registerHandler(AddonModWikiPrefetchHandler.instance); - CoreCronDelegate.register(AddonModWikiSyncCronHandler.instance); CoreContentLinksDelegate.registerHandler(AddonModWikiIndexLinkHandler.instance); CoreContentLinksDelegate.registerHandler(AddonModWikiListLinkHandler.instance); - CoreContentLinksDelegate.registerHandler(AddonModWikiCreateLinkHandler.instance); - CoreContentLinksDelegate.registerHandler(AddonModWikiEditLinkHandler.instance); - CoreContentLinksDelegate.registerHandler(AddonModWikiPageOrMapLinkHandler.instance); CoreTagAreaDelegate.registerHandler(AddonModWikiTagAreaHandler.instance); CoreCourseHelper.registerModuleReminderClick(ADDON_MOD_WIKI_COMPONENT);