playerthree/delta

Structural deltas between two JSON values, for streams of full-state frames (observer playback).

Consecutive projections share almost everything (terrain, rules, idle entities). diff encodes only what changed; patch rebuilds the next value from the previous one with structural sharing, so unchanged subtrees keep their identity and are never serialized or shipped again. Game-agnostic: arrays whose elements are all objects with a unique string/number id are matched by id (entity lists), every other array by index.

Values are JSON: undefined object members are absent, and every value a delta carries is detached from next.

Type Aliases

JsonDelta

type JsonDelta = 
  | {
  s: unknown;
}
  | {
  i?: (string | number)[];
  k: 1;
  u?: Record<string, JsonDelta>;
}
  | {
  n: number;
  p?: Record<string, JsonDelta>;
  x?: unknown[];
}
  | {
  d?: string[];
  o?: Record<string, JsonDelta>;
  v?: Record<string, JsonPrimitive>;
};

Defined in: packages/playerthree/src/delta.ts:12

Union Members

Type Literal
{
  s: unknown;
}

Replace the whole value.


Type Literal
{
  i?: (string | number)[];
  k: 1;
  u?: Record<string, JsonDelta>;
}

Array of objects keyed by id: changed or new elements by id; i is the new id order when membership or order changed.


Type Literal
{
  n: number;
  p?: Record<string, JsonDelta>;
  x?: unknown[];
}

Array by index: new length, primitive replacements as flat [index, value, …] pairs, nested deltas by index.


Type Literal
{
  d?: string[];
  o?: Record<string, JsonDelta>;
  v?: Record<string, JsonPrimitive>;
}

Object: new primitive member values, nested member deltas, removed member names.


JsonPrimitive

type JsonPrimitive = null | boolean | number | string;

Defined in: packages/playerthree/src/delta.ts:11

Structural deltas between two JSON values, for streams of full-state frames (observer playback).

Consecutive projections share almost everything (terrain, rules, idle entities). diff encodes only what changed; patch rebuilds the next value from the previous one with structural sharing, so unchanged subtrees keep their identity and are never serialized or shipped again. Game-agnostic: arrays whose elements are all objects with a unique string/number id are matched by id (entity lists), every other array by index.

Values are JSON: undefined object members are absent, and every value a delta carries is detached from next.

Functions

diff()

function diff(prev, next): JsonDelta | undefined;

Defined in: packages/playerthree/src/delta.ts:84

The delta from prev to next, or undefined when they are equal JSON. Never mutates or retains either argument.

Parameters

ParameterType
prevunknown
nextunknown

Returns

JsonDelta | undefined


patch()

function patch(prev, delta): unknown;

Defined in: packages/playerthree/src/delta.ts:106

prev with delta applied. prev is not mutated; unchanged subtrees are shared with it.

Parameters

ParameterType
prevunknown
deltaJsonDelta

Returns

unknown