public update()

in packages/sdk/src/core/contextInternal.ts [358:416]


	public update() {
		// Early out if no state changes occurred.
		if (this.generation === this.prevGeneration) {
			return;
		}

		this.prevGeneration = this.generation;

		const syncObjects = [
			...this.actorSet.values(),
			...this.assetsIterable(),
			...this.userSet.values(),
			...this.animationSet.values()
		] as Array<Patchable<any>>;

		const maxUpdatesPerTick = parseInt(process.env.MRE_MAX_UPDATES_PER_TICK) || 300;
		let updates = 0;
		for (const patchable of syncObjects) {
			if (updates >= maxUpdatesPerTick) {
				break;
			}

			const patch = patchable.internal.getPatchAndReset();
			if (!patch) {
				continue;
			} else {
				updates++;
			}

			if (patchable instanceof User) {
				this.protocol.sendPayload({
					type: 'user-update',
					user: patch as UserLike
				} as Payloads.UserUpdate);
			} else if (patchable instanceof Actor) {
				this.protocol.sendPayload({
					type: 'actor-update',
					actor: patch as ActorLike
				} as Payloads.ActorUpdate);
			} else if (patchable instanceof Animation) {
				this.protocol.sendPayload({
					type: 'animation-update',
					animation: patch as Partial<AnimationLike>
				} as Payloads.AnimationUpdate)
			} else if (patchable instanceof Asset) {
				this.protocol.sendPayload({
					type: 'asset-update',
					asset: patch as AssetLike
				} as Payloads.AssetUpdate);
			}
		}

		// only run if we finished sending all pending updates
		if (updates < maxUpdatesPerTick && this.nextUpdatePromise) {
			this.resolveNextUpdatePromise();
			this.nextUpdatePromise = null;
			this.resolveNextUpdatePromise = null;
		}
	}