Skip to content

Commit 179240a

Browse files
committed
One link transaction for any CurrentValue[...] = value
Hidden behind a boolean gatekeeper, Wolfram`Chatbook`FrontEnd`$efficientCurrentValue True by default
1 parent e990bbe commit 179240a

File tree

18 files changed

+129
-119
lines changed

18 files changed

+129
-119
lines changed

Source/Chatbook/Actions.wl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ disableAssistance[ cell_CellObject ] := (
304304
);
305305

306306
disableAssistance[ nbo_NotebookObject ] := (
307-
CurrentValue[ nbo, { TaggingRules, "ChatNotebookSettings", "Assistance" } ] = False
307+
setCurrentValue[ nbo, { TaggingRules, "ChatNotebookSettings", "Assistance" }, False ]
308308
);
309309

310310
disableAssistance // endDefinition;
@@ -424,16 +424,16 @@ rotateTabPage // endDefinition;
424424
writePageContent // beginDefinition;
425425

426426
writePageContent[ cell_CellObject, newPage_Integer, content: TextData[ $$textData ] ] /; $cloudNotebooks := (
427-
CurrentValue[ cell, { TaggingRules, "PageData", "CurrentPage" } ] = newPage;
428-
CurrentValue[ cell, TaggingRules ] = GeneralUtilities`ToAssociations @ CurrentValue[ cell, TaggingRules ];
427+
setCurrentValue[ cell, { TaggingRules, "PageData", "CurrentPage" }, newPage ];
428+
setCurrentValue[ cell, TaggingRules, GeneralUtilities`ToAssociations @ CurrentValue[ cell, TaggingRules ] ];
429429
NotebookWrite[ cell, ReplacePart[ NotebookRead @ cell, 1 -> content ] ];
430430
)
431431

432432
writePageContent[ cell_CellObject, newPage_Integer, content: TextData[ $$textData ] ] := (
433433
SelectionMove[ cell, All, CellContents, AutoScroll -> False ];
434434
NotebookWrite[ parentNotebook @ cell, content, None, AutoScroll -> False ];
435435
SelectionMove[ cell, After, Cell, AutoScroll -> False ];
436-
CurrentValue[ cell, { TaggingRules, "PageData", "CurrentPage" } ] = newPage;
436+
setCurrentValue[ cell, { TaggingRules, "PageData", "CurrentPage" }, newPage ];
437437
SetOptions[ cell, CellAutoOverwrite -> True, GeneratedCell -> True ]
438438
);
439439

@@ -1120,13 +1120,13 @@ setChatSectionSettings // beginDefinition;
11201120

11211121
setChatSectionSettings[ cell_CellObject, settings_Association ] :=
11221122
Module[ { cellSettings, nbSettings, newSettings },
1123-
KeyValueMap[ Function[ CurrentValue[ cell, { TaggingRules, "ChatNotebookSettings", #1 } ] = #2 ], settings ];
1123+
KeyValueMap[ Function[ setCurrentValue[ cell, { TaggingRules, "ChatNotebookSettings", #1 }, #2 ] ], settings ];
11241124

11251125
(* The rest of this is a workaround for bug 435058 *)
11261126
cellSettings = CurrentValue[ cell, { TaggingRules, "ChatNotebookSettings" } ];
11271127
nbSettings = CurrentValue[ parentNotebook @ cell, { TaggingRules, "ChatNotebookSettings" } ];
11281128
newSettings = Complement[ cellSettings, nbSettings ];
1129-
CurrentValue[ cell, { TaggingRules, "ChatNotebookSettings" } ] = newSettings;
1129+
setCurrentValue[ cell, { TaggingRules, "ChatNotebookSettings" }, newSettings ];
11301130
];
11311131

11321132
setChatSectionSettings // endDefinition;
@@ -1228,7 +1228,7 @@ ExclusionToggle[ nbo_NotebookObject, cellObjects: { __CellObject } ] :=
12281228
cells = ConfirmMatch[ NotebookRead @ cellObjects, { __Cell } ];
12291229
excluded = MatchQ[ cells, { Cell[ __, "ChatExcluded", ___ ], ___ } ];
12301230
toggled = ConfirmMatch[ If[ excluded, includeChatCells @ cells, excludeChatCells @ cells ], { __Cell } ];
1231-
CurrentValue[ cellObjects, Deletable ] = True;
1231+
setCurrentValue[ cellObjects, Deletable, True ];
12321232
NotebookDelete @ cellObjects;
12331233
NotebookWrite[ nbo, toggled, All ]
12341234
];
@@ -1260,7 +1260,7 @@ WidgetSend // beginDefinition;
12601260
WidgetSend[ cell_CellObject ] := withChatState @
12611261
Block[ { $alwaysOpen = True, cellPrint = cellPrintAfter @ cell, $finalCell = cell, $AutomaticAssistance = True },
12621262
(* TODO: this is currently the only UI method to turn this back on *)
1263-
CurrentValue[ parentNotebook @ cell, { TaggingRules, "ChatNotebookSettings", "Assistance" } ] = True;
1263+
setCurrentValue[ parentNotebook @ cell, { TaggingRules, "ChatNotebookSettings", "Assistance" }, True ];
12641264
SendChat @ cell
12651265
];
12661266

Source/Chatbook/ChatModes/Evaluate.wl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ evaluateWorkspaceChat[ nbo_NotebookObject, Dynamic[ input: _Symbol|_CurrentValue
4747
NotebookWrite[ nbo, cell ];
4848
moveChatInputToBottom @ nbo;
4949
cellObject = ConfirmMatch[ First[ Cells[ nbo, CellTags -> uuid ], $Failed ], _CellObject, "CellObject" ];
50-
CurrentValue[ cellObject, CellTags ] = { };
50+
setCurrentValue[ cellObject, CellTags, { } ];
5151
ConfirmMatch[ ChatCellEvaluate[ cellObject, nbo ], _ChatObject|Null, "ChatCellEvaluate" ]
5252
],
5353
throwInternalFailure
@@ -97,7 +97,7 @@ evaluateSidebarChat[ nbo_NotebookObject, sidebarCell_CellObject, input_, Dynamic
9797
];
9898

9999
cellObject = ConfirmMatch[ Last[ Cells[ scrollablePaneCell, CellTags -> uuid ], $Failed ], _CellObject, "SidebarChatInputCellObject" ];
100-
CurrentValue[ cellObject, CellTags ] = "SidebarTopCell";
100+
setCurrentValue[ cellObject, CellTags, "SidebarTopCell" ];
101101
ConfirmMatch[ ChatCellEvaluate[ cellObject, nbo ], _ChatObject|Null, "ChatCellEvaluate" ]
102102
],
103103
throwInternalFailure

Source/Chatbook/ChatModes/NotebookFocus.wl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ focusedNotebookDisplay[ chatNB_, appContainer_ ] := Enclose[
6666
focused = FirstCase[ info, KeyValuePattern[ "Focused" -> True ], current ];
6767

6868
(* focusedNotebookDisplay is dynamically updated so this TaggingRules value should be kept current *)
69-
CurrentValue[ chatNB, { TaggingRules, "FocusWindowTitle" } ] = Lookup[ focused, "WindowTitle", None ];
69+
setCurrentValue[ chatNB, { TaggingRules, "FocusWindowTitle" }, Lookup[ focused, "WindowTitle", None ] ];
7070

7171
label = If[ MatchQ[ appContainer, _CellObject ],
7272
Grid[

Source/Chatbook/ChatModes/UI.wl

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ sidebarNewChatButton[ nbo_NotebookObject, sidebarCell_CellObject ] :=
306306
removeSidebarScrollingCellContent[ nbo, sidebarCell ];
307307
removeSidebarChatSubDockedCell[ nbo, sidebarCell ];
308308
CurrentChatSettings[ sidebarCell, "ConversationUUID" ] = CreateUUID[ ];
309-
CurrentValue[ sidebarCell, { TaggingRules, "ConversationTitle" } ] = ""
309+
setCurrentValue[ sidebarCell, { TaggingRules, "ConversationTitle" }, "" ]
310310
,
311311
Appearance -> "Suppressed",
312312
Method -> "Queued"
@@ -347,7 +347,7 @@ sidebarOpenAsAssistantWindowButton[ nbo_NotebookObject, sidebarCell_CellObject ]
347347
removeSidebarScrollingCellContent[ nbo, sidebarCell ];
348348
removeSidebarChatSubDockedCell[ nbo, sidebarCell ];
349349
CurrentChatSettings[ sidebarCell, "ConversationUUID" ] = CreateUUID[ ];
350-
CurrentValue[ sidebarCell, { TaggingRules, "ConversationTitle" } ] = "";
350+
setCurrentValue[ sidebarCell, { TaggingRules, "ConversationTitle" }, "" ];
351351
FrontEndTokenExecute[ nbo, "HideSidebar" ];
352352
]
353353
],
@@ -381,7 +381,7 @@ sidebarHideButton[ nbo_NotebookObject ] := Button[
381381
],
382382
tr @ "SidebarToolbarButtonTooltipHideSidebar"
383383
],
384-
CurrentValue[ $FrontEndSession, "ShowNotebookAssistant" ] = False;
384+
setCurrentValue[ $FrontEndSession, "ShowNotebookAssistant", False ];
385385
FrontEndTokenExecute[nbo, "HideSidebar"],
386386
Appearance -> "Suppressed"
387387
]
@@ -452,12 +452,12 @@ makeWorkspaceChatSubDockedCellExpression // endDefinition;
452452
writeWorkspaceChatSubDockedCell // beginDefinition;
453453

454454
writeWorkspaceChatSubDockedCell[ nbo_NotebookObject, content_ ] := (
455-
CurrentValue[ nbo, DockedCells ] = Inherited;
456-
CurrentValue[ nbo, DockedCells ] = {
455+
setCurrentValue[ nbo, DockedCells, Inherited ];
456+
setCurrentValue[ nbo, DockedCells, {
457457
First @ Replace[ AbsoluteCurrentValue[ nbo, DockedCells ], c_Cell :> {c} ],
458-
makeWorkspaceChatSubDockedCellExpression[ content ] };
459-
(* Rewriting docked cells seems to steal focus from the chat input field, so restore it here: *)
460-
If[ SelectedCells @ nbo === { }, moveToChatInputField[ nbo, True ] ]
458+
makeWorkspaceChatSubDockedCellExpression[ content ] } ];
459+
(* Rewriting docked cells seems to steal focus from the chat input field, so restore it here: *)
460+
If[ SelectedCells @ nbo === { }, moveToChatInputField[ nbo, True ] ]
461461
)
462462

463463
writeWorkspaceChatSubDockedCell[ nbo_NotebookObject, WindowTitle ] := writeWorkspaceChatSubDockedCell[
@@ -480,7 +480,7 @@ writeWorkspaceChatSubDockedCell // endDefinition;
480480
(*removeWorkspaceChatSubDockedCell*)
481481
removeWorkspaceChatSubDockedCell // beginDefinition;
482482

483-
removeWorkspaceChatSubDockedCell[ nbo_NotebookObject ] := CurrentValue[ nbo, DockedCells ] = Inherited;
483+
removeWorkspaceChatSubDockedCell[ nbo_NotebookObject ] := setCurrentValue[ nbo, DockedCells, Inherited ];
484484

485485
removeWorkspaceChatSubDockedCell // endDefinition;
486486

@@ -523,7 +523,7 @@ newChatButton[ Dynamic[ nbo_ ] ] :=
523523
NotebookDelete @ Cells @ nbo;
524524
removeWorkspaceChatSubDockedCell @ nbo;
525525
CurrentChatSettings[ nbo, "ConversationUUID" ] = CreateUUID[ ];
526-
CurrentValue[ nbo, { TaggingRules, "ConversationTitle" } ] = "";
526+
setCurrentValue[ nbo, { TaggingRules, "ConversationTitle" }, "" ];
527527
moveChatInputToTop @ nbo;
528528
,
529529
Appearance -> "Suppressed",
@@ -677,7 +677,7 @@ makeSidebarChatInputCell[ initialContent_ ] := Cell[
677677
];
678678
(* spooky action at a distance: regenerating a side bar ChatOutput cell *)
679679
If[ cellTaggedQ[ thisCell, "RegenerateChatOutput" ],
680-
chatEvalCell = CurrentValue[ sidebarCell, { TaggingRules, "ChatEvaluationCell" } ];
680+
chatEvalCell = CurrentValue[ sidebarCell, { TaggingRules, "ChatEvaluationCell" } ]; (* get the CellObject stored in the TaggingRules *)
681681
With[ { sbc = sidebarCell }, (* "Set" is HoldFirst so we must inject values *)
682682
FrontEndExecute[ {
683683
FrontEnd`SetOptions[ thisCell, CellTags -> "SidebarChatInputCell" ],
@@ -688,7 +688,7 @@ makeSidebarChatInputCell[ initialContent_ ] := Cell[
688688
]
689689
,
690690
SynchronousUpdating -> False,
691-
TrackedSymbols :> { returnKeyDownQ }
691+
TrackedSymbols :> { returnKeyDownQ } (* changes to TaggingRules are automatically tracked *)
692692
],
693693
(* no need to templatize an attached cell as it is ephemeral *)
694694
PaneSelector[
@@ -2138,16 +2138,15 @@ loadingOverlay // endDefinition;
21382138
(* ::Subsection::Closed:: *)
21392139
(*restoreVerticalScrollbar*)
21402140
restoreVerticalScrollbar // beginDefinition;
2141-
restoreVerticalScrollbar[ nbo_NotebookObject ] := CurrentValue[ nbo, WindowElements ] = Inherited;
2141+
restoreVerticalScrollbar[ nbo_NotebookObject ] := setCurrentValue[ nbo, WindowElements, Inherited ];
21422142
restoreVerticalScrollbar // endDefinition;
21432143

21442144
(* ::**************************************************************************************************************:: *)
21452145
(* ::Subsection::Closed:: *)
21462146
(*hideVerticalScrollbar*)
21472147
hideVerticalScrollbar // beginDefinition;
21482148

2149-
hideVerticalScrollbar[ nbo_NotebookObject ] := CurrentValue[ nbo, WindowElements ] =
2150-
DeleteCases[ AbsoluteCurrentValue[ nbo, WindowElements ], "VerticalScrollBar" ];
2149+
hideVerticalScrollbar[ nbo_NotebookObject ] := setCurrentValue[ nbo, WindowElements, DeleteCases[ AbsoluteCurrentValue[ nbo, WindowElements ], "VerticalScrollBar" ] ];
21512150

21522151
hideVerticalScrollbar // endDefinition;
21532152

@@ -2713,16 +2712,16 @@ loadConversation[ nbo_NotebookObject, None, id_ ] := Enclose[
27132712
NotebookDelete @ First[ Cells[ nbo, AttachedCell -> True, CellStyle -> "ChatInputField" ], $Failed ];
27142713

27152714
WithCleanup[
2716-
CurrentValue[ nbo, Selectable ] = True,
2715+
setCurrentValue[ nbo, Selectable, True ],
27172716
SelectionMove[ nbo, Before, Notebook, AutoScroll -> True ];
27182717
ConfirmMatch[ NotebookWrite[ nbo, cells, AutoScroll -> False ], Null, "Write" ];
27192718
If[ Cells @ nbo === { }, NotebookWrite[ nbo, cells, AutoScroll -> False ] ],
2720-
CurrentValue[ nbo, Selectable ] = Inherited
2719+
setCurrentValue[ nbo, Selectable, Inherited ]
27212720
];
27222721

27232722
ChatbookAction[ "AttachWorkspaceChatInput", nbo ];
27242723
CurrentChatSettings[ nbo, "ConversationUUID" ] = uuid;
2725-
CurrentValue[ nbo, { TaggingRules, "ConversationTitle" } ] = title;
2724+
setCurrentValue[ nbo, { TaggingRules, "ConversationTitle" }, title ];
27262725
writeWorkspaceChatSubDockedCell[ nbo, WindowTitle ];
27272726
restoreVerticalScrollbar @ nbo;
27282727
moveToChatInputField[ nbo, True ]
@@ -2750,7 +2749,7 @@ loadConversation[ nbo_NotebookObject, sidebarCell_CellObject, id_ ] := Enclose[
27502749
];
27512750

27522751
CurrentChatSettings[ sidebarCell, "ConversationUUID" ] = uuid;
2753-
CurrentValue[ sidebarCell, { TaggingRules, "ConversationTitle" } ] = title;
2752+
setCurrentValue[ sidebarCell, { TaggingRules, "ConversationTitle" }, title ];
27542753
writeSidebarChatSubDockedCell[ nbo, sidebarCell, WindowTitle ];
27552754
(* TODO: moveToChatInputField[ nbo, True ] *)
27562755
] // withLoadingOverlay[ { nbo, sidebarCell } ],

Source/Chatbook/CloudToolbar.wl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,7 @@ toggleCloudPreferences // beginDefinition;
141141
(* These cells are added to the bottom of the stack of DockedCells *)
142142
toggleCloudPreferences[ nbo_NotebookObject ] :=
143143
(
144-
CurrentValue[ nbo, DockedCells ] =
145-
Replace[
144+
setCurrentValue[ nbo, DockedCells, Replace[
146145
CurrentValue[ nbo, DockedCells ],
147146
{
148147
(* ============ Remove if it exists ============ *)
@@ -164,7 +163,8 @@ toggleCloudPreferences[ nbo_NotebookObject ] :=
164163
$cloudPreferencesCell
165164
}
166165
}
167-
];
166+
]
167+
];
168168
)
169169

