lodash#xor TypeScript Examples
The following examples show how to use
lodash#xor.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: hub.reducer.ts From dashboard with Apache License 2.0 | 6 votes |
hubReducer = produce((draft: HubState, action: HubActionTypes) => {
switch (action.type) {
case FETCH_HUB_IMAGES:
draft.loading = true
break
case FETCH_HUB_IMAGES_SUCCESS:
draft.loading = false
draft.images = action.payload.images
draft.error = null
break
case FETCH_HUB_IMAGES_FAILURE:
draft.images = []
draft.loading = false
draft.error = action.payload.error
break
case PICK_FILTER:
draft.selectedFilters = xor(draft.selectedFilters, [
action.payload.filter,
])
break
case CLEAR_FILTERS:
draft.selectedFilters = draft.selectedFilters.filter(
(selectedFilter) => !action.payload.filters.includes(selectedFilter)
)
}
}, initialHubState)
Example #2
Source File: graph.ts From nebula-studio with Apache License 2.0 | 6 votes |
checkVertexesExist = async (payload) => {
const { preAddVertexes, inputIds, expand } = payload;
const preAddIds = preAddVertexes.map((i) => String(i.id));
const {
schema: { spaceVidType },
} = this.rootStore;
if (preAddIds.length !== inputIds.length) {
// Get the missing id in the returned result
const notIncludedIds = xor(preAddIds, inputIds);
// judge whether it is a vertex on the hanging edge
const existedIds = (await this.getVertexesOnHangingEdge({
ids: notIncludedIds as string[],
})) as any;
// If the id in notIncludedIds exists in the existedIds, means that its a vertex on the hanging edge
// otherwise it really does not exist in nebula
const notExistIds = notIncludedIds.filter((id) => !existedIds.includes(id));
const addIds = notIncludedIds.filter((id) => existedIds.includes(id));
if (notExistIds.length > 0) {
message.warning(`${notExistIds.join(', ')}${intl.get('explore.notExist')}`);
}
addIds.forEach((id) => {
const vertex: any = {
id: spaceVidType === 'INT64' ? Number(id) : String(id),
tags: [],
properties: {},
};
if (expand?.vertexStyle === 'groupByTag') {
vertex.group = 't';
}
preAddVertexes.push(vertex);
});
}
return preAddVertexes;
};
Example #3
Source File: DownloadManager.ts From jellyfin-audio-player with MIT License | 4 votes |
/**
* This is a component that tracks queued downloads, and starts them one-by-one,
* so that we don't overload react-native-fs, as well as the render performance.
*/
function DownloadManager () {
// Retrieve store helpers
const { queued, ids } = useTypedSelector((state) => state.downloads);
const rehydrated = useTypedSelector((state) => state._persist.rehydrated);
const dispatch = useAppDispatch();
// Keep state for the currently active downloads (i.e. the downloads that
// have actually been pushed out to react-native-fs).
const [hasRehydratedOrphans, setHasRehydratedOrphans] = useState(false);
const activeDownloads = useRef(new Set<EntityId>());
useEffect(() => {
// GUARD: Check if the queue is empty
if (!queued.length) {
// If so, clear any current downloads
activeDownloads.current.clear();
return;
}
// Apparently, the queue has changed, and we need to manage
// First, we pick the first n downloads
const queue = queued.slice(0, MAX_CONCURRENT_DOWNLOADS);
// We then filter for new downloads
queue.filter((id) => !activeDownloads.current.has(id))
.forEach((id) => {
// We dispatch the actual call to start downloading
dispatch(downloadTrack(id));
// And add it to the active downloads
activeDownloads.current.add(id);
});
// Lastly, if something isn't part of the queue, but is of active
// downloads, we can assume the download completed.
xor(Array.from(activeDownloads.current), queue)
.forEach((id) => activeDownloads.current.delete(id));
}, [queued, dispatch, activeDownloads]);
useEffect(() => {
// GUARD: We only run this functino once
if (hasRehydratedOrphans) {
return;
}
// GUARD: If the state has not been rehydrated, we cannot check against
// the store ids.
if (!rehydrated) {
return;
}
/**
* Whenever the store is cleared, existing downloads get "lost" because
* the only reference we have is the store. This function checks for
* those lost downloads and adds them to the store
*/
async function hydrateOrphanedDownloads() {
// Retrieve all files for this app
const files = await readDir(DocumentDirectoryPath);
// Loop through the mp3 files
files.filter((file) => file.isFile() && file.name.endsWith('.mp3'))
.forEach((file) => {
const id = file.name.replace('.mp3', '');
// GUARD: If the id is already in the store, there's nothing
// left for us to do.
if (ids.includes(id)) {
return;
}
// Add the download to the store
dispatch(completeDownload({
id,
location: file.path,
size: file.size,
}));
});
}
hydrateOrphanedDownloads();
setHasRehydratedOrphans(true);
}, [rehydrated, ids, hasRehydratedOrphans, dispatch]);
return null;
}
Example #4
Source File: route.ts From raydium-sdk with GNU General Public License v3.0 | 4 votes |
// static makeSwapInFixedOutInstruction() {}
// static makeSwapOutFixedOutInstruction() {}
/* ================= compute data ================= */
static computeAmountOut({
fromPoolKeys,
toPoolKeys,
fromPoolInfo,
toPoolInfo,
amountIn,
currencyOut,
slippage,
}: RouteComputeAmountOutParams) {
const { swap: fromPoolSwapEnabled } = Liquidity.getEnabledFeatures(fromPoolInfo);
const { swap: toPoolSwapEnabled } = Liquidity.getEnabledFeatures(toPoolInfo);
logger.assertArgument(fromPoolSwapEnabled && toPoolSwapEnabled, "pools swap not enabled", "pools", {
fromPoolKeys,
toPoolKeys,
fromPoolInfo,
toPoolInfo,
});
const tokenIn = amountIn instanceof TokenAmount ? amountIn.token : Token.WSOL;
const tokenOut = currencyOut instanceof Token ? currencyOut : Token.WSOL;
logger.assertArgument(
Liquidity.includesToken(tokenIn, fromPoolKeys) && Liquidity.includesToken(tokenOut, toPoolKeys),
"pools cannot be routed",
"pools",
{
fromPoolKeys,
toPoolKeys,
},
);
const fromPoolMints = [fromPoolKeys.baseMint.toBase58(), fromPoolKeys.quoteMint.toBase58()];
const toPoolMints = [toPoolKeys.baseMint.toBase58(), toPoolKeys.quoteMint.toBase58()];
const mints = [...fromPoolMints, ...toPoolMints];
const decimals = [
fromPoolInfo.baseDecimals,
fromPoolInfo.quoteDecimals,
toPoolInfo.baseDecimals,
toPoolInfo.quoteDecimals,
];
const mintIn = tokenIn.mint.toBase58();
const mintOut = tokenOut.mint.toBase58();
const xorMints = xor(fromPoolMints, toPoolMints);
logger.assertArgument(
xorMints.length === 2 && xorMints.includes(mintIn) && xorMints.includes(mintOut),
"xor tokens not match",
"pools",
{
fromPoolKeys,
toPoolKeys,
},
);
const intersectionMints = intersection(fromPoolMints, toPoolMints);
logger.assertArgument(intersectionMints.length === 1, "cannot found middle token of two pools", "pools", {
fromPoolKeys,
toPoolKeys,
});
const _middleMint = intersectionMints[0];
const index = mints.indexOf(_middleMint);
// logger.assertArgument(index !== -1, "cannot found middle token", "pools", {
// fromPoolKeys,
// toPoolKeys,
// });
const middleMintDecimals = decimals[index];
const middleMint = new PublicKey(_middleMint);
const middleToken = new Token(middleMint, middleMintDecimals);
logger.debug("from pool:", fromPoolKeys);
logger.debug("to pool:", toPoolKeys);
logger.debug("intersection mints:", intersectionMints);
logger.debug("xor mints:", xorMints);
logger.debug("middleMint:", _middleMint);
// TODO slippage and amount out
const {
amountOut: middleAmountOut,
minAmountOut: minMiddleAmountOut,
priceImpact: firstPriceImpact,
fee: firstFee,
} = Liquidity.computeAmountOut({
poolKeys: fromPoolKeys,
poolInfo: fromPoolInfo,
amountIn,
currencyOut: middleToken,
slippage,
});
const {
amountOut,
minAmountOut,
priceImpact: secondPriceImpact,
fee: secondFee,
} = Liquidity.computeAmountOut({
poolKeys: toPoolKeys,
poolInfo: toPoolInfo,
amountIn: minMiddleAmountOut,
currencyOut,
slippage,
});
let executionPrice: Price | null = null;
const amountInRaw = amountIn.raw;
const amountOutRaw = amountOut.raw;
const currencyIn = amountIn instanceof TokenAmount ? amountIn.token : amountIn.currency;
if (!amountInRaw.isZero() && !amountOutRaw.isZero()) {
executionPrice = new Price(currencyIn, amountInRaw, currencyOut, amountOutRaw);
logger.debug("executionPrice:", `1 ${currencyIn.symbol} ≈ ${executionPrice.toFixed()} ${currencyOut.symbol}`);
logger.debug(
"executionPrice invert:",
`1 ${currencyOut.symbol} ≈ ${executionPrice.invert().toFixed()} ${currencyIn.symbol}`,
);
}
return {
// middleAmountOut,
// minMiddleAmountOut,
amountOut,
minAmountOut,
executionPrice,
priceImpact: firstPriceImpact.add(secondPriceImpact),
fee: [firstFee, secondFee],
};
}