// @flow import type { OnError, OnOutput, PixieInput, TamePixie } from '../types.js' import { tamePixie } from './tamePixie.js' export type PixieCallbacks = { onError: OnError, onOutput: OnOutput } /** * Class-style pixies should inherit from this type. */ export class Pixie
{
props: P
constructor(props: P, callbacks?: PixieCallbacks) {
this.props = props
}
/**
* Called every time the props change.
*/
update(props: P, callbacks: PixieCallbacks): Promise (Constructor: Class {
return tamePixie(({ onError, onOutput }: PixieInput ) => {
const callbacks: PixieCallbacks = { onError, onOutput }
let instance: Pixie
let propsCache: P
return {
update(props: P) {
propsCache = props
if (!instance) {
instance = new Constructor(props, callbacks)
}
return instance.update(props, callbacks)
},
destroy() {
return instance.destroy(propsCache, callbacks)
}
}
})
}