Skip to content

Conversation

bananaumai
Copy link
Contributor

@bananaumai bananaumai commented Oct 4, 2020

About

  • removed duplicated import statement
  • fixed invalid assignment statement

Note

BTW, when I tries to run the example code for working with extension type, I have faced the following tsc compile error.

error TS2322: Type 'ExtensionCodec<MyContext>' is not assignable to type 'ExtensionCodecType<undefined>'.
  Types of property '__brand' are incompatible.
    Type 'MyContext' is not assignable to type 'undefined'.

I'm investigating the issue it right now. If there'll be something worth to be considered, I'll suggest another PR or raise up an issue.

* remove duplicated import statement
* fix invalid assignment statement
@gfx
Copy link
Member

gfx commented Oct 4, 2020

Looks good to me. Thank you!

For the context trick in ExtensionCodec, see its test:

context("custom extensions with custom context", () => {
class Context {
public expectations: Array<any> = [];
constructor(public ctxVal: number) {}
public hasVisited(val: any) {
this.expectations.push(val);
}
}
const extensionCodec = new ExtensionCodec<Context>();
class Magic<T> {
constructor(public val: T) {}
}
// Magic
extensionCodec.register({
type: 0,
encode: (object: unknown, context): Uint8Array | null => {
if (object instanceof Magic) {
context.hasVisited({ encoding: object.val });
return encode({ magic: object.val, ctx: context.ctxVal }, { extensionCodec, context });
} else {
return null;
}
},
decode: (data: Uint8Array, extType, context) => {
extType;
const decoded = decode(data, { extensionCodec, context }) as { magic: number };
context.hasVisited({ decoding: decoded.magic, ctx: context.ctxVal });
return new Magic(decoded.magic);
},
});

@gfx gfx closed this Oct 4, 2020
@gfx gfx reopened this Oct 4, 2020
@codecov-commenter
Copy link

Codecov Report

Merging #135 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #135   +/-   ##
=======================================
  Coverage   98.29%   98.29%           
=======================================
  Files          15       15           
  Lines         936      936           
  Branches      205      205           
=======================================
  Hits          920      920           
  Misses         16       16           

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 348fa77...7eaf1ea. Read the comment docs.

@gfx gfx merged commit 6ed7993 into msgpack:master Oct 4, 2020
@bananaumai bananaumai deleted the fix-readme-example-code branch October 4, 2020 06:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants