How to use the fp-ts-contrib/lib/TaskOption.chainOption function in fp-ts-contrib

To help you get started, we’ve selected a few fp-ts-contrib examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github SamHH / bukubrow-webext / src / modules / comms / browser.ts View on Github external
const browserTabsQuery = (x: Tabs.QueryQueryInfoType): TaskOption> =>
	TO.tryCatch(() => browser.tabs.query(x));

export const getActiveTab: TaskOption = pipe(
	browserTabsQuery({ active: true, currentWindow: true }),
	TO.chainOption(A.head),
);

export const getActiveWindowTabs: TaskOption> = pipe(
	browserTabsQuery({ currentWindow: true }),
	TO.chainOption(NEA.fromArray),
);

export const getAllTabs: TaskOption> = pipe(
	browserTabsQuery({}),
	TO.chainOption(NEA.fromArray),
);

export const onTabActivity = (cb: Lazy): void => {
	browser.tabs.onActivated.addListener(cb);
	browser.tabs.onUpdated.addListener(cb);
};

// The imperfect title includes check is because Firefox's href changes
// according to the extension in use, if any
const isNewTabPage: Predicate> = ({ url = '', title = '' }) =>
	['about:blank', 'chrome://newtab/'].includes(url) || title.includes('New Tab');

/// The active tab will not update quickly enough to allow this function to be
/// called safely in a loop. Therefore, the second argument forces consumers to
/// verify that this is only the first tab they're opening.
export const openBookmarkInAppropriateTab = (isFirstTab: boolean) => (url: string): Task => pipe(
github SamHH / bukubrow-webext / src / modules / badge.ts View on Github external
const updateBadge = (badgeOpt: BadgeDisplay): Task => async (): Promise => {
	const urlRes = await pipe(
		getActiveTab,
		TO.chainOption(tab => O.fromNullable(tab.url)),
		TO.chainOption(flow(
			fromString,
			O.fromEither,
		)),
		TO.map(flip(checkUrl)(urlState)),
		runTask,
	);

	if (O.isSome(urlRes)) {
		const [result, numMatches] = urlRes.value;

		if (badgeOpt === BadgeDisplay.None || result === URLMatch.None) {
			disableBadge();
			return;
		}

		const text = badgeOpt === BadgeDisplay.WithCount
github SamHH / bukubrow-webext / src / modules / comms / browser.ts View on Github external
import { BOOKMARKS_SCHEMA_VERSION } from 'Modules/config';
import { runIO } from 'Modules/fp';
import { sendIsomorphicMessage, IsomorphicMessage } from 'Modules/comms/isomorphic';
import { createUuid } from 'Modules/uuid';
import { error } from 'Modules/error';
import { LocalBookmark, LocalBookmarkUnsaved } from 'Modules/bookmarks';
import { StagedBookmarksGroup } from 'Modules/staged-groups';

const sequenceTTE = sequenceT(TE.taskEither);

const browserTabsQuery = (x: Tabs.QueryQueryInfoType): TaskOption> =>
	TO.tryCatch(() => browser.tabs.query(x));

export const getActiveTab: TaskOption = pipe(
	browserTabsQuery({ active: true, currentWindow: true }),
	TO.chainOption(A.head),
);

export const getActiveWindowTabs: TaskOption> = pipe(
	browserTabsQuery({ currentWindow: true }),
	TO.chainOption(NEA.fromArray),
);

export const getAllTabs: TaskOption> = pipe(
	browserTabsQuery({}),
	TO.chainOption(NEA.fromArray),
);

export const onTabActivity = (cb: Lazy): void => {
	browser.tabs.onActivated.addListener(cb);
	browser.tabs.onUpdated.addListener(cb);
};
github SamHH / bukubrow-webext / src / modules / badge.ts View on Github external
const updateBadge = (badgeOpt: BadgeDisplay): Task => async (): Promise => {
	const urlRes = await pipe(
		getActiveTab,
		TO.chainOption(tab => O.fromNullable(tab.url)),
		TO.chainOption(flow(
			fromString,
			O.fromEither,
		)),
		TO.map(flip(checkUrl)(urlState)),
		runTask,
	);

	if (O.isSome(urlRes)) {
		const [result, numMatches] = urlRes.value;

		if (badgeOpt === BadgeDisplay.None || result === URLMatch.None) {
			disableBadge();
			return;
		}