mirror of
https://github.com/balzack/databag.git
synced 2025-02-14 20:49:16 +00:00
testing app context in web
This commit is contained in:
parent
29d3555dc8
commit
40ecb17d70
@ -2,6 +2,11 @@ const TIMEOUT = 15000;
|
|||||||
|
|
||||||
//await new Promise(r => setTimeout(r, 2000));
|
//await new Promise(r => setTimeout(r, 2000));
|
||||||
|
|
||||||
|
export function createWebsocket(url) {
|
||||||
|
console.log("HERE");
|
||||||
|
return new WebSocket(url);
|
||||||
|
}
|
||||||
|
|
||||||
export function checkResponse(response) {
|
export function checkResponse(response) {
|
||||||
if(response.status >= 400 && response.status < 600) {
|
if(response.status >= 400 && response.status < 600) {
|
||||||
throw new Error(response.url + " failed");
|
throw new Error(response.url + " failed");
|
||||||
|
@ -11,9 +11,11 @@ import { CardContext } from './CardContext';
|
|||||||
import { ChannelContext } from './ChannelContext';
|
import { ChannelContext } from './ChannelContext';
|
||||||
import { StoreContext } from './StoreContext';
|
import { StoreContext } from './StoreContext';
|
||||||
import { UploadContext } from './UploadContext';
|
import { UploadContext } from './UploadContext';
|
||||||
|
import { createWebsocket } from 'api/fetchUtil';
|
||||||
|
|
||||||
export function useAppContext() {
|
export function useAppContext(websocket) {
|
||||||
const [state, setState] = useState({
|
const [state, setState] = useState({
|
||||||
|
disconnected: true,
|
||||||
});
|
});
|
||||||
const [appRevision, setAppRevision] = useState();
|
const [appRevision, setAppRevision] = useState();
|
||||||
|
|
||||||
@ -69,6 +71,11 @@ export function useAppContext() {
|
|||||||
let access = await setLogin(username, password, appName, appVersion, userAgent)
|
let access = await setLogin(username, password, appName, appVersion, userAgent)
|
||||||
updateState({ access: access.appToken });
|
updateState({ access: access.appToken });
|
||||||
storeContext.actions.setValue('login:timestamp', access.created);
|
storeContext.actions.setValue('login:timestamp', access.created);
|
||||||
|
accountContext.actions.setToken(access.appToken);
|
||||||
|
profileContext.actions.setToken(access.appToken);
|
||||||
|
cardContext.actions.setToken(access.appToken);
|
||||||
|
channelContext.actions.setToken(access.appToken);
|
||||||
|
|
||||||
setWebsocket(access.appToken)
|
setWebsocket(access.appToken)
|
||||||
localStorage.setItem("session", JSON.stringify({
|
localStorage.setItem("session", JSON.stringify({
|
||||||
access: access.appToken,
|
access: access.appToken,
|
||||||
@ -81,6 +88,11 @@ export function useAppContext() {
|
|||||||
let access = await setLogin(username, password, appName, appVersion, userAgent)
|
let access = await setLogin(username, password, appName, appVersion, userAgent)
|
||||||
updateState({ access: access.appToken });
|
updateState({ access: access.appToken });
|
||||||
storeContext.actions.setValue('login:timestamp', access.created);
|
storeContext.actions.setValue('login:timestamp', access.created);
|
||||||
|
accountContext.actions.setToken(access.appToken);
|
||||||
|
profileContext.actions.setToken(access.appToken);
|
||||||
|
cardContext.actions.setToken(access.appToken);
|
||||||
|
channelContext.actions.setToken(access.appToken);
|
||||||
|
|
||||||
setWebsocket(access.appToken)
|
setWebsocket(access.appToken)
|
||||||
localStorage.setItem("session", JSON.stringify({
|
localStorage.setItem("session", JSON.stringify({
|
||||||
access: access.appToken,
|
access: access.appToken,
|
||||||
@ -93,6 +105,11 @@ export function useAppContext() {
|
|||||||
let access = await setAccountAccess(token, appName, appVersion, userAgent)
|
let access = await setAccountAccess(token, appName, appVersion, userAgent)
|
||||||
updateState({ access: access.appToken });
|
updateState({ access: access.appToken });
|
||||||
storeContext.actions.setValue('login:timestamp', access.created);
|
storeContext.actions.setValue('login:timestamp', access.created);
|
||||||
|
accountContext.actions.setToken(access.appToken);
|
||||||
|
profileContext.actions.setToken(access.appToken);
|
||||||
|
cardContext.actions.setToken(access.appToken);
|
||||||
|
channelContext.actions.setToken(access.appToken);
|
||||||
|
|
||||||
setWebsocket(access.appToken)
|
setWebsocket(access.appToken)
|
||||||
localStorage.setItem("session", JSON.stringify({
|
localStorage.setItem("session", JSON.stringify({
|
||||||
access: access.appToken,
|
access: access.appToken,
|
||||||
@ -109,6 +126,11 @@ export function useAppContext() {
|
|||||||
console.log(err);
|
console.log(err);
|
||||||
}
|
}
|
||||||
updateState({ access: null });
|
updateState({ access: null });
|
||||||
|
accountContext.actions.clearToken();
|
||||||
|
profileContext.actions.clearToken();
|
||||||
|
cardContext.actions.clearToken();
|
||||||
|
channelContext.actions.clearToken();
|
||||||
|
|
||||||
clearWebsocket()
|
clearWebsocket()
|
||||||
localStorage.removeItem("session");
|
localStorage.removeItem("session");
|
||||||
}
|
}
|
||||||
@ -124,12 +146,6 @@ export function useAppContext() {
|
|||||||
}, [appRevision]);
|
}, [appRevision]);
|
||||||
|
|
||||||
const setWebsocket = (token) => {
|
const setWebsocket = (token) => {
|
||||||
|
|
||||||
accountContext.actions.setToken(token);
|
|
||||||
profileContext.actions.setToken(token);
|
|
||||||
cardContext.actions.setToken(token);
|
|
||||||
channelContext.actions.setToken(token);
|
|
||||||
|
|
||||||
let protocol;
|
let protocol;
|
||||||
if (window.location.protocol === 'http:') {
|
if (window.location.protocol === 'http:') {
|
||||||
protocol = 'ws://';
|
protocol = 'ws://';
|
||||||
@ -138,7 +154,7 @@ export function useAppContext() {
|
|||||||
protocol = 'wss://';
|
protocol = 'wss://';
|
||||||
}
|
}
|
||||||
|
|
||||||
ws.current = new WebSocket(protocol + window.location.host + "/status");
|
ws.current = createWebsocket(protocol + window.location.host + "/status");
|
||||||
ws.current.onmessage = (ev) => {
|
ws.current.onmessage = (ev) => {
|
||||||
try {
|
try {
|
||||||
let rev = JSON.parse(ev.data);
|
let rev = JSON.parse(ev.data);
|
||||||
|
@ -223,7 +223,7 @@ export function useCardContext() {
|
|||||||
const actions = {
|
const actions = {
|
||||||
setToken: (token) => {
|
setToken: (token) => {
|
||||||
if (access.current || syncing.current) {
|
if (access.current || syncing.current) {
|
||||||
throw new Error("invalid session state");
|
throw new Error("invalid card session state");
|
||||||
}
|
}
|
||||||
access.current = token;
|
access.current = token;
|
||||||
cards.current = new Map();
|
cards.current = new Map();
|
||||||
|
@ -104,7 +104,7 @@ export function useChannelContext() {
|
|||||||
const actions = {
|
const actions = {
|
||||||
setToken: (token) => {
|
setToken: (token) => {
|
||||||
if (access.current || syncing.current) {
|
if (access.current || syncing.current) {
|
||||||
throw new Error("invalid session state");
|
throw new Error("invalid channel session state");
|
||||||
}
|
}
|
||||||
access.current = token;
|
access.current = token;
|
||||||
channels.current = new Map();
|
channels.current = new Map();
|
||||||
|
@ -47,7 +47,7 @@ export function useProfileContext() {
|
|||||||
const actions = {
|
const actions = {
|
||||||
setToken: (token) => {
|
setToken: (token) => {
|
||||||
if (access.current || syncing.current) {
|
if (access.current || syncing.current) {
|
||||||
throw new Error("invalid session state");
|
throw new Error("invalid profile session state");
|
||||||
}
|
}
|
||||||
access.current = token;
|
access.current = token;
|
||||||
curRevision.current = null;
|
curRevision.current = null;
|
||||||
|
Loading…
Reference in New Issue
Block a user