170170
toggleCloudPreferences // endDefinition;

Source/Chatbook/CommonSymbols.wl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@ BeginPackage[ "Wolfram`Chatbook`Common`" ];
331331
`serviceIcon;
332332
`serviceName;
333333
`setCV;
334+
`setCurrentValue;
334335
`setProgressDisplay;
335336
`simpleResultQ;
336337
`simpleToolRequestParser;

Source/Chatbook/CreateChatNotebook.wl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ createLocalChatNotebook[ opts: OptionsPattern[ CreateChatNotebook ] ] :=
131131
NotebookClose @ nbo
132132
,
133133
Replace[ Cells @ nbo, { cell_CellObject, ___ } :> SelectionMove[ cell, Before, CellContents ] ];
134-
CurrentValue[ nbo, Visible ] = Inherited;
134+
setCurrentValue[ nbo, Visible, Inherited ];
135135
SetSelectedNotebook @ nbo
136136
]
137137
]

Source/Chatbook/Dialogs.wl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,14 +301,14 @@ cv // endDefinition;
301301
(* ::Subsubsection::Closed:: *)
302302
(*setCV*)
303303
setCV // beginDefinition;
304-
setCV[ scope_, keys___, value_ ] := CurrentValue[ scope, { TaggingRules, "ChatNotebookSettings", keys } ] = value;
304+
setCV[ scope_, keys___, value_ ] := setCurrentValue[ scope, { TaggingRules, "ChatNotebookSettings", keys }, value ];
305305
setCV // endDefinition;
306306

307307
(* ::**************************************************************************************************************:: *)
308308
(* ::Subsubsection::Closed:: *)
309309
(*unsetCV*)
310310
unsetCV // beginDefinition;
311-
unsetCV[ scope_, keys___ ] := CurrentValue[ scope, { TaggingRules, "ChatNotebookSettings", keys } ] = Inherited;
311+
unsetCV[ scope_, keys___ ] := setCurrentValue[ scope, { TaggingRules, "ChatNotebookSettings", keys }, Inherited ];
312312
unsetCV // endDefinition;
313313

314314
(* ::**************************************************************************************************************:: *)

Source/Chatbook/Feedback.wl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ sendDialogFeedback // beginDefinition;
401401

402402
sendDialogFeedback[ cell_CellObject, nbo_NotebookObject, data_Association, choices_Association ] := Enclose[
403403
Module[ { uuid, json, chosen, string, image, request, response },
404-
CurrentValue[ nbo, { TaggingRules, "Status" } ] = "Submitting";
404+
setCurrentValue[ nbo, { TaggingRules, "Status" }, "Submitting" ];
405405

406406
uuid = Replace[ CurrentValue[ cell, { TaggingRules, "FeedbackUUID" } ], Except[ _? StringQ ] :> Null ];
407407

@@ -465,8 +465,8 @@ checkResponse[ cell_CellObject, nbo_NotebookObject, response: HoldPattern[ _HTTP
465465
checkResponse[ cell_CellObject, nbo_NotebookObject, data_Association, 200 ] := Enclose[
466466
Module[ { uuid },
467467
uuid = ConfirmBy[ data[ "RequestUUID" ], StringQ, "UUID" ];
468-
CurrentValue[ cell, { TaggingRules, "FeedbackUUID" } ] = uuid;
469-
CurrentValue[ nbo , { TaggingRules, "Status" } ] = "Done";
468+
setCurrentValue[ cell, { TaggingRules, "FeedbackUUID" }, uuid ];
469+
setCurrentValue[ nbo , { TaggingRules, "Status" }, "Done" ];
470470

471471
SessionSubmit @ ScheduledTask[ NotebookClose @ nbo, { 3 } ]
472472
],
@@ -476,8 +476,8 @@ checkResponse[ cell_CellObject, nbo_NotebookObject, data_Association, 200 ] := E
476476
checkResponse[ cell_, nbo_, data_, code_ ] := Enclose[
477477
Module[ { text },
478478
text = ConfirmBy[ getErrorText[ data, code ], StringQ, "ErrorText" ];
479-
CurrentValue[ nbo, { TaggingRules, "ErrorText" } ] = text;
480-
CurrentValue[ nbo, { TaggingRules, "Status" } ] = "Error";
479+
setCurrentValue[ nbo, { TaggingRules, "ErrorText" }, text ];
480+
setCurrentValue[ nbo, { TaggingRules, "Status" }, "Error" ];
481481
],
482482
throwInternalFailure[ checkResponse[ cell, nbo, data, code ], ## ] &
483483
];

Source/Chatbook/FrontEnd.wl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ $dialogInputAllowed := ! Or[
2121
MathLink`PreemptionEnabledQ[ ] === False
2222
];
2323

