code: def invoke_with_curdoc(doc: Document, f: Callable[[], None]) -> None:
      from ..io.doc import patch_curdoc
  
      curdoc: Document | UnlockedDocumentProxy = (
          UnlockedDocumentProxy(doc)
          if getattr(f, "nolock", False)
          else doc
      )
  
      with patch_curdoc(curdoc):
          return f()
  
imports: from __future__ import annotations
  import logging
  import weakref
  from collections import defaultdict
  from functools import wraps
  from typing import TYPE_CHECKING, Any, Callable
  from ..core.enums import HoldPolicy, HoldPolicyType
  from ..events import _CONCRETE_EVENT_CLASSES, DocumentEvent, Event, ModelEvent
  from ..model import Model
  from ..models.callbacks import Callback
  from ..util.callback_manager import _check_callback
  from .events import DocumentPatchedEvent, ModelChangedEvent, RootAddedEvent, RootRemovedEvent, SessionCallbackAdded, SessionCallbackRemoved, TitleChangedEvent
  from .locking import UnlockedDocumentProxy
label_desc: Use image node_id 1 for calling a global function with example usage: invoke_with_curdoc(doc, f) and returns: f