@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.
Files changed (255) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +216 -0
  3. package/dist/alpha.d.ts +58 -0
  4. package/dist/baseTypes.d.ts +24 -0
  5. package/dist/baseTypes.d.ts.map +1 -0
  6. package/dist/baseTypes.js +7 -0
  7. package/dist/baseTypes.js.map +1 -0
  8. package/dist/container-definitions/containerExtensions.d.ts +137 -0
  9. package/dist/container-definitions/containerExtensions.d.ts.map +1 -0
  10. package/dist/container-definitions/containerExtensions.js +7 -0
  11. package/dist/container-definitions/containerExtensions.js.map +1 -0
  12. package/dist/container-definitions/index.d.ts +7 -0
  13. package/dist/container-definitions/index.d.ts.map +1 -0
  14. package/dist/container-definitions/index.js +7 -0
  15. package/dist/container-definitions/index.js.map +1 -0
  16. package/dist/container-definitions/runtime.d.ts +12 -0
  17. package/dist/container-definitions/runtime.d.ts.map +1 -0
  18. package/dist/container-definitions/runtime.js +7 -0
  19. package/dist/container-definitions/runtime.js.map +1 -0
  20. package/dist/core-interfaces/exposedUtilityTypes.d.ts +446 -0
  21. package/dist/core-interfaces/exposedUtilityTypes.d.ts.map +1 -0
  22. package/dist/core-interfaces/exposedUtilityTypes.js +11 -0
  23. package/dist/core-interfaces/exposedUtilityTypes.js.map +1 -0
  24. package/dist/core-interfaces/index.d.ts +10 -0
  25. package/dist/core-interfaces/index.d.ts.map +1 -0
  26. package/dist/core-interfaces/index.js +7 -0
  27. package/dist/core-interfaces/index.js.map +1 -0
  28. package/dist/core-interfaces/jsonDeserialized.d.ts +109 -0
  29. package/dist/core-interfaces/jsonDeserialized.d.ts.map +1 -0
  30. package/dist/core-interfaces/jsonDeserialized.js +7 -0
  31. package/dist/core-interfaces/jsonDeserialized.js.map +1 -0
  32. package/dist/core-interfaces/jsonSerializable.d.ts +120 -0
  33. package/dist/core-interfaces/jsonSerializable.d.ts.map +1 -0
  34. package/dist/core-interfaces/jsonSerializable.js +7 -0
  35. package/dist/core-interfaces/jsonSerializable.js.map +1 -0
  36. package/dist/core-interfaces/jsonSerializationErrors.d.ts +31 -0
  37. package/dist/core-interfaces/jsonSerializationErrors.d.ts.map +1 -0
  38. package/dist/core-interfaces/jsonSerializationErrors.js +7 -0
  39. package/dist/core-interfaces/jsonSerializationErrors.js.map +1 -0
  40. package/dist/core-interfaces/jsonType.d.ts +29 -0
  41. package/dist/core-interfaces/jsonType.d.ts.map +1 -0
  42. package/dist/core-interfaces/jsonType.js +7 -0
  43. package/dist/core-interfaces/jsonType.js.map +1 -0
  44. package/dist/datastorePresenceManagerFactory.d.ts +48 -0
  45. package/dist/datastorePresenceManagerFactory.d.ts.map +1 -0
  46. package/dist/datastorePresenceManagerFactory.js +79 -0
  47. package/dist/datastorePresenceManagerFactory.js.map +1 -0
  48. package/dist/datastoreSupport.d.ts +31 -0
  49. package/dist/datastoreSupport.d.ts.map +1 -0
  50. package/dist/datastoreSupport.js +82 -0
  51. package/dist/datastoreSupport.js.map +1 -0
  52. package/dist/events/events.d.ts +198 -0
  53. package/dist/events/events.d.ts.map +1 -0
  54. package/dist/events/events.js +157 -0
  55. package/dist/events/events.js.map +1 -0
  56. package/dist/experimentalAccess.d.ts +15 -0
  57. package/dist/experimentalAccess.d.ts.map +1 -0
  58. package/dist/experimentalAccess.js +46 -0
  59. package/dist/experimentalAccess.js.map +1 -0
  60. package/dist/exposedInternalTypes.d.ts +100 -0
  61. package/dist/exposedInternalTypes.d.ts.map +1 -0
  62. package/dist/exposedInternalTypes.js +19 -0
  63. package/dist/exposedInternalTypes.js.map +1 -0
  64. package/dist/exposedUtilityTypes.d.ts +63 -0
  65. package/dist/exposedUtilityTypes.d.ts.map +1 -0
  66. package/dist/exposedUtilityTypes.js +7 -0
  67. package/dist/exposedUtilityTypes.js.map +1 -0
  68. package/dist/index.d.ts +22 -0
  69. package/dist/index.d.ts.map +1 -0
  70. package/dist/index.js +21 -0
  71. package/dist/index.js.map +1 -0
  72. package/dist/internalTypes.d.ts +39 -0
  73. package/dist/internalTypes.d.ts.map +1 -0
  74. package/dist/internalTypes.js +14 -0
  75. package/dist/internalTypes.js.map +1 -0
  76. package/dist/latestMapValueManager.d.ts +182 -0
  77. package/dist/latestMapValueManager.d.ts.map +1 -0
  78. package/dist/latestMapValueManager.js +206 -0
  79. package/dist/latestMapValueManager.js.map +1 -0
  80. package/dist/latestValueControls.d.ts +44 -0
  81. package/dist/latestValueControls.d.ts.map +1 -0
  82. package/dist/latestValueControls.js +28 -0
  83. package/dist/latestValueControls.js.map +1 -0
  84. package/dist/latestValueManager.d.ts +69 -0
  85. package/dist/latestValueManager.d.ts.map +1 -0
  86. package/dist/latestValueManager.js +100 -0
  87. package/dist/latestValueManager.js.map +1 -0
  88. package/dist/latestValueTypes.d.ts +44 -0
  89. package/dist/latestValueTypes.d.ts.map +1 -0
  90. package/dist/latestValueTypes.js +7 -0
  91. package/dist/latestValueTypes.js.map +1 -0
  92. package/dist/notificationsManager.d.ts +101 -0
  93. package/dist/notificationsManager.d.ts.map +1 -0
  94. package/dist/notificationsManager.js +82 -0
  95. package/dist/notificationsManager.js.map +1 -0
  96. package/dist/package.json +15 -0
  97. package/dist/presence.d.ts +180 -0
  98. package/dist/presence.d.ts.map +1 -0
  99. package/dist/presence.js +23 -0
  100. package/dist/presence.js.map +1 -0
  101. package/dist/presenceDatastoreManager.d.ts +91 -0
  102. package/dist/presenceDatastoreManager.d.ts.map +1 -0
  103. package/dist/presenceDatastoreManager.js +248 -0
  104. package/dist/presenceDatastoreManager.js.map +1 -0
  105. package/dist/presenceManager.d.ts +20 -0
  106. package/dist/presenceManager.d.ts.map +1 -0
  107. package/dist/presenceManager.js +104 -0
  108. package/dist/presenceManager.js.map +1 -0
  109. package/dist/presenceStates.d.ts +78 -0
  110. package/dist/presenceStates.d.ts.map +1 -0
  111. package/dist/presenceStates.js +182 -0
  112. package/dist/presenceStates.js.map +1 -0
  113. package/dist/stateDatastore.d.ts +35 -0
  114. package/dist/stateDatastore.d.ts.map +1 -0
  115. package/dist/stateDatastore.js +26 -0
  116. package/dist/stateDatastore.js.map +1 -0
  117. package/dist/systemWorkspace.d.ts +51 -0
  118. package/dist/systemWorkspace.d.ts.map +1 -0
  119. package/dist/systemWorkspace.js +180 -0
  120. package/dist/systemWorkspace.js.map +1 -0
  121. package/dist/types.d.ts +92 -0
  122. package/dist/types.d.ts.map +1 -0
  123. package/dist/types.js +8 -0
  124. package/dist/types.js.map +1 -0
  125. package/dist/valueManager.d.ts +19 -0
  126. package/dist/valueManager.d.ts.map +1 -0
  127. package/dist/valueManager.js +26 -0
  128. package/dist/valueManager.js.map +1 -0
  129. package/lib/alpha.d.ts +58 -0
  130. package/lib/baseTypes.d.ts +24 -0
  131. package/lib/baseTypes.d.ts.map +1 -0
  132. package/lib/baseTypes.js +6 -0
  133. package/lib/baseTypes.js.map +1 -0
  134. package/lib/container-definitions/containerExtensions.d.ts +137 -0
  135. package/lib/container-definitions/containerExtensions.d.ts.map +1 -0
  136. package/lib/container-definitions/containerExtensions.js +6 -0
  137. package/lib/container-definitions/containerExtensions.js.map +1 -0
  138. package/lib/container-definitions/index.d.ts +7 -0
  139. package/lib/container-definitions/index.d.ts.map +1 -0
  140. package/lib/container-definitions/index.js +6 -0
  141. package/lib/container-definitions/index.js.map +1 -0
  142. package/lib/container-definitions/runtime.d.ts +12 -0
  143. package/lib/container-definitions/runtime.d.ts.map +1 -0
  144. package/lib/container-definitions/runtime.js +6 -0
  145. package/lib/container-definitions/runtime.js.map +1 -0
  146. package/lib/core-interfaces/exposedUtilityTypes.d.ts +446 -0
  147. package/lib/core-interfaces/exposedUtilityTypes.d.ts.map +1 -0
  148. package/lib/core-interfaces/exposedUtilityTypes.js +10 -0
  149. package/lib/core-interfaces/exposedUtilityTypes.js.map +1 -0
  150. package/lib/core-interfaces/index.d.ts +10 -0
  151. package/lib/core-interfaces/index.d.ts.map +1 -0
  152. package/lib/core-interfaces/index.js +6 -0
  153. package/lib/core-interfaces/index.js.map +1 -0
  154. package/lib/core-interfaces/jsonDeserialized.d.ts +109 -0
  155. package/lib/core-interfaces/jsonDeserialized.d.ts.map +1 -0
  156. package/lib/core-interfaces/jsonDeserialized.js +6 -0
  157. package/lib/core-interfaces/jsonDeserialized.js.map +1 -0
  158. package/lib/core-interfaces/jsonSerializable.d.ts +120 -0
  159. package/lib/core-interfaces/jsonSerializable.d.ts.map +1 -0
  160. package/lib/core-interfaces/jsonSerializable.js +6 -0
  161. package/lib/core-interfaces/jsonSerializable.js.map +1 -0
  162. package/lib/core-interfaces/jsonSerializationErrors.d.ts +31 -0
  163. package/lib/core-interfaces/jsonSerializationErrors.d.ts.map +1 -0
  164. package/lib/core-interfaces/jsonSerializationErrors.js +6 -0
  165. package/lib/core-interfaces/jsonSerializationErrors.js.map +1 -0
  166. package/lib/core-interfaces/jsonType.d.ts +29 -0
  167. package/lib/core-interfaces/jsonType.d.ts.map +1 -0
  168. package/lib/core-interfaces/jsonType.js +6 -0
  169. package/lib/core-interfaces/jsonType.js.map +1 -0
  170. package/lib/datastorePresenceManagerFactory.d.ts +48 -0
  171. package/lib/datastorePresenceManagerFactory.d.ts.map +1 -0
  172. package/lib/datastorePresenceManagerFactory.js +75 -0
  173. package/lib/datastorePresenceManagerFactory.js.map +1 -0
  174. package/lib/datastoreSupport.d.ts +31 -0
  175. package/lib/datastoreSupport.d.ts.map +1 -0
  176. package/lib/datastoreSupport.js +77 -0
  177. package/lib/datastoreSupport.js.map +1 -0
  178. package/lib/events/events.d.ts +198 -0
  179. package/lib/events/events.d.ts.map +1 -0
  180. package/lib/events/events.js +152 -0
  181. package/lib/events/events.js.map +1 -0
  182. package/lib/experimentalAccess.d.ts +15 -0
  183. package/lib/experimentalAccess.d.ts.map +1 -0
  184. package/lib/experimentalAccess.js +42 -0
  185. package/lib/experimentalAccess.js.map +1 -0
  186. package/lib/exposedInternalTypes.d.ts +100 -0
  187. package/lib/exposedInternalTypes.d.ts.map +1 -0
  188. package/lib/exposedInternalTypes.js +16 -0
  189. package/lib/exposedInternalTypes.js.map +1 -0
  190. package/lib/exposedUtilityTypes.d.ts +63 -0
  191. package/lib/exposedUtilityTypes.d.ts.map +1 -0
  192. package/lib/exposedUtilityTypes.js +6 -0
  193. package/lib/exposedUtilityTypes.js.map +1 -0
  194. package/lib/index.d.ts +22 -0
  195. package/lib/index.d.ts.map +1 -0
  196. package/lib/index.js +11 -0
  197. package/lib/index.js.map +1 -0
  198. package/lib/internalTypes.d.ts +39 -0
  199. package/lib/internalTypes.d.ts.map +1 -0
  200. package/lib/internalTypes.js +11 -0
  201. package/lib/internalTypes.js.map +1 -0
  202. package/lib/latestMapValueManager.d.ts +182 -0
  203. package/lib/latestMapValueManager.d.ts.map +1 -0
  204. package/lib/latestMapValueManager.js +202 -0
  205. package/lib/latestMapValueManager.js.map +1 -0
  206. package/lib/latestValueControls.d.ts +44 -0
  207. package/lib/latestValueControls.d.ts.map +1 -0
  208. package/lib/latestValueControls.js +24 -0
  209. package/lib/latestValueControls.js.map +1 -0
  210. package/lib/latestValueManager.d.ts +69 -0
  211. package/lib/latestValueManager.d.ts.map +1 -0
  212. package/lib/latestValueManager.js +96 -0
  213. package/lib/latestValueManager.js.map +1 -0
  214. package/lib/latestValueTypes.d.ts +44 -0
  215. package/lib/latestValueTypes.d.ts.map +1 -0
  216. package/lib/latestValueTypes.js +6 -0
  217. package/lib/latestValueTypes.js.map +1 -0
  218. package/lib/notificationsManager.d.ts +101 -0
  219. package/lib/notificationsManager.d.ts.map +1 -0
  220. package/lib/notificationsManager.js +78 -0
  221. package/lib/notificationsManager.js.map +1 -0
  222. package/lib/presence.d.ts +180 -0
  223. package/lib/presence.d.ts.map +1 -0
  224. package/lib/presence.js +20 -0
  225. package/lib/presence.js.map +1 -0
  226. package/lib/presenceDatastoreManager.d.ts +91 -0
  227. package/lib/presenceDatastoreManager.d.ts.map +1 -0
  228. package/lib/presenceDatastoreManager.js +244 -0
  229. package/lib/presenceDatastoreManager.js.map +1 -0
  230. package/lib/presenceManager.d.ts +20 -0
  231. package/lib/presenceManager.d.ts.map +1 -0
  232. package/lib/presenceManager.js +100 -0
  233. package/lib/presenceManager.js.map +1 -0
  234. package/lib/presenceStates.d.ts +78 -0
  235. package/lib/presenceStates.d.ts.map +1 -0
  236. package/lib/presenceStates.js +177 -0
  237. package/lib/presenceStates.js.map +1 -0
  238. package/lib/stateDatastore.d.ts +35 -0
  239. package/lib/stateDatastore.d.ts.map +1 -0
  240. package/lib/stateDatastore.js +21 -0
  241. package/lib/stateDatastore.js.map +1 -0
  242. package/lib/systemWorkspace.d.ts +51 -0
  243. package/lib/systemWorkspace.d.ts.map +1 -0
  244. package/lib/systemWorkspace.js +176 -0
  245. package/lib/systemWorkspace.js.map +1 -0
  246. package/lib/tsdoc-metadata.json +11 -0
  247. package/lib/types.d.ts +92 -0
  248. package/lib/types.d.ts.map +1 -0
  249. package/lib/types.js +7 -0
  250. package/lib/types.js.map +1 -0
  251. package/lib/valueManager.d.ts +19 -0
  252. package/lib/valueManager.d.ts.map +1 -0
  253. package/lib/valueManager.js +21 -0
  254. package/lib/valueManager.js.map +1 -0
  255. package/package.json +175 -0
@@ -0,0 +1,177 @@
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 { brandedObjectEntries } from "./internalTypes.js";
7
+ import { handleFromDatastore } from "./stateDatastore.js";
8
+ import { unbrandIVM } from "./valueManager.js";
9
+ function isValueDirectory(value) {
10
+ return "items" in value;
11
+ }
12
+ function mergeValueDirectory(base, update, timeDelta) {
13
+ if (!isValueDirectory(update)) {
14
+ if (base === undefined || update.rev > base.rev) {
15
+ return { ...update, timestamp: update.timestamp + timeDelta };
16
+ }
17
+ return base;
18
+ }
19
+ let mergeBase;
20
+ if (base === undefined) {
21
+ mergeBase = { rev: update.rev, items: {} };
22
+ }
23
+ else {
24
+ const baseIsDirectory = isValueDirectory(base);
25
+ if (base.rev >= update.rev) {
26
+ if (!baseIsDirectory) {
27
+ // base is leaf value that is more recent - nothing to do
28
+ return base;
29
+ }
30
+ // While base has more advanced revision, assume mis-ordering or
31
+ // missed and catchup update needs merged in.
32
+ mergeBase = base;
33
+ }
34
+ else {
35
+ mergeBase = { rev: update.rev, items: baseIsDirectory ? base.items : {} };
36
+ }
37
+ }
38
+ for (const [key, value] of Object.entries(update.items)) {
39
+ const baseElement = mergeBase.items[key];
40
+ mergeBase.items[key] = mergeValueDirectory(baseElement, value, timeDelta);
41
+ }
42
+ return mergeBase;
43
+ }
44
+ /**
45
+ * Updates remote state into the local [untracked] datastore.
46
+ *
47
+ * @param key - The key of the datastore to merge the untracked data into.
48
+ * @param remoteAllKnownState - The remote state to merge into the datastore.
49
+ * @param datastore - The datastore to merge the untracked data into.
50
+ *
51
+ * @remarks
52
+ * In the case of ignored unmonitored data, the client entries are not stored,
53
+ * though the value keys will be populated and often remain empty.
54
+ *
55
+ * @internal
56
+ */
57
+ export function mergeUntrackedDatastore(key, remoteAllKnownState, datastore, timeModifier) {
58
+ if (!(key in datastore)) {
59
+ datastore[key] = {};
60
+ }
61
+ const localAllKnownState = datastore[key];
62
+ for (const [clientSessionId, value] of brandedObjectEntries(remoteAllKnownState)) {
63
+ if (!("ignoreUnmonitored" in value)) {
64
+ localAllKnownState[clientSessionId] = mergeValueDirectory(localAllKnownState[clientSessionId], value, timeModifier);
65
+ }
66
+ }
67
+ }
68
+ class PresenceStatesImpl {
69
+ constructor(runtime, datastore, initialContent) {
70
+ this.runtime = runtime;
71
+ this.datastore = datastore;
72
+ // Prepare initial map content from initial state
73
+ {
74
+ const clientSessionId = this.runtime.clientSessionId;
75
+ let anyInitialValues = false;
76
+ // eslint-disable-next-line unicorn/no-array-reduce
77
+ const initial = Object.entries(initialContent).reduce((acc, [key, nodeFactory]) => {
78
+ const newNodeData = nodeFactory(key, handleFromDatastore(this));
79
+ acc.nodes[key] = newNodeData.manager;
80
+ if ("value" in newNodeData) {
81
+ acc.datastore[key] = acc.datastore[key] ?? {};
82
+ acc.datastore[key][clientSessionId] = newNodeData.value;
83
+ acc.newValues[key] = newNodeData.value;
84
+ anyInitialValues = true;
85
+ }
86
+ return acc;
87
+ }, {
88
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
89
+ nodes: {},
90
+ datastore,
91
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
92
+ newValues: {},
93
+ });
94
+ this.nodes = initial.nodes;
95
+ // props is the public view of nodes that limits the entries types to
96
+ // the public interface of the value manager with an additional type
97
+ // filter that beguiles the type system. So just reinterpret cast.
98
+ this.props = this.nodes;
99
+ if (anyInitialValues) {
100
+ this.runtime.localUpdate(initial.newValues, false);
101
+ }
102
+ }
103
+ }
104
+ knownValues(key) {
105
+ return {
106
+ self: this.runtime.clientSessionId,
107
+ states: this.datastore[key],
108
+ };
109
+ }
110
+ localUpdate(key, value, forceBroadcast) {
111
+ this.runtime.localUpdate({ [key]: value }, forceBroadcast);
112
+ }
113
+ update(key, clientId, value) {
114
+ const allKnownState = this.datastore[key];
115
+ allKnownState[clientId] = mergeValueDirectory(allKnownState[clientId], value, 0);
116
+ }
117
+ lookupClient(clientId) {
118
+ return this.runtime.lookupClient(clientId);
119
+ }
120
+ add(key, nodeFactory) {
121
+ assert(!(key in this.nodes), 0xa3c /* Already have entry for key in map */);
122
+ const nodeData = nodeFactory(key, handleFromDatastore(this));
123
+ this.nodes[key] = nodeData.manager;
124
+ if ("value" in nodeData) {
125
+ if (key in this.datastore) {
126
+ // Already have received state from other clients. Kept in `all`.
127
+ // TODO: Send current `all` state to state manager.
128
+ }
129
+ else {
130
+ this.datastore[key] = {};
131
+ }
132
+ this.datastore[key][this.runtime.clientSessionId] = nodeData.value;
133
+ this.runtime.localUpdate({ [key]: nodeData.value }, false);
134
+ }
135
+ }
136
+ ensureContent(content) {
137
+ for (const [key, nodeFactory] of Object.entries(content)) {
138
+ if (key in this.nodes) {
139
+ const node = unbrandIVM(this.nodes[key]);
140
+ if (!(node instanceof nodeFactory.instanceBase)) {
141
+ throw new TypeError(`State "${key}" previously created by different value manager.`);
142
+ }
143
+ }
144
+ else {
145
+ this.add(key, nodeFactory);
146
+ }
147
+ }
148
+ return this;
149
+ }
150
+ processUpdate(received, timeModifier, remoteDatastore) {
151
+ for (const [key, remoteAllKnownState] of Object.entries(remoteDatastore)) {
152
+ if (key in this.nodes) {
153
+ const node = unbrandIVM(this.nodes[key]);
154
+ for (const [clientSessionId, value] of brandedObjectEntries(remoteAllKnownState)) {
155
+ const client = this.runtime.lookupClient(clientSessionId);
156
+ node.update(client, received, value);
157
+ }
158
+ }
159
+ else {
160
+ // Assume all broadcast state is meant to be kept even if not currently registered.
161
+ mergeUntrackedDatastore(key, remoteAllKnownState, this.datastore, timeModifier);
162
+ }
163
+ }
164
+ }
165
+ }
166
+ /**
167
+ * Create a new PresenceStates using the DataStoreRuntime provided.
168
+ * @param initialContent - The initial value managers to register.
169
+ */
170
+ export function createPresenceStates(runtime, datastore, initialContent) {
171
+ const impl = new PresenceStatesImpl(runtime, datastore, initialContent);
172
+ return {
173
+ public: impl,
174
+ internal: impl,
175
+ };
176
+ }
177
+ //# sourceMappingURL=presenceStates.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"presenceStates.js","sourceRoot":"","sources":["../src/presenceStates.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,qCAAqC,CAAC;AAK7D,OAAO,EAAE,oBAAoB,EAAE,MAAM,oBAAoB,CAAC;AAE1D,OAAO,EAAE,mBAAmB,EAAuB,MAAM,qBAAqB,CAAC;AAE/E,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAgG/C,SAAS,gBAAgB,CAMxB,KAAoD;IAEpD,OAAO,OAAO,IAAI,KAAK,CAAC;AACzB,CAAC;AAED,SAAS,mBAAmB,CAM3B,IAA+D,EAC/D,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,OAAO,EAAE,SAAS,EAAE,cAAc,CAAC,CAAC;IAEjF,OAAO;QACN,MAAM,EAAE,IAAI;QACZ,QAAQ,EAAE,IAAI;KACd,CAAC;AACH,CAAC","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\";\n\nimport type { ClientConnectionId } from \"./baseTypes.js\";\nimport type { InternalTypes } from \"./exposedInternalTypes.js\";\nimport type { ClientRecord } from \"./internalTypes.js\";\nimport { brandedObjectEntries } from \"./internalTypes.js\";\nimport type { ClientSessionId, ISessionClient } from \"./presence.js\";\nimport { handleFromDatastore, type StateDatastore } from \"./stateDatastore.js\";\nimport type { PresenceStates, PresenceStatesSchema } from \"./types.js\";\nimport { unbrandIVM } from \"./valueManager.js\";\n\n/**\n * @internal\n */\nexport type MapSchemaElement<\n\tTSchema extends PresenceStatesSchema,\n\tPart extends keyof ReturnType<TSchema[keyof TSchema]>,\n\tKeys extends keyof TSchema = keyof TSchema,\n> = ReturnType<TSchema[Keys]>[Part];\n\n/**\n * @internal\n */\nexport interface PresenceRuntime {\n\treadonly clientSessionId: ClientSessionId;\n\tlookupClient(clientId: ClientConnectionId): ISessionClient;\n\tlocalUpdate(states: { [key: string]: ClientUpdateEntry }, forceBroadcast: boolean): void;\n}\n\ntype PresenceSubSchemaFromWorkspaceSchema<\n\tTSchema extends PresenceStatesSchema,\n\tPart extends keyof ReturnType<TSchema[keyof TSchema]>,\n> = {\n\t[Key in keyof TSchema]: MapSchemaElement<TSchema, Part, Key>;\n};\n\ntype MapEntries<TSchema extends PresenceStatesSchema> = PresenceSubSchemaFromWorkspaceSchema<\n\tTSchema,\n\t\"manager\"\n>;\n\n/**\n * ValueElementMap is a map of key to a map of clientId to ValueState.\n * It is not restricted to the schema of the map as it may receive updates from other clients\n * with managers that have not been registered locally. Each map node is responsible for keeping\n * all sessions state to be able to pick arbitrary client to rebroadcast to others.\n *\n * This generic aspect makes some typing difficult. The loose typing is not broadcast to the\n * consumers that are expected to maintain their schema over multiple versions of clients.\n *\n * @internal\n */\nexport interface ValueElementMap<_TSchema extends PresenceStatesSchema> {\n\t[key: string]: ClientRecord<InternalTypes.ValueDirectoryOrState<unknown>>;\n}\n\n// An attempt to make the type more precise, but it is not working.\n// If the casting in support code is too much we could keep two references to the same\n// complete datastore, but with the respective types desired.\n// type ValueElementMap<TSchema extends PresenceStatesNodeSchema> =\n// \t| {\n// \t\t\t[Key in keyof TSchema & string]?: {\n// \t\t\t\t[ClientSessionId: ClientSessionId]: InternalTypes.ValueDirectoryOrState<MapSchemaElement<TSchema,\"value\",Key>>;\n// \t\t\t};\n// \t }\n// \t| {\n// \t\t\t[key: string]: ClientRecord<InternalTypes.ValueDirectoryOrState<unknown>>;\n// \t };\n// interface ValueElementMap<TValue> {\n// \t[Id: string]: ClientRecord<InternalTypes.ValueDirectoryOrState<TValue>>;\n// \t// Version with local packed in is convenient for map, but not for join broadcast to serialize simply.\n// \t// [Id: string]: {\n// \t// \tlocal: InternalTypes.ValueDirectoryOrState<TValue>;\n// \t// \tall: ClientRecord<InternalTypes.ValueDirectoryOrState<TValue>>;\n// \t// };\n// }\n\n/**\n * @internal\n */\nexport type ClientUpdateEntry = InternalTypes.ValueDirectoryOrState<unknown> & {\n\tignoreUnmonitored?: true;\n};\n\ntype ClientUpdateRecord = ClientRecord<ClientUpdateEntry>;\n\ninterface ValueUpdateRecord {\n\t[valueKey: string]: ClientUpdateRecord;\n}\n\n/**\n * @internal\n */\nexport interface PresenceStatesInternal {\n\tensureContent<TSchemaAdditional extends PresenceStatesSchema>(\n\t\tcontent: TSchemaAdditional,\n\t): PresenceStates<TSchemaAdditional>;\n\tprocessUpdate(\n\t\treceived: number,\n\t\ttimeModifier: number,\n\t\tremoteDatastore: ValueUpdateRecord,\n\t\tsenderConnectionId: ClientConnectionId,\n\t): void;\n}\n\nfunction isValueDirectory<\n\tT,\n\tTValueState extends\n\t\t| InternalTypes.ValueRequiredState<T>\n\t\t| InternalTypes.ValueOptionalState<T>,\n>(\n\tvalue: InternalTypes.ValueDirectory<T> | TValueState,\n): value is InternalTypes.ValueDirectory<T> {\n\treturn \"items\" in value;\n}\n\nfunction mergeValueDirectory<\n\tT,\n\tTValueState extends\n\t\t| InternalTypes.ValueRequiredState<T>\n\t\t| InternalTypes.ValueOptionalState<T>,\n>(\n\tbase: TValueState | InternalTypes.ValueDirectory<T> | undefined,\n\tupdate: TValueState | InternalTypes.ValueDirectory<T>,\n\ttimeDelta: number,\n): TValueState | InternalTypes.ValueDirectory<T> {\n\tif (!isValueDirectory(update)) {\n\t\tif (base === undefined || update.rev > base.rev) {\n\t\t\treturn { ...update, timestamp: update.timestamp + timeDelta };\n\t\t}\n\t\treturn base;\n\t}\n\n\tlet mergeBase: InternalTypes.ValueDirectory<T>;\n\tif (base === undefined) {\n\t\tmergeBase = { rev: update.rev, items: {} };\n\t} else {\n\t\tconst baseIsDirectory = isValueDirectory(base);\n\t\tif (base.rev >= update.rev) {\n\t\t\tif (!baseIsDirectory) {\n\t\t\t\t// base is leaf value that is more recent - nothing to do\n\t\t\t\treturn base;\n\t\t\t}\n\t\t\t// While base has more advanced revision, assume mis-ordering or\n\t\t\t// missed and catchup update needs merged in.\n\t\t\tmergeBase = base;\n\t\t} else {\n\t\t\tmergeBase = { rev: update.rev, items: baseIsDirectory ? base.items : {} };\n\t\t}\n\t}\n\tfor (const [key, value] of Object.entries(update.items)) {\n\t\tconst baseElement = mergeBase.items[key];\n\t\tmergeBase.items[key] = mergeValueDirectory(baseElement, value, timeDelta);\n\t}\n\treturn mergeBase;\n}\n\n/**\n * Updates remote state into the local [untracked] datastore.\n *\n * @param key - The key of the datastore to merge the untracked data into.\n * @param remoteAllKnownState - The remote state to merge into the datastore.\n * @param datastore - The datastore to merge the untracked data into.\n *\n * @remarks\n * In the case of ignored unmonitored data, the client entries are not stored,\n * though the value keys will be populated and often remain empty.\n *\n * @internal\n */\nexport function mergeUntrackedDatastore(\n\tkey: string,\n\tremoteAllKnownState: ClientUpdateRecord,\n\tdatastore: ValueElementMap<PresenceStatesSchema>,\n\ttimeModifier: number,\n): void {\n\tif (!(key in datastore)) {\n\t\tdatastore[key] = {};\n\t}\n\tconst localAllKnownState = datastore[key];\n\tfor (const [clientSessionId, value] of brandedObjectEntries(remoteAllKnownState)) {\n\t\tif (!(\"ignoreUnmonitored\" in value)) {\n\t\t\tlocalAllKnownState[clientSessionId] = mergeValueDirectory(\n\t\t\t\tlocalAllKnownState[clientSessionId],\n\t\t\t\tvalue,\n\t\t\t\ttimeModifier,\n\t\t\t);\n\t\t}\n\t}\n}\n\nclass PresenceStatesImpl<TSchema extends PresenceStatesSchema>\n\timplements\n\t\tPresenceStatesInternal,\n\t\tPresenceStates<TSchema>,\n\t\tStateDatastore<\n\t\t\tkeyof TSchema & string,\n\t\t\tMapSchemaElement<TSchema, \"value\", keyof TSchema & string>\n\t\t>\n{\n\tprivate readonly nodes: MapEntries<TSchema>;\n\tpublic readonly props: PresenceStates<TSchema>[\"props\"];\n\n\tpublic constructor(\n\t\tprivate readonly runtime: PresenceRuntime,\n\t\tprivate readonly datastore: ValueElementMap<TSchema>,\n\t\tinitialContent: TSchema,\n\t) {\n\t\t// Prepare initial map content from initial state\n\t\t{\n\t\t\tconst clientSessionId = this.runtime.clientSessionId;\n\t\t\tlet anyInitialValues = false;\n\t\t\t// eslint-disable-next-line unicorn/no-array-reduce\n\t\t\tconst initial = Object.entries(initialContent).reduce(\n\t\t\t\t(acc, [key, nodeFactory]) => {\n\t\t\t\t\tconst newNodeData = nodeFactory(key, handleFromDatastore(this));\n\t\t\t\t\tacc.nodes[key as keyof TSchema] = newNodeData.manager;\n\t\t\t\t\tif (\"value\" in newNodeData) {\n\t\t\t\t\t\tacc.datastore[key] = acc.datastore[key] ?? {};\n\t\t\t\t\t\tacc.datastore[key][clientSessionId] = newNodeData.value;\n\t\t\t\t\t\tacc.newValues[key] = newNodeData.value;\n\t\t\t\t\t\tanyInitialValues = true;\n\t\t\t\t\t}\n\t\t\t\t\treturn acc;\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t// eslint-disable-next-line @typescript-eslint/consistent-type-assertions\n\t\t\t\t\tnodes: {} as MapEntries<TSchema>,\n\t\t\t\t\tdatastore,\n\t\t\t\t\t// eslint-disable-next-line @typescript-eslint/consistent-type-assertions\n\t\t\t\t\tnewValues: {} as { [key: string]: InternalTypes.ValueDirectoryOrState<unknown> },\n\t\t\t\t},\n\t\t\t);\n\t\t\tthis.nodes = initial.nodes;\n\t\t\t// props is the public view of nodes that limits the entries types to\n\t\t\t// the public interface of the value manager with an additional type\n\t\t\t// filter that beguiles the type system. So just reinterpret cast.\n\t\t\tthis.props = this.nodes as unknown as PresenceStates<TSchema>[\"props\"];\n\n\t\t\tif (anyInitialValues) {\n\t\t\t\tthis.runtime.localUpdate(initial.newValues, false);\n\t\t\t}\n\t\t}\n\t}\n\n\tpublic knownValues<Key extends keyof TSchema & string>(\n\t\tkey: Key,\n\t): {\n\t\tself: ClientSessionId | undefined;\n\t\tstates: ClientRecord<MapSchemaElement<TSchema, \"value\", Key>>;\n\t} {\n\t\treturn {\n\t\t\tself: this.runtime.clientSessionId,\n\t\t\tstates: this.datastore[key],\n\t\t};\n\t}\n\n\tpublic localUpdate<Key extends keyof TSchema & string>(\n\t\tkey: Key,\n\t\tvalue: MapSchemaElement<TSchema, \"value\", Key> & ClientUpdateEntry,\n\t\tforceBroadcast: boolean,\n\t): void {\n\t\tthis.runtime.localUpdate({ [key]: value }, forceBroadcast);\n\t}\n\n\tpublic update<Key extends keyof TSchema & string>(\n\t\tkey: Key,\n\t\tclientId: ClientSessionId,\n\t\tvalue: Exclude<MapSchemaElement<TSchema, \"value\", Key>, undefined>,\n\t): void {\n\t\tconst allKnownState = this.datastore[key];\n\t\tallKnownState[clientId] = mergeValueDirectory(allKnownState[clientId], value, 0);\n\t}\n\n\tpublic lookupClient(clientId: ClientConnectionId): ISessionClient {\n\t\treturn this.runtime.lookupClient(clientId);\n\t}\n\n\tpublic add<\n\t\tTKey extends string,\n\t\tTValue extends InternalTypes.ValueDirectoryOrState<unknown>,\n\t\tTValueManager,\n\t>(\n\t\tkey: TKey,\n\t\tnodeFactory: InternalTypes.ManagerFactory<TKey, TValue, TValueManager>,\n\t): asserts this is PresenceStates<\n\t\tTSchema & Record<TKey, InternalTypes.ManagerFactory<TKey, TValue, TValueManager>>\n\t> {\n\t\tassert(!(key in this.nodes), 0xa3c /* Already have entry for key in map */);\n\t\tconst nodeData = nodeFactory(key, handleFromDatastore(this));\n\t\tthis.nodes[key] = nodeData.manager;\n\t\tif (\"value\" in nodeData) {\n\t\t\tif (key in this.datastore) {\n\t\t\t\t// Already have received state from other clients. Kept in `all`.\n\t\t\t\t// TODO: Send current `all` state to state manager.\n\t\t\t} else {\n\t\t\t\tthis.datastore[key] = {};\n\t\t\t}\n\t\t\tthis.datastore[key][this.runtime.clientSessionId] = nodeData.value;\n\t\t\tthis.runtime.localUpdate({ [key]: nodeData.value }, false);\n\t\t}\n\t}\n\n\tpublic ensureContent<TSchemaAdditional extends PresenceStatesSchema>(\n\t\tcontent: TSchemaAdditional,\n\t): PresenceStates<TSchema & TSchemaAdditional> {\n\t\tfor (const [key, nodeFactory] of Object.entries(content)) {\n\t\t\tif (key in this.nodes) {\n\t\t\t\tconst node = unbrandIVM(this.nodes[key]);\n\t\t\t\tif (!(node instanceof nodeFactory.instanceBase)) {\n\t\t\t\t\tthrow new TypeError(`State \"${key}\" previously created by different value manager.`);\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tthis.add(key, nodeFactory);\n\t\t\t}\n\t\t}\n\t\treturn this as PresenceStates<TSchema & TSchemaAdditional>;\n\t}\n\n\tpublic processUpdate(\n\t\treceived: number,\n\t\ttimeModifier: number,\n\t\tremoteDatastore: ValueUpdateRecord,\n\t): void {\n\t\tfor (const [key, remoteAllKnownState] of Object.entries(remoteDatastore)) {\n\t\t\tif (key in this.nodes) {\n\t\t\t\tconst node = unbrandIVM(this.nodes[key]);\n\t\t\t\tfor (const [clientSessionId, value] of brandedObjectEntries(remoteAllKnownState)) {\n\t\t\t\t\tconst client = this.runtime.lookupClient(clientSessionId);\n\t\t\t\t\tnode.update(client, received, value);\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\t// Assume all broadcast state is meant to be kept even if not currently registered.\n\t\t\t\tmergeUntrackedDatastore(key, remoteAllKnownState, this.datastore, timeModifier);\n\t\t\t}\n\t\t}\n\t}\n}\n\n/**\n * Create a new PresenceStates using the DataStoreRuntime provided.\n * @param initialContent - The initial value managers to register.\n */\nexport function createPresenceStates<TSchema extends PresenceStatesSchema>(\n\truntime: PresenceRuntime,\n\tdatastore: ValueElementMap<PresenceStatesSchema>,\n\tinitialContent: TSchema,\n): { public: PresenceStates<TSchema>; internal: PresenceStatesInternal } {\n\tconst impl = new PresenceStatesImpl<TSchema>(runtime, datastore, initialContent);\n\n\treturn {\n\t\tpublic: impl,\n\t\tinternal: impl,\n\t};\n}\n"]}
@@ -0,0 +1,35 @@
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
+ /**
10
+ * @internal
11
+ */
12
+ export interface StateDatastore<TKey extends string, TValue extends InternalTypes.ValueDirectoryOrState<any> | undefined> {
13
+ localUpdate(key: TKey, value: TValue & {
14
+ ignoreUnmonitored?: true;
15
+ }, forceBroadcast: boolean): void;
16
+ update(key: TKey, clientSessionId: ClientSessionId, value: TValue): void;
17
+ knownValues(key: TKey): {
18
+ self: ClientSessionId | undefined;
19
+ states: ClientRecord<TValue>;
20
+ };
21
+ lookupClient(clientId: ClientConnectionId): ISessionClient;
22
+ }
23
+ /**
24
+ * Helper to get a handle from a datastore.
25
+ *
26
+ * @internal
27
+ */
28
+ export declare function handleFromDatastore<TKey extends string, TValue extends InternalTypes.ValueDirectoryOrState<unknown> | undefined>(datastore: StateDatastore<TKey, TValue>): InternalTypes.StateDatastoreHandle<TKey, Exclude<TValue, undefined>>;
29
+ /**
30
+ * Helper to get the datastore back from its handle.
31
+ *
32
+ * @internal
33
+ */
34
+ export declare function datastoreFromHandle<TKey extends string, TValue extends InternalTypes.ValueDirectoryOrState<any>>(handle: InternalTypes.StateDatastoreHandle<TKey, TValue>): StateDatastore<TKey, TValue>;
35
+ //# sourceMappingURL=stateDatastore.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stateDatastore.d.ts","sourceRoot":"","sources":["../src/stateDatastore.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,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;AACvD,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAiBrE;;GAEG;AACH,MAAM,WAAW,cAAc,CAC9B,IAAI,SAAS,MAAM,EACnB,MAAM,SAAS,aAAa,CAAC,qBAAqB,CAAC,GAAG,CAAC,GAAG,SAAS;IAEnE,WAAW,CACV,GAAG,EAAE,IAAI,EACT,KAAK,EAAE,MAAM,GAAG;QACf,iBAAiB,CAAC,EAAE,IAAI,CAAC;KACzB,EACD,cAAc,EAAE,OAAO,GACrB,IAAI,CAAC;IACR,MAAM,CAAC,GAAG,EAAE,IAAI,EAAE,eAAe,EAAE,eAAe,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IACzE,WAAW,CAAC,GAAG,EAAE,IAAI,GAAG;QACvB,IAAI,EAAE,eAAe,GAAG,SAAS,CAAC;QAClC,MAAM,EAAE,YAAY,CAAC,MAAM,CAAC,CAAC;KAC7B,CAAC;IACF,YAAY,CAAC,QAAQ,EAAE,kBAAkB,GAAG,cAAc,CAAC;CAC3D;AAED;;;;GAIG;AACH,wBAAgB,mBAAmB,CAIlC,IAAI,SAAS,MAAM,EACnB,MAAM,SAAS,aAAa,CAAC,qBAAqB,CAAC,OAAO,CAAC,GAAG,SAAS,EAEvE,SAAS,EAAE,cAAc,CAAC,IAAI,EAAE,MAAM,CAAC,GACrC,aAAa,CAAC,oBAAoB,CAAC,IAAI,EAAE,OAAO,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC,CAKtE;AAED;;;;GAIG;AACH,wBAAgB,mBAAmB,CAClC,IAAI,SAAS,MAAM,EACnB,MAAM,SAAS,aAAa,CAAC,qBAAqB,CAAC,GAAG,CAAC,EACtD,MAAM,EAAE,aAAa,CAAC,oBAAoB,CAAC,IAAI,EAAE,MAAM,CAAC,GAAG,cAAc,CAAC,IAAI,EAAE,MAAM,CAAC,CAExF"}
@@ -0,0 +1,21 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+ /**
6
+ * Helper to get a handle from a datastore.
7
+ *
8
+ * @internal
9
+ */
10
+ export function handleFromDatastore(datastore) {
11
+ return datastore;
12
+ }
13
+ /**
14
+ * Helper to get the datastore back from its handle.
15
+ *
16
+ * @internal
17
+ */
18
+ export function datastoreFromHandle(handle) {
19
+ return handle;
20
+ }
21
+ //# sourceMappingURL=stateDatastore.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stateDatastore.js","sourceRoot":"","sources":["../src/stateDatastore.ts"],"names":[],"mappings":"AAAA;;;GAGG;AA4CH;;;;GAIG;AACH,MAAM,UAAU,mBAAmB,CAOlC,SAAuC;IAEvC,OAAO,SAGN,CAAC;AACH,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,mBAAmB,CAGjC,MAAwD;IACzD,OAAO,MAAiD,CAAC;AAC1D,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport type { ClientConnectionId } from \"./baseTypes.js\";\nimport type { InternalTypes } from \"./exposedInternalTypes.js\";\nimport type { ClientRecord } from \"./internalTypes.js\";\nimport type { ClientSessionId, ISessionClient } from \"./presence.js\";\n\n// type StateDatastoreSchemaNode<\n// \tTValue extends InternalTypes.ValueDirectoryOrState<any> = InternalTypes.ValueDirectoryOrState<unknown>,\n// > = TValue extends InternalTypes.ValueDirectoryOrState<infer T> ? InternalTypes.ValueDirectoryOrState<T> : never;\n\n// /**\n// * @internal\n// */\n// export interface StateDatastoreSchema {\n// \t// This type is not precise. It may\n// \t// need to be replaced with PresenceStates schema pattern\n// \t// similar to what is commented out.\n// \t[key: string]: InternalTypes.ValueDirectoryOrState<unknown>;\n// \t// [key: string]: StateDatastoreSchemaNode;\n// }\n\n/**\n * @internal\n */\nexport interface StateDatastore<\n\tTKey extends string,\n\tTValue extends InternalTypes.ValueDirectoryOrState<any> | undefined,\n> {\n\tlocalUpdate(\n\t\tkey: TKey,\n\t\tvalue: TValue & {\n\t\t\tignoreUnmonitored?: true;\n\t\t},\n\t\tforceBroadcast: boolean,\n\t): void;\n\tupdate(key: TKey, clientSessionId: ClientSessionId, value: TValue): void;\n\tknownValues(key: TKey): {\n\t\tself: ClientSessionId | undefined;\n\t\tstates: ClientRecord<TValue>;\n\t};\n\tlookupClient(clientId: ClientConnectionId): ISessionClient;\n}\n\n/**\n * Helper to get a handle from a datastore.\n *\n * @internal\n */\nexport function handleFromDatastore<\n\t// Constraining TSchema would be great, but it seems nested types (at least with undefined) cause trouble.\n\t// TSchema as `unknown` still provides some type safety.\n\t// TSchema extends StateDatastoreSchema,\n\tTKey extends string /* & keyof TSchema */,\n\tTValue extends InternalTypes.ValueDirectoryOrState<unknown> | undefined,\n>(\n\tdatastore: StateDatastore<TKey, TValue>,\n): InternalTypes.StateDatastoreHandle<TKey, Exclude<TValue, undefined>> {\n\treturn datastore as unknown as InternalTypes.StateDatastoreHandle<\n\t\tTKey,\n\t\tExclude<TValue, undefined>\n\t>;\n}\n\n/**\n * Helper to get the datastore back from its handle.\n *\n * @internal\n */\nexport function datastoreFromHandle<\n\tTKey extends string,\n\tTValue extends InternalTypes.ValueDirectoryOrState<any>,\n>(handle: InternalTypes.StateDatastoreHandle<TKey, TValue>): StateDatastore<TKey, TValue> {\n\treturn handle as unknown as StateDatastore<TKey, TValue>;\n}\n"]}
@@ -0,0 +1,51 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+ import type { IAudience } from "@fluidframework/container-definitions";
6
+ import type { ClientConnectionId } from "./baseTypes.js";
7
+ import type { InternalTypes } from "./exposedInternalTypes.js";
8
+ import type { ClientSessionId, IPresence, PresenceEvents } from "./presence.js";
9
+ import type { PresenceStatesInternal } from "./presenceStates.js";
10
+ import type { PresenceStates, PresenceStatesSchema } from "./types.js";
11
+ import type { IEmitter } from "@fluidframework/presence/internal/events";
12
+ /**
13
+ * The system workspace's datastore structure.
14
+ *
15
+ * @internal
16
+ */
17
+ export interface SystemWorkspaceDatastore {
18
+ clientToSessionId: {
19
+ [ConnectionId: ClientConnectionId]: InternalTypes.ValueRequiredState<ClientSessionId>;
20
+ };
21
+ }
22
+ /**
23
+ * @internal
24
+ */
25
+ export interface SystemWorkspace extends Pick<IPresence, "getAttendees" | "getAttendee" | "getMyself"> {
26
+ /**
27
+ * Must be called when the current client acquires a new connection.
28
+ *
29
+ * @param clientConnectionId - The new client connection ID.
30
+ */
31
+ onConnectionAdded(clientConnectionId: ClientConnectionId): void;
32
+ /**
33
+ * Removes the client connection ID from the system workspace.
34
+ *
35
+ * @param clientConnectionId - The client connection ID to remove.
36
+ */
37
+ removeClientConnectionId(clientConnectionId: ClientConnectionId): void;
38
+ }
39
+ /**
40
+ * Instantiates the system workspace.
41
+ *
42
+ * @internal
43
+ */
44
+ export declare function createSystemWorkspace(clientSessionId: ClientSessionId, datastore: SystemWorkspaceDatastore, events: IEmitter<Pick<PresenceEvents, "attendeeJoined">>, audience: IAudience): {
45
+ workspace: SystemWorkspace;
46
+ statesEntry: {
47
+ internal: PresenceStatesInternal;
48
+ public: PresenceStates<PresenceStatesSchema>;
49
+ };
50
+ };
51
+ //# sourceMappingURL=systemWorkspace.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"systemWorkspace.d.ts","sourceRoot":"","sources":["../src/systemWorkspace.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,uCAAuC,CAAC;AAGvE,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AACzD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC;AAC/D,OAAO,KAAK,EACX,eAAe,EACf,SAAS,EAET,cAAc,EACd,MAAM,eAAe,CAAC;AAEvB,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,qBAAqB,CAAC;AAClE,OAAO,KAAK,EAAE,cAAc,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAC;AAEvE,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,0CAA0C,CAAC;AAEzE;;;;GAIG;AACH,MAAM,WAAW,wBAAwB;IACxC,iBAAiB,EAAE;QAClB,CAAC,YAAY,EAAE,kBAAkB,GAAG,aAAa,CAAC,kBAAkB,CAAC,eAAe,CAAC,CAAC;KACtF,CAAC;CACF;AA0CD;;GAEG;AACH,MAAM,WAAW,eAGhB,SAAQ,IAAI,CAAC,SAAS,EAAE,cAAc,GAAG,aAAa,GAAG,WAAW,CAAC;IACrE;;;;OAIG;IACH,iBAAiB,CAAC,kBAAkB,EAAE,kBAAkB,GAAG,IAAI,CAAC;IAEhE;;;;OAIG;IACH,wBAAwB,CAAC,kBAAkB,EAAE,kBAAkB,GAAG,IAAI,CAAC;CACvE;AA6KD;;;;GAIG;AACH,wBAAgB,qBAAqB,CACpC,eAAe,EAAE,eAAe,EAChC,SAAS,EAAE,wBAAwB,EACnC,MAAM,EAAE,QAAQ,CAAC,IAAI,CAAC,cAAc,EAAE,gBAAgB,CAAC,CAAC,EACxD,QAAQ,EAAE,SAAS,GACjB;IACF,SAAS,EAAE,eAAe,CAAC;IAC3B,WAAW,EAAE;QACZ,QAAQ,EAAE,sBAAsB,CAAC;QACjC,MAAM,EAAE,cAAc,CAAC,oBAAoB,CAAC,CAAC;KAC7C,CAAC;CACF,CASA"}
@@ -0,0 +1,176 @@
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 { SessionClientStatus } from "./presence.js";
7
+ class SessionClient {
8
+ constructor(sessionId, connectionId = undefined) {
9
+ this.sessionId = sessionId;
10
+ this.connectionId = connectionId;
11
+ /**
12
+ * Order is used to track the most recent client connection
13
+ * during a session.
14
+ */
15
+ this.order = 0;
16
+ this.connectionStatus =
17
+ connectionId === undefined
18
+ ? SessionClientStatus.Disconnected
19
+ : SessionClientStatus.Connected;
20
+ }
21
+ getConnectionId() {
22
+ if (this.connectionId === undefined) {
23
+ throw new Error("Client has never been connected");
24
+ }
25
+ return this.connectionId;
26
+ }
27
+ getConnectionStatus() {
28
+ return this.connectionStatus;
29
+ }
30
+ setConnectionId(connectionId) {
31
+ this.connectionId = connectionId;
32
+ this.connectionStatus = SessionClientStatus.Connected;
33
+ }
34
+ setDisconnected() {
35
+ this.connectionStatus = SessionClientStatus.Disconnected;
36
+ }
37
+ }
38
+ class SystemWorkspaceImpl {
39
+ constructor(clientSessionId, datastore, events, audience) {
40
+ this.datastore = datastore;
41
+ this.events = events;
42
+ this.audience = audience;
43
+ /**
44
+ * `attendees` is this client's understanding of the attendees in the
45
+ * session. The map covers entries for both session ids and connection
46
+ * ids, which are never expected to collide, but if they did for same
47
+ * client that would be fine.
48
+ * An entry is for session ID if the value's `sessionId` matches the key.
49
+ */
50
+ this.attendees = new Map();
51
+ this.selfAttendee = new SessionClient(clientSessionId);
52
+ this.attendees.set(clientSessionId, this.selfAttendee);
53
+ }
54
+ ensureContent(_content) {
55
+ throw new Error("Method not implemented.");
56
+ }
57
+ processUpdate(_received, _timeModifier, remoteDatastore, senderConnectionId) {
58
+ const postUpdateActions = [];
59
+ const audienceMembers = this.audience.getMembers();
60
+ const connectedAttendees = new Set();
61
+ for (const [clientConnectionId, value] of Object.entries(remoteDatastore.clientToSessionId)) {
62
+ const clientSessionId = value.value;
63
+ const { attendee, isNew } = this.ensureAttendee(clientSessionId, clientConnectionId,
64
+ /* order */ value.rev);
65
+ // Check new attendee against audience to see if they're currently connected
66
+ const isAttendeeConnected = audienceMembers.has(clientConnectionId);
67
+ if (isAttendeeConnected) {
68
+ connectedAttendees.add(attendee);
69
+ if (attendee.getConnectionStatus() === SessionClientStatus.Disconnected) {
70
+ attendee.setConnectionId(clientConnectionId);
71
+ }
72
+ if (isNew) {
73
+ // If the attendee is both new and in audience (i.e. currently connected), emit an attendeeJoined event.
74
+ postUpdateActions.push(() => this.events.emit("attendeeJoined", attendee));
75
+ }
76
+ }
77
+ // If the attendee is not in the audience, they are considered disconnected.
78
+ if (!connectedAttendees.has(attendee)) {
79
+ attendee.setDisconnected();
80
+ }
81
+ const knownSessionId = this.datastore.clientToSessionId[clientConnectionId];
82
+ if (knownSessionId === undefined) {
83
+ this.datastore.clientToSessionId[clientConnectionId] = value;
84
+ }
85
+ else {
86
+ assert(knownSessionId.value === value.value, 0xa5a /* Mismatched SessionId */);
87
+ }
88
+ }
89
+ // TODO: reorganize processUpdate and caller to process actions after all updates are processed.
90
+ for (const action of postUpdateActions) {
91
+ action();
92
+ }
93
+ }
94
+ onConnectionAdded(clientConnectionId) {
95
+ this.datastore.clientToSessionId[clientConnectionId] = {
96
+ rev: this.selfAttendee.order++,
97
+ timestamp: Date.now(),
98
+ value: this.selfAttendee.sessionId,
99
+ };
100
+ this.selfAttendee.setConnectionId(clientConnectionId);
101
+ this.attendees.set(clientConnectionId, this.selfAttendee);
102
+ }
103
+ removeClientConnectionId(clientConnectionId) {
104
+ const attendee = this.attendees.get(clientConnectionId);
105
+ if (!attendee) {
106
+ return;
107
+ }
108
+ // If the last known connectionID is different from the connection ID being removed, the attendee has reconnected,
109
+ // therefore we should not change the attendee connection status or emit a disconnect event.
110
+ const attendeeReconnected = attendee.getConnectionId() !== clientConnectionId;
111
+ const connected = attendee.getConnectionStatus() === SessionClientStatus.Connected;
112
+ if (!attendeeReconnected && connected) {
113
+ attendee.setDisconnected();
114
+ this.events.emit("attendeeDisconnected", attendee);
115
+ }
116
+ }
117
+ getAttendees() {
118
+ return new Set(this.attendees.values());
119
+ }
120
+ getAttendee(clientId) {
121
+ const attendee = this.attendees.get(clientId);
122
+ if (attendee) {
123
+ return attendee;
124
+ }
125
+ // TODO: Restore option to add attendee on demand to handle internal
126
+ // lookup cases that must come from internal data.
127
+ // There aren't any resiliency mechanisms in place to handle a missed
128
+ // ClientJoin right now.
129
+ throw new Error("Attendee not found");
130
+ }
131
+ getMyself() {
132
+ return this.selfAttendee;
133
+ }
134
+ /**
135
+ * Make sure the given client session and connection ID pair are represented
136
+ * in the attendee map. If not present, SessionClient is created and added
137
+ * to map. If present, make sure the current connection ID is updated.
138
+ */
139
+ ensureAttendee(clientSessionId, clientConnectionId, order) {
140
+ let attendee = this.attendees.get(clientSessionId);
141
+ let isNew = false;
142
+ if (attendee === undefined) {
143
+ // New attendee. Create SessionClient and add session ID based
144
+ // entry to map.
145
+ attendee = new SessionClient(clientSessionId, clientConnectionId);
146
+ this.attendees.set(clientSessionId, attendee);
147
+ isNew = true;
148
+ }
149
+ else if (order > attendee.order) {
150
+ // The given association is newer than the one we have.
151
+ // Update the order and current connection ID.
152
+ attendee.order = order;
153
+ attendee.setConnectionId(clientConnectionId);
154
+ isNew = true;
155
+ }
156
+ // Always update entry for the connection ID. (Okay if already set.)
157
+ this.attendees.set(clientConnectionId, attendee);
158
+ return { attendee, isNew };
159
+ }
160
+ }
161
+ /**
162
+ * Instantiates the system workspace.
163
+ *
164
+ * @internal
165
+ */
166
+ export function createSystemWorkspace(clientSessionId, datastore, events, audience) {
167
+ const workspace = new SystemWorkspaceImpl(clientSessionId, datastore, events, audience);
168
+ return {
169
+ workspace,
170
+ statesEntry: {
171
+ internal: workspace,
172
+ public: undefined,
173
+ },
174
+ };
175
+ }
176
+ //# sourceMappingURL=systemWorkspace.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"systemWorkspace.js","sourceRoot":"","sources":["../src/systemWorkspace.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,EAAE,MAAM,EAAE,MAAM,qCAAqC,CAAC;AAU7D,OAAO,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAC;AAiBpD,MAAM,aAAa;IASlB,YACiB,SAA0B,EAClC,eAA+C,SAAS;QADhD,cAAS,GAAT,SAAS,CAAiB;QAClC,iBAAY,GAAZ,YAAY,CAA4C;QAVjE;;;WAGG;QACI,UAAK,GAAW,CAAC,CAAC;QAQxB,IAAI,CAAC,gBAAgB;YACpB,YAAY,KAAK,SAAS;gBACzB,CAAC,CAAC,mBAAmB,CAAC,YAAY;gBAClC,CAAC,CAAC,mBAAmB,CAAC,SAAS,CAAC;IACnC,CAAC;IAEM,eAAe;QACrB,IAAI,IAAI,CAAC,YAAY,KAAK,SAAS,EAAE,CAAC;YACrC,MAAM,IAAI,KAAK,CAAC,iCAAiC,CAAC,CAAC;QACpD,CAAC;QACD,OAAO,IAAI,CAAC,YAAY,CAAC;IAC1B,CAAC;IAEM,mBAAmB;QACzB,OAAO,IAAI,CAAC,gBAAgB,CAAC;IAC9B,CAAC;IAEM,eAAe,CAAC,YAAgC;QACtD,IAAI,CAAC,YAAY,GAAG,YAAY,CAAC;QACjC,IAAI,CAAC,gBAAgB,GAAG,mBAAmB,CAAC,SAAS,CAAC;IACvD,CAAC;IAEM,eAAe;QAC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,IAAI,QAAQ,GAAG,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,eAAe,CAAC,CAAC;QACnD,IAAI,KAAK,GAAG,KAAK,CAAC;QAElB,IAAI,QAAQ,KAAK,SAAS,EAAE,CAAC;YAC5B,8DAA8D;YAC9D,gBAAgB;YAChB,QAAQ,GAAG,IAAI,aAAa,CAAC,eAAe,EAAE,kBAAkB,CAAC,CAAC;YAClE,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,eAAe,EAAE,QAAQ,CAAC,CAAC;YAC9C,KAAK,GAAG,IAAI,CAAC;QACd,CAAC;aAAM,IAAI,KAAK,GAAG,QAAQ,CAAC,KAAK,EAAE,CAAC;YACnC,uDAAuD;YACvD,8CAA8C;YAC9C,QAAQ,CAAC,KAAK,GAAG,KAAK,CAAC;YACvB,QAAQ,CAAC,eAAe,CAAC,kBAAkB,CAAC,CAAC;YAC7C,KAAK,GAAG,IAAI,CAAC;QACd,CAAC;QACD,oEAAoE;QACpE,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,kBAAkB,EAAE,QAAQ,CAAC,CAAC;QAEjD,OAAO,EAAE,QAAQ,EAAE,KAAK,EAAE,CAAC;IAC5B,CAAC;CACD;AAED;;;;GAIG;AACH,MAAM,UAAU,qBAAqB,CACpC,eAAgC,EAChC,SAAmC,EACnC,MAAwD,EACxD,QAAmB;IAQnB,MAAM,SAAS,GAAG,IAAI,mBAAmB,CAAC,eAAe,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,CAAC,CAAC;IACxF,OAAO;QACN,SAAS;QACT,WAAW,EAAE;YACZ,QAAQ,EAAE,SAAS;YACnB,MAAM,EAAE,SAA4D;SACpE;KACD,CAAC;AACH,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport type { IAudience } from \"@fluidframework/container-definitions\";\nimport { assert } from \"@fluidframework/core-utils/internal\";\n\nimport type { ClientConnectionId } from \"./baseTypes.js\";\nimport type { InternalTypes } from \"./exposedInternalTypes.js\";\nimport type {\n\tClientSessionId,\n\tIPresence,\n\tISessionClient,\n\tPresenceEvents,\n} from \"./presence.js\";\nimport { SessionClientStatus } from \"./presence.js\";\nimport type { PresenceStatesInternal } from \"./presenceStates.js\";\nimport type { PresenceStates, PresenceStatesSchema } from \"./types.js\";\n\nimport type { IEmitter } from \"@fluidframework/presence/internal/events\";\n\n/**\n * The system workspace's datastore structure.\n *\n * @internal\n */\nexport interface SystemWorkspaceDatastore {\n\tclientToSessionId: {\n\t\t[ConnectionId: ClientConnectionId]: InternalTypes.ValueRequiredState<ClientSessionId>;\n\t};\n}\n\nclass SessionClient implements ISessionClient {\n\t/**\n\t * Order is used to track the most recent client connection\n\t * during a session.\n\t */\n\tpublic order: number = 0;\n\n\tprivate connectionStatus: SessionClientStatus;\n\n\tpublic constructor(\n\t\tpublic readonly sessionId: ClientSessionId,\n\t\tprivate connectionId: ClientConnectionId | undefined = undefined,\n\t) {\n\t\tthis.connectionStatus =\n\t\t\tconnectionId === undefined\n\t\t\t\t? SessionClientStatus.Disconnected\n\t\t\t\t: SessionClientStatus.Connected;\n\t}\n\n\tpublic getConnectionId(): ClientConnectionId {\n\t\tif (this.connectionId === undefined) {\n\t\t\tthrow new Error(\"Client has never been connected\");\n\t\t}\n\t\treturn this.connectionId;\n\t}\n\n\tpublic getConnectionStatus(): SessionClientStatus {\n\t\treturn this.connectionStatus;\n\t}\n\n\tpublic setConnectionId(connectionId: ClientConnectionId): void {\n\t\tthis.connectionId = connectionId;\n\t\tthis.connectionStatus = SessionClientStatus.Connected;\n\t}\n\n\tpublic setDisconnected(): void {\n\t\tthis.connectionStatus = SessionClientStatus.Disconnected;\n\t}\n}\n\n/**\n * @internal\n */\nexport interface SystemWorkspace\n\t// Portion of IPresence that is handled by SystemWorkspace along with\n\t// responsiblity for emitting \"attendeeJoined\" events.\n\textends Pick<IPresence, \"getAttendees\" | \"getAttendee\" | \"getMyself\"> {\n\t/**\n\t * Must be called when the current client acquires a new connection.\n\t *\n\t * @param clientConnectionId - The new client connection ID.\n\t */\n\tonConnectionAdded(clientConnectionId: ClientConnectionId): void;\n\n\t/**\n\t * Removes the client connection ID from the system workspace.\n\t *\n\t * @param clientConnectionId - The client connection ID to remove.\n\t */\n\tremoveClientConnectionId(clientConnectionId: ClientConnectionId): void;\n}\n\nclass SystemWorkspaceImpl implements PresenceStatesInternal, SystemWorkspace {\n\tprivate readonly selfAttendee: SessionClient;\n\t/**\n\t * `attendees` is this client's understanding of the attendees in the\n\t * session. The map covers entries for both session ids and connection\n\t * ids, which are never expected to collide, but if they did for same\n\t * client that would be fine.\n\t * An entry is for session ID if the value's `sessionId` matches the key.\n\t */\n\tprivate readonly attendees = new Map<ClientConnectionId | ClientSessionId, SessionClient>();\n\n\tpublic constructor(\n\t\tclientSessionId: ClientSessionId,\n\t\tprivate readonly datastore: SystemWorkspaceDatastore,\n\t\tprivate readonly events: IEmitter<\n\t\t\tPick<PresenceEvents, \"attendeeJoined\" | \"attendeeDisconnected\">\n\t\t>,\n\t\tprivate readonly audience: IAudience,\n\t) {\n\t\tthis.selfAttendee = new SessionClient(clientSessionId);\n\t\tthis.attendees.set(clientSessionId, this.selfAttendee);\n\t}\n\n\tpublic ensureContent<TSchemaAdditional extends PresenceStatesSchema>(\n\t\t_content: TSchemaAdditional,\n\t): never {\n\t\tthrow new Error(\"Method not implemented.\");\n\t}\n\n\tpublic processUpdate(\n\t\t_received: number,\n\t\t_timeModifier: number,\n\t\tremoteDatastore: {\n\t\t\tclientToSessionId: {\n\t\t\t\t[\n\t\t\t\t\tConnectionId: ClientConnectionId\n\t\t\t\t]: InternalTypes.ValueRequiredState<ClientSessionId> & {\n\t\t\t\t\tignoreUnmonitored?: true;\n\t\t\t\t};\n\t\t\t};\n\t\t},\n\t\tsenderConnectionId: ClientConnectionId,\n\t): void {\n\t\tconst postUpdateActions: (() => void)[] = [];\n\t\tconst audienceMembers = this.audience.getMembers();\n\t\tconst connectedAttendees = new Set<SessionClient>();\n\t\tfor (const [clientConnectionId, value] of Object.entries(\n\t\t\tremoteDatastore.clientToSessionId,\n\t\t)) {\n\t\t\tconst clientSessionId = value.value;\n\t\t\tconst { attendee, isNew } = this.ensureAttendee(\n\t\t\t\tclientSessionId,\n\t\t\t\tclientConnectionId,\n\t\t\t\t/* order */ value.rev,\n\t\t\t);\n\n\t\t\t// Check new attendee against audience to see if they're currently connected\n\t\t\tconst isAttendeeConnected = audienceMembers.has(clientConnectionId);\n\n\t\t\tif (isAttendeeConnected) {\n\t\t\t\tconnectedAttendees.add(attendee);\n\t\t\t\tif (attendee.getConnectionStatus() === SessionClientStatus.Disconnected) {\n\t\t\t\t\tattendee.setConnectionId(clientConnectionId);\n\t\t\t\t}\n\t\t\t\tif (isNew) {\n\t\t\t\t\t// If the attendee is both new and in audience (i.e. currently connected), emit an attendeeJoined event.\n\t\t\t\t\tpostUpdateActions.push(() => this.events.emit(\"attendeeJoined\", attendee));\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// If the attendee is not in the audience, they are considered disconnected.\n\t\t\tif (!connectedAttendees.has(attendee)) {\n\t\t\t\tattendee.setDisconnected();\n\t\t\t}\n\n\t\t\tconst knownSessionId: InternalTypes.ValueRequiredState<ClientSessionId> | undefined =\n\t\t\t\tthis.datastore.clientToSessionId[clientConnectionId];\n\t\t\tif (knownSessionId === undefined) {\n\t\t\t\tthis.datastore.clientToSessionId[clientConnectionId] = value;\n\t\t\t} else {\n\t\t\t\tassert(knownSessionId.value === value.value, 0xa5a /* Mismatched SessionId */);\n\t\t\t}\n\t\t}\n\n\t\t// TODO: reorganize processUpdate and caller to process actions after all updates are processed.\n\t\tfor (const action of postUpdateActions) {\n\t\t\taction();\n\t\t}\n\t}\n\n\tpublic onConnectionAdded(clientConnectionId: ClientConnectionId): void {\n\t\tthis.datastore.clientToSessionId[clientConnectionId] = {\n\t\t\trev: this.selfAttendee.order++,\n\t\t\ttimestamp: Date.now(),\n\t\t\tvalue: this.selfAttendee.sessionId,\n\t\t};\n\n\t\tthis.selfAttendee.setConnectionId(clientConnectionId);\n\t\tthis.attendees.set(clientConnectionId, this.selfAttendee);\n\t}\n\n\tpublic removeClientConnectionId(clientConnectionId: ClientConnectionId): void {\n\t\tconst attendee = this.attendees.get(clientConnectionId);\n\t\tif (!attendee) {\n\t\t\treturn;\n\t\t}\n\n\t\t// If the last known connectionID is different from the connection ID being removed, the attendee has reconnected,\n\t\t// therefore we should not change the attendee connection status or emit a disconnect event.\n\t\tconst attendeeReconnected = attendee.getConnectionId() !== clientConnectionId;\n\t\tconst connected = attendee.getConnectionStatus() === SessionClientStatus.Connected;\n\t\tif (!attendeeReconnected && connected) {\n\t\t\tattendee.setDisconnected();\n\t\t\tthis.events.emit(\"attendeeDisconnected\", attendee);\n\t\t}\n\t}\n\n\tpublic getAttendees(): ReadonlySet<ISessionClient> {\n\t\treturn new Set(this.attendees.values());\n\t}\n\n\tpublic getAttendee(clientId: ClientConnectionId | ClientSessionId): ISessionClient {\n\t\tconst attendee = this.attendees.get(clientId);\n\t\tif (attendee) {\n\t\t\treturn attendee;\n\t\t}\n\n\t\t// TODO: Restore option to add attendee on demand to handle internal\n\t\t// lookup cases that must come from internal data.\n\t\t// There aren't any resiliency mechanisms in place to handle a missed\n\t\t// ClientJoin right now.\n\t\tthrow new Error(\"Attendee not found\");\n\t}\n\n\tpublic getMyself(): ISessionClient {\n\t\treturn this.selfAttendee;\n\t}\n\n\t/**\n\t * Make sure the given client session and connection ID pair are represented\n\t * in the attendee map. If not present, SessionClient is created and added\n\t * to map. If present, make sure the current connection ID is updated.\n\t */\n\tprivate ensureAttendee(\n\t\tclientSessionId: ClientSessionId,\n\t\tclientConnectionId: ClientConnectionId,\n\t\torder: number,\n\t): { attendee: SessionClient; isNew: boolean } {\n\t\tlet attendee = this.attendees.get(clientSessionId);\n\t\tlet isNew = false;\n\n\t\tif (attendee === undefined) {\n\t\t\t// New attendee. Create SessionClient and add session ID based\n\t\t\t// entry to map.\n\t\t\tattendee = new SessionClient(clientSessionId, clientConnectionId);\n\t\t\tthis.attendees.set(clientSessionId, attendee);\n\t\t\tisNew = true;\n\t\t} else if (order > attendee.order) {\n\t\t\t// The given association is newer than the one we have.\n\t\t\t// Update the order and current connection ID.\n\t\t\tattendee.order = order;\n\t\t\tattendee.setConnectionId(clientConnectionId);\n\t\t\tisNew = true;\n\t\t}\n\t\t// Always update entry for the connection ID. (Okay if already set.)\n\t\tthis.attendees.set(clientConnectionId, attendee);\n\n\t\treturn { attendee, isNew };\n\t}\n}\n\n/**\n * Instantiates the system workspace.\n *\n * @internal\n */\nexport function createSystemWorkspace(\n\tclientSessionId: ClientSessionId,\n\tdatastore: SystemWorkspaceDatastore,\n\tevents: IEmitter<Pick<PresenceEvents, \"attendeeJoined\">>,\n\taudience: IAudience,\n): {\n\tworkspace: SystemWorkspace;\n\tstatesEntry: {\n\t\tinternal: PresenceStatesInternal;\n\t\tpublic: PresenceStates<PresenceStatesSchema>;\n\t};\n} {\n\tconst workspace = new SystemWorkspaceImpl(clientSessionId, datastore, events, audience);\n\treturn {\n\t\tworkspace,\n\t\tstatesEntry: {\n\t\t\tinternal: workspace,\n\t\t\tpublic: undefined as unknown as PresenceStates<PresenceStatesSchema>,\n\t\t},\n\t};\n}\n"]}
@@ -0,0 +1,11 @@
1
+ // This file is read by tools that parse documentation comments conforming to the TSDoc standard.
2
+ // It should be published with your NPM package. It should not be tracked by Git.
3
+ {
4
+ "tsdocVersion": "0.12",
5
+ "toolPackages": [
6
+ {
7
+ "packageName": "@microsoft/api-extractor",
8
+ "packageVersion": "7.47.8"
9
+ }
10
+ ]
11
+ }