forked from EVOgeek/Vmeda.Online
		
	MOBILE-3817 core: Create WSObservable type
This commit is contained in:
		
							parent
							
								
									979e995166
								
							
						
					
					
						commit
						52a4322f0d
					
				| @ -17,12 +17,11 @@ import { CoreLogger } from '@singletons/logger'; | ||||
| import { CoreSites, CoreSitesCommonWSOptions } from '@services/sites'; | ||||
| import { CoreUtils } from '@services/utils/utils'; | ||||
| import { CoreCourses } from '@features/courses/services/courses'; | ||||
| import { CoreSite, CoreSiteWSPreSets } from '@classes/site'; | ||||
| import { CoreSite, CoreSiteWSPreSets, WSObservable } from '@classes/site'; | ||||
| import { CoreStatusWithWarningsWSResponse, CoreWSExternalWarning } from '@services/ws'; | ||||
| import { makeSingleton } from '@singletons'; | ||||
| import { CoreError } from '@classes/errors/error'; | ||||
| import { asyncObservable, firstValueFrom } from '@/core/utils/rxjs'; | ||||
| import { Observable } from 'rxjs'; | ||||
| import { map } from 'rxjs/operators'; | ||||
| 
 | ||||
| const ROOT_CACHE_KEY = 'mmaCourseCompletion:'; | ||||
| @ -119,7 +118,7 @@ export class AddonCourseCompletionProvider { | ||||
|     getCompletionObservable( | ||||
|         courseId: number, | ||||
|         options: AddonCourseCompletionGetCompletionOptions = {}, | ||||
|     ): Observable<AddonCourseCompletionCourseCompletionStatus> { | ||||
|     ): WSObservable<AddonCourseCompletionCourseCompletionStatus> { | ||||
|         return asyncObservable(async () => { | ||||
|             const site = await CoreSites.getSite(options.siteId); | ||||
| 
 | ||||
|  | ||||
| @ -125,7 +125,7 @@ export class CoreSite { | ||||
|     protected lastAutoLogin = 0; | ||||
|     protected offlineDisabled = false; | ||||
|     // eslint-disable-next-line @typescript-eslint/no-explicit-any
 | ||||
|     protected ongoingRequests: { [cacheId: string]: Observable<any> } = {}; | ||||
|     protected ongoingRequests: { [cacheId: string]: WSObservable<any> } = {}; | ||||
|     protected requestQueue: RequestQueueItem[] = []; | ||||
|     protected requestQueueTimeout: number | null = null; | ||||
|     protected tokenPluginFileWorks?: boolean; | ||||
| @ -504,10 +504,10 @@ export class CoreSite { | ||||
|      * @param method WS method to use. | ||||
|      * @param data Data to send to the WS. | ||||
|      * @param preSets Extra options. | ||||
|      * @return Observable. | ||||
|      * @return Observable returning the WS data. | ||||
|      */ | ||||
|     // eslint-disable-next-line @typescript-eslint/no-explicit-any
 | ||||
|     readObservable<T = unknown>(method: string, data: any, preSets?: CoreSiteWSPreSets): Observable<T> { | ||||
|     readObservable<T = unknown>(method: string, data: any, preSets?: CoreSiteWSPreSets): WSObservable<T> { | ||||
|         preSets = preSets || {}; | ||||
|         preSets.getFromCache = preSets.getFromCache ?? true; | ||||
|         preSets.saveToCache = preSets.saveToCache ?? true; | ||||
| @ -535,10 +535,10 @@ export class CoreSite { | ||||
|      * @param method WS method to use. | ||||
|      * @param data Data to send to the WS. | ||||
|      * @param preSets Extra options. | ||||
|      * @return Observable. | ||||
|      * @return Observable returning the WS data. | ||||
|      */ | ||||
|     // eslint-disable-next-line @typescript-eslint/no-explicit-any
 | ||||
|     writeObservable<T = unknown>(method: string, data: any, preSets?: CoreSiteWSPreSets): Observable<T> { | ||||
|     writeObservable<T = unknown>(method: string, data: any, preSets?: CoreSiteWSPreSets): WSObservable<T> { | ||||
|         preSets = preSets || {}; | ||||
|         preSets.getFromCache = preSets.getFromCache ?? false; | ||||
|         preSets.saveToCache = preSets.saveToCache ?? false; | ||||
| @ -566,7 +566,7 @@ export class CoreSite { | ||||
|      * @param method The WebService method to be called. | ||||
|      * @param data Arguments to pass to the method. | ||||
|      * @param preSets Extra options. | ||||
|      * @return Observable | ||||
|      * @return Observable returning the WS data. | ||||
|      * @description | ||||
|      * | ||||
|      * Sends a webservice request to the site. This method will automatically add the | ||||
| @ -576,7 +576,7 @@ export class CoreSite { | ||||
|      * data hasn't expired. | ||||
|      */ | ||||
|     // eslint-disable-next-line @typescript-eslint/no-explicit-any
 | ||||
|     requestObservable<T = unknown>(method: string, data: any, preSets: CoreSiteWSPreSets): Observable<T> { | ||||
|     requestObservable<T = unknown>(method: string, data: any, preSets: CoreSiteWSPreSets): WSObservable<T> { | ||||
|         if (this.isLoggedOut() && !ALLOWED_LOGGEDOUT_WS.includes(method)) { | ||||
|             // Site is logged out, it cannot call WebServices.
 | ||||
|             CoreEvents.trigger(CoreEvents.SESSION_EXPIRED, {}, this.id); | ||||
| @ -665,14 +665,14 @@ export class CoreSite { | ||||
|      * @param data Arguments to pass to the method. | ||||
|      * @param preSets Extra options related to the site. | ||||
|      * @param wsPreSets Extra options related to the WS call. | ||||
|      * @return Observable. | ||||
|      * @return Observable returning the WS data. | ||||
|      */ | ||||
|     protected performRequest<T = unknown>( | ||||
|         method: string, | ||||
|         data: unknown, | ||||
|         preSets: CoreSiteWSPreSets, | ||||
|         wsPreSets: CoreWSPreSets, | ||||
|     ): Observable<T> { | ||||
|     ): WSObservable<T> { | ||||
|         const subject = new Subject<T>(); | ||||
| 
 | ||||
|         const run = async () => { | ||||
| @ -1903,9 +1903,9 @@ export class CoreSite { | ||||
|      * @param readingStrategy Reading strategy. | ||||
|      * @return Observable returning site config. | ||||
|      */ | ||||
|     getConfigObservable(name?: undefined, readingStrategy?: CoreSitesReadingStrategy): Observable<CoreSiteConfig>; | ||||
|     getConfigObservable(name: string, readingStrategy?: CoreSitesReadingStrategy): Observable<string>; | ||||
|     getConfigObservable(name?: string, readingStrategy?: CoreSitesReadingStrategy): Observable<string | CoreSiteConfig> { | ||||
|     getConfigObservable(name?: undefined, readingStrategy?: CoreSitesReadingStrategy): WSObservable<CoreSiteConfig>; | ||||
|     getConfigObservable(name: string, readingStrategy?: CoreSitesReadingStrategy): WSObservable<string>; | ||||
|     getConfigObservable(name?: string, readingStrategy?: CoreSitesReadingStrategy): WSObservable<string | CoreSiteConfig> { | ||||
|         const preSets: CoreSiteWSPreSets = { | ||||
|             cacheKey: this.getConfigCacheKey(), | ||||
|             ...CoreSites.getReadingStrategyPreSets(readingStrategy), | ||||
| @ -2403,7 +2403,7 @@ export function chainRequests<T, O extends ObservableInput<any>>( | ||||
|     readingStrategy: CoreSitesReadingStrategy | undefined, | ||||
|     callback: (data: T, readingStrategy?: CoreSitesReadingStrategy) => O, | ||||
| ): OperatorFunction<T, ObservedValueOf<O>> { | ||||
|     return (source: Observable<T>) => new Observable<{ data: T; readingStrategy?: CoreSitesReadingStrategy }>(subscriber => { | ||||
|     return (source: WSObservable<T>) => new Observable<{ data: T; readingStrategy?: CoreSitesReadingStrategy }>(subscriber => { | ||||
|         let firstValue = true; | ||||
|         let isCompleted = false; | ||||
| 
 | ||||
|  | ||||
| @ -21,7 +21,7 @@ import { CoreLogger } from '@singletons/logger'; | ||||
| import { CoreSitesCommonWSOptions, CoreSites, CoreSitesReadingStrategy } from '@services/sites'; | ||||
| import { CoreTimeUtils } from '@services/utils/time'; | ||||
| import { CoreUtils } from '@services/utils/utils'; | ||||
| import { CoreSiteWSPreSets, CoreSite } from '@classes/site'; | ||||
| import { CoreSiteWSPreSets, CoreSite, WSObservable } from '@classes/site'; | ||||
| import { CoreConstants } from '@/core/constants'; | ||||
| import { makeSingleton, Translate } from '@singletons'; | ||||
| import { CoreStatusWithWarningsWSResponse, CoreWSExternalFile, CoreWSExternalWarning } from '@services/ws'; | ||||
| @ -54,7 +54,6 @@ import { CoreDatabaseCachingStrategy } from '@classes/database/database-table-pr | ||||
| import { SQLiteDB } from '@classes/sqlitedb'; | ||||
| import { CorePlatform } from '@services/platform'; | ||||
| import { CoreTime } from '@singletons/time'; | ||||
| import { Observable } from 'rxjs'; | ||||
| import { asyncObservable, firstValueFrom } from '@/core/utils/rxjs'; | ||||
| import { map } from 'rxjs/operators'; | ||||
| 
 | ||||
| @ -417,7 +416,7 @@ export class CoreCourseProvider { | ||||
|      * @return Observable that returns the blocks. | ||||
|      * @since 3.7 | ||||
|      */ | ||||
|     getCourseBlocksObservable(courseId: number, options: CoreSitesCommonWSOptions = {}): Observable<CoreCourseBlock[]> { | ||||
|     getCourseBlocksObservable(courseId: number, options: CoreSitesCommonWSOptions = {}): WSObservable<CoreCourseBlock[]> { | ||||
|         return asyncObservable(async () => { | ||||
|             const site = await CoreSites.getSite(options.siteId); | ||||
| 
 | ||||
| @ -955,7 +954,7 @@ export class CoreCourseProvider { | ||||
|     getSectionsObservable( | ||||
|         courseId: number, | ||||
|         options: CoreCourseGetSectionsOptions = {}, | ||||
|     ): Observable<CoreCourseWSSection[]> { | ||||
|     ): WSObservable<CoreCourseWSSection[]> { | ||||
|         options.includeStealthModules = options.includeStealthModules ?? true; | ||||
| 
 | ||||
|         return asyncObservable(async () => { | ||||
|  | ||||
| @ -26,10 +26,10 @@ import { makeSingleton, Translate } from '@singletons'; | ||||
| import { CoreWSExternalFile } from '@services/ws'; | ||||
| import { AddonCourseCompletion } from '@addons/coursecompletion/services/coursecompletion'; | ||||
| import moment from 'moment-timezone'; | ||||
| import { Observable, of } from 'rxjs'; | ||||
| import { of } from 'rxjs'; | ||||
| import { firstValueFrom, zipIncludingComplete } from '@/core/utils/rxjs'; | ||||
| import { catchError, map } from 'rxjs/operators'; | ||||
| import { chainRequests } from '@classes/site'; | ||||
| import { chainRequests, WSObservable } from '@classes/site'; | ||||
| 
 | ||||
| /** | ||||
|  * Helper to gather some common courses functions. | ||||
| @ -134,7 +134,7 @@ export class CoreCoursesHelperProvider { | ||||
|         courses: CoreEnrolledCourseDataWithExtraInfo[], | ||||
|         loadCategoryNames: boolean = false, | ||||
|         options: CoreSitesCommonWSOptions = {}, | ||||
|     ): Observable<CoreEnrolledCourseDataWithExtraInfo[]> { | ||||
|     ): WSObservable<CoreEnrolledCourseDataWithExtraInfo[]> { | ||||
|         if (!courses.length) { | ||||
|             return of([]); | ||||
|         } | ||||
| @ -245,7 +245,7 @@ export class CoreCoursesHelperProvider { | ||||
|      */ | ||||
|     getUserCoursesWithOptionsObservable( | ||||
|         options: CoreCoursesGetWithOptionsOptions = {}, | ||||
|     ): Observable<CoreEnrolledCourseDataWithExtraInfoAndOptions[]> { | ||||
|     ): WSObservable<CoreEnrolledCourseDataWithExtraInfoAndOptions[]> { | ||||
| 
 | ||||
|         return CoreCourses.getUserCoursesObservable(options).pipe( | ||||
|             chainRequests(options.readingStrategy, (courses, newReadingStrategy) => { | ||||
| @ -338,7 +338,7 @@ export class CoreCoursesHelperProvider { | ||||
|     protected loadCourseCompletedStatus( | ||||
|         course: CoreEnrolledCourseDataWithExtraInfo, | ||||
|         options: CoreSitesCommonWSOptions = {}, | ||||
|     ): Observable<CoreEnrolledCourseDataWithExtraInfo> { | ||||
|     ): WSObservable<CoreEnrolledCourseDataWithExtraInfo> { | ||||
|         if (course.completed !== undefined) { | ||||
|             // The WebService already returns the completed status, no need to fetch it.
 | ||||
|             return of(course); | ||||
|  | ||||
| @ -14,14 +14,14 @@ | ||||
| 
 | ||||
| import { Injectable } from '@angular/core'; | ||||
| import { CoreSites, CoreSitesCommonWSOptions, CoreSitesReadingStrategy } from '@services/sites'; | ||||
| import { CoreSite, CoreSiteWSPreSets } from '@classes/site'; | ||||
| import { CoreSite, CoreSiteWSPreSets, WSObservable } from '@classes/site'; | ||||
| import { makeSingleton } from '@singletons'; | ||||
| import { CoreStatusWithWarningsWSResponse, CoreWarningsWSResponse, CoreWSExternalFile, CoreWSExternalWarning } from '@services/ws'; | ||||
| import { CoreEvents } from '@singletons/events'; | ||||
| import { CoreWSError } from '@classes/errors/wserror'; | ||||
| import { CoreCourseAnyCourseDataWithExtraInfoAndOptions, CoreCourseWithImageAndColor } from './courses-helper'; | ||||
| import { asyncObservable, firstValueFrom, ignoreErrors, zipIncludingComplete } from '@/core/utils/rxjs'; | ||||
| import { of, Observable } from 'rxjs'; | ||||
| import { of } from 'rxjs'; | ||||
| import { map } from 'rxjs/operators'; | ||||
| 
 | ||||
| const ROOT_CACHE_KEY = 'mmCourses:'; | ||||
| @ -510,7 +510,7 @@ export class CoreCoursesProvider { | ||||
|         field: string = '', | ||||
|         value: string | number = '', | ||||
|         options: CoreSitesCommonWSOptions = {}, | ||||
|     ): Observable<CoreCourseSearchedData[]> { | ||||
|     ): WSObservable<CoreCourseSearchedData[]> { | ||||
|         return asyncObservable(async () => { | ||||
|             const siteId = options.siteId || CoreSites.getCurrentSiteId(); | ||||
|             const originalValue = value; | ||||
| @ -617,7 +617,7 @@ export class CoreCoursesProvider { | ||||
|         customFieldName: string, | ||||
|         customFieldValue: string, | ||||
|         options: CoreSitesCommonWSOptions, | ||||
|     ): Observable<CoreCourseSummaryData[]> { | ||||
|     ): WSObservable<CoreCourseSummaryData[]> { | ||||
|         return asyncObservable(async () => { | ||||
|             const site = await CoreSites.getSite(options. siteId); | ||||
| 
 | ||||
| @ -685,7 +685,7 @@ export class CoreCoursesProvider { | ||||
|     getCoursesAdminAndNavOptionsObservable( | ||||
|         courseIds: number[], | ||||
|         options: CoreSitesCommonWSOptions = {}, | ||||
|     ): Observable<{ | ||||
|     ): WSObservable<{ | ||||
|             navOptions: CoreCourseUserAdminOrNavOptionCourseIndexed; | ||||
|             admOptions: CoreCourseUserAdminOrNavOptionCourseIndexed; | ||||
|         }> { | ||||
| @ -780,7 +780,7 @@ export class CoreCoursesProvider { | ||||
|     getUserAdministrationOptionsObservable( | ||||
|         courseIds: number[], | ||||
|         options: CoreSitesCommonWSOptions = {}, | ||||
|     ): Observable<CoreCourseUserAdminOrNavOptionCourseIndexed> { | ||||
|     ): WSObservable<CoreCourseUserAdminOrNavOptionCourseIndexed> { | ||||
|         if (!courseIds || courseIds.length == 0) { | ||||
|             return of({}); | ||||
|         } | ||||
| @ -847,7 +847,7 @@ export class CoreCoursesProvider { | ||||
|     getUserNavigationOptionsObservable( | ||||
|         courseIds: number[], | ||||
|         options: CoreSitesCommonWSOptions = {}, | ||||
|     ): Observable<CoreCourseUserAdminOrNavOptionCourseIndexed> { | ||||
|     ): WSObservable<CoreCourseUserAdminOrNavOptionCourseIndexed> { | ||||
|         if (!courseIds || courseIds.length == 0) { | ||||
|             return of({}); | ||||
|         } | ||||
| @ -939,10 +939,10 @@ export class CoreCoursesProvider { | ||||
|     ): Promise<CoreEnrolledCourseData[]> { | ||||
|         strategy = strategy ?? (preferCache ? CoreSitesReadingStrategy.PREFER_CACHE : undefined); | ||||
| 
 | ||||
|         return this.getUserCoursesObservable({ | ||||
|         return firstValueFrom(this.getUserCoursesObservable({ | ||||
|             readingStrategy: strategy, | ||||
|             siteId, | ||||
|         }).toPromise(); | ||||
|         })); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
| @ -951,7 +951,7 @@ export class CoreCoursesProvider { | ||||
|      * @param options Options. | ||||
|      * @return Observable that returns the courses. | ||||
|      */ | ||||
|     getUserCoursesObservable(options: CoreSitesCommonWSOptions = {}): Observable<CoreEnrolledCourseData[]> { | ||||
|     getUserCoursesObservable(options: CoreSitesCommonWSOptions = {}): WSObservable<CoreEnrolledCourseData[]> { | ||||
|         return asyncObservable(async () => { | ||||
|             const site = await CoreSites.getSite(options.siteId); | ||||
| 
 | ||||
|  | ||||
| @ -14,12 +14,11 @@ | ||||
| 
 | ||||
| import { Injectable } from '@angular/core'; | ||||
| import { CoreSites, CoreSitesCommonWSOptions } from '@services/sites'; | ||||
| import { CoreSite, CoreSiteWSPreSets } from '@classes/site'; | ||||
| import { CoreSite, CoreSiteWSPreSets, WSObservable } from '@classes/site'; | ||||
| import { CoreCourseBlock } from '@features/course/services/course'; | ||||
| import { CoreStatusWithWarningsWSResponse } from '@services/ws'; | ||||
| import { makeSingleton } from '@singletons'; | ||||
| import { CoreError } from '@classes/errors/error'; | ||||
| import { Observable } from 'rxjs'; | ||||
| import { map } from 'rxjs/operators'; | ||||
| import { asyncObservable, firstValueFrom } from '@/core/utils/rxjs'; | ||||
| 
 | ||||
| @ -73,7 +72,7 @@ export class CoreCoursesDashboardProvider { | ||||
|      * @return Observable that returns the list of blocks. | ||||
|      * @since 3.6 | ||||
|      */ | ||||
|     getDashboardBlocksFromWSObservable(options: GetDashboardBlocksOptions = {}): Observable<CoreCourseBlock[]> { | ||||
|     getDashboardBlocksFromWSObservable(options: GetDashboardBlocksOptions = {}): WSObservable<CoreCourseBlock[]> { | ||||
|         return asyncObservable(async () => { | ||||
|             const site = await CoreSites.getSite(options.siteId); | ||||
| 
 | ||||
| @ -142,7 +141,7 @@ export class CoreCoursesDashboardProvider { | ||||
|      * @param options Options. | ||||
|      * @return observable that returns the list of blocks. | ||||
|      */ | ||||
|     getDashboardBlocksObservable(options: GetDashboardBlocksOptions = {}): Observable<CoreCoursesDashboardBlocks> { | ||||
|     getDashboardBlocksObservable(options: GetDashboardBlocksOptions = {}): WSObservable<CoreCoursesDashboardBlocks> { | ||||
|         return this.getDashboardBlocksFromWSObservable(options).pipe(map(blocks => { | ||||
|             let mainBlocks: CoreCourseBlock[] = []; | ||||
|             let sideBlocks: CoreCourseBlock[] = []; | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user