Added type guards for worker messages

This commit is contained in:
squidfunk 2019-12-18 22:06:59 +01:00
parent 68b1e44d1a
commit 63c8544371
4 changed files with 102 additions and 21 deletions

View File

@ -21,5 +21,3 @@
*/ */
export * from "./_" export * from "./_"
export * from "./article"
export * from "./section"

View File

@ -30,7 +30,7 @@
*/ */
export const enum PackerMessageType { export const enum PackerMessageType {
STRING, /* String data */ STRING, /* String data */
PACKED /* Packed data */ BINARY /* Packed data */
} }
/* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */
@ -38,7 +38,7 @@ export const enum PackerMessageType {
/** /**
* A message containing an unpacked string * A message containing an unpacked string
*/ */
interface StringMessage { export interface PackerStringMessage {
type: PackerMessageType.STRING /* Message type */ type: PackerMessageType.STRING /* Message type */
data: string /* Message data */ data: string /* Message data */
} }
@ -46,8 +46,8 @@ interface StringMessage {
/** /**
* A message containing a packed string * A message containing a packed string
*/ */
interface PackedMessage { export interface PackerBinaryMessage {
type: PackerMessageType.PACKED /* Message type */ type: PackerMessageType.BINARY /* Message type */
data: string /* Message data */ data: string /* Message data */
} }
@ -57,5 +57,35 @@ interface PackedMessage {
* A message exchanged with the packer worker * A message exchanged with the packer worker
*/ */
export type PackerMessage = export type PackerMessage =
| StringMessage | PackerStringMessage
| PackedMessage | 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
}

View File

@ -77,12 +77,12 @@ export function handler(message: PackerMessage): PackerMessage {
/* Pack an unpacked string */ /* Pack an unpacked string */
case PackerMessageType.STRING: case PackerMessageType.STRING:
return { return {
type: PackerMessageType.PACKED, type: PackerMessageType.BINARY,
data: pack(message.data) data: pack(message.data)
} }
/* Unpack a packed string */ /* Unpack a packed string */
case PackerMessageType.PACKED: case PackerMessageType.BINARY:
return { return {
type: PackerMessageType.STRING, type: PackerMessageType.STRING,
data: unpack(message.data) data: unpack(message.data)

View File

@ -20,10 +20,7 @@
* IN THE SOFTWARE. * IN THE SOFTWARE.
*/ */
import { Subject } from "rxjs"
import { SearchIndex, SearchResult } from "modules" import { SearchIndex, SearchResult } from "modules"
import { watchWorker } from "utilities"
/* ---------------------------------------------------------------------------- /* ----------------------------------------------------------------------------
* Types * Types
@ -44,7 +41,7 @@ export const enum SearchMessageType {
/** /**
* A message containing the data necessary to setup the search index * A message containing the data necessary to setup the search index
*/ */
interface SetupMessage { export interface SearchSetupMessage {
type: SearchMessageType.SETUP /* Message type */ type: SearchMessageType.SETUP /* Message type */
data: SearchIndex /* Message data */ data: SearchIndex /* Message data */
} }
@ -52,7 +49,7 @@ interface SetupMessage {
/** /**
* A message containing the a dump of the search index * A message containing the a dump of the search index
*/ */
interface DumpMessage { export interface SearchDumpMessage {
type: SearchMessageType.DUMP /* Message type */ type: SearchMessageType.DUMP /* Message type */
data: string /* Message data */ data: string /* Message data */
} }
@ -60,7 +57,7 @@ interface DumpMessage {
/** /**
* A message containing a search query * A message containing a search query
*/ */
interface QueryMessage { export interface SearchQueryMessage {
type: SearchMessageType.QUERY /* Message type */ type: SearchMessageType.QUERY /* Message type */
data: string /* Message data */ data: string /* Message data */
} }
@ -68,7 +65,7 @@ interface QueryMessage {
/** /**
* A message containing results for a search query * A message containing results for a search query
*/ */
interface ResultMessage { export interface SearchResultMessage {
type: SearchMessageType.RESULT /* Message type */ type: SearchMessageType.RESULT /* Message type */
data: SearchResult[] /* Message data */ data: SearchResult[] /* Message data */
} }
@ -79,7 +76,63 @@ interface ResultMessage {
* A message exchanged with the search worker * A message exchanged with the search worker
*/ */
export type SearchMessage = export type SearchMessage =
| SetupMessage | SearchSetupMessage
| DumpMessage | SearchDumpMessage
| QueryMessage | SearchQueryMessage
| ResultMessage | 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
}