Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(client-presence): replace reduce with for loop #23126

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 22 additions & 29 deletions packages/framework/presence/src/presenceStates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,44 +262,37 @@ class PresenceStatesImpl<TSchema extends PresenceStatesSchema>
// Prepare initial map content from initial state
{
const clientSessionId = this.runtime.clientSessionId;
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
const nodes = {} as MapEntries<TSchema>;
let anyInitialValues = false;
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
const newValues = {} as { [key: string]: InternalTypes.ValueDirectoryOrState<unknown> };
let cumulativeAllowableUpdateLatencyMs: number | undefined;
// eslint-disable-next-line unicorn/no-array-reduce
const initial = Object.entries(initialContent).reduce(
(acc, [key, nodeFactory]) => {
const newNodeData = nodeFactory(key, handleFromDatastore(this));
acc.nodes[key as keyof TSchema] = newNodeData.manager;
if ("initialData" in newNodeData) {
const { value, allowableUpdateLatencyMs } = newNodeData.initialData;
acc.datastore[key] = acc.datastore[key] ?? {};
acc.datastore[key][clientSessionId] = value;
acc.newValues[key] = value;
if (allowableUpdateLatencyMs !== undefined) {
cumulativeAllowableUpdateLatencyMs =
cumulativeAllowableUpdateLatencyMs === undefined
? allowableUpdateLatencyMs
: Math.min(cumulativeAllowableUpdateLatencyMs, allowableUpdateLatencyMs);
}
anyInitialValues = true;
for (const [key, nodeFactory] of Object.entries(initialContent)) {
const newNodeData = nodeFactory(key, handleFromDatastore(this));
nodes[key as keyof TSchema] = newNodeData.manager;
if ("initialData" in newNodeData) {
const { value, allowableUpdateLatencyMs } = newNodeData.initialData;
datastore[key] ??= {};
datastore[key][clientSessionId] = value;
newValues[key] = value;
if (allowableUpdateLatencyMs !== undefined) {
cumulativeAllowableUpdateLatencyMs =
cumulativeAllowableUpdateLatencyMs === undefined
? allowableUpdateLatencyMs
: Math.min(cumulativeAllowableUpdateLatencyMs, allowableUpdateLatencyMs);
}
return acc;
},
{
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
nodes: {} as MapEntries<TSchema>,
datastore,
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
newValues: {} as { [key: string]: InternalTypes.ValueDirectoryOrState<unknown> },
},
);
this.nodes = initial.nodes;
anyInitialValues = true;
}
}
this.nodes = nodes;
// props is the public view of nodes that limits the entries types to
// the public interface of the value manager with an additional type
// filter that beguiles the type system. So just reinterpret cast.
this.props = this.nodes as unknown as PresenceStates<TSchema>["props"];

if (anyInitialValues) {
this.runtime.localUpdate(initial.newValues, {
this.runtime.localUpdate(newValues, {
allowableUpdateLatencyMs:
cumulativeAllowableUpdateLatencyMs ?? this.controls.allowableUpdateLatencyMs,
});
Expand Down
Loading