Skip to content

Commit

Permalink
Merge pull request #1920 from martinlingstuyl/add-retry-mechanism
Browse files Browse the repository at this point in the history
Adds retry to updating list item in DynamicForm. Closes #1919
  • Loading branch information
joelfmrodrigues authored Dec 11, 2024
2 parents 25cebd4 + 0269e88 commit 40372e7
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions src/controls/dynamicForm/DynamicForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import "@pnp/sp/content-types";
import "@pnp/sp/folders";
import "@pnp/sp/items";
import { IFolder } from "@pnp/sp/folders";
import { IInstalledLanguageInfo } from "@pnp/sp/presets/all";
import { IInstalledLanguageInfo, IItemUpdateResult, IList } from "@pnp/sp/presets/all";
import { cloneDeep, isEqual } from "lodash";
import { ICustomFormatting, ICustomFormattingBodySection, ICustomFormattingNode } from "../../common/utilities/ICustomFormatting";
import SPservice from "../../services/SPService";
Expand All @@ -49,6 +49,10 @@ import { Icon } from "@fluentui/react/lib/Icon";

const stackTokens: IStackTokens = { childrenGap: 20 };

const timeout = (ms: number): Promise<void> => {
return new Promise((resolve) => setTimeout(resolve, ms));
};

/**
* DynamicForm Class Control
*/
Expand Down Expand Up @@ -611,7 +615,7 @@ export class DynamicForm extends React.Component<
// Set the content type ID for the target item
objects[contentTypeIdField] = contentTypeId;
// Update the just created folder or Document Set
const iur = await library.items.getById(folderId).update(objects);
const iur = await this.updateListItemRetry(library, folderId, objects);
if (onSubmitted) {
onSubmitted(
iur.data,
Expand Down Expand Up @@ -689,7 +693,7 @@ export class DynamicForm extends React.Component<
// Set the content type ID for the target item
objects[contentTypeIdField] = contentTypeId;
// Update the just created file
const iur = await library.items.getById(fileId).update(objects);
const iur = await this.updateListItemRetry(library, fileId, objects);
if (onSubmitted) {
onSubmitted(
iur.data,
Expand Down Expand Up @@ -1521,4 +1525,27 @@ export class DynamicForm extends React.Component<
const folder = sp.web.getFolderByServerRelativePath(`${serverRelativeLibraryPath}/${normalizedFolderPath}`);
return folder;
};
}

/**
* Updates a list item and retries the operation if a 409 (Save Conflict) was thrown.
* @param list The list/library on which to execute the operation
* @param itemId The item ID
* @param objects The values to update the item with
* @param retry The retry index
* @returns An update result
*/
private updateListItemRetry = async (list: IList, itemId: number, objects: {}, retry: number = 0): Promise<IItemUpdateResult> => {
try {
return await list.items.getById(itemId).update(objects);
}
catch (error)
{
if (error.status === 409 && retry < 3) {
await timeout(100);
return await this.updateListItemRetry(list, itemId, objects, retry + 1);
}

throw error;
}
}
}

0 comments on commit 40372e7

Please sign in to comment.