From 63c854437182ddb5083d88e698dab7cb7b60dc41 Mon Sep 17 00:00:00 2001 From: squidfunk Date: Wed, 18 Dec 2019 22:06:59 +0100 Subject: [PATCH] Added type guards for worker messages --- .../javascripts/templates/search/index.ts | 2 - .../javascripts/workers/packer/_/index.ts | 42 +++++++++-- .../javascripts/workers/packer/main/index.ts | 4 +- .../javascripts/workers/search/_/index.ts | 75 ++++++++++++++++--- 4 files changed, 102 insertions(+), 21 deletions(-) diff --git a/src/assets/javascripts/templates/search/index.ts b/src/assets/javascripts/templates/search/index.ts index 90cd73a21..d41a92f82 100644 --- a/src/assets/javascripts/templates/search/index.ts +++ b/src/assets/javascripts/templates/search/index.ts @@ -21,5 +21,3 @@ */ export * from "./_" -export * from "./article" -export * from "./section" diff --git a/src/assets/javascripts/workers/packer/_/index.ts b/src/assets/javascripts/workers/packer/_/index.ts index 657da1d64..dbf46052b 100644 --- a/src/assets/javascripts/workers/packer/_/index.ts +++ b/src/assets/javascripts/workers/packer/_/index.ts @@ -30,7 +30,7 @@ */ export const enum PackerMessageType { STRING, /* String data */ - PACKED /* Packed data */ + BINARY /* Packed data */ } /* ------------------------------------------------------------------------- */ @@ -38,7 +38,7 @@ export const enum PackerMessageType { /** * A message containing an unpacked string */ -interface StringMessage { +export interface PackerStringMessage { type: PackerMessageType.STRING /* Message type */ data: string /* Message data */ } @@ -46,8 +46,8 @@ interface StringMessage { /** * A message containing a packed string */ -interface PackedMessage { - type: PackerMessageType.PACKED /* Message type */ +export interface PackerBinaryMessage { + type: PackerMessageType.BINARY /* Message type */ data: string /* Message data */ } @@ -57,5 +57,35 @@ interface PackedMessage { * A message exchanged with the packer worker */ export type PackerMessage = - | StringMessage - | PackedMessage + | PackerStringMessage + | PackerBinaryMessage + +/* ---------------------------------------------------------------------------- + * Functions + * ------------------------------------------------------------------------- */ + +/** + * Type guard for packer binary messages + * + * @param message - Packer worker message + * + * @return Test result + */ +export function isPackerBinaryMessage( + message: PackerMessage +): message is PackerBinaryMessage { + return message.type === PackerMessageType.BINARY +} + +/** + * Type guard for packer string messages + * + * @param message - Packer worker message + * + * @return Test result + */ +export function isPackerStringMessage( + message: PackerMessage +): message is PackerStringMessage { + return message.type === PackerMessageType.STRING +} diff --git a/src/assets/javascripts/workers/packer/main/index.ts b/src/assets/javascripts/workers/packer/main/index.ts index 7a5e6b5f3..963130d7e 100644 --- a/src/assets/javascripts/workers/packer/main/index.ts +++ b/src/assets/javascripts/workers/packer/main/index.ts @@ -77,12 +77,12 @@ export function handler(message: PackerMessage): PackerMessage { /* Pack an unpacked string */ case PackerMessageType.STRING: return { - type: PackerMessageType.PACKED, + type: PackerMessageType.BINARY, data: pack(message.data) } /* Unpack a packed string */ - case PackerMessageType.PACKED: + case PackerMessageType.BINARY: return { type: PackerMessageType.STRING, data: unpack(message.data) diff --git a/src/assets/javascripts/workers/search/_/index.ts b/src/assets/javascripts/workers/search/_/index.ts index 81222f76b..9b7ecbfe7 100644 --- a/src/assets/javascripts/workers/search/_/index.ts +++ b/src/assets/javascripts/workers/search/_/index.ts @@ -20,10 +20,7 @@ * IN THE SOFTWARE. */ -import { Subject } from "rxjs" - import { SearchIndex, SearchResult } from "modules" -import { watchWorker } from "utilities" /* ---------------------------------------------------------------------------- * Types @@ -44,7 +41,7 @@ export const enum SearchMessageType { /** * A message containing the data necessary to setup the search index */ -interface SetupMessage { +export interface SearchSetupMessage { type: SearchMessageType.SETUP /* Message type */ data: SearchIndex /* Message data */ } @@ -52,7 +49,7 @@ interface SetupMessage { /** * A message containing the a dump of the search index */ -interface DumpMessage { +export interface SearchDumpMessage { type: SearchMessageType.DUMP /* Message type */ data: string /* Message data */ } @@ -60,7 +57,7 @@ interface DumpMessage { /** * A message containing a search query */ -interface QueryMessage { +export interface SearchQueryMessage { type: SearchMessageType.QUERY /* Message type */ data: string /* Message data */ } @@ -68,7 +65,7 @@ interface QueryMessage { /** * A message containing results for a search query */ -interface ResultMessage { +export interface SearchResultMessage { type: SearchMessageType.RESULT /* Message type */ data: SearchResult[] /* Message data */ } @@ -79,7 +76,63 @@ interface ResultMessage { * A message exchanged with the search worker */ export type SearchMessage = - | SetupMessage - | DumpMessage - | QueryMessage - | ResultMessage + | SearchSetupMessage + | SearchDumpMessage + | SearchQueryMessage + | SearchResultMessage + +/* ---------------------------------------------------------------------------- + * Functions + * ------------------------------------------------------------------------- */ + +/** + * Type guard for search setup messages + * + * @param message - Search worker message + * + * @return Test result + */ +export function isSearchSetupMessage( + message: SearchMessage +): message is SearchSetupMessage { + return message.type === SearchMessageType.SETUP +} + +/** + * Type guard for search dump messages + * + * @param message - Search worker message + * + * @return Test result + */ +export function isSearchDumpMessage( + message: SearchMessage +): message is SearchDumpMessage { + return message.type === SearchMessageType.DUMP +} + +/** + * Type guard for search query messages + * + * @param message - Search worker message + * + * @return Test result + */ +export function isSearchQueryMessage( + message: SearchMessage +): message is SearchQueryMessage { + return message.type === SearchMessageType.QUERY +} + +/** + * Type guard for search result messages + * + * @param message - Search worker message + * + * @return Test result + */ +export function isSearchResultMessage( + message: SearchMessage +): message is SearchResultMessage { + return message.type === SearchMessageType.RESULT +}