// @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 | void {} /** * Called before the pixie is destroyed. * This is a great place to clean up any resources. */ destroy(props: P, callbacks: PixieCallbacks) {} } /** * Turns a class-style pixie into a tame pixie. */ export function tameClassPixie

(Constructor: Class>): TamePixie

{ 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) } } }) }