Skip to content

Commit

Permalink
Return result of import in JS API, fixes #134.
Browse files Browse the repository at this point in the history
  • Loading branch information
RillingDev committed Dec 18, 2022
1 parent e30a7dd commit a8595ea
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 21 deletions.
12 changes: 11 additions & 1 deletion src/application/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ export interface ApplicationApi {
*
* May only be called after `ready` event was emitted.
*/
readonly setDeck: (newDeck: ExternalDeck<SlimExternalCard>) => void;
readonly setDeck: (
newDeck: ExternalDeck<SlimExternalCard>
) => SetDeckResult;

/**
* Shuffles the current deck.
Expand Down Expand Up @@ -65,3 +67,11 @@ export interface ExternalCard {
export type SlimExternalCard = Pick<ExternalCard, "passcode">;

export type Callback = () => void;

export interface SetDeckResult {
readonly deck: ExternalDeck<ExternalCard>;
/**
* Missing passcodes.
*/
readonly missing: ReadonlyArray<string>;
}
57 changes: 37 additions & 20 deletions src/application/bridge.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Card, Deck } from "@/core/lib";
import type { Card, Deck, ImportResult } from "@/core/lib";
import { DeckPart, FindCardBy, getLogger } from "@/core/lib";
import type {
ApplicationApi,
Expand Down Expand Up @@ -33,24 +33,39 @@ const toExternalCard = ({ passcode, name }: Card): ExternalCard => {
const fromExternalDeck = ({
name,
parts,
}: ExternalDeck<SlimExternalCard>): Deck => {
}: ExternalDeck<SlimExternalCard>): ImportResult => {
const missing: string[] = [];

const fromExternalCards = (
externalCards: ReadonlyArray<SlimExternalCard>
): Card[] => {
return externalCards
.filter(({ passcode }) => {
if (cardDatabase.hasCard(passcode, FindCardBy.PASSCODE)) {
return true;
}
missing.push(passcode);
return false;
})
.map(
({ passcode }) =>
cardDatabase.getCard(passcode, FindCardBy.PASSCODE)!
);
};

return {
name,
parts: {
[DeckPart.MAIN]: parts.main.map(fromExternalCard),
[DeckPart.EXTRA]: parts.extra.map(fromExternalCard),
[DeckPart.SIDE]: parts.side.map(fromExternalCard),
deck: {
name,
parts: {
[DeckPart.MAIN]: fromExternalCards(parts.main),
[DeckPart.EXTRA]: fromExternalCards(parts.extra),
[DeckPart.SIDE]: fromExternalCards(parts.side),
},
},
missing,
};
};

const fromExternalCard = ({ passcode }: SlimExternalCard): Card => {
if (!cardDatabase.hasCard(passcode, FindCardBy.PASSCODE)) {
throw new TypeError(`Card with passcode '${passcode}' not found.`);
}
return cardDatabase.getCard(passcode, FindCardBy.PASSCODE)!;
};

/**
* Minimal event emitter.
*/
Expand Down Expand Up @@ -103,22 +118,24 @@ export const useBridge = (): ApplicationApi => {
logger.debug("Exporting current deck state...");
return toExternalDeck(deckStore.deck);
},
setDeck(newDeck: ExternalDeck<SlimExternalCard>): void {
setDeck(newDeck: ExternalDeck<SlimExternalCard>) {
logger.debug("Replacing current deck state...");
const { deck, missing } = fromExternalDeck(newDeck);
deckStore.replace({
deck: fromExternalDeck(newDeck),
deck,
});
return { deck: toExternalDeck(deck), missing };
},
shuffleDeck(): void {
shuffleDeck() {
deckStore.shuffle();
},
sortDeck(): void {
sortDeck() {
deckStore.sort();
},
clearDeck(): void {
clearDeck() {
deckStore.clear();
},
on(event: ApplicationEvent, callback: Callback): void {
on(event: ApplicationEvent, callback: Callback) {
logger.debug(
`Registering event subscription for event '${event}'...`
);
Expand Down
3 changes: 3 additions & 0 deletions src/core/deck/DeckFileService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import { DefaultDeckPartConfig } from "./DeckPartConfig";

export interface ImportResult {
readonly deck: Deck;
/**
* Missing passcodes.
*/
readonly missing: ReadonlyArray<string>;
}

Expand Down

0 comments on commit a8595ea

Please sign in to comment.