dist/server-module/lance-gg.js
/**
     *  Class can be serialized using either:
     * - a class based netScheme
     * - an instance based netScheme
     * - completely dynamically (not implemented yet)
     *
     * @param {Object} serializer - Serializer instance
     * @param {Object} [options] - Options object
     * @param {Object} options.dataBuffer [optional] - Data buffer to write to. If null a new data buffer will be created
     * @param {Number} options.bufferOffset [optional] - The buffer data offset to start writing at. Default: 0
     * @param {String} options.dry [optional] - Does not actually write to the buffer (useful to gather serializeable size)
     * @return {Object} the serialized object.  Contains attributes: dataBuffer - buffer which contains the serialized data;  bufferOffset - offset where the serialized data starts.
     */
Class can be serialized using either:
- a class based netScheme
- an instance based netScheme
- completely dynamically (not implemented yet)

@param {Object} serializer - Serializer instance
@param {Object} [options] - Options object
@param {Object} options.dataBuffer [optional] - Data buffer to write to. If null a new data buffer will be created
@param {Number} options.bufferOffset [optional] - The buffer data offset to start writing at. Default: 0
@param {String} options.dry [optional] - Does not actually write to the buffer (useful to gather serializeable size)
@return {Object} the serialized object.  Contains attributes: dataBuffer - buffer which contains the serialized data;  bufferOffset - offset where the serialized data starts.
Class can be serialized using either : - a class based netScheme - an instance based netScheme - completely dynamically ( not implemented yet ) @param { Object } serializer - Serializer instance @param { Object } [ options ] - Options object @param { Object } options . dataBuffer [ optional ] - Data buffer to write to . If null a new data buffer will be created @param { Number } options . bufferOffset [ optional ] - The buffer data offset to start writing at . Default : 0 @param { String } options . dry [ optional ] - Does not actually write to the buffer ( useful to gather serializeable size ) @return { Object } the serialized object . Contains attributes : dataBuffer - buffer which contains the serialized data ; bufferOffset - offset where the serialized data starts .
serialize(serializer, options) {
        options = Object.assign({
            bufferOffset: 0
        }, options);
        let netScheme;
        let dataBuffer;
        let dataView;
        let classId = 0;
        let bufferOffset = options.bufferOffset;
        let localBufferOffset = 0; 
        if (this.classId) {
            classId = this.classId;
        } else {
            classId = Utils.hashStr(this.constructor.name);
        }
        if (this.netScheme) {
            netScheme = this.netScheme;
        } else if (this.constructor.netScheme) {
            netScheme = this.constructor.netScheme;
        } else {
            console.warn('no netScheme defined! This will result in awful performance');
        }
        if (options.dataBuffer == null && options.dry != true) {
            let bufferSize = this.serialize(serializer, { dry: true }).bufferOffset;
            dataBuffer = new ArrayBuffer(bufferSize);
        } else {
            dataBuffer = options.dataBuffer;
        }
        if (options.dry != true) {
            dataView = new DataView(dataBuffer);
            dataView.setUint8(bufferOffset + localBufferOffset, classId);
        }
        localBufferOffset += Uint8Array.BYTES_PER_ELEMENT;
        if (netScheme) {
            for (let property of Object.keys(netScheme).sort()) {
                if (options.dry != true) {
                    serializer.writeDataView(dataView, this[property], bufferOffset + localBufferOffset, netScheme[property]);
                }
                if (netScheme[property].type === BaseTypes.TYPES.STRING) {
                    localBufferOffset += Uint16Array.BYTES_PER_ELEMENT;
                    if (this[property] !== null && this[property] !== undefined)
                        localBufferOffset += this[property].length * Uint16Array.BYTES_PER_ELEMENT;
                } else if (netScheme[property].type === BaseTypes.TYPES.CLASSINSTANCE) {
                    let objectInstanceBufferOffset = this[property].serialize(serializer, { dry: true }).bufferOffset;
                    localBufferOffset += objectInstanceBufferOffset;
                } else if (netScheme[property].type === BaseTypes.TYPES.LIST) {
                    localBufferOffset += Uint16Array.BYTES_PER_ELEMENT;
                    for (let item of this[property]) {
                        if (netScheme[property].itemType === BaseTypes.TYPES.CLASSINSTANCE) {
                            let listBufferOffset = item.serialize(serializer, { dry: true }).bufferOffset;
                            localBufferOffset += listBufferOffset;
                        } else if (netScheme[property].itemType === BaseTypes.TYPES.STRING) {
                            localBufferOffset += Uint16Array.BYTES_PER_ELEMENT * (1 + item.length);
                        } else {
                            localBufferOffset += serializer.getTypeByteSize(netScheme[property].itemType);
                        }
                    }
                } else {
                    localBufferOffset += serializer.getTypeByteSize(netScheme[property].type);
                }
            }
        }
        return { dataBuffer, bufferOffset: localBufferOffset };
    }
