Skip to content

Commit

Permalink
fix: more generic BlockCodec, remove encoder & decoder props
Browse files Browse the repository at this point in the history
This is what we probably should have done to solve the conflict in cea9063
because we ended up with a `BlockCodec` that has too many properties and is
therefore not very useful.

This makes `BlockCodec` just an implementer of both `BlockEncoder` and
`BlockDecoder` but `codec()` returns you that, plus `encoder` and `decoder`
properties so you have all the things and can consume them however you like.

Ref: ipld/js-dag-cbor#18 (comment)
  • Loading branch information
rvagg committed Apr 8, 2021
1 parent 6d3bb8a commit b95e6cb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
31 changes: 17 additions & 14 deletions src/codecs/codec.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
// @ts-check

/**
* @template {number} Code
* @template T
* @typedef {import('./interface').BlockCodec<Code, T>} BlockCodec
*/
/**
* @template {number} Code
* @template T
* @typedef {import('./interface').BlockEncoder<Code, T>} BlockEncoder
*/
/**
* @template {number} Code
* @template T
* @typedef {import('./interface').BlockDecoder<Code, T>} BlockDecoder
*/

/**
* @template {string} Name
* @template {number} Code
* @template T
*
* @param {Object} options
* @param {Name} options.name
* @param {Code} options.code
* @param {(data:T) => Uint8Array} options.encode
* @param {(bytes:Uint8Array) => T} options.decode
* @returns {import('./interface').BlockCodec<Code, T>}
* @returns {BlockCodec<Code, T> & { encoder: BlockEncoder<Code, T>, decoder: BlockDecoder<Code, T> }}
*/
export const codec = ({ name, code, decode, encode }) => {
const decoder = new Decoder(name, code, decode)
Expand All @@ -19,12 +34,6 @@ export const codec = ({ name, code, decode, encode }) => {
return { name, code, decode, encode, decoder, encoder }
}

/**
* @template {number} Code
* @template T
* @typedef {import('./interface').BlockEncoder<Code, T>} BlockEncoder
*/

/**
* @class
* @template T
Expand All @@ -45,12 +54,6 @@ export class Encoder {
}
}

/**
* @template {number} Code
* @template T
* @typedef {import('./interface').BlockDecoder<Code, T>} BlockDecoder
*/

/**
* @class
* @template {number} Code
Expand Down
9 changes: 3 additions & 6 deletions src/codecs/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,11 @@ export interface BlockDecoder<Code extends number, T> {
* separate those capabilties as sender requires encoder and receiver
* requires decoder.
*/
export interface BlockCodec<Code extends number, T> extends BlockEncoder<Code, T>, BlockDecoder<Code, T> {
encoder: BlockEncoder<Code, T>,
decoder: BlockDecoder<Code, T>
}
export interface BlockCodec<Code extends number, T> extends BlockEncoder<Code, T>, BlockDecoder<Code, T> {}


// This just a hack to retain type information abouth the data that
// is incoded `T` Because it's a union `data` field is never going
// This just a hack to retain type information about the data that
// is encoded `T` Because it's a union `data` field is never going
// to be usable anyway.
export type ByteView<T> =
| Uint8Array
Expand Down

0 comments on commit b95e6cb

Please sign in to comment.