import { Datastore , Record , Schema , Table } from "@lumino/datastore" ; import { DisposableDelegate , IDisposable } from "@lumino/disposable" ; import { ISignal } from "@lumino/signaling" ; import { Observable } from "rxjs" ; export function signalToObservable < T > ( signal ) { return new Observable ( ( subscriber ) => { function slot ( _ , args ) { subscriber . next ( args ) ; } signal . connect ( slot ) ; return ( ) : void => { signal . disconnect ( slot ) ; } ; } ) ; } /**
 * A namespace for Datastore helper functions.
 */ export namespace DatastoreExt { param datastore param update returns export function withTransaction ( datastore , update ) { let id = "" ; if ( ! datastore . inTransaction ) { id = datastore . beginTransaction ( ) ; } try { update ( id ) ; } finally { if ( id ) { datastore . endTransaction ( ) ; } } return id ; } /**
     * A base type for describing the location of data in a datastore,
     * to be consumed by some object. The only requirement is that it
     * has a datastore object. Objects extending from this will, in general,
     * have some combination of table, record, and field locations.
     */ export type DataLocation = { /**
         * The datastore in which the data is contained.
         */ datastore : Datastore ; } ; /**
     * An interface for referring to a specific table in a datastore.
     */ export type TableLocation < S extends Schema > = { /**
         * The schema in question. This schema must exist in the datastore,
         * or an error may result in its usage.
         */ schema : S ; } ; /**
     * An interface for referring to a specific record in a datastore.
     */ export type RecordLocation < S extends Schema > = TableLocation < S > & { /**
         * The record in question.
         */ record : string ; } ; /**
     * An interface for referring to a specific field in a datastore.
     *
     * #### Notes
     * The field must exist in the schema.
     */ export type FieldLocation < S extends Schema , F extends keyof S [ "fields" ] > = RecordLocation < S > & { /**
         * The field in question.
         */ field : F ; } ; param datastore param loc returns export function getTable < S extends Schema > ( datastore , loc ) { return datastore . get ( loc . schema ) ; } param datastore param loc returns export function getRecord < S extends Schema > ( datastore , loc ) { return datastore . get ( loc . schema ) . get ( loc . record ) ; } param datastore param loc returns export function getField < S extends Schema , F extends keyof S [ "fields" ] > ( datastore , loc ) { const record = datastore . get ( loc . schema ) . get ( loc . record ) ; if ( ! record ) { throw Error ( `The record ${ loc . record } could not be found` ) ; } return record [ loc . field ] ; } param datastore param loc param update export function updateTable < S extends Schema > ( datastore , loc , update ) { const table = datastore . get ( loc . schema ) ; table . update ( update ) ; } param datastore param loc param update export function updateRecord < S extends Schema > ( datastore , loc , update ) { const table = datastore . get ( loc . schema ) ; table . update ( { [ loc . record ] : update , } ) ; } param datastore param loc param update export function updateField < S extends Schema , F extends keyof S [ "fields" ] > ( datastore , loc , update ) { const table = datastore . get ( loc . schema ) ; table . update ( { [ loc . record ] : { [ loc . field ] : update , } as Record . Update < S > , } ) ; } param datastore param loc param slot returns export function listenTable < S extends Schema > ( datastore , loc , slot , thisArg ? ) { const wrapper = ( source , args ) => { if ( ! args . change [ loc . schema . id ] ) { return ; } const tc = args . change [ loc . schema . id ] ! as Table . Change < S > ; slot . bind ( thisArg ) ( source , tc ) ; } ; datastore . changed . connect ( wrapper ) ; return new DisposableDelegate ( ( ) => { datastore . changed . disconnect ( wrapper ) ; } ) ; } param datastore param loc param slot returns export function listenRecord < S extends Schema > ( datastore , loc , slot , thisArg ? ) { const wrapper = ( source , args ) => { if ( ! args . change [ loc . schema . id ] || ! args . change [ loc . schema . id ] [ loc . record ] ) { return ; } const tc = args . change [ loc . schema . id ] ! as Table . Change < S > ; slot . bind ( thisArg ) ( source , tc [ loc . record ] ) ; } ; datastore . changed . connect ( wrapper ) ; return new DisposableDelegate ( ( ) => { datastore . changed . disconnect ( wrapper ) ; } ) ; } param datastore param loc param slot returns export function listenField < S extends Schema , F extends keyof S [ "fields" ] > ( datastore , loc , slot , thisArg ? ) { const wrapper = ( source , args ) => { if ( ! args . change [ loc . schema . id ] || ! args . change [ loc . schema . id ] [ loc . record ] || ! args . change [ loc . schema . id ] [ loc . record ] [ loc . field as string ] ) { return ; } const tc = args . change [ loc . schema . id ] ! as Table . Change < S > ; slot . bind ( thisArg ) ( source , tc [ loc . record ] [ loc . field ] ! ) ; } ; datastore . changed . connect ( wrapper ) ; return new DisposableDelegate ( ( ) => { datastore . changed . disconnect ( wrapper ) ; } ) ; } }