forked from zotero/zoterobib
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This replaces zotero-api-client plugin used previously (before citeproc-rs conversion).
- Loading branch information
Showing
3 changed files
with
78 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import api from 'zotero-api-client'; | ||
|
||
// Zbib only uses API for meta requests, these rarely, if ever, change hence are cached for 24 hours | ||
// to improve performance. localStorage is used to record time of the last request, then, if cache | ||
// is fresh, we use ask browser to use `force-cache` caching strategy which will go to the disk | ||
// cache before making requests | ||
|
||
const apiCheckCache = key => { | ||
var cacheTimes = {}, okToUseCache = false; | ||
try { | ||
cacheTimes = JSON.parse(localStorage.getItem('zotero-bib-api-cache')) || {}; | ||
} catch(_) { | ||
// ignore | ||
} | ||
|
||
if(key in cacheTimes) { | ||
okToUseCache = (Date.now() - cacheTimes[key]) < 24 * 60 * 60 * 1000; | ||
} | ||
|
||
if(!okToUseCache) { | ||
cacheTimes[key] = Date.now(); | ||
localStorage.setItem('zotero-bib-api-cache', JSON.stringify(cacheTimes)); | ||
} | ||
|
||
return okToUseCache; | ||
} | ||
|
||
const getItemTypes = async (retryOnFailure = true) => { | ||
try { | ||
return (await api() | ||
.itemTypes() | ||
.get({ cache: apiCheckCache('itemTypes') ? 'force-cache' : 'default'}) | ||
).getData(); | ||
} catch(e) { | ||
localStorage.removeItem('zotero-bib-api-cache'); | ||
if(retryOnFailure) { | ||
return getItemTypes(false); | ||
} else { | ||
throw e; | ||
} | ||
} | ||
}; | ||
|
||
const getItemTypeMeta = async (itemType, retryOnFailure = true) => { | ||
try { | ||
var [itemTypeR, itemTypeFieldsR, creatorTypesR] = await Promise.all([ | ||
api() | ||
.itemTypes() | ||
.get({ cache: apiCheckCache('itemTypes') ? 'force-cache' : 'default'}), | ||
api() | ||
.itemTypeFields(itemType) | ||
.get({ cache: apiCheckCache(`itemTypeFields-${itemType}`) ? 'force-cache' : 'default'}), | ||
api() | ||
.itemTypeCreatorTypes(itemType) | ||
.get({ cache: apiCheckCache(`itemTypeCreatorTypes-${itemType}`) ? 'force-cache' : 'default'}) | ||
]); | ||
} catch(e) { | ||
localStorage.removeItem('zotero-bib-api-cache'); | ||
if(retryOnFailure) { | ||
return getItemTypeMeta(itemType, false); | ||
} else { | ||
throw e; | ||
} | ||
} | ||
|
||
return { | ||
itemTypes: itemTypeR.getData(), | ||
itemTypeFields: itemTypeFieldsR.getData(), | ||
itemTypeCreatorTypes: creatorTypesR.getData() | ||
}; | ||
}; | ||
|
||
|
||
export { getItemTypes, getItemTypeMeta } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters