@fluidframework/presence 2.10.0-307399
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +216 -0
- package/dist/alpha.d.ts +58 -0
- package/dist/baseTypes.d.ts +24 -0
- package/dist/baseTypes.d.ts.map +1 -0
- package/dist/baseTypes.js +7 -0
- package/dist/baseTypes.js.map +1 -0
- package/dist/container-definitions/containerExtensions.d.ts +137 -0
- package/dist/container-definitions/containerExtensions.d.ts.map +1 -0
- package/dist/container-definitions/containerExtensions.js +7 -0
- package/dist/container-definitions/containerExtensions.js.map +1 -0
- package/dist/container-definitions/index.d.ts +7 -0
- package/dist/container-definitions/index.d.ts.map +1 -0
- package/dist/container-definitions/index.js +7 -0
- package/dist/container-definitions/index.js.map +1 -0
- package/dist/container-definitions/runtime.d.ts +12 -0
- package/dist/container-definitions/runtime.d.ts.map +1 -0
- package/dist/container-definitions/runtime.js +7 -0
- package/dist/container-definitions/runtime.js.map +1 -0
- package/dist/core-interfaces/exposedUtilityTypes.d.ts +446 -0
- package/dist/core-interfaces/exposedUtilityTypes.d.ts.map +1 -0
- package/dist/core-interfaces/exposedUtilityTypes.js +11 -0
- package/dist/core-interfaces/exposedUtilityTypes.js.map +1 -0
- package/dist/core-interfaces/index.d.ts +10 -0
- package/dist/core-interfaces/index.d.ts.map +1 -0
- package/dist/core-interfaces/index.js +7 -0
- package/dist/core-interfaces/index.js.map +1 -0
- package/dist/core-interfaces/jsonDeserialized.d.ts +109 -0
- package/dist/core-interfaces/jsonDeserialized.d.ts.map +1 -0
- package/dist/core-interfaces/jsonDeserialized.js +7 -0
- package/dist/core-interfaces/jsonDeserialized.js.map +1 -0
- package/dist/core-interfaces/jsonSerializable.d.ts +120 -0
- package/dist/core-interfaces/jsonSerializable.d.ts.map +1 -0
- package/dist/core-interfaces/jsonSerializable.js +7 -0
- package/dist/core-interfaces/jsonSerializable.js.map +1 -0
- package/dist/core-interfaces/jsonSerializationErrors.d.ts +31 -0
- package/dist/core-interfaces/jsonSerializationErrors.d.ts.map +1 -0
- package/dist/core-interfaces/jsonSerializationErrors.js +7 -0
- package/dist/core-interfaces/jsonSerializationErrors.js.map +1 -0
- package/dist/core-interfaces/jsonType.d.ts +29 -0
- package/dist/core-interfaces/jsonType.d.ts.map +1 -0
- package/dist/core-interfaces/jsonType.js +7 -0
- package/dist/core-interfaces/jsonType.js.map +1 -0
- package/dist/datastorePresenceManagerFactory.d.ts +48 -0
- package/dist/datastorePresenceManagerFactory.d.ts.map +1 -0
- package/dist/datastorePresenceManagerFactory.js +79 -0
- package/dist/datastorePresenceManagerFactory.js.map +1 -0
- package/dist/datastoreSupport.d.ts +31 -0
- package/dist/datastoreSupport.d.ts.map +1 -0
- package/dist/datastoreSupport.js +82 -0
- package/dist/datastoreSupport.js.map +1 -0
- package/dist/events/events.d.ts +198 -0
- package/dist/events/events.d.ts.map +1 -0
- package/dist/events/events.js +157 -0
- package/dist/events/events.js.map +1 -0
- package/dist/experimentalAccess.d.ts +15 -0
- package/dist/experimentalAccess.d.ts.map +1 -0
- package/dist/experimentalAccess.js +46 -0
- package/dist/experimentalAccess.js.map +1 -0
- package/dist/exposedInternalTypes.d.ts +100 -0
- package/dist/exposedInternalTypes.d.ts.map +1 -0
- package/dist/exposedInternalTypes.js +19 -0
- package/dist/exposedInternalTypes.js.map +1 -0
- package/dist/exposedUtilityTypes.d.ts +63 -0
- package/dist/exposedUtilityTypes.d.ts.map +1 -0
- package/dist/exposedUtilityTypes.js +7 -0
- package/dist/exposedUtilityTypes.js.map +1 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +21 -0
- package/dist/index.js.map +1 -0
- package/dist/internalTypes.d.ts +39 -0
- package/dist/internalTypes.d.ts.map +1 -0
- package/dist/internalTypes.js +14 -0
- package/dist/internalTypes.js.map +1 -0
- package/dist/latestMapValueManager.d.ts +182 -0
- package/dist/latestMapValueManager.d.ts.map +1 -0
- package/dist/latestMapValueManager.js +206 -0
- package/dist/latestMapValueManager.js.map +1 -0
- package/dist/latestValueControls.d.ts +44 -0
- package/dist/latestValueControls.d.ts.map +1 -0
- package/dist/latestValueControls.js +28 -0
- package/dist/latestValueControls.js.map +1 -0
- package/dist/latestValueManager.d.ts +69 -0
- package/dist/latestValueManager.d.ts.map +1 -0
- package/dist/latestValueManager.js +100 -0
- package/dist/latestValueManager.js.map +1 -0
- package/dist/latestValueTypes.d.ts +44 -0
- package/dist/latestValueTypes.d.ts.map +1 -0
- package/dist/latestValueTypes.js +7 -0
- package/dist/latestValueTypes.js.map +1 -0
- package/dist/notificationsManager.d.ts +101 -0
- package/dist/notificationsManager.d.ts.map +1 -0
- package/dist/notificationsManager.js +82 -0
- package/dist/notificationsManager.js.map +1 -0
- package/dist/package.json +15 -0
- package/dist/presence.d.ts +180 -0
- package/dist/presence.d.ts.map +1 -0
- package/dist/presence.js +23 -0
- package/dist/presence.js.map +1 -0
- package/dist/presenceDatastoreManager.d.ts +91 -0
- package/dist/presenceDatastoreManager.d.ts.map +1 -0
- package/dist/presenceDatastoreManager.js +248 -0
- package/dist/presenceDatastoreManager.js.map +1 -0
- package/dist/presenceManager.d.ts +20 -0
- package/dist/presenceManager.d.ts.map +1 -0
- package/dist/presenceManager.js +104 -0
- package/dist/presenceManager.js.map +1 -0
- package/dist/presenceStates.d.ts +78 -0
- package/dist/presenceStates.d.ts.map +1 -0
- package/dist/presenceStates.js +182 -0
- package/dist/presenceStates.js.map +1 -0
- package/dist/stateDatastore.d.ts +35 -0
- package/dist/stateDatastore.d.ts.map +1 -0
- package/dist/stateDatastore.js +26 -0
- package/dist/stateDatastore.js.map +1 -0
- package/dist/systemWorkspace.d.ts +51 -0
- package/dist/systemWorkspace.d.ts.map +1 -0
- package/dist/systemWorkspace.js +180 -0
- package/dist/systemWorkspace.js.map +1 -0
- package/dist/types.d.ts +92 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +8 -0
- package/dist/types.js.map +1 -0
- package/dist/valueManager.d.ts +19 -0
- package/dist/valueManager.d.ts.map +1 -0
- package/dist/valueManager.js +26 -0
- package/dist/valueManager.js.map +1 -0
- package/lib/alpha.d.ts +58 -0
- package/lib/baseTypes.d.ts +24 -0
- package/lib/baseTypes.d.ts.map +1 -0
- package/lib/baseTypes.js +6 -0
- package/lib/baseTypes.js.map +1 -0
- package/lib/container-definitions/containerExtensions.d.ts +137 -0
- package/lib/container-definitions/containerExtensions.d.ts.map +1 -0
- package/lib/container-definitions/containerExtensions.js +6 -0
- package/lib/container-definitions/containerExtensions.js.map +1 -0
- package/lib/container-definitions/index.d.ts +7 -0
- package/lib/container-definitions/index.d.ts.map +1 -0
- package/lib/container-definitions/index.js +6 -0
- package/lib/container-definitions/index.js.map +1 -0
- package/lib/container-definitions/runtime.d.ts +12 -0
- package/lib/container-definitions/runtime.d.ts.map +1 -0
- package/lib/container-definitions/runtime.js +6 -0
- package/lib/container-definitions/runtime.js.map +1 -0
- package/lib/core-interfaces/exposedUtilityTypes.d.ts +446 -0
- package/lib/core-interfaces/exposedUtilityTypes.d.ts.map +1 -0
- package/lib/core-interfaces/exposedUtilityTypes.js +10 -0
- package/lib/core-interfaces/exposedUtilityTypes.js.map +1 -0
- package/lib/core-interfaces/index.d.ts +10 -0
- package/lib/core-interfaces/index.d.ts.map +1 -0
- package/lib/core-interfaces/index.js +6 -0
- package/lib/core-interfaces/index.js.map +1 -0
- package/lib/core-interfaces/jsonDeserialized.d.ts +109 -0
- package/lib/core-interfaces/jsonDeserialized.d.ts.map +1 -0
- package/lib/core-interfaces/jsonDeserialized.js +6 -0
- package/lib/core-interfaces/jsonDeserialized.js.map +1 -0
- package/lib/core-interfaces/jsonSerializable.d.ts +120 -0
- package/lib/core-interfaces/jsonSerializable.d.ts.map +1 -0
- package/lib/core-interfaces/jsonSerializable.js +6 -0
- package/lib/core-interfaces/jsonSerializable.js.map +1 -0
- package/lib/core-interfaces/jsonSerializationErrors.d.ts +31 -0
- package/lib/core-interfaces/jsonSerializationErrors.d.ts.map +1 -0
- package/lib/core-interfaces/jsonSerializationErrors.js +6 -0
- package/lib/core-interfaces/jsonSerializationErrors.js.map +1 -0
- package/lib/core-interfaces/jsonType.d.ts +29 -0
- package/lib/core-interfaces/jsonType.d.ts.map +1 -0
- package/lib/core-interfaces/jsonType.js +6 -0
- package/lib/core-interfaces/jsonType.js.map +1 -0
- package/lib/datastorePresenceManagerFactory.d.ts +48 -0
- package/lib/datastorePresenceManagerFactory.d.ts.map +1 -0
- package/lib/datastorePresenceManagerFactory.js +75 -0
- package/lib/datastorePresenceManagerFactory.js.map +1 -0
- package/lib/datastoreSupport.d.ts +31 -0
- package/lib/datastoreSupport.d.ts.map +1 -0
- package/lib/datastoreSupport.js +77 -0
- package/lib/datastoreSupport.js.map +1 -0
- package/lib/events/events.d.ts +198 -0
- package/lib/events/events.d.ts.map +1 -0
- package/lib/events/events.js +152 -0
- package/lib/events/events.js.map +1 -0
- package/lib/experimentalAccess.d.ts +15 -0
- package/lib/experimentalAccess.d.ts.map +1 -0
- package/lib/experimentalAccess.js +42 -0
- package/lib/experimentalAccess.js.map +1 -0
- package/lib/exposedInternalTypes.d.ts +100 -0
- package/lib/exposedInternalTypes.d.ts.map +1 -0
- package/lib/exposedInternalTypes.js +16 -0
- package/lib/exposedInternalTypes.js.map +1 -0
- package/lib/exposedUtilityTypes.d.ts +63 -0
- package/lib/exposedUtilityTypes.d.ts.map +1 -0
- package/lib/exposedUtilityTypes.js +6 -0
- package/lib/exposedUtilityTypes.js.map +1 -0
- package/lib/index.d.ts +22 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +11 -0
- package/lib/index.js.map +1 -0
- package/lib/internalTypes.d.ts +39 -0
- package/lib/internalTypes.d.ts.map +1 -0
- package/lib/internalTypes.js +11 -0
- package/lib/internalTypes.js.map +1 -0
- package/lib/latestMapValueManager.d.ts +182 -0
- package/lib/latestMapValueManager.d.ts.map +1 -0
- package/lib/latestMapValueManager.js +202 -0
- package/lib/latestMapValueManager.js.map +1 -0
- package/lib/latestValueControls.d.ts +44 -0
- package/lib/latestValueControls.d.ts.map +1 -0
- package/lib/latestValueControls.js +24 -0
- package/lib/latestValueControls.js.map +1 -0
- package/lib/latestValueManager.d.ts +69 -0
- package/lib/latestValueManager.d.ts.map +1 -0
- package/lib/latestValueManager.js +96 -0
- package/lib/latestValueManager.js.map +1 -0
- package/lib/latestValueTypes.d.ts +44 -0
- package/lib/latestValueTypes.d.ts.map +1 -0
- package/lib/latestValueTypes.js +6 -0
- package/lib/latestValueTypes.js.map +1 -0
- package/lib/notificationsManager.d.ts +101 -0
- package/lib/notificationsManager.d.ts.map +1 -0
- package/lib/notificationsManager.js +78 -0
- package/lib/notificationsManager.js.map +1 -0
- package/lib/presence.d.ts +180 -0
- package/lib/presence.d.ts.map +1 -0
- package/lib/presence.js +20 -0
- package/lib/presence.js.map +1 -0
- package/lib/presenceDatastoreManager.d.ts +91 -0
- package/lib/presenceDatastoreManager.d.ts.map +1 -0
- package/lib/presenceDatastoreManager.js +244 -0
- package/lib/presenceDatastoreManager.js.map +1 -0
- package/lib/presenceManager.d.ts +20 -0
- package/lib/presenceManager.d.ts.map +1 -0
- package/lib/presenceManager.js +100 -0
- package/lib/presenceManager.js.map +1 -0
- package/lib/presenceStates.d.ts +78 -0
- package/lib/presenceStates.d.ts.map +1 -0
- package/lib/presenceStates.js +177 -0
- package/lib/presenceStates.js.map +1 -0
- package/lib/stateDatastore.d.ts +35 -0
- package/lib/stateDatastore.d.ts.map +1 -0
- package/lib/stateDatastore.js +21 -0
- package/lib/stateDatastore.js.map +1 -0
- package/lib/systemWorkspace.d.ts +51 -0
- package/lib/systemWorkspace.d.ts.map +1 -0
- package/lib/systemWorkspace.js +176 -0
- package/lib/systemWorkspace.js.map +1 -0
- package/lib/tsdoc-metadata.json +11 -0
- package/lib/types.d.ts +92 -0
- package/lib/types.d.ts.map +1 -0
- package/lib/types.js +7 -0
- package/lib/types.js.map +1 -0
- package/lib/valueManager.d.ts +19 -0
- package/lib/valueManager.d.ts.map +1 -0
- package/lib/valueManager.js +21 -0
- package/lib/valueManager.js.map +1 -0
- package/package.json +175 -0
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
import { assert } from "@fluidframework/core-utils/internal";
|
|
6
|
+
import { createPresenceStates, mergeUntrackedDatastore } from "./presenceStates.js";
|
|
7
|
+
const datastoreUpdateMessageType = "Pres:DatastoreUpdate";
|
|
8
|
+
const joinMessageType = "Pres:ClientJoin";
|
|
9
|
+
function isPresenceMessage(message) {
|
|
10
|
+
return message.type.startsWith("Pres:");
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Manages singleton datastore for all Presence.
|
|
14
|
+
*/
|
|
15
|
+
export class PresenceDatastoreManagerImpl {
|
|
16
|
+
constructor(clientSessionId, runtime, lookupClient, logger, systemWorkspaceDatastore, systemWorkspace) {
|
|
17
|
+
this.clientSessionId = clientSessionId;
|
|
18
|
+
this.runtime = runtime;
|
|
19
|
+
this.lookupClient = lookupClient;
|
|
20
|
+
this.logger = logger;
|
|
21
|
+
this.averageLatency = 0;
|
|
22
|
+
this.returnedMessages = 0;
|
|
23
|
+
this.refreshBroadcastRequested = false;
|
|
24
|
+
this.workspaces = new Map();
|
|
25
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
26
|
+
this.datastore = { "system:presence": systemWorkspaceDatastore };
|
|
27
|
+
this.workspaces.set("system:presence", systemWorkspace);
|
|
28
|
+
}
|
|
29
|
+
joinSession(clientId) {
|
|
30
|
+
// Broadcast join message to all clients
|
|
31
|
+
const updateProviders = [...this.runtime.getQuorum().getMembers().keys()].filter((quorumClientId) => quorumClientId !== clientId);
|
|
32
|
+
// Limit to three providers to prevent flooding the network.
|
|
33
|
+
// If none respond, others present will (should) after a delay.
|
|
34
|
+
if (updateProviders.length > 3) {
|
|
35
|
+
updateProviders.length = 3;
|
|
36
|
+
}
|
|
37
|
+
this.runtime.submitSignal(joinMessageType, {
|
|
38
|
+
sendTimestamp: Date.now(),
|
|
39
|
+
avgLatency: this.averageLatency,
|
|
40
|
+
data: this.datastore,
|
|
41
|
+
updateProviders,
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
getWorkspace(internalWorkspaceAddress, requestedContent) {
|
|
45
|
+
const existing = this.workspaces.get(internalWorkspaceAddress);
|
|
46
|
+
if (existing) {
|
|
47
|
+
return existing.internal.ensureContent(requestedContent);
|
|
48
|
+
}
|
|
49
|
+
let workspaceDatastore = this.datastore[internalWorkspaceAddress];
|
|
50
|
+
if (workspaceDatastore === undefined) {
|
|
51
|
+
workspaceDatastore = this.datastore[internalWorkspaceAddress] = {};
|
|
52
|
+
}
|
|
53
|
+
const localUpdate = (states, forceBroadcast) => {
|
|
54
|
+
// Check for connectivity before sending updates.
|
|
55
|
+
if (!this.runtime.connected) {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
const clientConnectionId = this.runtime.clientId;
|
|
59
|
+
assert(clientConnectionId !== undefined, 0xa59 /* Client connected without clientId */);
|
|
60
|
+
const currentClientToSessionValueState = this.datastore["system:presence"].clientToSessionId[clientConnectionId];
|
|
61
|
+
const updates = {};
|
|
62
|
+
for (const [key, value] of Object.entries(states)) {
|
|
63
|
+
updates[key] = { [this.clientSessionId]: value };
|
|
64
|
+
}
|
|
65
|
+
this.localUpdate({
|
|
66
|
+
// Always send current connection mapping for some resiliency against
|
|
67
|
+
// lost signals. This ensures that client session id found in `updates`
|
|
68
|
+
// (which is this client's client session id) is always represented in
|
|
69
|
+
// system workspace of recipient clients.
|
|
70
|
+
"system:presence": {
|
|
71
|
+
clientToSessionId: {
|
|
72
|
+
[clientConnectionId]: { ...currentClientToSessionValueState },
|
|
73
|
+
},
|
|
74
|
+
},
|
|
75
|
+
[internalWorkspaceAddress]: updates,
|
|
76
|
+
}, forceBroadcast);
|
|
77
|
+
};
|
|
78
|
+
const entry = createPresenceStates({
|
|
79
|
+
clientSessionId: this.clientSessionId,
|
|
80
|
+
lookupClient: this.lookupClient,
|
|
81
|
+
localUpdate,
|
|
82
|
+
}, workspaceDatastore, requestedContent);
|
|
83
|
+
this.workspaces.set(internalWorkspaceAddress, entry);
|
|
84
|
+
return entry.public;
|
|
85
|
+
}
|
|
86
|
+
localUpdate(data, _forceBroadcast) {
|
|
87
|
+
const content = {
|
|
88
|
+
sendTimestamp: Date.now(),
|
|
89
|
+
avgLatency: this.averageLatency,
|
|
90
|
+
// isComplete: false,
|
|
91
|
+
data,
|
|
92
|
+
};
|
|
93
|
+
this.runtime.submitSignal(datastoreUpdateMessageType, content);
|
|
94
|
+
}
|
|
95
|
+
broadcastAllKnownState() {
|
|
96
|
+
this.runtime.submitSignal(datastoreUpdateMessageType, {
|
|
97
|
+
sendTimestamp: Date.now(),
|
|
98
|
+
avgLatency: this.averageLatency,
|
|
99
|
+
isComplete: true,
|
|
100
|
+
data: this.datastore,
|
|
101
|
+
});
|
|
102
|
+
this.refreshBroadcastRequested = false;
|
|
103
|
+
}
|
|
104
|
+
processSignal(
|
|
105
|
+
// Note: IInboundSignalMessage is used here in place of IExtensionMessage
|
|
106
|
+
// as IExtensionMessage's strictly JSON `content` creates type compatibility
|
|
107
|
+
// issues with `ClientSessionId` keys and really unknown value content.
|
|
108
|
+
// IExtensionMessage is a subset of IInboundSignalMessage so this is safe.
|
|
109
|
+
// Change types of DatastoreUpdateMessage | ClientJoinMessage to
|
|
110
|
+
// IExtensionMessage<> derivatives to see the issues.
|
|
111
|
+
message, local) {
|
|
112
|
+
const received = Date.now();
|
|
113
|
+
assert(message.clientId !== null, 0xa3a /* Map received signal without clientId */);
|
|
114
|
+
if (!isPresenceMessage(message)) {
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
if (local) {
|
|
118
|
+
const deliveryDelta = received - message.content.sendTimestamp;
|
|
119
|
+
// Limit returnedMessages count to 256 such that newest message
|
|
120
|
+
// always contributes at least 1/256th to the average. Older
|
|
121
|
+
// messages have more weight, but that diminishes as new messages
|
|
122
|
+
// contribute.
|
|
123
|
+
this.returnedMessages = Math.min(this.returnedMessages + 1, 256);
|
|
124
|
+
this.averageLatency =
|
|
125
|
+
(this.averageLatency * (this.returnedMessages - 1) + deliveryDelta) /
|
|
126
|
+
this.returnedMessages;
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
const timeModifier = received -
|
|
130
|
+
(this.averageLatency + message.content.avgLatency + message.content.sendTimestamp);
|
|
131
|
+
if (message.type === joinMessageType) {
|
|
132
|
+
// It is possible for some signals to come in while client is not connected due
|
|
133
|
+
// to how work is scheduled. If we are not connected, we can't respond to the
|
|
134
|
+
// join request. We will make our own Join request once we are connected.
|
|
135
|
+
if (this.runtime.connected) {
|
|
136
|
+
this.prepareJoinResponse(message.content.updateProviders, message.clientId);
|
|
137
|
+
}
|
|
138
|
+
// It is okay to continue processing the contained updates even if we are not
|
|
139
|
+
// connected.
|
|
140
|
+
}
|
|
141
|
+
else {
|
|
142
|
+
assert(message.type === datastoreUpdateMessageType, 0xa3b /* Unexpected message type */);
|
|
143
|
+
if (message.content.isComplete) {
|
|
144
|
+
this.refreshBroadcastRequested = false;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
for (const [workspaceAddress, remoteDatastore] of Object.entries(message.content.data)) {
|
|
148
|
+
// Direct to the appropriate Presence Workspace, if present.
|
|
149
|
+
const workspace = this.workspaces.get(workspaceAddress);
|
|
150
|
+
if (workspace) {
|
|
151
|
+
workspace.internal.processUpdate(received, timeModifier, remoteDatastore, message.clientId);
|
|
152
|
+
}
|
|
153
|
+
else {
|
|
154
|
+
// All broadcast state is kept even if not currently registered, unless a value
|
|
155
|
+
// notes itself to be ignored.
|
|
156
|
+
let workspaceDatastore = this.datastore[workspaceAddress];
|
|
157
|
+
if (workspaceDatastore === undefined) {
|
|
158
|
+
workspaceDatastore = this.datastore[workspaceAddress] = {};
|
|
159
|
+
if (!workspaceAddress.startsWith("system:")) {
|
|
160
|
+
// TODO: Emit workspaceActivated event for PresenceEvents
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
for (const [key, remoteAllKnownState] of Object.entries(remoteDatastore)) {
|
|
164
|
+
mergeUntrackedDatastore(key, remoteAllKnownState, workspaceDatastore, timeModifier);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Handles responding to another client joining the session.
|
|
171
|
+
*
|
|
172
|
+
* @param updateProviders - list of client connection id's that requestor selected
|
|
173
|
+
* to provide response
|
|
174
|
+
* @param requestor - `requestor` is only used in telemetry. While it is the requestor's
|
|
175
|
+
* client connection id, that is not most important. It is important that this is a
|
|
176
|
+
* unique shared id across all clients that might respond as we want to monitor the
|
|
177
|
+
* response patterns. The convenience of being client connection id will allow
|
|
178
|
+
* correlation with other telemetry where it is often called just `clientId`.
|
|
179
|
+
*/
|
|
180
|
+
prepareJoinResponse(updateProviders, requestor) {
|
|
181
|
+
this.refreshBroadcastRequested = true;
|
|
182
|
+
// We must be connected to receive this message, so clientId should be defined.
|
|
183
|
+
// If it isn't then, not really a problem; just won't be in provider or quorum list.
|
|
184
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
185
|
+
const clientId = this.runtime.clientId;
|
|
186
|
+
// const requestor = message.clientId;
|
|
187
|
+
if (updateProviders.includes(clientId)) {
|
|
188
|
+
// Send all current state to the new client
|
|
189
|
+
this.broadcastAllKnownState();
|
|
190
|
+
this.logger?.sendTelemetryEvent({
|
|
191
|
+
eventName: "JoinResponse",
|
|
192
|
+
details: {
|
|
193
|
+
type: "broadcastAll",
|
|
194
|
+
requestor,
|
|
195
|
+
role: "primary",
|
|
196
|
+
},
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
else {
|
|
200
|
+
// Schedule a broadcast to the new client after a delay only to send if
|
|
201
|
+
// another broadcast hasn't been seen in the meantime. The delay is based
|
|
202
|
+
// on the position in the quorum list. It doesn't have to be a stable
|
|
203
|
+
// list across all clients. We need something to provide suggested order
|
|
204
|
+
// to prevent a flood of broadcasts.
|
|
205
|
+
let relativeResponseOrder;
|
|
206
|
+
const quorumMembers = this.runtime.getQuorum().getMembers();
|
|
207
|
+
const self = quorumMembers.get(clientId);
|
|
208
|
+
if (self) {
|
|
209
|
+
// Compute order quorum join order (indicated by sequenceNumber)
|
|
210
|
+
relativeResponseOrder = 0;
|
|
211
|
+
for (const { sequenceNumber } of quorumMembers.values()) {
|
|
212
|
+
if (sequenceNumber < self.sequenceNumber) {
|
|
213
|
+
relativeResponseOrder++;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
else {
|
|
218
|
+
// Order past quorum members + arbitrary additional offset up to 10
|
|
219
|
+
relativeResponseOrder = quorumMembers.size + Math.random() * 10;
|
|
220
|
+
}
|
|
221
|
+
// These numbers have been chosen arbitrarily to start with.
|
|
222
|
+
// 20 is minimum wait time, 20 is the additional wait time per provider
|
|
223
|
+
// given an chance before us with named providers given more time.
|
|
224
|
+
const waitTime = 20 + 20 * (3 * updateProviders.length + relativeResponseOrder);
|
|
225
|
+
setTimeout(() => {
|
|
226
|
+
// Make sure a broadcast is still needed and we are currently connected.
|
|
227
|
+
// If not connected, nothing we can do.
|
|
228
|
+
if (this.refreshBroadcastRequested && this.runtime.connected) {
|
|
229
|
+
this.broadcastAllKnownState();
|
|
230
|
+
this.logger?.sendTelemetryEvent({
|
|
231
|
+
eventName: "JoinResponse",
|
|
232
|
+
details: {
|
|
233
|
+
type: "broadcastAll",
|
|
234
|
+
requestor,
|
|
235
|
+
role: "secondary",
|
|
236
|
+
order: relativeResponseOrder,
|
|
237
|
+
},
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
}, waitTime);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
//# sourceMappingURL=presenceDatastoreManager.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"presenceDatastoreManager.js","sourceRoot":"","sources":["../src/presenceDatastoreManager.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,qCAAqC,CAAC;AAY7D,OAAO,EAAE,oBAAoB,EAAE,uBAAuB,EAAE,MAAM,qBAAqB,CAAC;AAmCpF,MAAM,0BAA0B,GAAG,sBAAsB,CAAC;AAW1D,MAAM,eAAe,GAAG,iBAAiB,CAAC;AAW1C,SAAS,iBAAiB,CACzB,OAA8B;IAE9B,OAAO,OAAO,CAAC,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC;AACzC,CAAC;AAcD;;GAEG;AACH,MAAM,OAAO,4BAA4B;IAQxC,YACkB,eAAgC,EAChC,OAA0B,EAC1B,YAA2D,EAC3D,MAAuC,EACxD,wBAAkD,EAClD,eAA0D;QALzC,oBAAe,GAAf,eAAe,CAAiB;QAChC,YAAO,GAAP,OAAO,CAAmB;QAC1B,iBAAY,GAAZ,YAAY,CAA+C;QAC3D,WAAM,GAAN,MAAM,CAAiC;QAVjD,mBAAc,GAAG,CAAC,CAAC;QACnB,qBAAgB,GAAG,CAAC,CAAC;QACrB,8BAAyB,GAAG,KAAK,CAAC;QAEzB,eAAU,GAAG,IAAI,GAAG,EAAqD,CAAC;QAU1F,yEAAyE;QACzE,IAAI,CAAC,SAAS,GAAG,EAAE,iBAAiB,EAAE,wBAAwB,EAAuB,CAAC;QACtF,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,iBAAiB,EAAE,eAAe,CAAC,CAAC;IACzD,CAAC;IAEM,WAAW,CAAC,QAA4B;QAC9C,wCAAwC;QACxC,MAAM,eAAe,GAAG,CAAC,GAAG,IAAI,CAAC,OAAO,CAAC,SAAS,EAAE,CAAC,UAAU,EAAE,CAAC,IAAI,EAAE,CAAC,CAAC,MA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ACF,CAAC;YACF,CAAC;iBAAM,CAAC;gBACP,mEAAmE;gBACnE,qBAAqB,GAAG,aAAa,CAAC,IAAI,GAAG,IAAI,CAAC,MAAM,EAAE,GAAG,EAAE,CAAC;YACjE,CAAC;YACD,4DAA4D;YAC5D,uEAAuE;YACvE,kEAAkE;YAClE,MAAM,QAAQ,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC,GAAG,eAAe,CAAC,MAAM,GAAG,qBAAqB,CAAC,CAAC;YAChF,UAAU,CAAC,GAAG,EAAE;gBACf,wEAAwE;gBACxE,uCAAuC;gBACvC,IAAI,IAAI,CAAC,yBAAyB,IAAI,IAAI,CAAC,OAAO,CAAC,SAAS,EAAE,CAAC;oBAC9D,IAAI,CAAC,sBAAsB,EAAE,CAAC;oBAC9B,IAAI,CAAC,MAAM,EAAE,kBAAkB,CAAC;wBAC/B,SAAS,EAAE,cAAc;wBACzB,OAAO,EAAE;4BACR,IAAI,EAAE,cAAc;4BACpB,SAAS;4BACT,IAAI,EAAE,WAAW;4BACjB,KAAK,EAAE,qBAAqB;yBAC5B;qBACD,CAAC,CAAC;gBACJ,CAAC;YACF,CAAC,EAAE,QAAQ,CAAC,CAAC;QACd,CAAC;IACF,CAAC;CACD","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { assert } from \"@fluidframework/core-utils/internal\";\nimport type { IInboundSignalMessage } from \"@fluidframework/runtime-definitions/internal\";\nimport type { ITelemetryLoggerExt } from \"@fluidframework/telemetry-utils/internal\";\n\nimport type { ClientConnectionId } from \"./baseTypes.js\";\nimport type { IEphemeralRuntime } from \"./internalTypes.js\";\nimport type { ClientSessionId, ISessionClient } from \"./presence.js\";\nimport type {\n\tClientUpdateEntry,\n\tPresenceStatesInternal,\n\tValueElementMap,\n} from \"./presenceStates.js\";\nimport { createPresenceStates, mergeUntrackedDatastore } from \"./presenceStates.js\";\nimport type { SystemWorkspaceDatastore } from \"./systemWorkspace.js\";\nimport type {\n\tPresenceStates,\n\tPresenceStatesSchema,\n\tPresenceWorkspaceAddress,\n} from \"./types.js\";\n\nimport type { IExtensionMessage } from \"@fluidframework/presence/internal/container-definitions/internal\";\n\ninterface PresenceStatesEntry<TSchema extends PresenceStatesSchema> {\n\tpublic: PresenceStates<TSchema>;\n\tinternal: PresenceStatesInternal;\n}\n\ninterface SystemDatastore {\n\t\"system:presence\": SystemWorkspaceDatastore;\n}\n\ntype InternalWorkspaceAddress = `${\"s\" | \"n\"}:${PresenceWorkspaceAddress}`;\n\ntype PresenceDatastore = SystemDatastore & {\n\t[WorkspaceAddress: string]: ValueElementMap<PresenceStatesSchema>;\n};\n\ninterface GeneralDatastoreMessageContent {\n\t[WorkspaceAddress: string]: {\n\t\t[StateValueManagerKey: string]: {\n\t\t\t[ClientSessionId: ClientSessionId]: ClientUpdateEntry;\n\t\t};\n\t};\n}\n\ntype DatastoreMessageContent = SystemDatastore & GeneralDatastoreMessageContent;\n\nconst datastoreUpdateMessageType = \"Pres:DatastoreUpdate\";\ninterface DatastoreUpdateMessage extends IInboundSignalMessage {\n\ttype: typeof datastoreUpdateMessageType;\n\tcontent: {\n\t\tsendTimestamp: number;\n\t\tavgLatency: number;\n\t\tisComplete?: true;\n\t\tdata: DatastoreMessageContent;\n\t};\n}\n\nconst joinMessageType = \"Pres:ClientJoin\";\ninterface ClientJoinMessage extends IInboundSignalMessage {\n\ttype: typeof joinMessageType;\n\tcontent: {\n\t\tupdateProviders: ClientConnectionId[];\n\t\tsendTimestamp: number;\n\t\tavgLatency: number;\n\t\tdata: DatastoreMessageContent;\n\t};\n}\n\nfunction isPresenceMessage(\n\tmessage: IInboundSignalMessage,\n): message is DatastoreUpdateMessage | ClientJoinMessage {\n\treturn message.type.startsWith(\"Pres:\");\n}\n\n/**\n * @internal\n */\nexport interface PresenceDatastoreManager {\n\tjoinSession(clientId: ClientConnectionId): void;\n\tgetWorkspace<TSchema extends PresenceStatesSchema>(\n\t\tinternalWorkspaceAddress: InternalWorkspaceAddress,\n\t\trequestedContent: TSchema,\n\t): PresenceStates<TSchema>;\n\tprocessSignal(message: IExtensionMessage, local: boolean): void;\n}\n\n/**\n * Manages singleton datastore for all Presence.\n */\nexport class PresenceDatastoreManagerImpl implements PresenceDatastoreManager {\n\tprivate readonly datastore: PresenceDatastore;\n\tprivate averageLatency = 0;\n\tprivate returnedMessages = 0;\n\tprivate refreshBroadcastRequested = false;\n\n\tprivate readonly workspaces = new Map<string, PresenceStatesEntry<PresenceStatesSchema>>();\n\n\tpublic constructor(\n\t\tprivate readonly clientSessionId: ClientSessionId,\n\t\tprivate readonly runtime: IEphemeralRuntime,\n\t\tprivate readonly lookupClient: (clientId: ClientSessionId) => ISessionClient,\n\t\tprivate readonly logger: ITelemetryLoggerExt | undefined,\n\t\tsystemWorkspaceDatastore: SystemWorkspaceDatastore,\n\t\tsystemWorkspace: PresenceStatesEntry<PresenceStatesSchema>,\n\t) {\n\t\t// eslint-disable-next-line @typescript-eslint/consistent-type-assertions\n\t\tthis.datastore = { \"system:presence\": systemWorkspaceDatastore } as PresenceDatastore;\n\t\tthis.workspaces.set(\"system:presence\", systemWorkspace);\n\t}\n\n\tpublic joinSession(clientId: ClientConnectionId): void {\n\t\t// Broadcast join message to all clients\n\t\tconst updateProviders = [...this.runtime.getQuorum().getMembers().keys()].filter(\n\t\t\t(quorumClientId) => quorumClientId !== clientId,\n\t\t);\n\t\t// Limit to three providers to prevent flooding the network.\n\t\t// If none respond, others present will (should) after a delay.\n\t\tif (updateProviders.length > 3) {\n\t\t\tupdateProviders.length = 3;\n\t\t}\n\t\tthis.runtime.submitSignal(joinMessageType, {\n\t\t\tsendTimestamp: Date.now(),\n\t\t\tavgLatency: this.averageLatency,\n\t\t\tdata: this.datastore,\n\t\t\tupdateProviders,\n\t\t} satisfies ClientJoinMessage[\"content\"]);\n\t}\n\n\tpublic getWorkspace<TSchema extends PresenceStatesSchema>(\n\t\tinternalWorkspaceAddress: InternalWorkspaceAddress,\n\t\trequestedContent: TSchema,\n\t): PresenceStates<TSchema> {\n\t\tconst existing = this.workspaces.get(internalWorkspaceAddress);\n\t\tif (existing) {\n\t\t\treturn existing.internal.ensureContent(requestedContent);\n\t\t}\n\n\t\tlet workspaceDatastore = this.datastore[internalWorkspaceAddress];\n\t\tif (workspaceDatastore === undefined) {\n\t\t\tworkspaceDatastore = this.datastore[internalWorkspaceAddress] = {};\n\t\t}\n\n\t\tconst localUpdate = (\n\t\t\tstates: { [key: string]: ClientUpdateEntry },\n\t\t\tforceBroadcast: boolean,\n\t\t): void => {\n\t\t\t// Check for connectivity before sending updates.\n\t\t\tif (!this.runtime.connected) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst clientConnectionId = this.runtime.clientId;\n\t\t\tassert(clientConnectionId !== undefined, 0xa59 /* Client connected without clientId */);\n\t\t\tconst currentClientToSessionValueState =\n\t\t\t\tthis.datastore[\"system:presence\"].clientToSessionId[clientConnectionId];\n\n\t\t\tconst updates: GeneralDatastoreMessageContent[InternalWorkspaceAddress] = {};\n\t\t\tfor (const [key, value] of Object.entries(states)) {\n\t\t\t\tupdates[key] = { [this.clientSessionId]: value };\n\t\t\t}\n\t\t\tthis.localUpdate(\n\t\t\t\t{\n\t\t\t\t\t// Always send current connection mapping for some resiliency against\n\t\t\t\t\t// lost signals. This ensures that client session id found in `updates`\n\t\t\t\t\t// (which is this client's client session id) is always represented in\n\t\t\t\t\t// system workspace of recipient clients.\n\t\t\t\t\t\"system:presence\": {\n\t\t\t\t\t\tclientToSessionId: {\n\t\t\t\t\t\t\t[clientConnectionId]: { ...currentClientToSessionValueState },\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t\t[internalWorkspaceAddress]: updates,\n\t\t\t\t},\n\t\t\t\tforceBroadcast,\n\t\t\t);\n\t\t};\n\n\t\tconst entry = createPresenceStates(\n\t\t\t{\n\t\t\t\tclientSessionId: this.clientSessionId,\n\t\t\t\tlookupClient: this.lookupClient,\n\t\t\t\tlocalUpdate,\n\t\t\t},\n\t\t\tworkspaceDatastore,\n\t\t\trequestedContent,\n\t\t);\n\n\t\tthis.workspaces.set(internalWorkspaceAddress, entry);\n\t\treturn entry.public;\n\t}\n\n\tprivate localUpdate(data: DatastoreMessageContent, _forceBroadcast: boolean): void {\n\t\tconst content = {\n\t\t\tsendTimestamp: Date.now(),\n\t\t\tavgLatency: this.averageLatency,\n\t\t\t// isComplete: false,\n\t\t\tdata,\n\t\t} satisfies DatastoreUpdateMessage[\"content\"];\n\t\tthis.runtime.submitSignal(datastoreUpdateMessageType, content);\n\t}\n\n\tprivate broadcastAllKnownState(): void {\n\t\tthis.runtime.submitSignal(datastoreUpdateMessageType, {\n\t\t\tsendTimestamp: Date.now(),\n\t\t\tavgLatency: this.averageLatency,\n\t\t\tisComplete: true,\n\t\t\tdata: this.datastore,\n\t\t} satisfies DatastoreUpdateMessage[\"content\"]);\n\t\tthis.refreshBroadcastRequested = false;\n\t}\n\n\tpublic processSignal(\n\t\t// Note: IInboundSignalMessage is used here in place of IExtensionMessage\n\t\t// as IExtensionMessage's strictly JSON `content` creates type compatibility\n\t\t// issues with `ClientSessionId` keys and really unknown value content.\n\t\t// IExtensionMessage is a subset of IInboundSignalMessage so this is safe.\n\t\t// Change types of DatastoreUpdateMessage | ClientJoinMessage to\n\t\t// IExtensionMessage<> derivatives to see the issues.\n\t\tmessage: IInboundSignalMessage | DatastoreUpdateMessage | ClientJoinMessage,\n\t\tlocal: boolean,\n\t): void {\n\t\tconst received = Date.now();\n\t\tassert(message.clientId !== null, 0xa3a /* Map received signal without clientId */);\n\t\tif (!isPresenceMessage(message)) {\n\t\t\treturn;\n\t\t}\n\t\tif (local) {\n\t\t\tconst deliveryDelta = received - message.content.sendTimestamp;\n\t\t\t// Limit returnedMessages count to 256 such that newest message\n\t\t\t// always contributes at least 1/256th to the average. Older\n\t\t\t// messages have more weight, but that diminishes as new messages\n\t\t\t// contribute.\n\t\t\tthis.returnedMessages = Math.min(this.returnedMessages + 1, 256);\n\t\t\tthis.averageLatency =\n\t\t\t\t(this.averageLatency * (this.returnedMessages - 1) + deliveryDelta) /\n\t\t\t\tthis.returnedMessages;\n\t\t\treturn;\n\t\t}\n\n\t\tconst timeModifier =\n\t\t\treceived -\n\t\t\t(this.averageLatency + message.content.avgLatency + message.content.sendTimestamp);\n\n\t\tif (message.type === joinMessageType) {\n\t\t\t// It is possible for some signals to come in while client is not connected due\n\t\t\t// to how work is scheduled. If we are not connected, we can't respond to the\n\t\t\t// join request. We will make our own Join request once we are connected.\n\t\t\tif (this.runtime.connected) {\n\t\t\t\tthis.prepareJoinResponse(message.content.updateProviders, message.clientId);\n\t\t\t}\n\t\t\t// It is okay to continue processing the contained updates even if we are not\n\t\t\t// connected.\n\t\t} else {\n\t\t\tassert(message.type === datastoreUpdateMessageType, 0xa3b /* Unexpected message type */);\n\t\t\tif (message.content.isComplete) {\n\t\t\t\tthis.refreshBroadcastRequested = false;\n\t\t\t}\n\t\t}\n\n\t\tfor (const [workspaceAddress, remoteDatastore] of Object.entries(message.content.data)) {\n\t\t\t// Direct to the appropriate Presence Workspace, if present.\n\t\t\tconst workspace = this.workspaces.get(workspaceAddress);\n\t\t\tif (workspace) {\n\t\t\t\tworkspace.internal.processUpdate(\n\t\t\t\t\treceived,\n\t\t\t\t\ttimeModifier,\n\t\t\t\t\tremoteDatastore,\n\t\t\t\t\tmessage.clientId,\n\t\t\t\t);\n\t\t\t} else {\n\t\t\t\t// All broadcast state is kept even if not currently registered, unless a value\n\t\t\t\t// notes itself to be ignored.\n\t\t\t\tlet workspaceDatastore = this.datastore[workspaceAddress];\n\t\t\t\tif (workspaceDatastore === undefined) {\n\t\t\t\t\tworkspaceDatastore = this.datastore[workspaceAddress] = {};\n\t\t\t\t\tif (!workspaceAddress.startsWith(\"system:\")) {\n\t\t\t\t\t\t// TODO: Emit workspaceActivated event for PresenceEvents\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tfor (const [key, remoteAllKnownState] of Object.entries(remoteDatastore)) {\n\t\t\t\t\tmergeUntrackedDatastore(key, remoteAllKnownState, workspaceDatastore, timeModifier);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Handles responding to another client joining the session.\n\t *\n\t * @param updateProviders - list of client connection id's that requestor selected\n\t * to provide response\n\t * @param requestor - `requestor` is only used in telemetry. While it is the requestor's\n\t * client connection id, that is not most important. It is important that this is a\n\t * unique shared id across all clients that might respond as we want to monitor the\n\t * response patterns. The convenience of being client connection id will allow\n\t * correlation with other telemetry where it is often called just `clientId`.\n\t */\n\tprivate prepareJoinResponse(\n\t\tupdateProviders: ClientConnectionId[],\n\t\trequestor: ClientConnectionId,\n\t): void {\n\t\tthis.refreshBroadcastRequested = true;\n\t\t// We must be connected to receive this message, so clientId should be defined.\n\t\t// If it isn't then, not really a problem; just won't be in provider or quorum list.\n\t\t// eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n\t\tconst clientId = this.runtime.clientId!;\n\t\t// const requestor = message.clientId;\n\t\tif (updateProviders.includes(clientId)) {\n\t\t\t// Send all current state to the new client\n\t\t\tthis.broadcastAllKnownState();\n\t\t\tthis.logger?.sendTelemetryEvent({\n\t\t\t\teventName: \"JoinResponse\",\n\t\t\t\tdetails: {\n\t\t\t\t\ttype: \"broadcastAll\",\n\t\t\t\t\trequestor,\n\t\t\t\t\trole: \"primary\",\n\t\t\t\t},\n\t\t\t});\n\t\t} else {\n\t\t\t// Schedule a broadcast to the new client after a delay only to send if\n\t\t\t// another broadcast hasn't been seen in the meantime. The delay is based\n\t\t\t// on the position in the quorum list. It doesn't have to be a stable\n\t\t\t// list across all clients. We need something to provide suggested order\n\t\t\t// to prevent a flood of broadcasts.\n\t\t\tlet relativeResponseOrder;\n\t\t\tconst quorumMembers = this.runtime.getQuorum().getMembers();\n\t\t\tconst self = quorumMembers.get(clientId);\n\t\t\tif (self) {\n\t\t\t\t// Compute order quorum join order (indicated by sequenceNumber)\n\t\t\t\trelativeResponseOrder = 0;\n\t\t\t\tfor (const { sequenceNumber } of quorumMembers.values()) {\n\t\t\t\t\tif (sequenceNumber < self.sequenceNumber) {\n\t\t\t\t\t\trelativeResponseOrder++;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\t// Order past quorum members + arbitrary additional offset up to 10\n\t\t\t\trelativeResponseOrder = quorumMembers.size + Math.random() * 10;\n\t\t\t}\n\t\t\t// These numbers have been chosen arbitrarily to start with.\n\t\t\t// 20 is minimum wait time, 20 is the additional wait time per provider\n\t\t\t// given an chance before us with named providers given more time.\n\t\t\tconst waitTime = 20 + 20 * (3 * updateProviders.length + relativeResponseOrder);\n\t\t\tsetTimeout(() => {\n\t\t\t\t// Make sure a broadcast is still needed and we are currently connected.\n\t\t\t\t// If not connected, nothing we can do.\n\t\t\t\tif (this.refreshBroadcastRequested && this.runtime.connected) {\n\t\t\t\t\tthis.broadcastAllKnownState();\n\t\t\t\t\tthis.logger?.sendTelemetryEvent({\n\t\t\t\t\t\teventName: \"JoinResponse\",\n\t\t\t\t\t\tdetails: {\n\t\t\t\t\t\t\ttype: \"broadcastAll\",\n\t\t\t\t\t\t\trequestor,\n\t\t\t\t\t\t\trole: \"secondary\",\n\t\t\t\t\t\t\torder: relativeResponseOrder,\n\t\t\t\t\t\t},\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t}, waitTime);\n\t\t}\n\t}\n}\n"]}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
import type { IEphemeralRuntime } from "./internalTypes.js";
|
|
6
|
+
import type { ClientSessionId, IPresence } from "./presence.js";
|
|
7
|
+
import type { IContainerExtension } from "@fluidframework/presence/internal/container-definitions/internal";
|
|
8
|
+
/**
|
|
9
|
+
* Portion of the container extension requirements ({@link IContainerExtension}) that are delegated to presence manager.
|
|
10
|
+
*
|
|
11
|
+
* @internal
|
|
12
|
+
*/
|
|
13
|
+
export type PresenceExtensionInterface = Required<Pick<IContainerExtension<never>, "processSignal">>;
|
|
14
|
+
/**
|
|
15
|
+
* Instantiates Presence Manager
|
|
16
|
+
*
|
|
17
|
+
* @internal
|
|
18
|
+
*/
|
|
19
|
+
export declare function createPresenceManager(runtime: IEphemeralRuntime, clientSessionId?: ClientSessionId): IPresence & PresenceExtensionInterface;
|
|
20
|
+
//# sourceMappingURL=presenceManager.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"presenceManager.d.ts","sourceRoot":"","sources":["../src/presenceManager.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAUH,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AAC5D,OAAO,KAAK,EACX,eAAe,EACf,SAAS,EAGT,MAAM,eAAe,CAAC;AAWvB,OAAO,KAAK,EACX,mBAAmB,EAEnB,MAAM,kEAAkE,CAAC;AAI1E;;;;GAIG;AACH,MAAM,MAAM,0BAA0B,GAAG,QAAQ,CAChD,IAAI,CAAC,mBAAmB,CAAC,KAAK,CAAC,EAAE,eAAe,CAAC,CACjD,CAAC;AAoIF;;;;GAIG;AACH,wBAAgB,qBAAqB,CACpC,OAAO,EAAE,iBAAiB,EAC1B,eAAe,GAAE,eAAsD,GACrE,SAAS,GAAG,0BAA0B,CAExC"}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
import { createSessionId } from "@fluidframework/id-compressor/internal";
|
|
6
|
+
import { createChildMonitoringContext } from "@fluidframework/telemetry-utils/internal";
|
|
7
|
+
import { PresenceDatastoreManagerImpl } from "./presenceDatastoreManager.js";
|
|
8
|
+
import { createSystemWorkspace } from "./systemWorkspace.js";
|
|
9
|
+
import { createEmitter } from "@fluidframework/presence/internal/events";
|
|
10
|
+
/**
|
|
11
|
+
* The Presence manager
|
|
12
|
+
*/
|
|
13
|
+
class PresenceManager {
|
|
14
|
+
constructor(runtime, clientSessionId) {
|
|
15
|
+
this.events = createEmitter();
|
|
16
|
+
this.mc = undefined;
|
|
17
|
+
const logger = runtime.logger;
|
|
18
|
+
if (logger) {
|
|
19
|
+
this.mc = createChildMonitoringContext({ logger, namespace: "Presence" });
|
|
20
|
+
this.mc.logger.sendTelemetryEvent({ eventName: "PresenceInstantiated" });
|
|
21
|
+
}
|
|
22
|
+
[this.datastoreManager, this.systemWorkspace] = setupSubComponents(clientSessionId, runtime, this.events, this.mc?.logger);
|
|
23
|
+
runtime.on("connected", this.onConnect.bind(this));
|
|
24
|
+
runtime.on("disconnected", () => {
|
|
25
|
+
if (runtime.clientId !== undefined) {
|
|
26
|
+
this.removeClientConnectionId(runtime.clientId);
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
runtime.getAudience().on("removeMember", this.removeClientConnectionId.bind(this));
|
|
30
|
+
// Check if already connected at the time of construction.
|
|
31
|
+
// If constructed during data store load, the runtime may already be connected
|
|
32
|
+
// and the "connected" event will be raised during completion. With construction
|
|
33
|
+
// delayed we expect that "connected" event has passed.
|
|
34
|
+
// Note: In some manual testing, this does not appear to be enough to
|
|
35
|
+
// always trigger an initial connect.
|
|
36
|
+
const clientId = runtime.clientId;
|
|
37
|
+
if (clientId !== undefined && runtime.connected) {
|
|
38
|
+
this.onConnect(clientId);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
onConnect(clientConnectionId) {
|
|
42
|
+
this.systemWorkspace.onConnectionAdded(clientConnectionId);
|
|
43
|
+
this.datastoreManager.joinSession(clientConnectionId);
|
|
44
|
+
}
|
|
45
|
+
removeClientConnectionId(clientConnectionId) {
|
|
46
|
+
this.systemWorkspace.removeClientConnectionId(clientConnectionId);
|
|
47
|
+
}
|
|
48
|
+
getAttendees() {
|
|
49
|
+
return this.systemWorkspace.getAttendees();
|
|
50
|
+
}
|
|
51
|
+
getAttendee(clientId) {
|
|
52
|
+
return this.systemWorkspace.getAttendee(clientId);
|
|
53
|
+
}
|
|
54
|
+
getMyself() {
|
|
55
|
+
return this.systemWorkspace.getMyself();
|
|
56
|
+
}
|
|
57
|
+
getStates(workspaceAddress, requestedContent) {
|
|
58
|
+
return this.datastoreManager.getWorkspace(`s:${workspaceAddress}`, requestedContent);
|
|
59
|
+
}
|
|
60
|
+
getNotifications(workspaceAddress, requestedContent) {
|
|
61
|
+
return this.datastoreManager.getWorkspace(`n:${workspaceAddress}`, requestedContent);
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Check for Presence message and process it.
|
|
65
|
+
*
|
|
66
|
+
* @param address - Address of the message
|
|
67
|
+
* @param message - Message to be processed
|
|
68
|
+
* @param local - Whether the message originated locally (`true`) or remotely (`false`)
|
|
69
|
+
*/
|
|
70
|
+
processSignal(address, message, local) {
|
|
71
|
+
this.datastoreManager.processSignal(message, local);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Helper for Presence Manager setup
|
|
76
|
+
*
|
|
77
|
+
* Presence Manager is outermost layer of the presence system and has two main
|
|
78
|
+
* sub-components:
|
|
79
|
+
* 1. PresenceDatastoreManager: Manages the unified general data for states and
|
|
80
|
+
* registry for workspaces.
|
|
81
|
+
* 2. SystemWorkspace: Custom internal workspace for system states including
|
|
82
|
+
* attendee management. It is registered with the PresenceDatastoreManager.
|
|
83
|
+
*/
|
|
84
|
+
function setupSubComponents(clientSessionId, runtime, events, logger) {
|
|
85
|
+
const systemWorkspaceDatastore = {
|
|
86
|
+
clientToSessionId: {},
|
|
87
|
+
};
|
|
88
|
+
const systemWorkspaceConfig = createSystemWorkspace(clientSessionId, systemWorkspaceDatastore, events, runtime.getAudience());
|
|
89
|
+
const datastoreManager = new PresenceDatastoreManagerImpl(clientSessionId, runtime, systemWorkspaceConfig.workspace.getAttendee.bind(systemWorkspaceConfig.workspace), logger, systemWorkspaceDatastore, systemWorkspaceConfig.statesEntry);
|
|
90
|
+
return [datastoreManager, systemWorkspaceConfig.workspace];
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Instantiates Presence Manager
|
|
94
|
+
*
|
|
95
|
+
* @internal
|
|
96
|
+
*/
|
|
97
|
+
export function createPresenceManager(runtime, clientSessionId = createSessionId()) {
|
|
98
|
+
return new PresenceManager(runtime, clientSessionId);
|
|
99
|
+
}
|
|
100
|
+
//# sourceMappingURL=presenceManager.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"presenceManager.js","sourceRoot":"","sources":["../src/presenceManager.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,eAAe,EAAE,MAAM,wCAAwC,CAAC;AAKzE,OAAO,EAAE,4BAA4B,EAAE,MAAM,0CAA0C,CAAC;AAWxF,OAAO,EAAE,4BAA4B,EAAE,MAAM,+BAA+B,CAAC;AAE7E,OAAO,EAAE,qBAAqB,EAAE,MAAM,sBAAsB,CAAC;AAY7D,OAAO,EAAE,aAAa,EAAE,MAAM,0CAA0C,CAAC;AAWzE;;GAEG;AACH,MAAM,eAAe;IAQpB,YAAmB,OAA0B,EAAE,eAAgC;QAJ/D,WAAM,GAAG,aAAa,EAAkB,CAAC;QAExC,OAAE,GAAkC,SAAS,CAAC;QAG9D,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;QAC9B,IAAI,MAAM,EAAE,CAAC;YACZ,IAAI,CAAC,EAAE,GAAG,4BAA4B,CAAC,EAAE,MAAM,EAAE,SAAS,EAAE,UAAU,EAAE,CAAC,CAAC;YAC1E,IAAI,CAAC,EAAE,CAAC,MAAM,CAAC,kBAAkB,CAAC,EAAE,SAAS,EAAE,sBAAsB,EAAE,CAAC,CAAC;QAC1E,CAAC;QAED,CAAC,IAAI,CAAC,gBAAgB,EAAE,IAAI,CAAC,eAAe,CAAC,GAA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AEvE,OAAO,IAAI,eAAe,CAAC,OAAO,EAAE,eAAe,CAAC,CAAC;AACtD,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { createSessionId } from \"@fluidframework/id-compressor/internal\";\nimport type {\n\tITelemetryLoggerExt,\n\tMonitoringContext,\n} from \"@fluidframework/telemetry-utils/internal\";\nimport { createChildMonitoringContext } from \"@fluidframework/telemetry-utils/internal\";\n\nimport type { ClientConnectionId } from \"./baseTypes.js\";\nimport type { IEphemeralRuntime } from \"./internalTypes.js\";\nimport type {\n\tClientSessionId,\n\tIPresence,\n\tISessionClient,\n\tPresenceEvents,\n} from \"./presence.js\";\nimport type { PresenceDatastoreManager } from \"./presenceDatastoreManager.js\";\nimport { PresenceDatastoreManagerImpl } from \"./presenceDatastoreManager.js\";\nimport type { SystemWorkspace, SystemWorkspaceDatastore } from \"./systemWorkspace.js\";\nimport { createSystemWorkspace } from \"./systemWorkspace.js\";\nimport type {\n\tPresenceStates,\n\tPresenceWorkspaceAddress,\n\tPresenceStatesSchema,\n} from \"./types.js\";\n\nimport type {\n\tIContainerExtension,\n\tIExtensionMessage,\n} from \"@fluidframework/presence/internal/container-definitions/internal\";\nimport type { IEmitter } from \"@fluidframework/presence/internal/events\";\nimport { createEmitter } from \"@fluidframework/presence/internal/events\";\n\n/**\n * Portion of the container extension requirements ({@link IContainerExtension}) that are delegated to presence manager.\n *\n * @internal\n */\nexport type PresenceExtensionInterface = Required<\n\tPick<IContainerExtension<never>, \"processSignal\">\n>;\n\n/**\n * The Presence manager\n */\nclass PresenceManager implements IPresence, PresenceExtensionInterface {\n\tprivate readonly datastoreManager: PresenceDatastoreManager;\n\tprivate readonly systemWorkspace: SystemWorkspace;\n\n\tpublic readonly events = createEmitter<PresenceEvents>();\n\n\tprivate readonly mc: MonitoringContext | undefined = undefined;\n\n\tpublic constructor(runtime: IEphemeralRuntime, clientSessionId: ClientSessionId) {\n\t\tconst logger = runtime.logger;\n\t\tif (logger) {\n\t\t\tthis.mc = createChildMonitoringContext({ logger, namespace: \"Presence\" });\n\t\t\tthis.mc.logger.sendTelemetryEvent({ eventName: \"PresenceInstantiated\" });\n\t\t}\n\n\t\t[this.datastoreManager, this.systemWorkspace] = setupSubComponents(\n\t\t\tclientSessionId,\n\t\t\truntime,\n\t\t\tthis.events,\n\t\t\tthis.mc?.logger,\n\t\t);\n\n\t\truntime.on(\"connected\", this.onConnect.bind(this));\n\n\t\truntime.on(\"disconnected\", () => {\n\t\t\tif (runtime.clientId !== undefined) {\n\t\t\t\tthis.removeClientConnectionId(runtime.clientId);\n\t\t\t}\n\t\t});\n\n\t\truntime.getAudience().on(\"removeMember\", this.removeClientConnectionId.bind(this));\n\n\t\t// Check if already connected at the time of construction.\n\t\t// If constructed during data store load, the runtime may already be connected\n\t\t// and the \"connected\" event will be raised during completion. With construction\n\t\t// delayed we expect that \"connected\" event has passed.\n\t\t// Note: In some manual testing, this does not appear to be enough to\n\t\t// always trigger an initial connect.\n\t\tconst clientId = runtime.clientId;\n\t\tif (clientId !== undefined && runtime.connected) {\n\t\t\tthis.onConnect(clientId);\n\t\t}\n\t}\n\n\tprivate onConnect(clientConnectionId: ClientConnectionId): void {\n\t\tthis.systemWorkspace.onConnectionAdded(clientConnectionId);\n\t\tthis.datastoreManager.joinSession(clientConnectionId);\n\t}\n\n\tprivate removeClientConnectionId(clientConnectionId: ClientConnectionId): void {\n\t\tthis.systemWorkspace.removeClientConnectionId(clientConnectionId);\n\t}\n\n\tpublic getAttendees(): ReadonlySet<ISessionClient> {\n\t\treturn this.systemWorkspace.getAttendees();\n\t}\n\n\tpublic getAttendee(clientId: ClientConnectionId | ClientSessionId): ISessionClient {\n\t\treturn this.systemWorkspace.getAttendee(clientId);\n\t}\n\n\tpublic getMyself(): ISessionClient {\n\t\treturn this.systemWorkspace.getMyself();\n\t}\n\n\tpublic getStates<TSchema extends PresenceStatesSchema>(\n\t\tworkspaceAddress: PresenceWorkspaceAddress,\n\t\trequestedContent: TSchema,\n\t): PresenceStates<TSchema> {\n\t\treturn this.datastoreManager.getWorkspace(`s:${workspaceAddress}`, requestedContent);\n\t}\n\n\tpublic getNotifications<TSchema extends PresenceStatesSchema>(\n\t\tworkspaceAddress: PresenceWorkspaceAddress,\n\t\trequestedContent: TSchema,\n\t): PresenceStates<TSchema> {\n\t\treturn this.datastoreManager.getWorkspace(`n:${workspaceAddress}`, requestedContent);\n\t}\n\n\t/**\n\t * Check for Presence message and process it.\n\t *\n\t * @param address - Address of the message\n\t * @param message - Message to be processed\n\t * @param local - Whether the message originated locally (`true`) or remotely (`false`)\n\t */\n\tpublic processSignal(address: string, message: IExtensionMessage, local: boolean): void {\n\t\tthis.datastoreManager.processSignal(message, local);\n\t}\n}\n\n/**\n * Helper for Presence Manager setup\n *\n * Presence Manager is outermost layer of the presence system and has two main\n * sub-components:\n * 1. PresenceDatastoreManager: Manages the unified general data for states and\n * registry for workspaces.\n * 2. SystemWorkspace: Custom internal workspace for system states including\n * attendee management. It is registered with the PresenceDatastoreManager.\n */\nfunction setupSubComponents(\n\tclientSessionId: ClientSessionId,\n\truntime: IEphemeralRuntime,\n\tevents: IEmitter<PresenceEvents>,\n\tlogger: ITelemetryLoggerExt | undefined,\n): [PresenceDatastoreManager, SystemWorkspace] {\n\tconst systemWorkspaceDatastore: SystemWorkspaceDatastore = {\n\t\tclientToSessionId: {},\n\t};\n\tconst systemWorkspaceConfig = createSystemWorkspace(\n\t\tclientSessionId,\n\t\tsystemWorkspaceDatastore,\n\t\tevents,\n\t\truntime.getAudience(),\n\t);\n\tconst datastoreManager = new PresenceDatastoreManagerImpl(\n\t\tclientSessionId,\n\t\truntime,\n\t\tsystemWorkspaceConfig.workspace.getAttendee.bind(systemWorkspaceConfig.workspace),\n\t\tlogger,\n\t\tsystemWorkspaceDatastore,\n\t\tsystemWorkspaceConfig.statesEntry,\n\t);\n\treturn [datastoreManager, systemWorkspaceConfig.workspace];\n}\n\n/**\n * Instantiates Presence Manager\n *\n * @internal\n */\nexport function createPresenceManager(\n\truntime: IEphemeralRuntime,\n\tclientSessionId: ClientSessionId = createSessionId() as ClientSessionId,\n): IPresence & PresenceExtensionInterface {\n\treturn new PresenceManager(runtime, clientSessionId);\n}\n"]}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
import type { ClientConnectionId } from "./baseTypes.js";
|
|
6
|
+
import type { InternalTypes } from "./exposedInternalTypes.js";
|
|
7
|
+
import type { ClientRecord } from "./internalTypes.js";
|
|
8
|
+
import type { ClientSessionId, ISessionClient } from "./presence.js";
|
|
9
|
+
import type { PresenceStates, PresenceStatesSchema } from "./types.js";
|
|
10
|
+
/**
|
|
11
|
+
* @internal
|
|
12
|
+
*/
|
|
13
|
+
export type MapSchemaElement<TSchema extends PresenceStatesSchema, Part extends keyof ReturnType<TSchema[keyof TSchema]>, Keys extends keyof TSchema = keyof TSchema> = ReturnType<TSchema[Keys]>[Part];
|
|
14
|
+
/**
|
|
15
|
+
* @internal
|
|
16
|
+
*/
|
|
17
|
+
export interface PresenceRuntime {
|
|
18
|
+
readonly clientSessionId: ClientSessionId;
|
|
19
|
+
lookupClient(clientId: ClientConnectionId): ISessionClient;
|
|
20
|
+
localUpdate(states: {
|
|
21
|
+
[key: string]: ClientUpdateEntry;
|
|
22
|
+
}, forceBroadcast: boolean): void;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* ValueElementMap is a map of key to a map of clientId to ValueState.
|
|
26
|
+
* It is not restricted to the schema of the map as it may receive updates from other clients
|
|
27
|
+
* with managers that have not been registered locally. Each map node is responsible for keeping
|
|
28
|
+
* all sessions state to be able to pick arbitrary client to rebroadcast to others.
|
|
29
|
+
*
|
|
30
|
+
* This generic aspect makes some typing difficult. The loose typing is not broadcast to the
|
|
31
|
+
* consumers that are expected to maintain their schema over multiple versions of clients.
|
|
32
|
+
*
|
|
33
|
+
* @internal
|
|
34
|
+
*/
|
|
35
|
+
export interface ValueElementMap<_TSchema extends PresenceStatesSchema> {
|
|
36
|
+
[key: string]: ClientRecord<InternalTypes.ValueDirectoryOrState<unknown>>;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* @internal
|
|
40
|
+
*/
|
|
41
|
+
export type ClientUpdateEntry = InternalTypes.ValueDirectoryOrState<unknown> & {
|
|
42
|
+
ignoreUnmonitored?: true;
|
|
43
|
+
};
|
|
44
|
+
type ClientUpdateRecord = ClientRecord<ClientUpdateEntry>;
|
|
45
|
+
interface ValueUpdateRecord {
|
|
46
|
+
[valueKey: string]: ClientUpdateRecord;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* @internal
|
|
50
|
+
*/
|
|
51
|
+
export interface PresenceStatesInternal {
|
|
52
|
+
ensureContent<TSchemaAdditional extends PresenceStatesSchema>(content: TSchemaAdditional): PresenceStates<TSchemaAdditional>;
|
|
53
|
+
processUpdate(received: number, timeModifier: number, remoteDatastore: ValueUpdateRecord, senderConnectionId: ClientConnectionId): void;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Updates remote state into the local [untracked] datastore.
|
|
57
|
+
*
|
|
58
|
+
* @param key - The key of the datastore to merge the untracked data into.
|
|
59
|
+
* @param remoteAllKnownState - The remote state to merge into the datastore.
|
|
60
|
+
* @param datastore - The datastore to merge the untracked data into.
|
|
61
|
+
*
|
|
62
|
+
* @remarks
|
|
63
|
+
* In the case of ignored unmonitored data, the client entries are not stored,
|
|
64
|
+
* though the value keys will be populated and often remain empty.
|
|
65
|
+
*
|
|
66
|
+
* @internal
|
|
67
|
+
*/
|
|
68
|
+
export declare function mergeUntrackedDatastore(key: string, remoteAllKnownState: ClientUpdateRecord, datastore: ValueElementMap<PresenceStatesSchema>, timeModifier: number): void;
|
|
69
|
+
/**
|
|
70
|
+
* Create a new PresenceStates using the DataStoreRuntime provided.
|
|
71
|
+
* @param initialContent - The initial value managers to register.
|
|
72
|
+
*/
|
|
73
|
+
export declare function createPresenceStates<TSchema extends PresenceStatesSchema>(runtime: PresenceRuntime, datastore: ValueElementMap<PresenceStatesSchema>, initialContent: TSchema): {
|
|
74
|
+
public: PresenceStates<TSchema>;
|
|
75
|
+
internal: PresenceStatesInternal;
|
|
76
|
+
};
|
|
77
|
+
export {};
|
|
78
|
+
//# sourceMappingURL=presenceStates.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"presenceStates.d.ts","sourceRoot":"","sources":["../src/presenceStates.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAIH,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AACzD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC;AAC/D,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAEvD,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAErE,OAAO,KAAK,EAAE,cAAc,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAC;AAGvE;;GAEG;AACH,MAAM,MAAM,gBAAgB,CAC3B,OAAO,SAAS,oBAAoB,EACpC,IAAI,SAAS,MAAM,UAAU,CAAC,OAAO,CAAC,MAAM,OAAO,CAAC,CAAC,EACrD,IAAI,SAAS,MAAM,OAAO,GAAG,MAAM,OAAO,IACvC,UAAU,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;AAEpC;;GAEG;AACH,MAAM,WAAW,eAAe;IAC/B,QAAQ,CAAC,eAAe,EAAE,eAAe,CAAC;IAC1C,YAAY,CAAC,QAAQ,EAAE,kBAAkB,GAAG,cAAc,CAAC;IAC3D,WAAW,CAAC,MAAM,EAAE;QAAE,CAAC,GAAG,EAAE,MAAM,GAAG,iBAAiB,CAAA;KAAE,EA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