public cacheAssetCreation()

in packages/sdk/src/internal/adapters/multipeer/session.ts [341:382]


	public cacheAssetCreation(assetId: Guid, creatorId: Guid, duration?: number) {
		const syncAsset = {
			id: assetId,
			creatorMessageId: creatorId,
			duration
		} as Partial<SyncAsset>;
		this.assetSet.set(assetId, syncAsset);
		const creator = this.assetCreatorSet.get(creatorId);

		// Updates are cached on send, creates are cached on receive,
		// so it's possible something was updated while it was loading.
		// Merge those updates into creation once the create comes back.
		if (creator.payload.type === 'create-asset' && syncAsset.update) {
			creator.payload.definition = deepmerge(
				creator.payload.definition,
				syncAsset.update.payload.asset
			);
			syncAsset.update = undefined;
		}

		// update end times on playing media instances with the now-known duration
		for (const syncActor of this.actorSet.values()) {
			for (const activeMediaInstance of (syncActor.activeMediaInstances || [])) {
				if (activeMediaInstance.message.payload.mediaAssetId !== assetId ||
					activeMediaInstance.message.payload.options.looping === true ||
					activeMediaInstance.message.payload.options.paused === true ||
					duration === undefined) {
					continue;
				}

				let timeRemaining: number = syncAsset.duration;
				if (activeMediaInstance.message.payload.options.time !== undefined) {
					timeRemaining -= activeMediaInstance.message.payload.options.time;
				}
				if (activeMediaInstance.message.payload.options.pitch !== undefined) {
					timeRemaining /= Math.pow(2.0,
						(activeMediaInstance.message.payload.options.pitch / 12.0));
				}
				activeMediaInstance.expirationTime = activeMediaInstance.basisTime + timeRemaining;
			}
		}
	}