-
Notifications
You must be signed in to change notification settings - Fork 31
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 #75 from xwp/update/reuse-downloader-instances
Video downloader registry. Refactor params passing in the router.
- Loading branch information
Showing
10 changed files
with
219 additions
and
65 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
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,38 @@ | ||
import VideoDownloader from '../components/VideoDownloader'; | ||
|
||
/** | ||
* The `VideoDownloader` web component contains a download method that is potentially | ||
* long running. This registry allows different pages to share a single `VideoDownloader` | ||
* instance per videoId. | ||
* | ||
* This helps maintain the component's state even across page loads. | ||
*/ | ||
export default class VideoDownloaderRegistry { | ||
constructor() { | ||
this.instances = {}; | ||
} | ||
|
||
/** | ||
* Creates a new `VideoDownloader` instance for the given `videoId`. | ||
* | ||
* @param {string} videoId Video ID. | ||
* | ||
* @returns {VideoDownloader} Instantiated VideoDownloader. | ||
*/ | ||
create(videoId) { | ||
this.instances[videoId] = new VideoDownloader(); | ||
|
||
return this.instances[videoId]; | ||
} | ||
|
||
/** | ||
* Returns a previously instantiated VideoDownloader. | ||
* | ||
* @param {string} videoId Get the `VideoDownloader` instance for this video id. | ||
* | ||
* @returns {VideoDownloader|null} `VideoDownloader` instance. | ||
*/ | ||
get(videoId) { | ||
return this.instances[videoId] || null; | ||
} | ||
} |
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 |
---|---|---|
@@ -1,20 +1,32 @@ | ||
import appendVideoToGallery from '../utils/appendVideoToGallery'; | ||
import getPoster from './partials/Poster.partial'; | ||
|
||
export default ({ mainContent, videoDataArray, navigate }) => { | ||
if (videoDataArray[0]) { | ||
const mainVideoData = videoDataArray[0] || {}; | ||
/** | ||
* @param {RouterContext} routerContext Context object passed by the Router. | ||
*/ | ||
export default (routerContext) => { | ||
const { mainContent, apiData } = routerContext; | ||
if (apiData[0]) { | ||
const mainVideoData = apiData[0] || {}; | ||
mainContent.appendChild(getPoster(mainVideoData)); | ||
} | ||
|
||
const videosByCategories = videoDataArray.reduce((acc, videoData) => { | ||
const videosByCategories = apiData.reduce((acc, videoData) => { | ||
videoData.categories.forEach((category) => { | ||
if (!(category in acc)) acc[category] = []; | ||
acc[category].push(videoData); | ||
}); | ||
return acc; | ||
}, {}); | ||
|
||
Object.keys(videosByCategories).forEach((category, index) => { | ||
appendVideoToGallery(videosByCategories[category], navigate, category, mainContent, index); | ||
const localContext = { | ||
category, | ||
index, | ||
}; | ||
appendVideoToGallery({ | ||
...routerContext, | ||
apiData: videosByCategories[category], | ||
}, localContext); | ||
}); | ||
}; |
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
Oops, something went wrong.