Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
kenny-ciq committed Dec 5, 2022
1 parent 4569a31 commit e5b3781
Showing 1 changed file with 83 additions and 78 deletions.
161 changes: 83 additions & 78 deletions toolbox/fdc3-workbench/src/store/AppChannelStore.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { makeObservable, observable, action, runInAction, toJS } from "mobx";
import fdc3, {ContextType, Channel, Fdc3Listener} from "../utility/Fdc3Api";
import fdc3, { ContextType, Channel, Fdc3Listener } from "../utility/Fdc3Api";
import systemLogStore from "./SystemLogStore";
import { nanoid } from "nanoid";

Expand All @@ -9,48 +9,47 @@ interface ListenerOptionType {
type: string | undefined;
}

interface Fdc3Channel {
id: string;
channel: Channel;
currentListener?:ListenerOptionType|null;
broadcastError?: string;
context?: ContextType|null;
listenerError? : string;
export interface Fdc3Channel {
id: string;
channel: Channel;
currentListener?: ListenerOptionType | null;
broadcastError?: string;
context?: ContextType | null;
listenerError?: string;
}
class AppChannelStore {
appChannelsList: Fdc3Channel[] = [];

currentAppChannel: Fdc3Channel | null = null;

appChannelListeners: Fdc3Listener[] = [];
appChannelListeners: Fdc3Listener[] = [];

constructor() {
makeObservable(this, {
appChannelsList: observable,
currentAppChannel: observable,
appChannelListeners: observable,
getOrCreateChannel: action,
appChannelListeners: observable,
getOrCreateChannel: action,
leaveChannel: action,
broadcast: action,
});

}

async getOrCreateChannel(channelId: string) {
async getOrCreateChannel(channelId: string) {
try {
const currentAppChannel = await fdc3.getOrCreateChannel(channelId);
const isSuccess = currentAppChannel !== null;
if (isSuccess) {
this.currentAppChannel = {
id: channelId,
channel: currentAppChannel
};
let foundChannel = this.appChannelsList.find((channel)=>channel.id === channelId);
if (!foundChannel) {
this.appChannelsList.push(this.currentAppChannel);
}
}
if (isSuccess) {
this.currentAppChannel = {
id: channelId,
channel: currentAppChannel,
};
let foundChannel = this.appChannelsList.find((channel) => channel.id === channelId);
if (!foundChannel) {
this.appChannelsList.push(this.currentAppChannel);
}
}

runInAction(() => {
systemLogStore.addLog({
name: "getOrCreateChannel",
Expand All @@ -59,6 +58,7 @@ class AppChannelStore {
variant: "text",
});
});
return currentAppChannel;
} catch (e) {
systemLogStore.addLog({
name: "getOrCreateChannel",
Expand All @@ -67,18 +67,19 @@ class AppChannelStore {
variant: "code",
body: JSON.stringify(e, null, 4),
});
return;
}
}

isContextListenerExists(channelId: string, type: string | undefined) {
return !!this.appChannelListeners?.find((listener) => listener.type === type && listener.channelId === channelId);
isContextListenerExists(channelId: string, type: string | undefined) {
return !!this.appChannelListeners?.find((listener) => listener.type === type && listener.channelId === channelId);
}

isAppChannelExists(channelId: string) {
isAppChannelExists(channelId: string) {
return !!this.appChannelsList.find((channel) => channel.id === channelId);
}

async broadcast(channelId: string, context: ContextType) {
async broadcast(channelId: string, context: ContextType) {
if (!context) {
systemLogStore.addLog({
name: "appbroadcast",
Expand All @@ -87,9 +88,9 @@ class AppChannelStore {
variant: "text",
});
}

//check that we're on a channel
let currentChannel = this.appChannelsList.find((channel)=>channel.id === channelId);
let currentChannel = this.appChannelsList.find((channel) => channel.id === channelId);

if (!currentChannel) {
systemLogStore.addLog({
Expand All @@ -100,26 +101,25 @@ class AppChannelStore {
});
return;
}

try {
await currentChannel.channel.broadcast(toJS(context));
systemLogStore.addLog({
name: "appbroadcast",
type: "success",
body: JSON.stringify(context, null, 4),
variant: "code",
value: channelId
value: channelId,
});
} catch (e) {
systemLogStore.addLog({
name: "appbroadcast",
type: "error",
body: JSON.stringify(e, null, 4),
variant: "code",
value: channelId
value: channelId,
});
}

}

async leaveChannel() {
Expand Down Expand Up @@ -162,58 +162,63 @@ class AppChannelStore {
}
}

async addChannelListener(channelId: string, newListener: string | undefined ){
try{
let currentChannel = this.appChannelsList.find((channel)=>channel.id === channelId);
let foundListener = this.appChannelListeners.find((currentListener)=>currentListener.type === newListener && currentListener.channelId === channelId);

if (!foundListener && currentChannel && newListener !== undefined) {
const listenerId = nanoid();
const contactListener = await currentChannel.channel.addContextListener(newListener?.toLowerCase() === "all" ? null : newListener, (context, metaData?: any) => {
const currentListener = this.appChannelListeners.find((listener)=>listener.type === newListener && listener.channelId === channelId);

runInAction(() => {
if (currentListener) {
currentListener.lastReceivedContext = context;
currentListener.metaData = metaData
}
});

systemLogStore.addLog({
name: "receivedAppContextListener",
type: "info",
value: `Channel [${channelId}] Received context via '[${newListener}]' listener`,
variant: "code",
body: JSON.stringify(context, null, 4),
});
});

runInAction(() => {
systemLogStore.addLog({
name: "addAppContextListener",
type: "success",
value: `A context listener for '[${newListener}]' has been added on channel [${channelId}]`,
variant: "text",
});
this.appChannelListeners.push({ id: listenerId, type: newListener, listener: contactListener, channelId});
});
}

} catch(e) {
systemLogStore.addLog({
async addChannelListener(channelId: string, newListener: string | undefined) {
try {
let currentChannel = this.appChannelsList.find((channel) => channel.id === channelId);
let foundListener = this.appChannelListeners.find(
(currentListener) => currentListener.type === newListener && currentListener.channelId === channelId
);

if (!foundListener && currentChannel && newListener !== undefined) {
const listenerId = nanoid();
const contactListener = await currentChannel.channel.addContextListener(
newListener?.toLowerCase() === "all" ? null : newListener,
(context, metaData?: any) => {
const currentListener = this.appChannelListeners.find(
(listener) => listener.type === newListener && listener.channelId === channelId
);

runInAction(() => {
if (currentListener) {
currentListener.lastReceivedContext = context;
currentListener.metaData = metaData;
}
});

systemLogStore.addLog({
name: "receivedAppContextListener",
type: "info",
value: `Channel [${channelId}] Received context via '[${newListener}]' listener`,
variant: "code",
body: JSON.stringify(context, null, 4),
});
}
);

runInAction(() => {
systemLogStore.addLog({
name: "addAppContextListener",
type: "success",
value: `A context listener for '[${newListener}]' has been added on channel [${channelId}]`,
variant: "text",
});
this.appChannelListeners.push({ id: listenerId, type: newListener, listener: contactListener, channelId });
});
}
} catch (e) {
systemLogStore.addLog({
name: "addAppContextListener",
type: "error",
value: `Failed to add a context listener for '[${newListener}]' on channel [${channelId}]`,
variant: "code",
body: JSON.stringify(e, null, 4),
});
}

}
}
}

removeContextListener(id: string) {
removeContextListener(id: string) {
const listenerIndex = this.appChannelListeners?.findIndex((listener) => listener.id === id);
const listener = this.appChannelListeners[listenerIndex];
const listener = this.appChannelListeners[listenerIndex];
if (listenerIndex !== -1) {
try {
this.appChannelListeners[listenerIndex].listener.unsubscribe();
Expand Down

0 comments on commit e5b3781

Please sign in to comment.