working protocols

This commit is contained in:
Andrew Gundersen 2021-06-29 11:30:35 -05:00
commit a791f973dc
8 changed files with 114 additions and 109 deletions

View file

@ -1,10 +1,9 @@
import { postAuth, postLogin, postLogout } from "@/api/account";
import { endSession, launchSession } from "@/session";
import { getToken, setToken, setProfile, getProfile, clearStore } from "./store";
import { getToken, setToken, setProfile, clearStore } from "./store";
import { parseAuthRes } from "./auth";
import { ipcEmit } from "@/composables/useEmitter";
import { messages } from "@/messages";
export const accountAuth = async (): Promise<Error | AuthState> => {
@ -84,13 +83,4 @@ export const accountLogout = async (): Promise<Error | void> => {
}
export const updateAppState = (): void => {
const profile = getProfile();
ipcEmit("set-profile", profile);
ipcEmit("init-messages", messages)
}

View file

@ -2,7 +2,7 @@
import { IpcListener } from "@/composables/useIpcMain"
import { sendMessage } from '@/session';
import { collect } from "@/audio";
import { updateAppState } from "@/account";
import { updateAppState } from "@/session";
import { onNavBar } from "@/window";
const CLIENT_MESSAGE_CHANNEL = "post-session-send"

View file

@ -10,8 +10,8 @@
*/
import initIpcMain from "@/ipc/index";
import { accountAuth, updateAppState } from "./account";
import { launchSession } from "./session";
import { accountAuth } from "./account";
import { launchSession, updateAppState } from "./session";
import createWindow from "./window";
let authState: AuthState | null;

View file

@ -1,40 +1,67 @@
import { ipcEmit } from "@/composables/useEmitter";
export let messages: Message[] = [];
export default class Messages {
export const setMessages = (init: {messages: Message[]}) => {
console.log("setMessages", init);
messages = init.messages;
ipcEmit("init-messages", messages);
}
messages: Message[];
export const newMessage = (message: Message) => {
console.log("newMessage", message);
messages.push(message);
ipcEmit("add-message", message);
}
constructor(messages: Message[]) {
console.log(messages);
this.messages = messages;
this.emit();
}
export const annotateMessage = (annotation: Annotation) => {
console.log("annotateMessage", annotation);
emit() {
ipcEmit("init-messages", this.messages);
}
for (let i in messages) {
update(update: Update) {
if (messages[i].uid == annotation.uid) {
if (update.name === "add") {
this.messages.push(update.data as Message);
ipcEmit("add-message", update.data);
}
else if (update.name === "annotate") {
this._annotate(update.data as Annotation);
ipcEmit("update-message", update.data);
}
else {
this._delete(update.data as string);
ipcEmit("delete-message", update.data);
}
}
_annotate(annotation: Annotation) {
for (let i in this.messages) {
if (this.messages[i].uid == annotation.uid) {
if (annotation.name == "content") {
this.messages[i].content = annotation.data as string[];
}
if (annotation.name == "context") {
this.messages[i].context = annotation.data as string;
}
ipcEmit("update-message", annotation);
if (annotation.data.hasOwnProperty("content")) {
const data = annotation.data as ContentData;
messages[i].content = data.content;
}
if (annotation.data.hasOwnProperty("context")) {
const data = annotation.data as ContextData;
messages[i].context = data.context;
}
ipcEmit("update-message", annotation);
break;
}
}
}
}
_delete(uid: string) {
for (var i = 0; i < this.messages.length; i++) {
if (this.messages[i].uid === uid) {
this.messages.splice(i, 1);
break;
}
}
}
}

View file

@ -4,7 +4,7 @@ import { IpcRendererListener } from "./composables/useIpcRend"
* Shared state imports
* */
import { setProfile } from "./shared/profile";
import { setMessages, addMessage, updateMessage } from "./shared/messages";
import { setMessages, addMessage, updateMessage, deleteMessage } from "./shared/messages";
import { setConnectionStatus } from "./shared/connectionStatus";
const SET_PROFILE_CHANNEL = "set-profile";
@ -12,6 +12,7 @@ const SET_PROFILE_CHANNEL = "set-profile";
const INIT_MESSAGES_CHANNEL = "init-messages";
const ADD_MESSAGE_CHANNEL = "add-message";
const UPDATE_MESSAGE_CHANNEL = "update-message";
const DELETE_MESSAGE_CHANNEL = "delete-message";
const CONNECTION_STATUS_CHANNEL = "set-connection-status";
export const setProfileListener = new IpcRendererListener({
@ -35,6 +36,11 @@ export const updateMessagesListener = new IpcRendererListener({
listenerCallback: updateMessage
});
export const deleteMessagesListener = new IpcRendererListener({
channel: DELETE_MESSAGE_CHANNEL,
listenerCallback: deleteMessage
});
export const connectionStatusListener = new IpcRendererListener({
channel: CONNECTION_STATUS_CHANNEL,
listenerCallback: setConnectionStatus

View file

@ -5,55 +5,46 @@ import useScroll from "@/render/composables/useScroll";
export const messages: Ref<Array<Message>> = ref([]);
export const setMessages: IpcListenerCallback<Array<Message>> = (payload) => {
console.log("setMessages", payload);
messages.value = payload as Array<Message>;
}
export const addMessage: IpcListenerCallback<Message> = (payload) => {
console.log("addMessage", payload);
messages.value.push(payload as Message);
}
export const updateMessage: IpcListenerCallback<Annotation> = (payload) => {
console.log("updateMessage", payload);
const annotation = payload as Annotation;
/* find the target message */
let targetMessage = messages.value.filter((m: Message) => {
return m.uid === annotation.uid;
})[0];
for (let i in messages.value) {
if (targetMessage) {
if (messages.value[i].uid == annotation.uid) {
if (annotation.name == "content") {
messages.value[i].content = annotation.data as string[];
}
if (annotation.name == "context") {
messages.value[i].context = annotation.data as string;
}
/* update the content (add a new bubble) */
if (annotation.data.hasOwnProperty("content")) {
const data = annotation.data as ContentData;
targetMessage.content = data.content;
}
/* and/or update the context */
if (annotation.data.hasOwnProperty("context")) {
const data = annotation.data as ContextData;
targetMessage.context = data.context;
}
}
}
// messages.value = [
// {
// content: ["Welcome Andrew"],
// context: "Crimata",
// modifier: "ai",
// uid: "b5dd3d1b-cef0-4057-aa0f-c72e5c4cd67d"
// },
// {
// content: ["show contacts"],
// context: "false",
// modifier: "client",
// uid: "ee2819a3-1125-4881-9942-ca5b3d0ad502"
// }]
export const deleteMessage: IpcListenerCallback<string> = (payload) => {
const uid = payload as string;
for (var i = 0; i < messages.value.length; i++) {
if (messages.value[i].uid === uid) {
messages.value.splice(i, 1);
break;
}
}
}
// setTimeout(() => {

View file

@ -3,52 +3,34 @@
// import useAudio from "@/audio";
import { setMessages, newMessage, annotateMessage } from "./messages";
import Messages from "./messages";
import { getProfile } from "./store";
import useWebsockets from "./composables/useWebsockets";
import {config} from "@/config";
import { ipcEmit } from "@/composables/useEmitter";
/* start and stop audio functionality */
// const { initAudio, closeAudio } = useAudio();
const isInitMessage = (object: any): object is Message[] => {
return 'messages' in object;
};
let messages: Messages;
const isNewMessage = (object: any): object is Message => {
return 'content' in object;
};
const isAnnotation = (object: any): object is Annotation => {
return 'uid' in object;
};
const deauthenticate = (): void => {
console.log('deauthenticating')
};
/**
* Controls for interfacing with the platform.
* Takes an onMessage callback which we define below.
*/
const onMessageCallback = (payload: string) => {
const message = JSON.parse(payload);
if (message === "CLOSE_AUTH_FAIL") {
deauthenticate();
console.log("deauthenticating");
}
else if (isInitMessage(message)) {
setMessages(message);
else if (message.header === "init") {
messages = new Messages(message.body);
}
else if (isNewMessage(message)) {
newMessage(message);
}
else if (isAnnotation(message)) {
annotateMessage(message);
else {
if (messages) {
messages.update(message.body);
}
}
}
@ -57,6 +39,17 @@ const connectionStatusCallback = (status: string) => {
ipcEmit('set-connection-status', status);
}
export const updateAppState = (): void => {
const profile = getProfile();
ipcEmit("set-profile", profile);
if (messages)
messages.emit();
}
const statusOptions = {
openMessage: "Connected",
pongMessage: "Nominal",

View file

@ -1,4 +1,9 @@
interface Update {
name: string;
data: Message[] | Message | Annotation | string;
}
interface Message {
modifier: string;
content: string[];
@ -6,16 +11,9 @@ interface Message {
uid: string;
}
interface ContentData {
content: string[];
}
interface ContextData {
context: string;
}
interface Annotation {
data: ContentData | ContextData;
name: string;
data: string | string[];
uid: string;
}