{"id":489558,"date":"2026-08-03T12:09:51","date_gmt":"2026-08-03T12:09:51","guid":{"rendered":"https:\/\/savepearlharbor.com\/?p=489558"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=489558","title":{"rendered":"Next.js, optimistic UI \u0438 autosave"},"content":{"rendered":"<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>Optimistic update \u043c\u0435\u043d\u044f\u0435\u0442 \u043a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0434\u043e \u043e\u0442\u0432\u0435\u0442\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0430. Autosave \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 \u043d\u043e\u0432\u0443\u044e \u0432\u0435\u0440\u0441\u0438\u044e \u043f\u043e\u0441\u043b\u0435 \u043f\u0430\u0443\u0437\u044b \u0432\u043e \u0432\u0432\u043e\u0434\u0435. \u0414\u043e \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f action \u0432 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0442 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435, \u0437\u0430\u043f\u0440\u043e\u0441 \u0432 \u0440\u0430\u0431\u043e\u0442\u0435 \u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u044f\u044f \u043f\u043e\u0434\u0442\u0432\u0435\u0440\u0436\u0434\u0451\u043d\u043d\u0430\u044f \u0432\u0435\u0440\u0441\u0438\u044f \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0435.<\/p>\n<p>\u041f\u0440\u0438 optimistic create \u043a\u043b\u0438\u0435\u043d\u0442 \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c \u0441 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u043c id, \u0430 \u0441\u0435\u0440\u0432\u0435\u0440 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0434\u0440\u0443\u0433\u043e\u0439 id. \u041e\u0448\u0438\u0431\u043a\u0430 \u043f\u043e\u0441\u043b\u0435 \u0447\u0430\u0441\u0442\u0438\u0447\u043d\u043e\u0439 \u0437\u0430\u043f\u0438\u0441\u0438 \u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0435 \u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u0439 rollback. \u041f\u0440\u0438 autosave \u043e\u0442\u0432\u0435\u0442\u044b \u043f\u0440\u0438\u0445\u043e\u0434\u044f\u0442 \u043d\u0435 \u0432 \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0438. \u0421\u0442\u0430\u0440\u044b\u0439 \u043e\u0442\u0432\u0435\u0442 \u043c\u0435\u043d\u044f\u0435\u0442 \u0441\u0442\u0430\u0442\u0443\u0441 \u0443\u0436\u0435 \u043f\u043e\u0441\u043b\u0435 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u043d\u043e\u0432\u043e\u0439 \u0432\u0435\u0440\u0441\u0438\u0438. updatedAt \u043c\u043e\u0436\u0435\u0442 \u043f\u0435\u0440\u0435\u0439\u0442\u0438 \u0432\u043f\u0435\u0440\u0451\u0434 \u0434\u043e \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u0437\u0430\u043f\u0438\u0441\u0438.<\/p>\n<p>\u0412 \u0443\u0447\u0435\u0431\u043d\u043e\u043c \u043f\u0440\u043e\u0435\u043a\u0442\u0435\u00a0<a href=\"https:\/\/workbench-ts.onrender.com\/\" rel=\"noopener noreferrer nofollow\">Workbench<\/a>\u00a0optimistic create \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 clientId \u0434\u043b\u044f \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u043e\u0433\u043e id \u043d\u0430 \u043a\u043b\u0438\u0435\u043d\u0442\u0435 \u0438 \u0441\u0435\u0440\u0432\u0435\u0440\u0435. Autosave-\u0434\u0435\u043c\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0451\u0442 requestId \u0432 \u043a\u0430\u0436\u0434\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441. \u0420\u0435\u0434\u0430\u043a\u0442\u043e\u0440 \u0445\u0440\u0430\u043d\u0438\u0442 \u0441\u0442\u0430\u0442\u0443\u0441\u044b saving, saved \u0438 error \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e \u043e\u0442 \u0442\u0435\u043a\u0441\u0442\u0430 \u0437\u0430\u043c\u0435\u0442\u043a\u0438.<\/p>\n<h3>Optimistic insert<\/h3>\n<p>\u041a\u043b\u0438\u0435\u043d\u0442 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c \u0432 store \u0434\u043e \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f server action. \u0424\u043e\u0440\u043c\u0430 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 title \u0438 clientId. Optimistic id \u0441\u0442\u0440\u043e\u0438\u0442\u0441\u044f \u0438\u0437 \u0442\u043e\u0433\u043e \u0436\u0435 clientId.<\/p>\n<pre><code class=\"typescript\">const [clientId, setClientId] = useState(() =&gt; crypto.randomUUID());const optimisticId = useMemo(  () =&gt; `i-${clientId}`,  [clientId]);&lt;form  action={formAction}  onSubmit={(event) =&gt; {    const formData = new FormData(event.currentTarget);    const title = String(formData.get(\"title\") ?? \"\").trim();    if (title.length &lt; 2) return;    startTransition(() =&gt; {      insertItemLocal({        id: optimisticId,        title,        createdAt: new Date().toISOString(),      });    });  }}&gt;  &lt;input type=\"hidden\" name=\"clientId\" value={clientId} \/&gt;  &lt;input name=\"title\" \/&gt;&lt;\/form&gt;<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:87px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>Store \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 id\u00a0<code>i-${clientId}<\/code>\u00a0\u0434\u043e \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f action.<\/p>\n<h3>Id \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0435<\/h3>\n<p>Server action \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442 clientId \u0447\u0435\u0440\u0435\u0437 Zod \u0438 \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u0442 id \u043f\u043e \u0442\u043e\u0439 \u0436\u0435 \u0444\u043e\u0440\u043c\u0443\u043b\u0435.<\/p>\n<pre><code class=\"typescript\">const schema = z.object({  title: titleSchema,  clientId: z.string().uuid(),});const parsed = schema.safeParse({  title: formData.get(\"title\"),  clientId: formData.get(\"clientId\"),});if (!parsed.success) {  return {    ok: false,    error: null,    fieldErrors: {      title: parsed.error.flatten().fieldErrors.title?.[0],    },  };}const itemId = `i-${parsed.data.clientId}`;<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u041a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u0430\u044f \u0437\u0430\u043f\u0438\u0441\u044c \u0438 \u0441\u0435\u0440\u0432\u0435\u0440\u043d\u0430\u044f \u0437\u0430\u043f\u0438\u0441\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \u043e\u0434\u0438\u043d id. \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439 id \u0441 \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0437\u0430\u043c\u0435\u043d\u043e\u0439 \u0437\u0434\u0435\u0441\u044c \u043d\u0435 \u043d\u0443\u0436\u0435\u043d. \u0421\u0441\u044b\u043b\u043a\u0438 \u043d\u0430 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c, \u0432\u044b\u0431\u0440\u0430\u043d\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438 optimistic state \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u044e\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u0442\u0435\u043c \u0436\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \u043f\u043e\u0441\u043b\u0435 \u043e\u0442\u0432\u0435\u0442\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0430.<\/p>\n<h3>\u041f\u043e\u0432\u0442\u043e\u0440\u043d\u0430\u044f \u0437\u0430\u043f\u0438\u0441\u044c<\/h3>\n<p>\u041e\u0434\u0438\u043d action \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u0439\u0442\u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e \u043f\u043e\u0441\u043b\u0435 retry, \u0434\u0432\u043e\u0439\u043d\u043e\u0439 \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0438 \u0438\u043b\u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u0433\u043e \u0432\u044b\u0437\u043e\u0432\u0430 \u0444\u043e\u0440\u043c\u044b. \u0421\u0435\u0440\u0432\u0435\u0440 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442 \u043d\u0430\u043b\u0438\u0447\u0438\u0435 id \u043f\u0435\u0440\u0435\u0434 \u0432\u0441\u0442\u0430\u0432\u043a\u043e\u0439.<\/p>\n<pre><code class=\"javascript\">const itemId = `i-${parsed.data.clientId}`;const db = await readWorkbenchDb();if (!db.items.some((item) =&gt; item.id === itemId)) {  db.items.unshift({    id: itemId,    title: parsed.data.title,    createdAt: new Date().toISOString(),  });  await writeWorkbenchDb(db);}<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u041f\u043e\u0432\u0442\u043e\u0440\u043d\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441 \u0441 \u0442\u0435\u043c \u0436\u0435 clientId \u043d\u0435 \u0441\u043e\u0437\u0434\u0430\u0451\u0442 \u0432\u0442\u043e\u0440\u043e\u0439 \u043e\u0431\u044a\u0435\u043a\u0442.<\/p>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0443\u0441\u043f\u0435\u0448\u043d\u043e\u0433\u043e \u043e\u0442\u0432\u0435\u0442\u0430 \u0444\u043e\u0440\u043c\u0430 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 clientId. \u0421\u043b\u0435\u0434\u0443\u044e\u0449\u0430\u044f optimistic-\u0432\u0441\u0442\u0430\u0432\u043a\u0430 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u043d\u043e\u0432\u044b\u0439 id.<\/p>\n<pre><code class=\"typescript\">useEffect(() =&gt; {  if (!state.ok) return;  startTransition(() =&gt; {    setClientId(crypto.randomUUID());  });}, [state]);<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<h3>\u041e\u0448\u0438\u0431\u043a\u0430 \u043f\u043e\u0441\u043b\u0435 \u0437\u0430\u043f\u0438\u0441\u0438<\/h3>\n<p>\u0421\u0435\u0440\u0432\u0435\u0440 \u043c\u043e\u0436\u0435\u0442 \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u043e\u0448\u0438\u0431\u043a\u0443 \u043f\u043e\u0437\u0436\u0435. \u0421\u0431\u043e\u0439 \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u043f\u043e\u0441\u043b\u0435 \u0437\u0430\u043f\u0438\u0441\u0438 \u0444\u0430\u0439\u043b\u0430 \u0438\u043b\u0438 \u0431\u0430\u0437\u044b, \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438, revalidate \u0438\u043b\u0438 \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043a\u0438 \u043e\u0442\u0432\u0435\u0442\u0430. \u041a\u043b\u0438\u0435\u043d\u0442 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 error, \u0445\u043e\u0442\u044f \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c \u0443\u0436\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0435. \u041b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u0439 rollback \u0432 \u0442\u0430\u043a\u043e\u043c \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0438 \u0443\u0434\u0430\u043b\u0438\u0442 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c \u0438\u0437 store. \u041f\u043e\u0441\u043b\u0435 reload \u043e\u043d\u0430 \u043f\u043e\u044f\u0432\u0438\u0442\u0441\u044f \u0441\u043d\u043e\u0432\u0430.<\/p>\n<p>\u0412 optimistic-lab \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d partial-\u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0439. Action \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442 \u043e\u0431\u044a\u0435\u043a\u0442 \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043e\u0448\u0438\u0431\u043a\u0443 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438. \u041a\u043b\u0438\u0435\u043d\u0442 \u043f\u0435\u0440\u0435\u0447\u0438\u0442\u044b\u0432\u0430\u0435\u0442 \u0441\u0435\u0440\u0432\u0435\u0440\u043d\u044b\u0439 snapshot.<\/p>\n<pre><code class=\"typescript\">if (!state.ok &amp;&amp; (state.error || state.fieldErrors?.title)) {  if (hadOptimisticInsertRef.current) {    hadOptimisticInsertRef.current = false;    void refreshFromServer();    startTransition(() =&gt; {      setClientId(crypto.randomUUID());    });  }}<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p><code>refreshFromServer<\/code>\u00a0\u0437\u0430\u043c\u0435\u043d\u044f\u0435\u0442 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u0439 store \u0434\u0430\u043d\u043d\u044b\u043c\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u0435\u0440\u0432\u0435\u0440 \u0432\u0435\u0440\u043d\u0443\u043b \u043f\u043e\u0441\u043b\u0435 \u0437\u0430\u043f\u0438\u0441\u0438. \u0414\u043b\u044f \u043e\u0448\u0438\u0431\u043e\u043a, \u0432\u043e\u0437\u043d\u0438\u043a\u0448\u0438\u0445 \u0434\u043e \u0437\u0430\u043f\u0438\u0441\u0438, \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u0439 rollback \u043e\u0441\u0442\u0430\u0451\u0442\u0441\u044f \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u044b\u043c. \u0414\u043b\u044f \u043d\u0435\u0438\u0437\u0432\u0435\u0441\u0442\u043d\u043e\u0433\u043e \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u043f\u043e\u0441\u043b\u0435 action \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043f\u0435\u0440\u0435\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u044f.<\/p>\n<h3>Autosave \u0438 debounce<\/h3>\n<p>Debounce \u0443\u0434\u0430\u043b\u044f\u0435\u0442 \u0442\u0430\u0439\u043c\u0435\u0440, \u0435\u0441\u043b\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u043b \u0432\u0432\u043e\u0434 \u0434\u043e \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u0430. \u0423\u0436\u0435 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043d\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441 debounce \u043d\u0435 \u043e\u0442\u043c\u0435\u043d\u044f\u0435\u0442. \u041f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u043c\u0435\u043d\u044f\u0435\u0442 \u0442\u0435\u043a\u0441\u0442, \u0437\u0430\u043f\u0440\u043e\u0441 A \u0443\u0445\u043e\u0434\u0438\u0442 \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440. \u0421\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442 \u0437\u0430\u043f\u0440\u043e\u0441 B. \u0421\u0435\u0440\u0432\u0435\u0440 \u043c\u043e\u0436\u0435\u0442 \u0432\u0435\u0440\u043d\u0443\u0442\u044c B \u0440\u0430\u043d\u044c\u0448\u0435 A. \u041e\u0431\u0430 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u0441\u044f, \u0435\u0441\u043b\u0438 \u043a\u043b\u0438\u0435\u043d\u0442 \u043d\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442 \u043f\u0440\u0438\u043d\u0430\u0434\u043b\u0435\u0436\u043d\u043e\u0441\u0442\u044c \u043e\u0442\u0432\u0435\u0442\u0430 \u043a \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u043c\u0443 \u0437\u0430\u043f\u0440\u043e\u0441\u0443.<\/p>\n<p>\u0412 autosave-lab \u043a\u043b\u0438\u0435\u043d\u0442 \u0445\u0440\u0430\u043d\u0438\u0442 requestId \u0430\u043a\u0442\u0438\u0432\u043d\u043e\u0433\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f.<\/p>\n<pre><code class=\"typescript\">const activeRequestIdRef = useRef&lt;string | null&gt;(null);const timerRef = useRef&lt;number | null&gt;(null);useEffect(() =&gt; {  if (timerRef.current) {    window.clearTimeout(timerRef.current);  }  timerRef.current = window.setTimeout(() =&gt; {    const requestId = crypto.randomUUID();    activeRequestIdRef.current = requestId;    setIsSaving(true);    setServerError(null);    void saveAutosaveLabAction({      text: draft,      requestId,    }).then((result) =&gt; {      \/\/ \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043e\u0442\u0432\u0435\u0442\u0430    });  }, 700);  return () =&gt; {    if (timerRef.current) {      window.clearTimeout(timerRef.current);    }  };}, [draft]);<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u041d\u043e\u0432\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441 \u0437\u0430\u043c\u0435\u043d\u044f\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0432\u00a0<code>activeRequestIdRef<\/code>.<\/p>\n<h3>RequestId \u0432 \u043e\u0442\u0432\u0435\u0442\u0435<\/h3>\n<p>Server action \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 requestId \u0438 \u043f\u0440\u0438 \u0443\u0441\u043f\u0435\u0445\u0435 \u0438 \u043f\u0440\u0438 \u043e\u0448\u0438\u0431\u043a\u0435.<\/p>\n<pre><code class=\"javascript\">export type AutosaveLabResult =  | {      ok: true;      value: {        updatedAt: string;        requestId: string;      };    }  | {      ok: false;      error: string;      requestId: string;    };<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u041a\u043b\u0438\u0435\u043d\u0442 \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0435\u0442 id \u043e\u0442\u0432\u0435\u0442\u0430 \u0441 id \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430.<\/p>\n<pre><code class=\"javascript\">const responseRequestId = result.ok  ? result.value.requestId  : result.requestId;if (activeRequestIdRef.current !== responseRequestId) {  return;}<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u041e\u0442\u0432\u0435\u0442 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 A \u043d\u0435 \u043c\u0435\u043d\u044f\u0435\u0442 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u043f\u043e\u0441\u043b\u0435 \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 B. \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0441\u044f \u0438 \u043a \u043e\u0448\u0438\u0431\u043a\u0430\u043c. \u0421\u0442\u0430\u0440\u0430\u044f \u043e\u0448\u0438\u0431\u043a\u0430 \u043d\u0435 \u0434\u043e\u043b\u0436\u043d\u0430 \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0438\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0443\u044e \u0444\u043e\u0440\u043c\u0443 \u0432 error, \u0435\u0441\u043b\u0438 \u0431\u043e\u043b\u0435\u0435 \u043d\u043e\u0432\u0430\u044f \u0432\u0435\u0440\u0441\u0438\u044f \u0443\u0436\u0435 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0430.<\/p>\n<h3>\u0421\u0442\u0430\u0442\u0443\u0441\u044b \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f<\/h3>\n<p>\u0422\u0435\u043a\u0441\u0442 \u0437\u0430\u043c\u0435\u0442\u043a\u0438 \u0438 \u0441\u0442\u0430\u0442\u0443\u0441 \u0437\u0430\u043f\u0438\u0441\u0438 \u0445\u0440\u0430\u043d\u044f\u0442\u0441\u044f \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e. \u0418\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0440\u0435\u0434\u0430\u043a\u0442\u043e\u0440\u0430 \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0435\u0442 draft, \u0437\u0430\u043f\u0443\u0441\u043a action \u0441\u0442\u0430\u0432\u0438\u0442 saving. \u0423\u0441\u043f\u0435\u0448\u043d\u044b\u0439 \u043e\u0442\u0432\u0435\u0442 \u0430\u043a\u0442\u0438\u0432\u043d\u043e\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0441\u0442\u0430\u0432\u0438\u0442 saved, \u043e\u0448\u0438\u0431\u043a\u0430 \u0430\u043a\u0442\u0438\u0432\u043d\u043e\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0441\u0442\u0430\u0432\u0438\u0442 error.<\/p>\n<pre><code class=\"javascript\">type NoteSaveInfo =  | {      status: \"idle\";      error: null;      lastSavedAt?: string;    }  | {      status: \"saving\";      error: null;      lastSavedAt?: string;    }  | {      status: \"saved\";      error: null;      lastSavedAt: string;    }  | {      status: \"error\";      error: string;      lastSavedAt?: string;    };<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u0421\u0442\u0430\u0442\u0443\u0441 \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442 \u0441\u0435\u0440\u0432\u0435\u0440\u043d\u0443\u044e \u0437\u0430\u043f\u0438\u0441\u044c. \u0422\u0435\u043a\u0441\u0442 \u0432 \u0440\u0435\u0434\u0430\u043a\u0442\u043e\u0440\u0435 \u043c\u043e\u0436\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0431\u043e\u043b\u0435\u0435 \u043d\u043e\u0432\u0443\u044e \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u0443\u044e \u0432\u0435\u0440\u0441\u0438\u044e.<\/p>\n<h3>updatedAt \u043f\u043e\u0441\u043b\u0435 \u043e\u0442\u0432\u0435\u0442\u0430<\/h3>\n<p>\u0420\u0435\u0434\u0430\u043a\u0442\u043e\u0440 \u043c\u0435\u043d\u044f\u0435\u0442 contentHtml \u0432 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e\u043c store \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u0432\u0432\u043e\u0434\u0430. updatedAt \u0438 lastSavedAt \u043c\u0435\u043d\u044f\u044e\u0442\u0441\u044f \u043f\u043e\u0441\u043b\u0435 \u0443\u0441\u043f\u0435\u0448\u043d\u043e\u0433\u043e \u043e\u0442\u0432\u0435\u0442\u0430 action.<\/p>\n<pre><code class=\"javascript\">setNoteSaveInfoById((prev) =&gt; ({  ...prev,  [noteId]: {    status: \"saving\",    error: null,  },}));const timer = window.setTimeout(() =&gt; {  const updatedAt = new Date().toISOString();  void updateNoteContentAction({    noteId,    contentHtml: nextHtml,    updatedAt,  }).then((result) =&gt; {    if (!result.ok) {      setNoteSaveInfoById((prev) =&gt; ({        ...prev,        [noteId]: {          status: \"error\",          error: result.error,        },      }));      void refreshFromServer();      return;    }    setDb((prev) =&gt; ({      ...prev,      notes: prev.notes.map((note) =&gt;        note.id === noteId          ? { ...note, updatedAt }          : note      ),    }));    setNoteSaveInfoById((prev) =&gt; ({      ...prev,      [noteId]: {        status: \"saved\",        lastSavedAt: updatedAt,        error: null,      },    }));  });}, 700);<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u0414\u043e\u00a0<code>result.ok<\/code>\u00a0\u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 saving. \u041e\u0448\u0438\u0431\u043a\u0430 action \u043d\u0435 \u043c\u0435\u043d\u044f\u0435\u0442 updatedAt.<\/p>\n<p>\u0412 autosave-lab \u0441\u0435\u0440\u0432\u0435\u0440 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 updatedAt. \u041a\u043b\u0438\u0435\u043d\u0442 \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u0442 \u0435\u0433\u043e \u043f\u043e\u0441\u043b\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 requestId.<\/p>\n<pre><code class=\"javascript\">if (!result.ok) {  setServerError(result.error);  setIsSaving(false);  return;}setLastSavedAt(result.value.updatedAt);setIsSaving(false);<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<h3>\u041f\u0435\u0440\u0435\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u044f<\/h3>\n<p>Autosave \u043c\u043e\u0436\u0435\u0442 \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c\u0441\u044f \u043e\u0448\u0438\u0431\u043a\u043e\u0439 \u0441\u0435\u0442\u0438, \u0437\u0430\u043f\u0438\u0441\u0438 \u0438\u043b\u0438 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438. \u041b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u0439 draft \u043a \u044d\u0442\u043e\u043c\u0443 \u043c\u043e\u043c\u0435\u043d\u0442\u0443 \u0443\u0436\u0435 \u043e\u0442\u043b\u0438\u0447\u0430\u0435\u0442\u0441\u044f \u043e\u0442 \u0441\u0435\u0440\u0432\u0435\u0440\u043d\u043e\u0439 \u0432\u0435\u0440\u0441\u0438\u0438.<\/p>\n<p>\u0412\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f\u00a0<code>refreshFromServer<\/code>\u00a0\u043f\u043e\u0441\u043b\u0435 \u043e\u0448\u0438\u0431\u043a\u0438 \u0437\u0430\u043f\u0438\u0441\u0438. Store \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u0441\u043e\u0445\u0440\u0430\u043d\u0451\u043d\u043d\u044b\u0435 projects, sections \u0438 notes. \u0421\u0442\u0430\u0442\u0443\u0441 \u043e\u0441\u0442\u0430\u0451\u0442\u0441\u044f error \u0434\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0438\u043b\u0438 \u043d\u043e\u0432\u043e\u0433\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f.<\/p>\n<pre><code class=\"javascript\">if (!result.ok) {  setNoteSaveInfoById((prev) =&gt; ({    ...prev,    [noteId]: {      status: \"error\",      error: result.error,    },  }));  void refreshFromServer();  return;}              <\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u0427\u0435\u0440\u043d\u043e\u0432\u0438\u043a \u0440\u0435\u0434\u0430\u043a\u0442\u043e\u0440\u0430 \u0438 \u0441\u0435\u0440\u0432\u0435\u0440\u043d\u044b\u0439 snapshot \u043f\u043e\u0441\u043b\u0435 \u0442\u0430\u043a\u043e\u0433\u043e \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u0442\u0440\u0435\u0431\u0443\u044e\u0442 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u044f. \u041c\u043e\u0436\u043d\u043e \u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c draft \u0432 \u043f\u043e\u043b\u0435 \u0438 \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c error. \u041c\u043e\u0436\u043d\u043e \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c draft \u0441\u0435\u0440\u0432\u0435\u0440\u043d\u044b\u043c \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u044b\u043c. \u0412\u044b\u0431\u043e\u0440 \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0440\u0435\u0434\u0430\u043a\u0442\u043e\u0440\u0430 \u0438 \u043f\u0440\u0430\u0432\u0438\u043b \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u0442\u0435\u043a\u0441\u0442\u0430. \u0412 \u043f\u0440\u043e\u0435\u043a\u0442\u0435 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u043d\u0442\u0435\u043d\u0442 \u043e\u0441\u0442\u0430\u0451\u0442\u0441\u044f \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b\u043c \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u0432\u0432\u043e\u0434\u0430. \u0421\u0435\u0440\u0432\u0435\u0440\u043d\u044b\u0439 snapshot \u043f\u0435\u0440\u0435\u0447\u0438\u0442\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f store.<\/p>\n<h3>\u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430<\/h3>\n<p>Optimistic insert \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442\u0441\u044f \u0441 \u043e\u0434\u043d\u0438\u043c clientId. \u041a\u043b\u0438\u0435\u043d\u0442 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u043e\u0431\u044a\u0435\u043a\u0442 \u0432 store, action \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442 \u043e\u0431\u044a\u0435\u043a\u0442 \u0441 \u0442\u0435\u043c \u0436\u0435 id, \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441 \u043d\u0435 \u0441\u043e\u0437\u0434\u0430\u0451\u0442 \u0434\u0443\u0431\u043b\u044c. Partial-\u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0439 \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442 \u043e\u0431\u044a\u0435\u043a\u0442 \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043e\u0448\u0438\u0431\u043a\u0443. \u041f\u043e\u0441\u043b\u0435\u00a0<code>refreshFromServer<\/code>\u00a0\u043e\u0431\u044a\u0435\u043a\u0442 \u043e\u0441\u0442\u0430\u0451\u0442\u0441\u044f \u0432 UI. \u041b\u043e\u043a\u0430\u043b\u044c\u043d\u043e\u0435 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0431\u0435\u0437 \u043f\u0435\u0440\u0435\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u043e\u043a\u0430\u0437\u0430\u043b\u043e \u0431\u044b \u0434\u0440\u0443\u0433\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0434\u043e reload.<\/p>\n<p>Autosave \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442\u0441\u044f \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u043c\u0438 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f\u043c\u0438 \u0442\u0435\u043a\u0441\u0442\u0430 \u0441 \u0440\u0430\u0437\u043d\u043e\u0439 \u0437\u0430\u0434\u0435\u0440\u0436\u043a\u043e\u0439 \u043e\u0442\u0432\u0435\u0442\u043e\u0432. \u0421\u0442\u0430\u0440\u044b\u0439 requestId \u043d\u0435 \u043c\u0435\u043d\u044f\u0435\u0442 saved, error \u0438 updatedAt \u043f\u043e\u0441\u043b\u0435 \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u043d\u043e\u0432\u043e\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430. \u041e\u0448\u0438\u0431\u043a\u0430 \u0437\u0430\u043f\u0438\u0441\u0438 \u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u0442\u0430\u0442\u0443\u0441 error. updatedAt \u043e\u0441\u0442\u0430\u0451\u0442\u0441\u044f \u043f\u0440\u0435\u0436\u043d\u0438\u043c. \u0421\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0443\u0441\u043f\u0435\u0448\u043d\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441 \u043c\u0435\u043d\u044f\u0435\u0442 lastSavedAt \u043f\u043e\u0441\u043b\u0435 \u043e\u0442\u0432\u0435\u0442\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0430.<\/p>\n<\/div>\n<p>\u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/1066080\/\">https:\/\/habr.com\/ru\/articles\/1066080\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Optimistic update \u043c\u0435\u043d\u044f\u0435\u0442 \u043a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0434\u043e \u043e\u0442\u0432\u0435\u0442\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0430. Autosave \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 \u043d\u043e\u0432\u0443\u044e \u0432\u0435\u0440\u0441\u0438\u044e \u043f\u043e\u0441\u043b\u0435 \u043f\u0430\u0443\u0437\u044b \u0432\u043e \u0432\u0432\u043e\u0434\u0435. \u0414\u043e \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f action \u0432 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0442 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435, \u0437\u0430\u043f\u0440\u043e\u0441 \u0432 \u0440\u0430\u0431\u043e\u0442\u0435 \u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u044f\u044f \u043f\u043e\u0434\u0442\u0432\u0435\u0440\u0436\u0434\u0451\u043d\u043d\u0430\u044f \u0432\u0435\u0440\u0441\u0438\u044f \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0435.\u041f\u0440\u0438 optimistic create \u043a\u043b\u0438\u0435\u043d\u0442 \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c \u0441 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u043c id, \u0430 \u0441\u0435\u0440\u0432\u0435\u0440 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0434\u0440\u0443\u0433\u043e\u0439 id. \u041e\u0448\u0438\u0431\u043a\u0430 \u043f\u043e\u0441\u043b\u0435 \u0447\u0430\u0441\u0442\u0438\u0447\u043d\u043e\u0439 \u0437\u0430\u043f\u0438\u0441\u0438 \u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0435 \u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u0439 rollback. \u041f\u0440\u0438 autosave \u043e\u0442\u0432\u0435\u0442\u044b \u043f\u0440\u0438\u0445\u043e\u0434\u044f\u0442 \u043d\u0435 \u0432 \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0438. \u0421\u0442\u0430\u0440\u044b\u0439 \u043e\u0442\u0432\u0435\u0442 \u043c\u0435\u043d\u044f\u0435\u0442 \u0441\u0442\u0430\u0442\u0443\u0441 \u0443\u0436\u0435 \u043f\u043e\u0441\u043b\u0435 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u043d\u043e\u0432\u043e\u0439 \u0432\u0435\u0440\u0441\u0438\u0438. updatedAt \u043c\u043e\u0436\u0435\u0442 \u043f\u0435\u0440\u0435\u0439\u0442\u0438 \u0432\u043f\u0435\u0440\u0451\u0434 \u0434\u043e \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u0437\u0430\u043f\u0438\u0441\u0438.\u0412 \u0443\u0447\u0435\u0431\u043d\u043e\u043c \u043f\u0440\u043e\u0435\u043a\u0442\u0435\u00a0Workbench\u00a0optimistic create \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 clientId \u0434\u043b\u044f \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u043e\u0433\u043e id \u043d\u0430 \u043a\u043b\u0438\u0435\u043d\u0442\u0435 \u0438 \u0441\u0435\u0440\u0432\u0435\u0440\u0435. Autosave-\u0434\u0435\u043c\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0451\u0442 requestId \u0432 \u043a\u0430\u0436\u0434\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441. \u0420\u0435\u0434\u0430\u043a\u0442\u043e\u0440 \u0445\u0440\u0430\u043d\u0438\u0442 \u0441\u0442\u0430\u0442\u0443\u0441\u044b saving, saved \u0438 error \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e \u043e\u0442 \u0442\u0435\u043a\u0441\u0442\u0430 \u0437\u0430\u043c\u0435\u0442\u043a\u0438.Optimistic insert\u041a\u043b\u0438\u0435\u043d\u0442 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c \u0432 store \u0434\u043e \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f server action. \u0424\u043e\u0440\u043c\u0430 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 title \u0438 clientId. Optimistic id \u0441\u0442\u0440\u043e\u0438\u0442\u0441\u044f \u0438\u0437 \u0442\u043e\u0433\u043e \u0436\u0435 clientId.const [clientId, setClientId] = useState(() =&gt; crypto.randomUUID());const optimisticId = useMemo(  () =&gt; `i-${clientId}`,  [clientId]);&lt;form  action={formAction}  onSubmit={(event) =&gt; {    const formData = new FormData(event.currentTarget);    const title = String(formData.get(&#171;title&#187;) ?? &#171;&#187;).trim();    if (title.length &lt; 2) return;    startTransition(() =&gt; {      insertItemLocal({        id: optimisticId,        title,        createdAt: new Date().toISOString(),      });    });  }}&gt;  &lt;input type=&#187;hidden&#187; name=&#187;clientId&#187; value={clientId} \/&gt;  &lt;input name=&#187;title&#187; \/&gt;&lt;\/form&gt;Store \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 id\u00a0i-${clientId}\u00a0\u0434\u043e \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f action.Id \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0435Server action \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442 clientId \u0447\u0435\u0440\u0435\u0437 Zod \u0438 \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u0442 id \u043f\u043e \u0442\u043e\u0439 \u0436\u0435 \u0444\u043e\u0440\u043c\u0443\u043b\u0435.const schema = z.object({  title: titleSchema,  clientId: z.string().uuid(),});const parsed = schema.safeParse({  title: formData.get(&#171;title&#187;),  clientId: formData.get(&#171;clientId&#187;),});if (!parsed.success) {  return {    ok: false,    error: null,    fieldErrors: {      title: parsed.error.flatten().fieldErrors.title?.[0],    },  };}const itemId = `i-${parsed.data.clientId}`;\u041a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u0430\u044f \u0437\u0430\u043f\u0438\u0441\u044c \u0438 \u0441\u0435\u0440\u0432\u0435\u0440\u043d\u0430\u044f \u0437\u0430\u043f\u0438\u0441\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \u043e\u0434\u0438\u043d id. \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439 id \u0441 \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0437\u0430\u043c\u0435\u043d\u043e\u0439 \u0437\u0434\u0435\u0441\u044c \u043d\u0435 \u043d\u0443\u0436\u0435\u043d. \u0421\u0441\u044b\u043b\u043a\u0438 \u043d\u0430 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c, \u0432\u044b\u0431\u0440\u0430\u043d\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438 optimistic state \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u044e\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u0442\u0435\u043c \u0436\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \u043f\u043e\u0441\u043b\u0435 \u043e\u0442\u0432\u0435\u0442\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0430.\u041f\u043e\u0432\u0442\u043e\u0440\u043d\u0430\u044f \u0437\u0430\u043f\u0438\u0441\u044c\u041e\u0434\u0438\u043d action \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u0439\u0442\u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e \u043f\u043e\u0441\u043b\u0435 retry, \u0434\u0432\u043e\u0439\u043d\u043e\u0439 \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0438 \u0438\u043b\u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u0433\u043e \u0432\u044b\u0437\u043e\u0432\u0430 \u0444\u043e\u0440\u043c\u044b. \u0421\u0435\u0440\u0432\u0435\u0440 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442 \u043d\u0430\u043b\u0438\u0447\u0438\u0435 id \u043f\u0435\u0440\u0435\u0434 \u0432\u0441\u0442\u0430\u0432\u043a\u043e\u0439.const itemId = `i-${parsed.data.clientId}`;const db = await readWorkbenchDb();if (!db.items.some((item) =&gt; item.id === itemId)) {  db.items.unshift({    id: itemId,    title: parsed.data.title,    createdAt: new Date().toISOString(),  });  await writeWorkbenchDb(db);}\u041f\u043e\u0432\u0442\u043e\u0440\u043d\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441 \u0441 \u0442\u0435\u043c \u0436\u0435 clientId \u043d\u0435 \u0441\u043e\u0437\u0434\u0430\u0451\u0442 \u0432\u0442\u043e\u0440\u043e\u0439 \u043e\u0431\u044a\u0435\u043a\u0442.\u041f\u043e\u0441\u043b\u0435 \u0443\u0441\u043f\u0435\u0448\u043d\u043e\u0433\u043e \u043e\u0442\u0432\u0435\u0442\u0430 \u0444\u043e\u0440\u043c\u0430 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 clientId. \u0421\u043b\u0435\u0434\u0443\u044e\u0449\u0430\u044f optimistic-\u0432\u0441\u0442\u0430\u0432\u043a\u0430 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u043d\u043e\u0432\u044b\u0439 id.useEffect(() =&gt; {  if (!state.ok) return;  startTransition(() =&gt; {    setClientId(crypto.randomUUID());  });}, [state]);\u041e\u0448\u0438\u0431\u043a\u0430 \u043f\u043e\u0441\u043b\u0435 \u0437\u0430\u043f\u0438\u0441\u0438\u0421\u0435\u0440\u0432\u0435\u0440 \u043c\u043e\u0436\u0435\u0442 \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u043e\u0448\u0438\u0431\u043a\u0443 \u043f\u043e\u0437\u0436\u0435. \u0421\u0431\u043e\u0439 \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u043f\u043e\u0441\u043b\u0435 \u0437\u0430\u043f\u0438\u0441\u0438 \u0444\u0430\u0439\u043b\u0430 \u0438\u043b\u0438 \u0431\u0430\u0437\u044b, \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438, revalidate \u0438\u043b\u0438 \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043a\u0438 \u043e\u0442\u0432\u0435\u0442\u0430. \u041a\u043b\u0438\u0435\u043d\u0442 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 error, \u0445\u043e\u0442\u044f \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c \u0443\u0436\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0435. \u041b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u0439 rollback \u0432 \u0442\u0430\u043a\u043e\u043c \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0438 \u0443\u0434\u0430\u043b\u0438\u0442 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c \u0438\u0437 store. \u041f\u043e\u0441\u043b\u0435 reload \u043e\u043d\u0430 \u043f\u043e\u044f\u0432\u0438\u0442\u0441\u044f \u0441\u043d\u043e\u0432\u0430.\u0412 optimistic-lab \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d partial-\u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0439. Action \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442 \u043e\u0431\u044a\u0435\u043a\u0442 \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043e\u0448\u0438\u0431\u043a\u0443 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438. \u041a\u043b\u0438\u0435\u043d\u0442 \u043f\u0435\u0440\u0435\u0447\u0438\u0442\u044b\u0432\u0430\u0435\u0442 \u0441\u0435\u0440\u0432\u0435\u0440\u043d\u044b\u0439 snapshot.if (!state.ok &amp;&amp; (state.error || state.fieldErrors?.title)) {  if (hadOptimisticInsertRef.current) {    hadOptimisticInsertRef.current = false;    void refreshFromServer();    startTransition(() =&gt; {      setClientId(crypto.randomUUID());    });  }}refreshFromServer\u00a0\u0437\u0430\u043c\u0435\u043d\u044f\u0435\u0442 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u0439 store \u0434\u0430\u043d\u043d\u044b\u043c\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u0435\u0440\u0432\u0435\u0440 \u0432\u0435\u0440\u043d\u0443\u043b \u043f\u043e\u0441\u043b\u0435 \u0437\u0430\u043f\u0438\u0441\u0438. \u0414\u043b\u044f \u043e\u0448\u0438\u0431\u043e\u043a, \u0432\u043e\u0437\u043d\u0438\u043a\u0448\u0438\u0445 \u0434\u043e \u0437\u0430\u043f\u0438\u0441\u0438, \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u0439 rollback \u043e\u0441\u0442\u0430\u0451\u0442\u0441\u044f \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u044b\u043c. \u0414\u043b\u044f \u043d\u0435\u0438\u0437\u0432\u0435\u0441\u0442\u043d\u043e\u0433\u043e \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u043f\u043e\u0441\u043b\u0435 action \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043f\u0435\u0440\u0435\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u044f.Autosave \u0438 debounceDebounce \u0443\u0434\u0430\u043b\u044f\u0435\u0442 \u0442\u0430\u0439\u043c\u0435\u0440, \u0435\u0441\u043b\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u043b \u0432\u0432\u043e\u0434 \u0434\u043e \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u0430. \u0423\u0436\u0435 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043d\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441 debounce \u043d\u0435 \u043e\u0442\u043c\u0435\u043d\u044f\u0435\u0442. \u041f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u043c\u0435\u043d\u044f\u0435\u0442 \u0442\u0435\u043a\u0441\u0442, \u0437\u0430\u043f\u0440\u043e\u0441 A \u0443\u0445\u043e\u0434\u0438\u0442 \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440. \u0421\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442 \u0437\u0430\u043f\u0440\u043e\u0441 B. \u0421\u0435\u0440\u0432\u0435\u0440 \u043c\u043e\u0436\u0435\u0442 \u0432\u0435\u0440\u043d\u0443\u0442\u044c B \u0440\u0430\u043d\u044c\u0448\u0435 A. \u041e\u0431\u0430 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u0441\u044f, \u0435\u0441\u043b\u0438 \u043a\u043b\u0438\u0435\u043d\u0442 \u043d\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442 \u043f\u0440\u0438\u043d\u0430\u0434\u043b\u0435\u0436\u043d\u043e\u0441\u0442\u044c \u043e\u0442\u0432\u0435\u0442\u0430 \u043a \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u043c\u0443 \u0437\u0430\u043f\u0440\u043e\u0441\u0443.\u0412 autosave-lab \u043a\u043b\u0438\u0435\u043d\u0442 \u0445\u0440\u0430\u043d\u0438\u0442 requestId \u0430\u043a\u0442\u0438\u0432\u043d\u043e\u0433\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f.const activeRequestIdRef = useRef&lt;string | null&gt;(null);const timerRef = useRef&lt;number | null&gt;(null);useEffect(() =&gt; {  if (timerRef.current) {    window.clearTimeout(timerRef.current);  }  timerRef.current = window.setTimeout(() =&gt; {    const requestId = crypto.randomUUID();    activeRequestIdRef.current = requestId;    setIsSaving(true);    setServerError(null);    void saveAutosaveLabAction({      text: draft,      requestId,    }).then((result) =&gt; {      \/\/ \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043e\u0442\u0432\u0435\u0442\u0430    });  }, 700);  return () =&gt; {    if (timerRef.current) {      window.clearTimeout(timerRef.current);    }  };}, [draft]);\u041d\u043e\u0432\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441 \u0437\u0430\u043c\u0435\u043d\u044f\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0432\u00a0activeRequestIdRef.RequestId \u0432 \u043e\u0442\u0432\u0435\u0442\u0435Server action \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 requestId \u0438 \u043f\u0440\u0438 \u0443\u0441\u043f\u0435\u0445\u0435 \u0438 \u043f\u0440\u0438 \u043e\u0448\u0438\u0431\u043a\u0435.export type AutosaveLabResult =  | {      ok: true;      value: {        updatedAt: string;        requestId: string;      };    }  | {      ok: false;      error: string;      requestId: string;    };\u041a\u043b\u0438\u0435\u043d\u0442 \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0435\u0442 id \u043e\u0442\u0432\u0435\u0442\u0430 \u0441 id \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430.const responseRequestId = result.ok  ? result.value.requestId  : result.requestId;if (activeRequestIdRef.current !== responseRequestId) {  return;}\u041e\u0442\u0432\u0435\u0442 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 A \u043d\u0435 \u043c\u0435\u043d\u044f\u0435\u0442 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u043f\u043e\u0441\u043b\u0435 \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 B. \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0441\u044f \u0438 \u043a \u043e\u0448\u0438\u0431\u043a\u0430\u043c. \u0421\u0442\u0430\u0440\u0430\u044f \u043e\u0448\u0438\u0431\u043a\u0430 \u043d\u0435 \u0434\u043e\u043b\u0436\u043d\u0430 \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0438\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0443\u044e \u0444\u043e\u0440\u043c\u0443 \u0432 error, \u0435\u0441\u043b\u0438 \u0431\u043e\u043b\u0435\u0435 \u043d\u043e\u0432\u0430\u044f \u0432\u0435\u0440\u0441\u0438\u044f \u0443\u0436\u0435 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0430.\u0421\u0442\u0430\u0442\u0443\u0441\u044b \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f\u0422\u0435\u043a\u0441\u0442 \u0437\u0430\u043c\u0435\u0442\u043a\u0438 \u0438 \u0441\u0442\u0430\u0442\u0443\u0441 \u0437\u0430\u043f\u0438\u0441\u0438 \u0445\u0440\u0430\u043d\u044f\u0442\u0441\u044f \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e. \u0418\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0440\u0435\u0434\u0430\u043a\u0442\u043e\u0440\u0430 \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0435\u0442 draft, \u0437\u0430\u043f\u0443\u0441\u043a action \u0441\u0442\u0430\u0432\u0438\u0442 saving. \u0423\u0441\u043f\u0435\u0448\u043d\u044b\u0439 \u043e\u0442\u0432\u0435\u0442 \u0430\u043a\u0442\u0438\u0432\u043d\u043e\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0441\u0442\u0430\u0432\u0438\u0442 saved, \u043e\u0448\u0438\u0431\u043a\u0430 \u0430\u043a\u0442\u0438\u0432\u043d\u043e\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0441\u0442\u0430\u0432\u0438\u0442 error.type NoteSaveInfo =  | {      status: &#171;idle&#187;;      error: null;      lastSavedAt?: string;    }  | {      status: &#171;saving&#187;;      error: null;      lastSavedAt?: string;    }  | {      status: &#171;saved&#187;;      error: null;      lastSavedAt: string;    }  | {      status: &#171;error&#187;;      error: string;      lastSavedAt?: string;    };\u0421\u0442\u0430\u0442\u0443\u0441 \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442 \u0441\u0435\u0440\u0432\u0435\u0440\u043d\u0443\u044e \u0437\u0430\u043f\u0438\u0441\u044c. \u0422\u0435\u043a\u0441\u0442 \u0432 \u0440\u0435\u0434\u0430\u043a\u0442\u043e\u0440\u0435 \u043c\u043e\u0436\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0431\u043e\u043b\u0435\u0435 \u043d\u043e\u0432\u0443\u044e \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u0443\u044e \u0432\u0435\u0440\u0441\u0438\u044e.updatedAt \u043f\u043e\u0441\u043b\u0435 \u043e\u0442\u0432\u0435\u0442\u0430\u0420\u0435\u0434\u0430\u043a\u0442\u043e\u0440 \u043c\u0435\u043d\u044f\u0435\u0442 contentHtml \u0432 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e\u043c store \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u0432\u0432\u043e\u0434\u0430. updatedAt \u0438 lastSavedAt \u043c\u0435\u043d\u044f\u044e\u0442\u0441\u044f \u043f\u043e\u0441\u043b\u0435 \u0443\u0441\u043f\u0435\u0448\u043d\u043e\u0433\u043e \u043e\u0442\u0432\u0435\u0442\u0430 action.setNoteSaveInfoById((prev) =&gt; ({  &#8230;prev,  [noteId]: {    status: &#171;saving&#187;,    error: null,  },}));const timer = window.setTimeout(() =&gt; {  const updatedAt = new Date().toISOString();  void updateNoteContentAction({    noteId,    contentHtml: nextHtml,    updatedAt,  }).then((result) =&gt; {    if (!result.ok) {      setNoteSaveInfoById((prev) =&gt; ({        &#8230;prev,        [noteId]: {          status: &#171;error&#187;,          error: result.error,        },      }));      void refreshFromServer();      return;    }    setDb((prev) =&gt; ({      &#8230;prev,      notes: prev.notes.map((note) =&gt;        note.id === noteId          ? { &#8230;note, updatedAt }          : note      ),    }));    setNoteSaveInfoById((prev) =&gt; ({      &#8230;prev,      [noteId]: {        status: &#171;saved&#187;,        lastSavedAt: updatedAt,        error: null,      },    }));  });}, 700);\u0414\u043e\u00a0result.ok\u00a0\u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 saving. \u041e\u0448\u0438\u0431\u043a\u0430 action \u043d\u0435 \u043c\u0435\u043d\u044f\u0435\u0442 updatedAt.\u0412 autosave-lab \u0441\u0435\u0440\u0432\u0435\u0440 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 updatedAt. \u041a\u043b\u0438\u0435\u043d\u0442 \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u0442 \u0435\u0433\u043e \u043f\u043e\u0441\u043b\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 requestId.if (!result.ok) {  setServerError(result.error);  setIsSaving(false);  return;}setLastSavedAt(result.value.updatedAt);setIsSaving(false);\u041f\u0435\u0440\u0435\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u044fAutosave \u043c\u043e\u0436\u0435\u0442 \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c\u0441\u044f \u043e\u0448\u0438\u0431\u043a\u043e\u0439 \u0441\u0435\u0442\u0438, \u0437\u0430\u043f\u0438\u0441\u0438 \u0438\u043b\u0438 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438. \u041b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u0439 draft \u043a \u044d\u0442\u043e\u043c\u0443 \u043c\u043e\u043c\u0435\u043d\u0442\u0443 \u0443\u0436\u0435 \u043e\u0442\u043b\u0438\u0447\u0430\u0435\u0442\u0441\u044f \u043e\u0442 \u0441\u0435\u0440\u0432\u0435\u0440\u043d\u043e\u0439 \u0432\u0435\u0440\u0441\u0438\u0438.\u0412\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f\u00a0refreshFromServer\u00a0\u043f\u043e\u0441\u043b\u0435 \u043e\u0448\u0438\u0431\u043a\u0438 \u0437\u0430\u043f\u0438\u0441\u0438. Store \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u0441\u043e\u0445\u0440\u0430\u043d\u0451\u043d\u043d\u044b\u0435 projects, sections \u0438 notes. \u0421\u0442\u0430\u0442\u0443\u0441 \u043e\u0441\u0442\u0430\u0451\u0442\u0441\u044f error \u0434\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0438\u043b\u0438 \u043d\u043e\u0432\u043e\u0433\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f.if (!result.ok) {  setNoteSaveInfoById((prev) =&gt; ({    &#8230;prev,    [noteId]: {      status: &#171;error&#187;,      error: result.error,    },  }));  void refreshFromServer();  return;}              \u0427\u0435\u0440\u043d\u043e\u0432\u0438\u043a \u0440\u0435\u0434\u0430\u043a\u0442\u043e\u0440\u0430 \u0438 \u0441\u0435\u0440\u0432\u0435\u0440\u043d\u044b\u0439 snapshot \u043f\u043e\u0441\u043b\u0435 \u0442\u0430\u043a\u043e\u0433\u043e \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u0442\u0440\u0435\u0431\u0443\u044e\u0442 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u044f. \u041c\u043e\u0436\u043d\u043e \u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c draft \u0432 \u043f\u043e\u043b\u0435 \u0438 \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c error. \u041c\u043e\u0436\u043d\u043e \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c draft \u0441\u0435\u0440\u0432\u0435\u0440\u043d\u044b\u043c \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u044b\u043c. \u0412\u044b\u0431\u043e\u0440 \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0440\u0435\u0434\u0430\u043a\u0442\u043e\u0440\u0430 \u0438 \u043f\u0440\u0430\u0432\u0438\u043b \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u0442\u0435\u043a\u0441\u0442\u0430. \u0412 \u043f\u0440\u043e\u0435\u043a\u0442\u0435 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u043d\u0442\u0435\u043d\u0442 \u043e\u0441\u0442\u0430\u0451\u0442\u0441\u044f \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b\u043c \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u0432\u0432\u043e\u0434\u0430. \u0421\u0435\u0440\u0432\u0435\u0440\u043d\u044b\u0439 snapshot \u043f\u0435\u0440\u0435\u0447\u0438\u0442\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f store.\u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430Optimistic insert \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442\u0441\u044f \u0441 \u043e\u0434\u043d\u0438\u043c clientId. \u041a\u043b\u0438\u0435\u043d\u0442 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u043e\u0431\u044a\u0435\u043a\u0442 \u0432 store, action \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442 \u043e\u0431\u044a\u0435\u043a\u0442 \u0441 \u0442\u0435\u043c \u0436\u0435 id, \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441 \u043d\u0435 \u0441\u043e\u0437\u0434\u0430\u0451\u0442 \u0434\u0443\u0431\u043b\u044c. Partial-\u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0439 \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442 \u043e\u0431\u044a\u0435\u043a\u0442 \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043e\u0448\u0438\u0431\u043a\u0443. \u041f\u043e\u0441\u043b\u0435\u00a0refreshFromServer\u00a0\u043e\u0431\u044a\u0435\u043a\u0442 \u043e\u0441\u0442\u0430\u0451\u0442\u0441\u044f \u0432 UI. \u041b\u043e\u043a\u0430\u043b\u044c\u043d\u043e\u0435 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0431\u0435\u0437 \u043f\u0435\u0440\u0435\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u043e\u043a\u0430\u0437\u0430\u043b\u043e \u0431\u044b \u0434\u0440\u0443\u0433\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0434\u043e reload.Autosave \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442\u0441\u044f \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u043c\u0438 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f\u043c\u0438 \u0442\u0435\u043a\u0441\u0442\u0430 \u0441 \u0440\u0430\u0437\u043d\u043e\u0439 \u0437\u0430\u0434\u0435\u0440\u0436\u043a\u043e\u0439 \u043e\u0442\u0432\u0435\u0442\u043e\u0432. \u0421\u0442\u0430\u0440\u044b\u0439 requestId \u043d\u0435 \u043c\u0435\u043d\u044f\u0435\u0442 saved, error \u0438 updatedAt \u043f\u043e\u0441\u043b\u0435 \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u043d\u043e\u0432\u043e\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430. \u041e\u0448\u0438\u0431\u043a\u0430 \u0437\u0430\u043f\u0438\u0441\u0438 \u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u0442\u0430\u0442\u0443\u0441 error. updatedAt \u043e\u0441\u0442\u0430\u0451\u0442\u0441\u044f \u043f\u0440\u0435\u0436\u043d\u0438\u043c. \u0421\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0443\u0441\u043f\u0435\u0448\u043d\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441 \u043c\u0435\u043d\u044f\u0435\u0442 lastSavedAt \u043f\u043e\u0441\u043b\u0435 \u043e\u0442\u0432\u0435\u0442\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0430.\u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 https:\/\/habr.com\/ru\/articles\/1066080\/<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-489558","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/489558","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=489558"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/489558\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=489558"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=489558"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=489558"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}