/** * Binds to a Database record. * * @license * Copyright 2022 Marco Fucci di Napoli (mfucci@gmail.com) * SPDX-License-Identifier: Apache-2.0 */ import React from "react"; import { Record as RecordConnection } from "../../database/Record"; import { DatabaseContext } from "./DatabaseContext"; type Props = { id: string, onValue?: (value: T | undefined) => any, render?: (value: T, updater: (update: Partial) => void) => any, }; type State = { connection?: RecordConnection; value?: T, }; export class Record extends React.Component, State> { static contextType = DatabaseContext; declare context: React.ContextType; constructor(props: Props) { super(props); this.state = {}; } async componentDidMount() { const { databaseManager, id } = {...this.props, ...this.context}; const connection = await (await databaseManager.getDatabase()).getRecord(id); connection.on("update", value => this.handleValueUpdate(value)); this.handleValueUpdate(connection.get()); this.setState({ connection }); } private handleValueUpdate(value?: T) { const { onValue } = this.props; onValue?.(value); this.setState({ value }); } private updateValue(update: Partial) { const { connection, value } = this.state; if (connection === undefined || value === undefined) return; connection.set({ ...value, ...update }); } componentWillUnmount() { const { connection } = this.state; connection?.close(); } render() { const { value, render } = {...this.state, ...this.props}; if (value === undefined || render === undefined) return null; return render(value, update => this.updateValue(update)); } }