import { Conn } from "./net" ; import { DenoFile , DenoProcess , ResourceMap } from "./deno" ; import * as fs from "fs" ; import * as cp from "child_process" ; import { DenoProcessImpl } from "./process" ; import { DenoFileImpl } from "./file" ; export interface ResourceTable { openFile ( file ) : DenoFile ; openProcess ( proc ) : DenoProcess ; getFile ( rid ) : DenoFile ; getProcess ( rid ) : DenoProcess ; map ( ) : ResourceMap ; close ( rid ) : void ; delete ( rid ) : void ; } function resourceTable ( ) { const files = new Map ( ) ; const processes = new Map ( ) ; const conns = new Map ( ) ; let resourceId = 3 ; function openFile ( file ) { const rid = resourceId ++ ; const ret = new DenoFileImpl ( rid , file ) ; files . set ( rid , ret ) ; return ret ; } function openProcess ( proc ) { const rid = resourceId ++ ; const ret = new DenoProcessImpl ( rid , proc ) ; processes . set ( rid , ret ) ; return ret ; } function close ( rid ) { if ( files . has ( rid ) ) { const file = files . get ( rid ) ; file . close ( ) ; } else if ( processes . has ( rid ) ) { const proc = processes . get ( rid ) ; proc . close ( ) ; } else if ( conns . has ( rid ) ) { const conn = conns . get ( rid ) ; conn . close ( ) ; } } function del ( rid ) { if ( files . has ( rid ) ) { files . delete ( rid ) ; } else if ( processes . has ( rid ) ) { processes . delete ( rid ) ; } else if ( conns . has ( rid ) ) { processes . delete ( rid ) ; } } function getFile ( rid ) { const file = files . get ( rid ) ; if ( ! file ) { throw new Error ( "file not found: rid=" + rid ) ; } return file ; } function getProcess ( rid ) { const proc = processes . get ( rid ) ; if ( ! proc ) { throw new Error ( "process not found: rid=" + rid ) ; } return ; } function map ( ) { return Object . fromEntries ( [ ... [ ... files . entries ( ) ] . map ( e => [ e [ 0 ] , "file" ] ) , ... [ ... processes . entries ( ) ] . map ( e => [ e [ 0 ] , "process" ] ) , ... [ ... conns . entries ( ) ] . map ( e => [ e [ 0 ] , "conn" ] ) ] ) ; } return { getFile , getProcess , openFile , openProcess , close , delete : del , map } ; } export const ResourceTable = resourceTable ( ) ;