///
import { Collab, CollabID } from "../core";
/**
* A serializer for values of type `T`.
*
* Collabs with a generic type `T`, like [[CVar]]`` or
* [[CValueSet]]``, need a Serializer\ so that they can send
* values of type `T` from one replica to another.
*
* By default,
* all built-in Collabs use [[DefaultSerializer]], which permits
* JSON values and some others. To use more general `T`
* or to optimize the encoding, you must supply your own
* Serializer\, typically in the Collab constructor's
* `options` parameter.
*
* Serializers provided with the library (some only in package @collabs/core)
* include [[DefaultSerializer]],
* [CollabIDSerializer](../../core/classes/CollabIDSerializer.html),
* [StringSerializer](../../core/classes/StringSerializer.html),
* [Uint8ArraySerializer](../../core/classes/Uint8ArraySerializer.html),
* [ArraySerializer](../../core/classes/ArraySerializer.html),
* [PairSerializer](../../core/classes/PairSerializer.html),
* and [ConstSerializer](../../core/classes/ConstSerializer.html).
*/
export interface Serializer {
/**
* Returns a Uint8Array that is the serialized form of value.
*
* To recover the original value, use [[deserialize]].
*/
serialize(value: T): Uint8Array;
/**
* Inverse of [[serialize]].
*
* Typically, this returns a deep clone of the original value, not
* the same literal reference. Indeed, it is impossible to return
* the original reference on a different replica.
*/
deserialize(message: Uint8Array): T;
}
/**
* Default [[Serializer]].
*
* Supported types are a superset of JSON:
* - Primitive types (string, number, boolean, undefined, null)
* - Arrays and plain (non-class) objects, serialized recursively
* - [[CollabID]]s (covered by the previous case since they are plain objects)
* - Uint8Array
* - [[Optional]], with T serialized recursively.
*
* All other types cause an error during [[serialize]].
*
* Construct using [[getInstance]].
*/
export declare class DefaultSerializer implements Serializer {
private constructor();
private static instance;
/**
* Returns an instance of [[DefaultSerializer]].
*
* Internally, all instances are the same literal object.
*/
static getInstance(): DefaultSerializer;
serialize(value: T): Uint8Array;
deserialize(message: Uint8Array): T;
}
/**
* Serializer for Uint8Array that is the identity function.
*
* This is a singleton class; use [[instance]]
* instead of the constructor.
*/
export declare class Uint8ArraySerializer implements Serializer {
private constructor();
serialize(value: Uint8Array): Uint8Array;
deserialize(message: Uint8Array): Uint8Array;
static readonly instance: Uint8ArraySerializer;
}
/**
* Serializer for string that uses utf-8 encoding.
*
* This is a singleton class; use [[instance]]
* instead of the constructor.
*/
export declare class StringSerializer implements Serializer {
private constructor();
serialize(value: string): Uint8Array;
deserialize(message: Uint8Array): string;
static readonly instance: StringSerializer;
}
/**
* Serializes T\[\] using a serializer for T. This is slightly more efficient
* than [[DefaultSerializer]], and it works with arbitrary T.
*
* Construct using [[getInstance]].
*/
export declare class ArraySerializer implements Serializer {
private readonly valueSerializer;
private constructor();
serialize(values: T[]): Uint8Array;
deserialize(message: Uint8Array): T[];
private static cache;
/**
* Returns an instance of [[ArraySerializer]] that uses valueSerializer
* to serialize each value.
*
* This method may cache instances internally to save memory.
*/
static getInstance(valueSerializer: Serializer): ArraySerializer;
}
/**
* Serializes \[T, U\] using serializers for T and U. This is slightly more efficient
* than [[DefaultSerializer]], and it works with arbitrary T and U.
*/
export declare class PairSerializer implements Serializer<[T, U]> {
private readonly oneSerializer;
private readonly twoSerializer;
constructor(oneSerializer: Serializer, twoSerializer: Serializer);
serialize(value: [T, U]): Uint8Array;
deserialize(message: Uint8Array): [T, U];
}
/**
* Serializes a fixed value as an empty Uint8Array.
*/
export declare class ConstSerializer implements Serializer {
readonly value: T;
/**
* @param value The value that [[deserialize]] will always return.
*/
constructor(value: T);
serialize(_value: T): Uint8Array;
deserialize(_message: Uint8Array): T;
}
/**
* Serializes [[CollabID]]s. This is slightly more efficient
* than [[DefaultSerializer]].
*
* Construct using [[getInstance]].
*/
export declare class CollabIDSerializer implements Serializer> {
private constructor();
private static instance;
/**
* Returns an instance of [[CollabIDSerializer]].
*
* Internally, all instances are the same literal object.
*/
static getInstance(): CollabIDSerializer;
serialize(value: CollabID): Uint8Array;
deserialize(message: Uint8Array): CollabID;
}
/**
* Internal utility for working with protobuf encodings.
*
* Apply this function to protobuf.js uint64 and sint64 output values
* to convert them to the nearest JS number (double).
* For safe integers, this is exact.
*/
export declare function int64AsNumber(num: number | Long): number;
/**
* Internal utility for working with protobuf encodings.
*
* Returns whether the optional property `prop` is present in
* the deserialized protobufjs message `message`. (Accessing a not-present
* property directly will return its type's default value
* instead of `undefined`.)
*/
export declare function protobufHas(message: Partial>, prop: N): boolean;