-
Notifications
You must be signed in to change notification settings - Fork 354
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Base
as an interface instead of a class
#172
Conversation
Related #161 |
One additional thing, for UInt, probably want to extends BN (When going for BigInt, probably need to extends that). I'm currently doing a lot of stuff such as -
(in the observable API - they are all BlockNumber, but have a conversion for each and then a final wrap) For the time-being, will probably just fake add/sub/mul (a couple of them), that way when we extend properly, there is no change, just thing being more effective) |
cb7b919
to
91c3ec8
Compare
Things I'm not 100% satisfied:
Still TODO:
|
this.raw = Moment.decode(input); | ||
|
||
return this; | ||
// FIXME this returns a new Object unfortunately, can't "replace" current value |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove from, as per your other PR.
|
||
const BITLENGTH = 64; | ||
|
||
// A wrapper around seconds/timestamps. Internally the representation only has | ||
// second precicion (aligning with Rust), so any numbers passed an/out are always | ||
// per-second. For any encoding/decoding the 1000 multiplier would be applied to | ||
// get it in line with JavaScript formats | ||
export default class Moment extends Base<Date> { | ||
export default class Moment extends Date implements Codec<Moment> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this correct, i.e. Codec<Moment>
(e.g. Bool above extends built-in JS Boolean
class, one of the 2 is wrong)
// Two solutions: | ||
// - either use static | ||
// - or completely remove from*, and force to use constructor | ||
return new Text(u8aToUtf8(input.subarray(offset, offset + length.toNumber()))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeap, the other PR needs to go first. (This will just create too much messy-ness)
@@ -4,6 +4,11 @@ | |||
|
|||
import Enum from './Enum'; | |||
|
|||
enum TestEnum { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could actually go full-hog ts specs now. (Previously it was problematic since I fed it garbage data - either way, not needed right now)
super( | ||
value instanceof U8aFixed | ||
? value | ||
: U8aFixed.trimLength(toU8a(value), bitLength / 8) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just check current master as well, I had some hoops to jump through - well, if not corrects, tests do catch this one.
private _bitLength: BitLength; | ||
private _isHexJson: boolean; | ||
|
||
constructor (value: AnyNumber = 0, bitLength: BitLength = 64, isHexJson: boolean = true) { | ||
super( | ||
UInt.decode(value) | ||
UInt.decode(value).toNumber() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cannot use toNumber here - JS numbers are limited to 53 bits (on uints) - rather toString() it into a decimal representation.
@@ -11,4 +11,16 @@ export type AnyNumber = UInt | BN | Uint8Array | number | string; | |||
|
|||
export type AnyU8a = U8a | Uint8Array | Array<number> | string; | |||
|
|||
export interface Codec<T> { | |||
byteLength(): number; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could, to get past the BN vs Array issue, call this encodedLength
, which is actually exactly what it is. (As a property/getter)
This is a clusterfuck of merge conflicts. Will open a new one once everything else is ready. |
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Proposal: remove the Base class, use an interface
Codec<T>
instead, that all api-codec types needs to implement (+/- similar to a rust trait).In particular, use as much as possible existing JavaScript classes (Array, Map, ...) and create child classes that implement
Codec<T>
.Types:
Note: Tests don't change at all, cf
*.spec.js
, only syntax simplifications.@jacogr If this is something that's interesting, I'll have a look more into it tomorrow.