Options
All
  • Public
  • Public/Protected
  • All
Menu

Class TypeHelper

This is a helper class that is used to register custom message types in order for the client to support them. By default, the Client supports the following message types:

Composite message types can be created using TypeHelper.compositeHelper method.

See TypeHelper.helper for examples on how to implement support for new types or custom type helpers for existing message types.

Hierarchy

  • TypeHelper

Index

Methods

Static compositeHelper

  • compositeHelper(...messageTypes: string[]): ITypeHelper
  • This method is used to create composite message types from other types, such as JSON, XML, etc.

    // Register the json-xml-json-binary composite message type with the type helper
    amps.TypeHelper.helper(
        'compositejxjb',
        amps.TypeHelper.compositeHelper('json', 'xml', 'json', 'binary')
    );
    

    Parameters

    • Rest ...messageTypes: string[]

    Returns ITypeHelper

    A created ITypeHelper-compatible object.

Static helper

  • This method provides a helper for parsing messages of different types. If a helper was provided by a user, registers it for future use.

    Create a custom helper for XML messages:

    var xmlTypeHelper = {
        serialize: function(data) {
            return [new XMLSerializer().serializeToString(data)];
        },
        deserialize: function(data) {
            if (data.constructor === String) {
                return new DOMParser().parseFromString(data);
            }
            else {
                // Binary buffer, need to decode utf8
                return new DOMParser().parseFromString(
                    decodeURIComponent(escape(Uint8ToString(data)))
                );
            }
        }
    };
    
    // Register the above XML custom helper for parsing all XML messages automatically
    amps.TypeHelper.helper('xml', xmlTypeHelper);
    

    In case the custom parsing behavior is expected, it is possible to override the default type helper:

    // create a JSON type helper that does not parse JSON data into native JS objects keeping it as a string
    var jsonHelper = {
        serialize: function(data) {
            return [data];
        },
        deserialize: function(data) {
            return JSON.stringify(data);
        }
    };
    
    // override the default type helper
    amps.TypeHelper.helper('json', jsonHelper);
    

    Register the json-json-json-binary composite message type with the type helper:

    amps.TypeHelper.helper(
        'compositejjjbl',
        amps.TypeHelper.compositeHelper('json', 'json', 'json', 'binary')
    );
    

    Set the custom delimiter for NVFIX format:

    amps.TypeHelper.helper('nvfix').delimiter('%01');
    

    Register the custom helper for NVFIX format:

    var nvfixTypeHelper = {
        serialize: function(data) {
            // already formatted fix/nvfix string
            if (typeof data === 'string') {
                return [data];
            }
    
            // otherwise, we assume it's an object with keys and values
            return [
                Object.keys(data).map(function(key) {
                    return key + '=' + data[key];
                }).join('\x01') + '\x01'
            ];
        },
        deserialize: function(data) {
            var parsedData = {};
            String.fromCharCode.apply(null, new Int8Array(data))
                .split('\x01')
                .slice(0, -1)
                .map(function(keyValue) {
                     var keyValueTuple = keyValue.split('=');
                     var key = keyValueTuple[0];
    
                     // no '=' inside of the value
                     if (keyValueTuple.length === 2) {
                         parsedData[key] = keyValueTuple[1];
                     }
                     else {
                         parsedData[key] = keyValue.slice(key.length + 1);
                     }
                });
    
            return parsedData;
        }
    };
    
    // Register the above NVFIX custom helper for parsing all NVFIX messages automatically
    amps.TypeHelper.helper('nvfix', nvfixTypeHelper);
    

    Parameters

    • messageType: string

      The type of message to handle.

    • Optional newHelper: ITypeHelper

      A helper object with predefined functions that can be provided for custom parsing/handling of a message type.

    Returns ITypeHelper

    The helper object assigned/registered.