MCPcopy Create free account
hub / github.com/apitable/apitable / applyLocalData

Method applyLocalData

packages/core/src/sync/room.ts:228–278  ·  view source on GitHub ↗
(changesetMap: Record<string, ILocalChangeset>, opBufferMap: Record<string, IOperation[]>)

Source from the content-addressed store, hash-verified

226 }
227
228 async applyLocalData(changesetMap: Record<string, ILocalChangeset>, opBufferMap: Record<string, IOperation[]>) {
229 if (Object.keys(changesetMap).length) {
230 const res = await Api.applyResourceChangesets(Object.values(changesetMap), this.roomId);
231 const { success, message, code } = res.data;
232 if (!success && code !== 4001) {
233 throw new Error(`changesetMap: ${message}`);
234 }
235 }
236
237 if (Object.keys(opBufferMap).length) {
238 const changesets: ILocalChangeset[] = [];
239 for (const resourceId in opBufferMap) {
240 if (changesetMap[resourceId]) {
241 const changeset = BufferStorage.ops2Changeset(
242 opBufferMap[resourceId]!,
243 changesetMap[resourceId]!.baseRevision + 1,
244 resourceId,
245 changesetMap[resourceId]!.resourceType
246 );
247 changesets.push(changeset);
248 continue;
249 }
250 const opBuffer = opBufferMap[resourceId]!;
251 const revision = opBuffer[0]!.revision;
252 const resourceType = opBuffer[0]!.resourceType;
253 const changeset = BufferStorage.ops2Changeset(opBuffer, revision as number, resourceId, resourceType as ResourceType);
254
255 if (opBuffer.every((operation) => !operation.mainLinkDstId)) {
256 const res = await Api.applyResourceChangesets([changeset], this.roomId);
257 const { success, message } = res.data;
258 if (!success) {
259 throw new Error(`single changesets: ${message}`);
260 }
261 delete opBufferMap[resourceId];
262 continue;
263 }
264
265 changesets.push(changeset);
266 }
267
268 if (!changesets.length) {
269 return;
270 }
271
272 const res = await Api.applyResourceChangesets(Object.values(changesets), this.roomId);
273 const { success, message } = res.data;
274 if (!success) {
275 throw new Error(`opBufferMap: ${message}`);
276 }
277 }
278 }
279
280 /**
281 * The backend server actively pushes changesets submitted by others

Callers 2

applyFunction · 0.80

Calls 3

ops2ChangesetMethod · 0.80
keysMethod · 0.65
pushMethod · 0.45

Tested by

no test coverage detected