Skip to content

Commit b0c0739

Browse files
committed
MOBILE-3890 course: Add cache for modules
1 parent 18e177d commit b0c0739

File tree

1 file changed

+48
-26
lines changed

1 file changed

+48
-26
lines changed

src/core/features/course/services/course-force-language.ts

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,14 @@ export class CoreCourseForceLanguageService {
3737
protected lastNavigationCheck: {
3838
courseId: number;
3939
courseLang: string | undefined;
40+
cmId: number;
41+
cmLang: string | undefined;
4042
timestamp: number;
4143
}= {
4244
courseId: 0,
4345
courseLang: undefined,
46+
cmId: 0,
47+
cmLang: undefined,
4448
timestamp: 0,
4549
};
4650

@@ -81,37 +85,46 @@ export class CoreCourseForceLanguageService {
8185
protected async getForcedLanguageFromRoute(origin: string): Promise<string | undefined> {
8286
let course = CoreNavigator.getRouteParam<CoreCourseSearchedData>('course');
8387
const courseId = course?.id ?? CoreNavigator.getRouteNumberParam('courseId');
88+
8489
if (!courseId) {
8590
// Not in a course/module, empty the cache and return.
8691
this.lastNavigationCheck.courseId = 0;
8792
this.lastNavigationCheck.courseLang = undefined;
93+
this.lastNavigationCheck.cmId = 0;
94+
this.lastNavigationCheck.cmLang = undefined;
8895

8996
return;
9097
}
9198

92-
const modLang = await this.getModuleForcedLangFromRoute(courseId);
93-
if (modLang) {
94-
return modLang;
99+
if (origin === 'module') {
100+
const modLang = await this.getModuleForcedLangFromRoute(courseId);
101+
if (modLang) {
102+
return modLang;
103+
}
104+
} else {
105+
this.lastNavigationCheck.cmId = 0;
106+
this.lastNavigationCheck.cmLang = undefined;
95107
}
96108

109+
let lang: string | undefined = undefined;
97110
if (this.lastNavigationCheck.courseId === courseId) {
98-
return this.lastNavigationCheck.courseLang;
99-
}
100-
this.lastNavigationCheck.courseId = courseId;
101-
102-
if (course?.lang !== undefined) {
103-
this.lastNavigationCheck.courseLang = course.lang;
111+
console.error('Using cached course language', this.lastNavigationCheck.courseLang);
112+
113+
lang = this.lastNavigationCheck.courseLang;
114+
} else if (course?.lang !== undefined) {
115+
lang = course.lang;
116+
} else {
117+
course = await CorePromiseUtils.ignoreErrors(
118+
CoreCourses.getCourseByField('id', courseId, { readingStrategy: CoreSitesReadingStrategy.PREFER_CACHE }),
119+
);
104120

105-
return course.lang;
121+
lang = course?.lang;
106122
}
107123

108-
course = await CorePromiseUtils.ignoreErrors(
109-
CoreCourses.getCourseByField('id', courseId, { readingStrategy: CoreSitesReadingStrategy.PREFER_CACHE }),
110-
);
111-
112-
this.lastNavigationCheck.courseLang = course?.lang;
124+
this.lastNavigationCheck.courseId = courseId;
125+
this.lastNavigationCheck.courseLang = lang;
113126

114-
return course?.lang;
127+
return lang;
115128
}
116129

117130
/**
@@ -122,21 +135,30 @@ export class CoreCourseForceLanguageService {
122135
*/
123136
protected async getModuleForcedLangFromRoute(courseId: number): Promise<string | undefined> {
124137
const cmId = CoreNavigator.getRouteNumberParam('cmId');
138+
let lang: string | undefined = undefined;
125139
if (cmId) {
126-
// TODO: In the future this should directly return the module language instead of checking the delegate.
127-
const module = await CorePromiseUtils.ignoreErrors(
128-
CoreCourse.getModule(cmId, courseId, undefined, true),
129-
);
130-
131-
if (module) {
132-
const lang = await CorePromiseUtils.ignoreErrors(
133-
CoreCourseModuleDelegate.getModuleForcedLang(module),
140+
if (this.lastNavigationCheck.cmId === cmId) {
141+
console.error('Using cached cm language', this.lastNavigationCheck.cmLang);
142+
143+
lang = this.lastNavigationCheck.cmLang;
144+
} else {
145+
// TODO: In the future this should directly return the module language instead of checking the delegate.
146+
const module = await CorePromiseUtils.ignoreErrors(
147+
CoreCourse.getModule(cmId, courseId, undefined, true),
134148
);
135-
if (lang) {
136-
return lang;
149+
150+
if (module) {
151+
lang = await CorePromiseUtils.ignoreErrors(
152+
CoreCourseModuleDelegate.getModuleForcedLang(module),
153+
);
137154
}
138155
}
139156
}
157+
158+
this.lastNavigationCheck.cmId = cmId ?? 0;
159+
this.lastNavigationCheck.cmLang = lang;
160+
161+
return lang;
140162
}
141163

142164
}

0 commit comments

Comments
 (0)