Merge branch 'better-documents'

This commit is contained in:
Youwen Wu 2024-02-15 02:43:59 -08:00
commit 76decf8237
3 changed files with 25 additions and 11 deletions

View file

@ -23,10 +23,18 @@ export default function News() {
</div> </div>
<ul className='text-slate-50 px-6 list-disc'> <ul className='text-slate-50 px-6 list-disc'>
<li key={1}> <li key={1}>
eeXiv 2.1 has been released! Documents are now statically generated eeXiv 2.2 has been released! You can now select document version and
for instant loading speeds.{' '} export as BibTex.{' '}
</li>
<li key={2}>
We are working on becoming a{' '}
<a
href='https://www.doi.org/the-foundation/about-us/'
target='_blank'
>
ISO 26324 DOI registry!
</a>
</li> </li>
<li key={2}>Mobile support is currently in beta.</li>
<li key={3}> <li key={3}>
eeXiv is currently under active development! There may be major eeXiv is currently under active development! There may be major
updates, breaking changes, or weird bugs. Report bugs, suggest new updates, breaking changes, or weird bugs. Report bugs, suggest new

View file

@ -1,5 +1,11 @@
import { Document, Author, Affiliation, Topic, Nationality } from './data' import { Document, Author, Affiliation, Topic, Nationality } from './data'
/**
* Loads a document with the given ID using a web worker if available, and returns a promise that resolves with the document.
*
* @param {string} id - The ID of the document to load
* @return {Promise<Document>} A promise that resolves with the loaded document, or rejects with an error
*/
export const loadDocument = (id: string): Promise<Document> => { export const loadDocument = (id: string): Promise<Document> => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (typeof Worker !== 'undefined') { if (typeof Worker !== 'undefined') {
@ -8,13 +14,12 @@ export const loadDocument = (id: string): Promise<Document> => {
{ type: 'module' } { type: 'module' }
) )
worker.onmessage = (e: MessageEvent<{ [key: string]: Document }>) => { worker.onmessage = (e: MessageEvent<Document | undefined>) => {
const data = e.data const data = e.data
const doc: Document | undefined = data[id] if (!data) {
if (!doc) {
return reject(new Error('404')) return reject(new Error('404'))
} else { } else {
resolve(doc) resolve(data)
} }
worker.terminate() worker.terminate()
} }
@ -24,7 +29,7 @@ export const loadDocument = (id: string): Promise<Document> => {
worker.terminate() worker.terminate()
} }
worker.postMessage('LOAD') worker.postMessage(id)
} else { } else {
reject( reject(
new Error( new Error(
@ -34,6 +39,9 @@ export const loadDocument = (id: string): Promise<Document> => {
} }
}) })
} }
/**
* @deprecated This function doesn't improve efficiency and shouldn't be used
*/
export const loadAllDocuments = (): Promise<{ [key: string]: Document }> => { export const loadAllDocuments = (): Promise<{ [key: string]: Document }> => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (typeof Worker !== 'undefined') { if (typeof Worker !== 'undefined') {

View file

@ -1,7 +1,5 @@
import { documents } from '../data' import { documents } from '../data'
onmessage = (e) => { onmessage = (e) => {
if (e.data === 'LOAD') { typeof e.data === 'string' && postMessage(documents[e.data])
self.postMessage(documents)
}
} }