mirror of
https://github.com/squidfunk/mkdocs-material.git
synced 2024-06-14 11:52:32 +03:00
Added type guards for worker messages
This commit is contained in:
parent
68b1e44d1a
commit
63c8544371
@ -21,5 +21,3 @@
|
||||
*/
|
||||
|
||||
export * from "./_"
|
||||
export * from "./article"
|
||||
export * from "./section"
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user