-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathofflineOnline.js
91 lines (83 loc) · 2.89 KB
/
offlineOnline.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/**
* File for functions that can either load data from offline library or API, depending on which one exists
* Prefers offline library data
*/
import {ERRORS} from "./errors";
import {
loadTextTocOffline,
loadTextOffline,
loadLinksOffline,
getOfflineVersionObjectsAvailable,
loadOfflineSectionMetadataCompat, getAllTranslationsOffline
} from "./offline";
import api from "./api";
export const loadText = function(ref, context, versions, fallbackOnDefaultVersions=true) {
/**
if `context`, only return section no matter what. default is true
versions is object with keys { en, he } specifying version titles of requested ref
*/
if (typeof context === "undefined") { context = true; }
return loadTextOffline(ref, context, versions, fallbackOnDefaultVersions)
.then((result) => {
if (result?.missingLangs?.length) {
throw ERRORS.MISSING_OFFLINE_DATA;
}
return result;
})
.catch(error => {
if (error === ERRORS.MISSING_OFFLINE_DATA) {
return api.textApi(ref, context, versions)
.then(data => {
api.processTextApiData(ref, context, versions, data);
return data;
})
}
console.error("Error loading offline file", error);
return Promise.reject(error);
})
.catch((error) => {
return Promise.reject(error);
})
};
export const loadVersions = async (ref) => {
let versions = getOfflineVersionObjectsAvailable(ref);
if (!versions) {
versions = await api.versions(ref, true);
}
return versions;
};
export const loadTranslations = async (ref) => {
const offlineTranslations = await getAllTranslationsOffline(ref);
let translations = offlineTranslations?.translations || [];
if (!offlineTranslations || offlineTranslations.missingVersions.length) {
translations = await api.translations(ref);
}
return translations;
}
export const loadRelated = async function(ref, online) {
const cached = api._related[ref];
if (!!cached) { return cached; }
const loader = online ? api.related : loadLinksOffline;
const response = await loader(ref);
api._related[ref] = response;
return response;
};
const _textToc = {};
export const textToc = function(title) {
return new Promise((resolve, reject) => {
const resolver = function(data) {
_textToc[title] = data;
Sefaria.cacheVersionObjectByTitle(data.versions, title);
resolve(data);
};
if (title in _textToc) {
resolve(_textToc[title]);
} else {
loadTextTocOffline(title)
.then(resolver)
.catch(()=>{
api._request(title, 'index', true, {}).then(resolver)
});
}
});
};