-
Notifications
You must be signed in to change notification settings - Fork 248
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #274 from slytter/master
Add preloading to stories
- Loading branch information
Showing
8 changed files
with
114 additions
and
8 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 |
---|---|---|
|
@@ -2,4 +2,5 @@ | |
node_modules | ||
dist | ||
stats.json | ||
report.html | ||
report.html | ||
.idea |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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 {Story} from "../interfaces"; | ||
import {useEffect} from "react"; | ||
|
||
|
||
// Caches given Story[] using HTMLImageElement and HTMLVideoElement | ||
const cacheContent = async (contents: Story[]) => { | ||
const promises = contents.map((content) => { | ||
return new Promise(function (resolve, reject) { | ||
if(!content.url) return | ||
|
||
if(content.type === 'video') { | ||
const video = document.createElement('video'); | ||
video.src = content.url; | ||
video.onloadeddata = resolve; | ||
video.onerror = reject; | ||
return; | ||
} | ||
|
||
const img = new Image(); | ||
img.src = content.url; | ||
img.onload = resolve; | ||
img.onerror = reject; | ||
|
||
}) | ||
}) | ||
|
||
await Promise.all(promises); | ||
} | ||
|
||
// Keeps track of urls that have been loaded | ||
const urlsLoaded = new Set<string>(); | ||
|
||
// Pushes urls to urlsLoaded | ||
const markUrlsLoaded = (contents: Story[]) => { | ||
contents.forEach((content) => { | ||
urlsLoaded.add(content.url) | ||
}) | ||
} | ||
|
||
|
||
// Returns true if given Story should be preloaded | ||
const shouldPreload = (content: Story) => { | ||
if (!content.url) return false | ||
if (urlsLoaded.has(content.url)) return false | ||
if (content.preloadResource !== undefined) return content.preloadResource | ||
if (content.type === 'video') return false | ||
|
||
return true | ||
} | ||
|
||
// Preloads images and videos from given Story[] using a cursor and preloadCount | ||
// Preload count is the number of images/videos to preload after the cursor | ||
// Cursor is the current index to start preloading from | ||
export const usePreLoader = (contents: Story[], cursor: number, preloadCount: number) => { | ||
useEffect(() => { | ||
const start = cursor + 1; | ||
const end = cursor + preloadCount + 1; | ||
|
||
const toPreload = contents | ||
.slice(start, end) | ||
.filter(shouldPreload); | ||
|
||
markUrlsLoaded(toPreload) | ||
cacheContent(toPreload) | ||
}, [cursor, preloadCount, contents]) | ||
} |
d8deeb6
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:
react-insta-stories – ./
react-insta-stories-git-master-mohitk05.vercel.app
react-insta-stories.vercel.app
react-insta-stories-mohitk05.vercel.app