Skip to content

Commit

Permalink
a pass for strictNullChecks (many more left!)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielweck committed Jul 24, 2019
1 parent 6eb74a2 commit 4f5ce31
Show file tree
Hide file tree
Showing 20 changed files with 124 additions and 101 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"build": "cross-env NODE_ENV=production webpack --config webpack.config.js && ncp src/package.json dist/package.json",
"build:dev:main": "webpack --config webpack.config.main.js",
"start": "npm run build && cross-env DEBUG=r2:*,readium-desktop:* electron .",
"lint:ts": "tslint --project tsconfig.json -c tslint.json './src/**/*.ts' './src/**/*.tsx'",
"lint:ts": "tslint --project tsconfig.json -c tslint.json './src/**/*.ts' './src/**/*.tsx' './test/**/*.ts' './test/**/*.tsx'",
"lint:editorconfig": "eclint check '**/*' '!.vscode/**/*' '!.git/**/*' '!node_modules/**/*' '!resources/**/*' '!src/renderer/assets/**/*' '!dist/**/*' '!**/.DS_Store'",
"lint": "npm run lint:editorconfig && npm run lint:ts",
"start:dev:renderer-reader": "concurrently --kill-others \"npm run start:dev:renderer\" \"npm run start:dev:reader\"",
Expand Down
4 changes: 2 additions & 2 deletions src/common/models/timestampable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
// ==LICENSE-END==

export interface Timestampable {
createdAt: string; // ISO 8601 string
updatedAt: string; // ISO 8601 string
createdAt?: string; // ISO 8601 string
updatedAt?: string; // ISO 8601 string
}
2 changes: 1 addition & 1 deletion src/common/redux/states/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
// ==LICENSE-END==

export interface LoggerState {
lastMessage: string;
lastMessage: string | null;
}
14 changes: 8 additions & 6 deletions src/common/services/opds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,13 @@ export class OPDSParser {
files: [],
};

