11import { CrdtReader } from '../../../../json-crdt-patch/util/binary/CrdtDecoder' ;
2- import { MsgPackDecoderFast } from '../../../../json-pack/msgpack' ;
2+ import { CborDecoderBase } from '../../../../json-pack/cbor/CborDecoderBase' ;
3+ import { CRDT_MAJOR } from './constants' ;
34
4- export class ViewDecoder extends MsgPackDecoderFast < CrdtReader > {
5+ export class ViewDecoder extends CborDecoderBase < CrdtReader > {
56 protected time : number = - 1 ;
67
78 constructor ( ) {
@@ -32,49 +33,30 @@ export class ViewDecoder extends MsgPackDecoderFast<CrdtReader> {
3233 return ! peek ? undefined : this . cNode ( ) ;
3334 }
3435
35- public cNode ( ) : unknown {
36+ protected cNode ( ) : unknown {
3637 const reader = this . reader ;
3738 this . ts ( ) ;
38- const byte = reader . u8 ( ) ;
39- if ( byte <= 0b10001111 ) return this . cObj ( byte & 0b1111 ) ;
40- else if ( byte <= 0b10011111 ) return this . cArr ( byte & 0b1111 ) ;
41- else if ( byte <= 0b10111111 ) return this . cStr ( byte & 0b11111 ) ;
42- else {
43- switch ( byte ) {
44- case 0xc4 :
45- return this . cBin ( reader . u8 ( ) ) ;
46- case 0xc5 :
47- return this . cBin ( reader . u16 ( ) ) ;
48- case 0xc6 :
49- return this . cBin ( reader . u32 ( ) ) ;
50- case 0xd4 :
51- return this . val ( ) ;
52- case 0xd5 :
53- return null ;
54- case 0xd6 :
55- return this . cNode ( ) ;
56- case 0xde :
57- return this . cObj ( reader . u16 ( ) ) ;
58- case 0xdf :
59- return this . cObj ( reader . u32 ( ) ) ;
60- case 0xdc :
61- return this . cArr ( reader . u16 ( ) ) ;
62- case 0xdd :
63- return this . cArr ( reader . u32 ( ) ) ;
64- case 0xd9 :
65- return this . cStr ( reader . u8 ( ) ) ;
66- case 0xda :
67- return this . cStr ( reader . u16 ( ) ) ;
68- case 0xdb :
69- return this . cStr ( reader . u32 ( ) ) ;
70- case 0xc7 :
71- return this . cTup ( ) ;
72- }
39+ const octet = reader . u8 ( ) ;
40+ const major = octet >> 5 ;
41+ const minor = octet & 0b11111 ;
42+ const length = minor < 24 ? minor : minor === 24 ? reader . u8 ( ) : minor === 25 ? reader . u16 ( ) : reader . u32 ( ) ;
43+ switch ( major ) {
44+ case CRDT_MAJOR . CON : return this . cCon ( length ) ;
45+ case CRDT_MAJOR . VAL : return this . cNode ( ) ;
46+ case CRDT_MAJOR . VEC : return this . cVec ( length ) ;
47+ case CRDT_MAJOR . OBJ : return this . cObj ( length ) ;
48+ case CRDT_MAJOR . STR : return this . cStr ( length ) ;
49+ case CRDT_MAJOR . BIN : return this . cBin ( length ) ;
50+ case CRDT_MAJOR . ARR : return this . cArr ( length ) ;
7351 }
7452 return undefined ;
7553 }
7654
77- public cObj ( length : number ) : Record < string , unknown > {
55+ protected cCon ( length : number ) : unknown {
56+ return ! length ? this . val ( ) : ( this . ts ( ) , null ) ;
57+ }
58+
59+ protected cObj ( length : number ) : Record < string , unknown > {
7860 const obj : Record < string , unknown > = { } ;
7961 for ( let i = 0 ; i < length ; i ++ ) {
8062 const key : string = this . key ( ) ;
@@ -84,10 +66,8 @@ export class ViewDecoder extends MsgPackDecoderFast<CrdtReader> {
8466 return obj ;
8567 }
8668
87- public cTup ( ) : unknown [ ] {
69+ protected cVec ( length : number ) : unknown [ ] {
8870 const reader = this . reader ;
89- const length = this . reader . u8 ( ) ;
90- reader . x ++ ;
9171 const obj : unknown [ ] = [ ] ;
9272 for ( let i = 0 ; i < length ; i ++ ) {
9373 const octet = reader . peak ( ) ;
@@ -99,7 +79,7 @@ export class ViewDecoder extends MsgPackDecoderFast<CrdtReader> {
9979 return obj ;
10080 }
10181
102- public cArr ( length : number ) : unknown [ ] {
82+ protected cArr ( length : number ) : unknown [ ] {
10383 const arr : unknown [ ] = [ ] ;
10484 for ( let i = 0 ; i < length ; i ++ ) {
10585 const values = this . cArrChunk ( ) ;
@@ -108,7 +88,7 @@ export class ViewDecoder extends MsgPackDecoderFast<CrdtReader> {
10888 return arr ;
10989 }
11090
111- private cArrChunk ( ) : unknown [ ] | undefined {
91+ protected cArrChunk ( ) : unknown [ ] | undefined {
11292 const [ deleted , length ] = this . reader . b1vu28 ( ) ;
11393 this . ts ( ) ;
11494 if ( deleted ) {
@@ -120,7 +100,7 @@ export class ViewDecoder extends MsgPackDecoderFast<CrdtReader> {
120100 }
121101 }
122102
123- public cStr ( length : number ) : string {
103+ protected cStr ( length : number ) : string {
124104 const reader = this . reader ;
125105 let str = '' ;
126106 for ( let i = 0 ; i < length ; i ++ ) {
@@ -137,7 +117,7 @@ export class ViewDecoder extends MsgPackDecoderFast<CrdtReader> {
137117 return str ;
138118 }
139119
140- public cBin ( length : number ) : Uint8Array {
120+ protected cBin ( length : number ) : Uint8Array {
141121 const reader = this . reader ;
142122 const buffers : Uint8Array [ ] = [ ] ;
143123 let totalLength = 0 ;
0 commit comments