/**
 * React Native SQLite Demo
 * Copyright (c) 2021 Bruce Lefebvre <bruce@brucelefebvre.com>
 * https://github.com/blefebvre/react-native-sqlite-demo/blob/master/LICENSE
 */ import React , { useContext , useState } from "react" ; import { Database , sqliteDatabase } from "../database/Database" ; import { inMemoryDatabase } from "../database/InMemoryDatabase" ; import { List } from "../types/List" ; const DatabaseContext = React . createContext < Database | undefined > ( undefined ) ; const ListsContext = React . createContext < List [ ] | undefined > ( undefined ) ; type SetLists = ( lists : List [ ] ) => void ; const SetListsContext = React . createContext < SetLists | undefined > ( undefined ) ; export const ListContextProvider = function ( { children } ) { const [ lists , setLists ] = useState < List [ ] > ( [ ] ) ; return ( < DatabaseContext . Provider value = { sqliteDatabase } > < ListsContext . Provider value = { lists } > < SetListsContext . Provider value = { setLists } > { children } < /SetListsContext.Provider> < /ListsContext.Provider> < /DatabaseContext.Provider>); } ; export function useDatabase ( ) { const database = useContext ( DatabaseContext ) ; if ( database === undefined ) { throw new Error ( "useDatabase must be used within a ListContextProvider" ) ; } return database ; } export function useListsContext ( ) { const listsContext = useContext ( ListsContext ) ; if ( listsContext === undefined ) { throw new Error ( "useListsContext must be used within a ListContextProvider" ) ; } return listsContext ; } export function useSetListsContext ( ) { const listsUpdateContext = useContext ( SetListsContext ) ; if ( listsUpdateContext === undefined ) { throw new Error ( "useSetListsContext must be used within a ListContextProvider" ) ; } return listsUpdateContext ; }