// Fill authors
for (const author of entry.authors) {
publication.authors.push({
name: author.name,
});
if (publication.authors) {
// Fill authors
for (const author of entry.authors) {
publication.authors.push({
name: author.name,
});
}
}

// Set language
Expand All @@ -78,7 +80,7 @@ export class OPDSParser {
ext: extObj,
};
}
if (link.type === TYPE_EPUB) {
if (publication.files && link.type === TYPE_EPUB) {
// We found the EPUB link
const urlObj = new URL(link.href);
let extObj = path.extname(urlObj.pathname);
Expand Down
4 changes: 2 additions & 2 deletions src/common/views/opds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ export enum OpdsResultType {
export interface OpdsResultView {
title: string;
type: OpdsResultType;
navigation: OpdsLinkView[];
publications: OpdsPublicationView[];
navigation?: OpdsLinkView[];
publications?: OpdsPublicationView[];
}
11 changes: 7 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ setLcpNativePluginPath(lcpNativePluginPath);

// Global reference to the main window,
// so the garbage collector doesn't close it.
let mainWindow: BrowserWindow = null;
let mainWindow: BrowserWindow | null = null;

initSessions();

Expand Down Expand Up @@ -180,7 +180,7 @@ async function createWindow() {

// Redirect link to an external browser
const handleRedirect = (event: any, url: any) => {
if (url === mainWindow.webContents.getURL()) {
if (!mainWindow || url === mainWindow.webContents.getURL()) {
return;
}

Expand All @@ -202,8 +202,11 @@ async function createWindow() {

const debounceSavedWindowsRectangle = debounce(savedWindowsRectangle, 500);

mainWindow.on("move", () =>
debounceSavedWindowsRectangle(mainWindow.getBounds()));
mainWindow.on("move", () => {
if (mainWindow) {
debounceSavedWindowsRectangle(mainWindow.getBounds());
}
});
}

function registerProtocol() {
Expand Down
10 changes: 5 additions & 5 deletions src/main/api/catalog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@ export const CATALOG_CONFIG_ID = "catalog";
@injectable()
export class CatalogApi {
@inject("publication-repository")
private publicationRepository: PublicationRepository;
private readonly publicationRepository!: PublicationRepository;

@inject("config-repository")
private configRepository: ConfigRepository;
private readonly configRepository!: ConfigRepository;

@inject("locator-repository")
private locatorRepository: LocatorRepository;
private readonly locatorRepository!: LocatorRepository;

@inject("publication-view-converter")
private publicationViewConverter: PublicationViewConverter;
private readonly publicationViewConverter!: PublicationViewConverter;

@inject("translator")
private translator: Translator;
private readonly translator!: Translator;

public async get(): Promise<CatalogView> {
const __ = this.translator.translate.bind(this.translator);
Expand Down
6 changes: 3 additions & 3 deletions src/main/api/lcp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ import { PublicationRepository } from "readium-desktop/main/db/repository/public
@injectable()
export class LcpApi {
@inject("store")
private store: Store<any>;
private readonly store!: Store<any>;

@inject("publication-repository")
private publicationRepository: PublicationRepository;
private readonly publicationRepository!: PublicationRepository;

@inject("lcp-manager")
private lcpManager: LcpManager;
private readonly lcpManager!: LcpManager;

public async renewPublicationLicense(data: any): Promise<void> {
const { publication } = data;
Expand Down
15 changes: 5 additions & 10 deletions src/main/api/opds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,11 @@ import { httpGet } from "readium-desktop/common/utils";

@injectable()
export class OpdsApi {
private opdsFeedRepository: OpdsFeedRepository;
private opdsFeedViewConverter: OpdsFeedViewConverter;

constructor(
@inject("opds-feed-repository") opdsFeedRepository: OpdsFeedRepository,
@inject("opds-feed-view-converter") opdsFeedViewConverter: OpdsFeedViewConverter,
) {
this.opdsFeedRepository = opdsFeedRepository;
this.opdsFeedViewConverter = opdsFeedViewConverter;
}
@inject("opds-feed-repository")
private readonly opdsFeedRepository!: OpdsFeedRepository;

@inject("opds-feed-view-converter")
private readonly opdsFeedViewConverter!: OpdsFeedViewConverter;

public async getFeed(data: any): Promise<OpdsFeedView> {
const { identifier } = data;
Expand Down
28 changes: 12 additions & 16 deletions src/main/api/publication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,14 @@ import { open } from "readium-desktop/common/redux/actions/toast";

@injectable()
export class PublicationApi {
private publicationRepository: PublicationRepository;
private publicationViewConverter: PublicationViewConverter;
private catalogService: CatalogService;

constructor(
@inject("publication-repository") publicationRepository: PublicationRepository,
@inject("publication-view-converter") publicationViewConverter: PublicationViewConverter,
@inject("catalog-service") catalogService: CatalogService,
) {
this.publicationRepository = publicationRepository;
this.publicationViewConverter = publicationViewConverter;
this.catalogService = catalogService;
}
@inject("publication-repository")
private readonly publicationRepository!: PublicationRepository;

@inject("publication-view-converter")
private readonly publicationViewConverter!: PublicationViewConverter;

@inject("catalog-service")
private readonly catalogService!: CatalogService;

public async get(data: any): Promise<PublicationView> {
const { identifier } = data;
Expand Down Expand Up @@ -86,7 +81,7 @@ export class PublicationApi {
return this.publicationRepository.getAllTags();
}

public async importOpdsEntry(data: any): Promise<PublicationView[]> {
public async importOpdsEntry(data: any) {
const { url, base64OpdsPublication, downloadSample, title } = data;
this.dispatchToastRequest(ToastType.DownloadStarted, "message.download.start", title);
let publication;
Expand All @@ -97,7 +92,6 @@ export class PublicationApi {
publication = await this.catalogService.importOpdsPublication(opdsPublication, downloadSample);
}
this.dispatchToastRequest(ToastType.DownloadComplete, "message.download.success", publication.title);
return null;
}

public async import(data: any): Promise<PublicationView[]> {
Expand All @@ -108,7 +102,9 @@ export class PublicationApi {

for (const path of paths) {
const newDoc = await this.catalogService.importFile(path);
newDocs.push(newDoc);
if (newDoc) {
newDocs.push(newDoc);
}
}

return newDocs.map((doc) => {
Expand Down
6 changes: 3 additions & 3 deletions src/main/api/reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ import { LocatorType } from "readium-desktop/common/models/locator";
@injectable()
export class ReaderApi {
@inject("locator-repository")
private locatorRepository: LocatorRepository;
private readonly locatorRepository!: LocatorRepository;

@inject("locator-view-converter")
private locatorViewConverter: LocatorViewConverter;
private readonly locatorViewConverter!: LocatorViewConverter;

public async setLastReadingLocation(data: any): Promise<LocatorView> {
const { publication, locator } = data;
Expand Down Expand Up @@ -54,7 +54,7 @@ export class ReaderApi {
return this.locatorViewConverter.convertDocumentToView(savedDoc);
}

public async getLastReadingLocation(data: any): Promise<LocatorView> {
public async getLastReadingLocation(data: any): Promise<LocatorView | null> {
const { publication } = data;
const docs = await this.locatorRepository.findByPublicationIdentifierAndLocatorType(
publication.identifier,
Expand Down
20 changes: 11 additions & 9 deletions src/main/converter/opds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import {
convertMultiLangStringToString,
} from "readium-desktop/common/utils";

import { CoverView } from "readium-desktop/common/views/publication";

@injectable()
export class OpdsFeedViewConverter {
public convertDocumentToView(document: OpdsFeedDocument): OpdsFeedView {
Expand All @@ -48,7 +50,7 @@ export class OpdsFeedViewConverter {
return {
title,
url: link.Href,
publicationCount: (link.Children) ? link.Children.length : null,
publicationCount: (link.Children) ? link.Children.length : undefined,
};
}

Expand All @@ -63,13 +65,13 @@ export class OpdsFeedViewConverter {
tags = metadata.Subject.map((subject) => convertMultiLangStringToString(subject.Name));
}

let publishedAt = null;
let publishedAt: string | undefined;

if (metadata.PublicationDate) {
publishedAt = moment(metadata.PublicationDate).toISOString();
}

let cover = null;
let cover: CoverView | undefined;

if (publication.Images && publication.Images.length > 0) {
cover = {
Expand All @@ -78,8 +80,8 @@ export class OpdsFeedViewConverter {
}

// Get odps entry
let url = null;
let sampleUrl = null;
let url: string | undefined;
let sampleUrl: string | undefined;
const links = publication.Links.filter(
(link: any) => {
return (link.TypeLink.indexOf(";type=entry;profile=opds-catalog") > 0);
Expand All @@ -96,7 +98,7 @@ export class OpdsFeedViewConverter {
sampleUrl = sampleLinks[0].Href;
}

let base64OpdsPublication = null;
let base64OpdsPublication: string | undefined;
if (links.length > 0) {
url = links[0].Href;
} else {
Expand Down Expand Up @@ -144,7 +146,7 @@ export class OpdsFeedViewConverter {
buyUrl: buyLink && buyLink.Href,
borrowUrl: borrowLink && borrowLink.Href,
subscribeUrl: subscribeLink && subscribeLink.Href,
hasSample: sampleUrl && true,
hasSample: (typeof sampleUrl !== "undefined") && true,
base64OpdsPublication,
isFree,
};
Expand All @@ -153,8 +155,8 @@ export class OpdsFeedViewConverter {
public convertOpdsFeedToView(feed: OPDSFeed): OpdsResultView {
const title = convertMultiLangStringToString(feed.Metadata.Title);
let type = OpdsResultType.NavigationFeed;
let navigation = null;
let publications = null;
let navigation: OpdsLinkView[] | undefined;
let publications: OpdsPublicationView[] | undefined;

if (feed.Publications) {
// result page containing publications
Expand Down
4 changes: 2 additions & 2 deletions src/main/db/document/publication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ import { Timestampable } from "readium-desktop/common/models/timestampable";

interface Resources {
filePublication: string;
opdsPublication: string;
opdsPublication?: string;
}

export interface PublicationDocument extends Identifiable, Timestampable {
resources: Resources;
opdsPublication: any;
opdsPublication?: any;
title: string;
tags?: string[];
files?: File[];
Expand Down
Loading

0 comments on commit 4f5ce31

Please sign in to comment.