serialize ( serializer , options ) { options = Object . assign ( { bufferOffset : 0 } , options ) ; let netScheme ; let dataBuffer ; let dataView ; let classId = 0 ; let bufferOffset = options . bufferOffset ; let localBufferOffset = 0 ; if ( this . classId ) { classId = this . classId ; } else { classId = Utils . hashStr ( this . constructor . name ) ; } if ( this . netScheme ) { netScheme = this . netScheme ; } else if ( this . constructor . netScheme ) { netScheme = this . constructor . netScheme ; } else { console . warn ( 'no netScheme defined! This will result in awful performance' ) ; } if ( options . dataBuffer == null && options . dry != true ) { let bufferSize = this . serialize ( serializer , { dry : true } ) . bufferOffset ; dataBuffer = new ArrayBuffer ( bufferSize ) ; } else { dataBuffer = options . dataBuffer ; } if ( options . dry != true ) { dataView = new DataView ( dataBuffer ) ; dataView . setUint8 ( bufferOffset + localBufferOffset , classId ) ; } localBufferOffset += Uint8Array . BYTES_PER_ELEMENT ; if ( netScheme ) { for ( let property of Object . keys ( netScheme ) . sort ( ) ) { if ( options . dry != true ) { serializer . writeDataView ( dataView , this [ property ] , bufferOffset + localBufferOffset , netScheme [ property ] ) ; } if ( netScheme [ property ] . type === BaseTypes . TYPES . STRING ) { localBufferOffset += Uint16Array . BYTES_PER_ELEMENT ; if ( this [ property ] !== null && this [ property ] !== undefined ) localBufferOffset += this [ property ] . length * Uint16Array . BYTES_PER_ELEMENT ; } else if ( netScheme [ property ] . type === BaseTypes . TYPES . CLASSINSTANCE ) { let objectInstanceBufferOffset = this [ property ] . serialize ( serializer , { dry : true } ) . bufferOffset ; localBufferOffset += objectInstanceBufferOffset ; } else if ( netScheme [ property ] . type === BaseTypes . TYPES . LIST ) { localBufferOffset += Uint16Array . BYTES_PER_ELEMENT ; for ( let item of this [ property ] ) { if ( netScheme [ property ] . itemType === BaseTypes . TYPES . CLASSINSTANCE ) { let listBufferOffset = item . serialize ( serializer , { dry : true } ) . bufferOffset ; localBufferOffset += listBufferOffset ; } else if ( netScheme [ property ] . itemType === BaseTypes . TYPES . STRING ) { localBufferOffset += Uint16Array . BYTES_PER_ELEMENT * ( 1 + item . length ) ; } else { localBufferOffset += serializer . getTypeByteSize ( netScheme [ property ] . itemType ) ; } } } else { localBufferOffset += serializer . getTypeByteSize ( netScheme [ property ] . type ) ; } } } return { dataBuffer , bufferOffset : localBufferOffset } ; }
Class can be serialized using either:
 - a class based netScheme
 - an instance based netScheme
 - completely dynamically (not implemented yet)
Class can be serialized using either : - a class based netScheme - an instance based netScheme - completely dynamically ( not implemented yet )
// used for counting the bufferOffset // instance classId // instance netScheme // todo define behaviour when a netScheme is undefined // TODO: currently we serialize every node twice, once to calculate the size //       of the buffers and once to write them out.  This can be reduced to //       a single pass by starting with a large (and static) ArrayBuffer and //       recursively building it up. // buffer has one Uint8Array for class id, then payload // first set the id of the class, so that the deserializer can fetch information about it // advance the offset counter // write the property to buffer // derive the size of the string // derive the size of the included class // derive the size of the list // list starts with number of elements // todo inelegant, currently doesn't support list of lists // size includes string length plus double-byte characters // advance offset