import { SocketIOGraphQLClient } from "@n1ru4l/socket-io-graphql-client" ; import { applyLiveQueryJSONPatch } from "@n1ru4l/graphql-live-query-patch-json-patch" ; import { applyAsyncIterableIteratorToSink } from "@n1ru4l/push-pull-async-iterable-iterator" ; import { Environment , Network , RecordSource , Store , Observable , GraphQLResponse , RequestParameters , Variables , } from "relay-runtime" ; const throttle = < T extends Array < any > > ( fn , wait ) => { let isCalled = false ; return ( ... args ) => { if ( ! isCalled ) { fn ( ... args ) ; isCalled = true ; setTimeout ( function ( ) { isCalled = false ; } , wait ) ; } } ; } ; const attachNotifyGarbageCollectionBehaviourToStore = ( store ) : Store => { const notify = store . notify . bind ( store ) ; const scheduleGarbageCollection = throttle ( store . scheduleGC . bind ( store ) , 5000 ) ; const newNotify = ( ... args ) => { scheduleGarbageCollection ( ) ; return notify ( ... args ) ; } ; store . notify = newNotify ; return store ; } ; export const createRelayEnvironment = ( networkInterface ) => { const execute = ( request , variables ) => { if ( ! request . text ) throw new Error ( "Missing document." ) ; const { text : operation , name } = request ; return Observable . create < GraphQLResponse > ( ( sink ) => applyAsyncIterableIteratorToSink ( applyLiveQueryJSONPatch ( networkInterface . execute ( { operation , variables , operationName : name , } ) ) , sink ) ) ; } ; const network = Network . create ( execute , execute ) ; const store = attachNotifyGarbageCollectionBehaviourToStore ( new Store ( new RecordSource ( ) ) ) ; return new Environment ( { network , store , } ) ; } ;