43 lines
1.6 KiB
JavaScript
43 lines
1.6 KiB
JavaScript
import React, { createContext, useContext, useRef } from 'react';
|
|
import JamClientProxy from '../jamClientProxy';
|
|
|
|
import { FakeJamClientProxy } from '../fakeJamClientProxy';
|
|
import { FakeJamClientRecordings } from '../fakeJamClientRecordings';
|
|
import { FakeJamClientMessages } from '../fakeJamClientMessages';
|
|
import { useJamKazamApp } from './JamKazamAppContext';
|
|
|
|
const JamClientContext = createContext(null);
|
|
|
|
export const JamClientProvider = ({ children }) => {
|
|
//assign an instance of JamClientProxy to a ref so that it persists across renders
|
|
//if development environment, use FakeJamClientProxy
|
|
//otherwise use JamClientProxy
|
|
//initialize the proxy when the provider is mounted
|
|
//and provide it to the context value
|
|
|
|
//get the app instance from JamKazamAppContext
|
|
const app = useJamKazamApp();
|
|
|
|
const proxyRef = useRef(null);
|
|
if (process.env.NODE_ENV === 'development') {
|
|
const fakeJamClientMessages = new FakeJamClientMessages();
|
|
const proxy = new FakeJamClientProxy(app, fakeJamClientMessages); // Pass appropriate parameters
|
|
proxyRef.current = proxy.init();
|
|
// For testing purposes, we can add some fake recordings
|
|
const fakeJamClientRecordings = new FakeJamClientRecordings(app, proxyRef.current, fakeJamClientMessages);
|
|
proxyRef.current.SetFakeRecordingImpl(fakeJamClientRecordings);
|
|
} else {
|
|
if (!proxyRef.current) {
|
|
const proxy = new JamClientProxy(app, console);
|
|
proxyRef.current = proxy.init();
|
|
}
|
|
}
|
|
return (
|
|
<JamClientContext.Provider value={proxyRef.current}>
|
|
{children}
|
|
</JamClientContext.Provider>
|
|
);
|
|
};
|
|
|
|
export const useJamClient = () => useContext(JamClientContext);
|