-
Notifications
You must be signed in to change notification settings - Fork 834
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
80 additions
and
6 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
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,66 @@ | ||
import { getImage, putImage } from "../db/images"; | ||
|
||
export class LocalImageManager { | ||
static instance: LocalImageManager; | ||
queue: { image: string; timestamp: number; fullImage?: boolean }[] = []; | ||
|
||
private constructor() { | ||
this.process(); | ||
} | ||
|
||
async process() { | ||
this.queue = this.queue | ||
.filter((x) => (new Date().getTime() - x.timestamp) / 1000 > 3600) | ||
.sort((a, b) => (a.timestamp < b.timestamp ? 1 : -1)); | ||
const nextEl = this.queue.pop(); | ||
if (nextEl) { | ||
await this.storeImageInLocalStorage(nextEl.image, nextEl.fullImage); | ||
} | ||
await this.sleep(15); | ||
this.process(); | ||
} | ||
|
||
async sleep(timer) { | ||
await new Promise((resolve) => setTimeout(resolve, timer * 1000)); | ||
} | ||
|
||
bulkAddToQueue(elements: { image: string }[]) { | ||
elements.forEach((el) => this.addToQueue(el)); | ||
} | ||
|
||
async addToQueue({ image }: { image: string }) { | ||
if (this.queue.find((x) => x.image === image)) { | ||
return; | ||
} | ||
|
||
try { | ||
const parsedEl = await getImage("images", `image-${image}`); | ||
if (parsedEl) { | ||
this.queue.push({ | ||
image, | ||
timestamp: parsedEl.timestamp, | ||
fullImage: parsedEl.fullImage || false, | ||
}); | ||
} else { | ||
this.queue.push({ image, timestamp: 0 }); | ||
} | ||
} catch (e) { | ||
this.queue.push({ image, timestamp: 0 }); | ||
} | ||
} | ||
|
||
static getInstance() { | ||
if (!this.instance) { | ||
this.instance = new LocalImageManager(); | ||
} | ||
return this.instance; | ||
} | ||
|
||
storeImageInLocalStorage( | ||
url: string, | ||
fullImage?: boolean, | ||
overridenUrl?: string | ||
) { | ||
return new Promise((resolve, reject) => {}); | ||
} | ||
} |
c184e9d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
backpack – ./
backpack-200ms.vercel.app
backpack-git-master-200ms.vercel.app
devnet.backpack.app
www.backpack.app
backpack.app