24+
$efficientCurrentValue = True;
25+
2426
(* ::**************************************************************************************************************:: *)
2527
(* ::Section::Closed:: *)
2628
(*Tasks*)
@@ -1271,6 +1273,16 @@ $statelessProgressIndicator =
12711273
(* ::Section::Closed:: *)
12721274
(*Misc*)
12731275

1276+
(* ::**************************************************************************************************************:: *)
1277+
(* ::Subsection::Closed:: *)
1278+
(*CurrentValue*)
1279+
1280+
(* CurrentValue[...] = value actually calls CurrentValue twice, requiring two MathLink transactions.
1281+
If we only want to set the value and are not using the returned value then only make one MathLink call. *)
1282+
setCurrentValue[ args__, value_ ] := (CurrentValue[ args ] = value) /; $cloudNotebooks
1283+
setCurrentValue[ args__, value_ ] := MathLink`CallFrontEndHeld @ FrontEnd`SetValue @ FEPrivate`Set[ CurrentValue[ args ], value ] /; TrueQ[ $efficientCurrentValue ]
1284+
setCurrentValue[ args__, value_ ] := (CurrentValue[ args ] = value)
1285+
12741286
(* ::**************************************************************************************************************:: *)
12751287
(* ::Subsection::Closed:: *)
12761288
(*mouseDown*)

0 commit comments

Comments
 (0)