Skip to content

Commit

Permalink
Reject/throw as appropriate in ES6 exported methods if window.fdc3 is…
Browse files Browse the repository at this point in the history
… not available
  • Loading branch information
mattjamieson committed Jan 26, 2021
1 parent d742b89 commit 452a105
Show file tree
Hide file tree
Showing 2 changed files with 234 additions and 134 deletions.
47 changes: 34 additions & 13 deletions src/api/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,74 +7,95 @@ import {
Listener,
} from '..';

const unavailableError = new Error(
'FDC3 DesktopAgent not available at `window.fdc3`.'
);

const rejectIfNoGlobal = (f: () => Promise<any>) => {
return window.fdc3 ? f() : Promise.reject(unavailableError);
};

const throwIfNoGlobal = (f: () => any) => {
if (!window.fdc3) {
throw unavailableError;
}
return f();
};

export const open: (name: string, context?: Context) => Promise<void> = (
name,
context
) => {
return window.fdc3.open(name, context);
return rejectIfNoGlobal(() => window.fdc3.open(name, context));
};

export const findIntent: (
intent: string,
context?: Context
) => Promise<AppIntent> = (intent, context) => {
return window.fdc3.findIntent(intent, context);
return rejectIfNoGlobal(() => window.fdc3.findIntent(intent, context));
};

export const findIntentsByContext: (
context: Context
) => Promise<Array<AppIntent>> = context => {
return window.fdc3.findIntentsByContext(context);
return rejectIfNoGlobal(() => window.fdc3.findIntentsByContext(context));
};

export const broadcast: (context: Context) => void = context => {
window.fdc3.broadcast(context);
throwIfNoGlobal(() => window.fdc3.broadcast(context));
};

export const raiseIntent: (
intent: string,
context: Context,
target?: string
) => Promise<IntentResolution> = (intent, context, target) => {
return window.fdc3.raiseIntent(intent, context, target);
return rejectIfNoGlobal(() =>
window.fdc3.raiseIntent(intent, context, target)
);
};

export const addIntentListener: (
intent: string,
handler: ContextHandler
) => Listener = (intent, handler) => {
return window.fdc3.addIntentListener(intent, handler);
return throwIfNoGlobal(() => window.fdc3.addIntentListener(intent, handler));
};

export const addContextListener: (
contextTypeOrHandler: string | ContextHandler,
handler?: ContextHandler
) => Listener = (a, b) => {
if (typeof a !== 'function') {
return window.fdc3.addContextListener(a as string, b as ContextHandler);
return throwIfNoGlobal(() =>
window.fdc3.addContextListener(a as string, b as ContextHandler)
);
} else {
return window.fdc3.addContextListener(a as ContextHandler);
return throwIfNoGlobal(() =>
window.fdc3.addContextListener(a as ContextHandler)
);
}
};

export const getSystemChannels: () => Promise<Array<Channel>> = () => {
return window.fdc3.getSystemChannels();
return rejectIfNoGlobal(() => window.fdc3.getSystemChannels());
};

export const joinChannel: (channelId: string) => Promise<void> = channelId => {
return window.fdc3.joinChannel(channelId);
return rejectIfNoGlobal(() => window.fdc3.joinChannel(channelId));
};

export const getOrCreateChannel: (
channelId: string
) => Promise<Channel> = channelId => {
return window.fdc3.getOrCreateChannel(channelId);
return rejectIfNoGlobal(() => window.fdc3.getOrCreateChannel(channelId));
};

export const getCurrentChannel: () => Promise<Channel | null> = () => {
return window.fdc3.getCurrentChannel();
return rejectIfNoGlobal(() => window.fdc3.getCurrentChannel());
};

export const leaveCurrentChannel: () => Promise<void> = () => {
return window.fdc3.leaveCurrentChannel();
return rejectIfNoGlobal(() => window.fdc3.leaveCurrentChannel());
};
Loading

0 comments on commit 452a105

Please sign in to comment.