I have a <slide-show>
component that displays a list of images with timing, transitions etc. and emits a "finished" event when it's done.
Now I want to embed this component in another one that recurses in a tree of directories, sending a new list of images after each "finished" events. The code currently looks like this :
import { Component, Host, h, Prop, State, Event, EventEmitter } from '@stencil/core'
import * as path from 'path'
import isImage from 'is-image'
function waitForEvent(eventEmitter:EventEmitter<any>, eventType:string) {
return new Promise(function (resolve) {
eventEmitter.on(eventType, resolve)
})
}
@Component({
tag: 'slide-script',
styleUrl: 'slide-script.css',
shadow: true,
})
export class SlideScript {
@Prop() src: string
@State() images: Array<string>
@Event() next: EventEmitter<boolean>
componentWillLoad() {
this.process(this.src)
}
async process(dir: string) {
console.log(dir)
return fetch(path.join('dir', dir))
.then(response =>
response.json()
.then(data => {
this.images = data.files.filter(isImage)
this.images = this.images.map(im => path.join('img', dir, im))
// the above will start/update the slideshow
waitForEvent(this.next, "onFinished")
.then(() => {
data.subdirs.reduce(
async (prev: Promise<void>, sub: string) => {
await prev
return this.process(path.join(dir, sub))
},
Promise.resolve() // reduce initial value
)
})
})
)
}
handleFinished(e) {
console.log('finished')
this.next.emit(e)
}
render() {
return (
<Host>
<slide-show images={this.images} onFinished={(e) => this.handleFinished(e)} />
</Host>
);
}
}
the waitForEvent
function does not work as stencil's EventEmitter is not a Node EventEmitter and has no .on
method ...
How should I modify it ? or how to do it otherwise ? Thanks !