async function getImageUrls(): Promise {
// read images
}
function getCurrentImageUrl(): string | null {
return document.querySelector('#domNodeId').getAttribute('src');
}
function addToSlider(imageUrls: string[]): void {
// ...
}
function moveSliderToImageUrl(imageUrl: string): void {
// ...
}
let imageUrls = await getImageUrls();
addToSlider(imageUrls);
const currentImageUrl = getCurrentImageUrl();
if (currentImageUrl && !imageUrls.includes(currentImageUrl))
imageUrls = [currentImageUrl].imageUrls
else if(currentImageUrl)
moveSliderToImageUrl(currentImageUrl);
getImageUrls
. In the real implementation there was a lot more of code involved and this issue wasn't obvious anymore, because when i wrote the line addToSlider(imageUrls);
i assumed imageUrls
already contains all urls without thinking on the current image url, which is added later.ImageUrls
) instead of a simple array.
class ImageUrls{
#imageUrls: string[];
#currentImageUrl: string;
constructor(imageUrls: string[], currentImageUrl: string) {
if (currentImageUrl)
this.#imageUrls = [currentImageUrl].concat(imageUrls);
else
this.#imageUrls = imageUrls;
this.#currentImageUrl = currentImageUrl;
}
get imageUrls(): string[] {
return this.#imageUrls;
}
get currentImageUrl(): string {
return this.#currentImageUrl;
}
}
async function getImageUrls(): Promise {
// read images
}
function getCurrentImageUrl(): Promise {
return document.querySelector('#domNodeId').getAttribute('src');
}
function addToSlider(imageUrls: ImageUrls): void {
// ...
}
function moveSliderToImageUrl(imageUrl: string): void {
// ...
}
async function getInstance(): ImageUrls {
const imageUrls = await getImageUrls();
const currentImageUrl = getCurrentImageUrl();
return new ImageUrls(imageUrls, currentImageUrl);
}
const imageUrls = await getInstance();
addToSlider(imageUrls);
if (imageUrls.currentImageUrl && imageUrls.includes(currentImageUrl))
moveSliderToImageUrl(imageUrls.currentImageUrl);
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
2 | |
1 | |
1 | |
1 | |
1 | |
1 | |
1 | |
1 | |
1 |