From 54dcceb5ef2c42f84fc6e3584cdaaccf7ac433f7 Mon Sep 17 00:00:00 2001 From: Roman Dvornov Date: Wed, 24 Jul 2024 15:33:46 +0200 Subject: [PATCH] Tweak Encoding type and encodings docs --- docs/encodings.md | 18 +++++++++++------- src/core/utils/load-data.types.ts | 16 +++++++--------- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/docs/encodings.md b/docs/encodings.md index efe3402d..f62b3410 100644 --- a/docs/encodings.md +++ b/docs/encodings.md @@ -4,21 +4,25 @@ Discovery.js allows the configuration of custom encodings in addition to the def - `name`: A unique string identifier for the encoding. - `test`: A function that receives the first chunk of the payload and a `done` flag (indicating whether more payload is expected or if it's complete) and returns a boolean to indicate if the encoding is applicable. -- `streaming` (optional, defaults to `false`): Determines if the encoding supports streaming. If `true`, the `decode` function is passed an async iterator that yields `Uint8Array` instances; if `false`, `decode` receives the entire payload as a single `Uint8Array`. -- `decode`: A function that decodes the payload into a JavaScript value. It accepts an async iterator or `Uint8Array`, depending on the `streaming` option. +- `streaming` (optional, defaults to `false`): Determines if the encoding supports streaming processing. +- `decode`: A function that decodes the payload into a JavaScript value. It accepts an async iterator if `streaming` is `true`, or `Uint8Array` otherwise. The TypeScript type definition for an encoding is as follows: ```ts type Encoding = { name: string; - test(chunk: Uint8Array, done: boolean): boolean; - streaming?: boolean; - decode(data: AsyncIterableIterator | Uint8Array): any; -}; + test(chunk: Uint8Array): boolean; +} & ({ + streaming: true; + decode(iterator: AsyncIterableIterator): Promise; +} | { + streaming: false; + decode(payload: Uint8Array): any; +}); ``` -Encodings can be set using the `encodings` option in both `App` and `Widget` configurations. However, it currently only affects the `App` instance, as only `App` has methods for loading data. The [Loading Data API](./load-data.md) applies the specified encodings to the data payload, and `App` integrates these encodings into its `load*` method calls if they are specified upon initialization. +Encodings can be set using the `encodings` option in `Model`, `Widget` and `App` configurations. The [Loading Data API](./load-data.md) applies the specified encodings to the data payload, and `Model` (base class for `App` and `Widget`) integrates these encodings into its `loadData*` method calls if they are specified upon initialization. In addition to `App` and `Widget`, preloaders can pass the `encodings` configuration to a data loader if specified in `loadDataOptions`. diff --git a/src/core/utils/load-data.types.ts b/src/core/utils/load-data.types.ts index f518739f..474518a1 100644 --- a/src/core/utils/load-data.types.ts +++ b/src/core/utils/load-data.types.ts @@ -1,18 +1,16 @@ import type { Observer } from '../observer.js'; export type Encoding = - | { + { name: string; - test: (chunk: Uint8Array) => boolean; + test(chunk: Uint8Array): boolean; + } & ({ streaming: true; - decode: (iterator: AsyncIterableIterator) => Promise; - } - | { - name: string; - test: (chunk: Uint8Array) => boolean; + decode(iterator: AsyncIterableIterator): Promise; + } | { streaming: false; - decode: (payload: Uint8Array) => any; - }; + decode(payload: Uint8Array): any; + }); export type LoadDataResult = { state: Observer;