Skip to content

Commit

Permalink
Tweak Encoding type and encodings docs
Browse files Browse the repository at this point in the history
  • Loading branch information
lahmatiy committed Jul 24, 2024
1 parent 32660bb commit 54dcceb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
18 changes: 11 additions & 7 deletions docs/encodings.md
Original file line number Diff line number Diff line change
Expand Up @@ -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> | Uint8Array): any;
};
test(chunk: Uint8Array): boolean;
} & ({
streaming: true;
decode(iterator: AsyncIterableIterator<Uint8Array>): Promise<any>;
} | {
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`.

Expand Down
16 changes: 7 additions & 9 deletions src/core/utils/load-data.types.ts
Original file line number Diff line number Diff line change
@@ -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<Uint8Array>) => Promise<any>;
}
| {
name: string;
test: (chunk: Uint8Array) => boolean;
decode(iterator: AsyncIterableIterator<Uint8Array>): Promise<any>;
} | {
streaming: false;
decode: (payload: Uint8Array) => any;
};
decode(payload: Uint8Array): any;
});

export type LoadDataResult = {
state: Observer<LoadDataState>;
Expand Down

0 comments on commit 54dcceb

Please sign in to comment.