Skip to content

Commit 88471cd

Browse files
committed
Added some optimizations
1 parent edc6273 commit 88471cd

File tree

2 files changed

+61
-94
lines changed

2 files changed

+61
-94
lines changed

Listeners.m

+11-11
Original file line numberDiff line numberDiff line change
@@ -468,8 +468,8 @@
468468
<|
469469
"ID"->id,
470470
"Track"->Internal`TrackExpression[listener, id],
471-
"Variable"->HoldPattern[listener],
472-
"Callback"->HoldPattern[callback]
471+
"Variable"->Hold[listener],
472+
"Callback"->Hold[callback]
473473
|>
474474
]
475475
]
@@ -482,15 +482,15 @@
482482

483483
setListenerCallback[li_Listener, val_]:=
484484
Replace[listenerData[li, "Callback"],
485-
Verbatim[HoldPattern][var_]:>
485+
Verbatim[Hold][var_]:>
486486
Set[var, val]
487487
];
488488
setListenerCallback~SetAttributes~HoldRest;
489489

490490

491491
setListenerCallbackDelayed[li_Listener, val_]:=
492492
Replace[listenerData[li,"Callback"],
493-
Verbatim[HoldPattern][var_]:>
493+
Verbatim[Hold][var_]:>
494494
SetDelayed[var, val]
495495
];
496496
setListenerCallbackDelayed~SetAttributes~HoldRest;
@@ -534,7 +534,7 @@
534534
},
535535
If[cb[method, li["Name"], args]=!=False,
536536
Replace[sym,
537-
Verbatim[HoldPattern][var_]:>
537+
Verbatim[Hold][var_]:>
538538
With[{v=fn[var, args]},
539539
v
540540
]
@@ -623,8 +623,8 @@
623623
data=listenerData[li]
624624
},
625625
If[data["Callback"][[1]]["Remove", li["Name"]]=!=False,
626-
Clear@@Evaluate[data["Variable"]];
627-
Internal`SetValueNoTrack@@Append[Hold@@data["Variable"], data["ID"]];
626+
(*Clear@@Evaluate[data["Variable"]];*)
627+
Internal`SetValueNoTrack@@Append[data["Variable"], data["ID"]];
628628
dropListener[li];,
629629
callbackFailure["Remove", li["Name"]]
630630
]
@@ -703,7 +703,7 @@
703703
{
704704
boxes=
705705
Replace[getListenerVariable[li],
706-
Verbatim[HoldPattern][var_]:>
706+
Verbatim[Hold][var_]:>
707707
DynamicBox[ToBoxes@var]
708708
]
709709
},
@@ -740,13 +740,13 @@
740740
Thread[
741741
Replace[
742742
Map[listenerData[#, "Variable"]&, lis],
743-
Except[_HoldPattern]->Nothing,
743+
Except[_Hold]->Nothing,
744744
1
745745
],
746-
HoldPattern
746+
Hold
747747
],
748748
{
749-
Verbatim[HoldPattern][s_]:>
749+
Verbatim[Hold][s_]:>
750750
DynamicBox[
751751
s;
752752
ToBoxes@expr,

Listeners.nb

+50-83
Original file line numberDiff line numberDiff line change
@@ -1001,14 +1001,12 @@ Cell[BoxData[
10011001
RowBox[{"listener", ",", " ", "id"}], "]"}]}], ",", "\n",
10021002
"\t\t\t\t",
10031003
RowBox[{"\"\<Variable\>\"", "\[Rule]",
1004-
RowBox[{"HoldPattern", "[", "listener", "]"}]}], ",", "\n",
1005-
"\t\t\t\t",
1004+
RowBox[{"Hold", "[", "listener", "]"}]}], ",", "\n", "\t\t\t\t",
10061005
RowBox[{"\"\<Callback\>\"", "\[Rule]",
1007-
RowBox[{"HoldPattern", "[", "callback", "]"}]}]}], "\n", "\t\t\t\t",
1008-
"|>"}]}], "\n", "\t\t\t", "]"}]}]}], "\n", "\t\t",
1009-
"]"}]}]], "CodeInput",
1010-
CellLabel->
1011-
"In[7340]:=",ExpressionUUID->"8f082985-3a4a-4257-8966-71a48256bd86"]
1006+
RowBox[{"Hold", "[", "callback", "]"}]}]}], "\n", "\t\t\t\t",
1007+
"|>"}]}], "\n", "\t\t\t", "]"}]}]}], "\n", "\t\t",
1008+
"]"}]}]], \
1009+
"CodeInput",ExpressionUUID->"8f082985-3a4a-4257-8966-71a48256bd86"]
10121010
}, Closed]],
10131011

10141012
Cell[CellGroupData[{
@@ -1028,16 +1026,14 @@ Cell[BoxData[{
10281026

10291027
RowBox[{
10301028
RowBox[{
1031-
RowBox[{"Verbatim", "[", "HoldPattern", "]"}], "[", "var_", "]"}], ":>",
1032-
"\n", "\t\t\t",
1029+
RowBox[{"Verbatim", "[", "Hold", "]"}], "[", "var_", "]"}], ":>", "\n",
1030+
"\t\t\t",
10331031
RowBox[{"Set", "[",
10341032
RowBox[{"var", ",", " ", "val"}], "]"}]}]}], "\n", "\t\t\t", "]"}]}],
10351033
";"}], "\n",
10361034
RowBox[{
10371035
RowBox[{"setListenerCallback", "~", "SetAttributes", "~", "HoldRest"}],
1038-
";"}]}], "CodeInput",
1039-
CellLabel->
1040-
"In[7341]:=",ExpressionUUID->"47fbc8d1-d00f-44e3-9831-df876d7a04cd"],
1036+
";"}]}], "CodeInput",ExpressionUUID->"47fbc8d1-d00f-44e3-9831-df876d7a04cd"],
10411037

10421038
Cell[BoxData[{
10431039
RowBox[{
@@ -1050,17 +1046,15 @@ Cell[BoxData[{
10501046
RowBox[{"li", ",", "\"\<Callback\>\""}], "]"}], ",", "\n", "\t\t",
10511047
RowBox[{
10521048
RowBox[{
1053-
RowBox[{"Verbatim", "[", "HoldPattern", "]"}], "[", "var_", "]"}], ":>",
1054-
"\n", "\t\t\t",
1049+
RowBox[{"Verbatim", "[", "Hold", "]"}], "[", "var_", "]"}], ":>", "\n",
1050+
"\t\t\t",
10551051
RowBox[{"SetDelayed", "[",
10561052
RowBox[{"var", ",", " ", "val"}], "]"}]}]}], "\n", "\t\t\t", "]"}]}],
10571053
";"}], "\n",
10581054
RowBox[{
10591055
RowBox[{
10601056
"setListenerCallbackDelayed", "~", "SetAttributes", "~", "HoldRest"}],
1061-
";"}]}], "CodeInput",
1062-
CellLabel->
1063-
"In[7342]:=",ExpressionUUID->"19586cb2-f9aa-4ec8-9b49-78d5bc5f1bce"],
1057+
";"}]}], "CodeInput",ExpressionUUID->"19586cb2-f9aa-4ec8-9b49-78d5bc5f1bce"],
10641058

10651059
Cell[BoxData[{
10661060
RowBox[{"callbackFailure", "~", "SetAttributes", "~", "HoldRest"}], "\n",
@@ -1145,11 +1139,11 @@ Cell[BoxData[{
11451139
RowBox[{"li", "[", "\"\<Name\>\"", "]"}], ",", " ", "args"}], "]"}],
11461140
"=!=", "False"}], ",", "\n", "\t\t\t",
11471141
RowBox[{"Replace", "[",
1148-
RowBox[{"sym", ",", "\n", "\t", "\t\t\t",
1142+
RowBox[{"sym", ",", "\n", "\t\t\t\t",
11491143
RowBox[{
11501144
RowBox[{
1151-
RowBox[{"Verbatim", "[", "HoldPattern", "]"}], "[", "var_", "]"}],
1152-
":>", "\n", "\t", "\t\t\t\t",
1145+
RowBox[{"Verbatim", "[", "Hold", "]"}], "[", "var_", "]"}], ":>",
1146+
"\n", "\t", "\t\t\t\t",
11531147
RowBox[{"With", "[",
11541148
RowBox[{
11551149
RowBox[{"{",
@@ -1174,9 +1168,8 @@ Cell[BoxData[{
11741168
RowBox[{"listenerObject", "[", "name", "]"}], ",", " ", "method", ",",
11751169
" ", "fn", ",", " ", "args"}], "]"}]}], ";"}], "\n",
11761170
RowBox[{"updateListenerTemplate", "~", "SetAttributes", "~",
1177-
"HoldAllComplete"}]}], "CodeInput",
1178-
CellLabel->
1179-
"In[7345]:=",ExpressionUUID->"7f1d3af1-cef1-4bea-8872-d8d53e2c3772"]
1171+
"HoldAllComplete"}]}], \
1172+
"CodeInput",ExpressionUUID->"7f1d3af1-cef1-4bea-8872-d8d53e2c3772"]
11801173
}, Closed]],
11811174

11821175
Cell[CellGroupData[{
@@ -1365,25 +1358,26 @@ Cell[BoxData[
13651358
RowBox[{"\"\<Remove\>\"", ",", " ",
13661359
RowBox[{"li", "[", "\"\<Name\>\"", "]"}]}], "]"}], "=!=", "False"}],
13671360
",", "\n", "\t\t\t",
1361+
RowBox[{"(*",
1362+
RowBox[{
1363+
RowBox[{"Clear", "@@",
1364+
RowBox[{"Evaluate", "[",
1365+
RowBox[{"data", "[", "\"\<Variable\>\"", "]"}], "]"}]}], ";"}],
1366+
"*)"}], "\n", "\t\t\t",
13681367
RowBox[{
1369-
RowBox[{"Clear", "@@",
1370-
RowBox[{"Evaluate", "[",
1371-
RowBox[{"data", "[", "\"\<Variable\>\"", "]"}], "]"}]}], ";", "\n",
1372-
"\t", "\t\t",
13731368
RowBox[{"Internal`SetValueNoTrack", "@@",
13741369
RowBox[{"Append", "[",
13751370
RowBox[{
1376-
RowBox[{"Hold", "@@",
1377-
RowBox[{"data", "[", "\"\<Variable\>\"", "]"}]}], ",", " ",
1371+
RowBox[{"data", "[", "\"\<Variable\>\"", "]"}], ",", " ",
13781372
RowBox[{"data", "[", "\"\<ID\>\"", "]"}]}], "]"}]}], ";", "\n",
13791373
"\t\t\t",
13801374
RowBox[{"dropListener", "[", "li", "]"}], ";"}], ",", "\n", "\t\t\t",
13811375
RowBox[{"callbackFailure", "[",
13821376
RowBox[{"\"\<Remove\>\"", ",", " ",
13831377
RowBox[{"li", "[", "\"\<Name\>\"", "]"}]}], "]"}]}], "\n", "\t\t\t",
1384-
"]"}]}], "\n", "\t\t", "]"}]}]], "CodeInput",
1385-
CellLabel->
1386-
"In[7356]:=",ExpressionUUID->"49fef233-b557-4314-82a5-61def3d84690"]
1378+
"]"}]}], "\n", "\t\t",
1379+
"]"}]}]], \
1380+
"CodeInput",ExpressionUUID->"49fef233-b557-4314-82a5-61def3d84690"]
13871381
}, Closed]],
13881382

13891383
Cell[CellGroupData[{
@@ -1501,16 +1495,16 @@ Cell[BoxData[
15011495
"\t\t\t\t\t",
15021496
RowBox[{
15031497
RowBox[{
1504-
RowBox[{"Verbatim", "[", "HoldPattern", "]"}], "[", "var_", "]"}], ":>",
1505-
"\n", "\t\t\t\t\t\t",
1498+
RowBox[{"Verbatim", "[", "Hold", "]"}], "[", "var_", "]"}], ":>",
1499+
"\n", "\t\t\t\t\t\t",
15061500
RowBox[{"DynamicBox", "[",
15071501
RowBox[{"ToBoxes", "@", "var"}], "]"}]}]}], "\n", "\t\t\t\t\t",
15081502
"]"}]}], "\n", "\t\t\t", "}"}], ",", "\n", "\t\t",
15091503
RowBox[{"InterpretationBox", "[", "\n", "\t\t\t",
15101504
RowBox[{"boxes", ",", "\n", "\t\t\t", "li"}], "\n", "\t\t\t", "]"}]}],
1511-
"\n", "\t\t", "]"}]}]], "CodeInput",
1512-
CellLabel->
1513-
"In[7363]:=",ExpressionUUID->"62319269-f018-4994-8634-104d03028e68"]
1505+
"\n", "\t\t",
1506+
"]"}]}]], \
1507+
"CodeInput",ExpressionUUID->"62319269-f018-4994-8634-104d03028e68"]
15141508
}, Closed]]
15151509
}, Open ]],
15161510

@@ -1562,17 +1556,16 @@ Cell[BoxData[{
15621556
RowBox[{"#", ",", " ", "\"\<Variable\>\""}], "]"}], "&"}],
15631557
",", " ", "lis"}], "]"}], ",", " ", "\n", "\t\t\t\t\t\t\t",
15641558
RowBox[{
1565-
RowBox[{"Except", "[", "_HoldPattern", "]"}], "\[Rule]",
1566-
"Nothing"}], ",", " ", "\n", "\t\t\t\t\t\t\t", "1"}], "\n",
1567-
"\t\t\t\t\t\t\t", "]"}], ",", "\n", "\t\t\t\t\t\t",
1568-
"HoldPattern"}], "\n", "\t\t\t\t\t\t", "]"}], ",", "\n",
1569-
"\t\t\t\t\t",
1570-
RowBox[{"{", "\n", "\t", "\t", "\t", "\t\t\t",
1559+
RowBox[{"Except", "[", "_Hold", "]"}], "\[Rule]", "Nothing"}],
1560+
",", " ", "\n", "\t\t\t\t\t\t\t", "1"}], "\n", "\t\t\t\t\t\t\t",
1561+
"]"}], ",", "\n", "\t\t\t\t\t\t", "Hold"}], "\n", "\t\t\t\t\t\t",
1562+
"]"}], ",", "\n", "\t\t\t\t\t",
1563+
RowBox[{"{", "\n", "\t\t\t\t\t\t",
15711564
RowBox[{
15721565
RowBox[{
15731566
RowBox[{
1574-
RowBox[{"Verbatim", "[", "HoldPattern", "]"}], "[", "s_", "]"}],
1575-
":>", "\n", "\t", "\t", "\t", "\t", "\t\t\t",
1567+
RowBox[{"Verbatim", "[", "Hold", "]"}], "[", "s_", "]"}], ":>",
1568+
"\n", "\t", "\t", "\t", "\t", "\t\t\t",
15761569
RowBox[{"DynamicBox", "[", "\n", "\t\t\t\t\t\t\t\t",
15771570
RowBox[{
15781571
RowBox[{"s", ";", "\n", "\t\t\t\t\t\t\t\t",
@@ -1627,9 +1620,8 @@ Cell[BoxData[{
16271620
"\n", "\t\t", "ops"}], "\n", "\t\t", "]"}]}], ";"}], "\n",
16281621
RowBox[{
16291622
RowBox[{"ListenerDynamic", "~", "SetAttributes", "~", "HoldFirst"}],
1630-
";"}]}], "CodeInput",
1631-
CellLabel->
1632-
"In[7364]:=",ExpressionUUID->"5a638b84-5a1d-4753-9270-9f29feb0f613"]
1623+
";"}]}], "CodeInput",ExpressionUUID->"5a638b84-5a1d-4753-9270-9f29feb0f613"]\
1624+
16331625
}, Closed]]
16341626
}, Open ]],
16351627

@@ -1661,7 +1653,7 @@ Cell[BoxData[
16611653

16621654
Cell[BoxData[
16631655
RowBox[{"ContextRemove", "[", "\"\<Listeners`\>\"", "]"}]], "Input",
1664-
CellLabel->"In[52]:=",ExpressionUUID->"18ff1545-8615-4a25-a139-40aff5aab986"],
1656+
CellLabel->"In[67]:=",ExpressionUUID->"18ff1545-8615-4a25-a139-40aff5aab986"],
16651657

16661658
Cell[BoxData[
16671659
RowBox[{"Block", "[",
@@ -1672,54 +1664,29 @@ Cell[BoxData[
16721664
RowBox[{"$Path", ",", " ",
16731665
RowBox[{"NotebookDirectory", "[", "]"}]}], "]"}]}], "}"}], ",",
16741666
RowBox[{"<<", "Listeners`"}]}], "]"}]], "Input",
1675-
CellLabel->"In[53]:=",ExpressionUUID->"8e495724-0684-4e7a-bd54-a76edba3ae72"],
1667+
CellLabel->"In[68]:=",ExpressionUUID->"8e495724-0684-4e7a-bd54-a76edba3ae72"],
16761668

16771669
Cell[BoxData[
16781670
RowBox[{"Get", "[",
16791671
"\"\<https://github.com/b3m2a1/mathematica-tools/raw/master/Listeners.m\>\"\
16801672
", "]"}]], "Input",
16811673
CellLabel->"In[42]:=",ExpressionUUID->"b960096c-be8d-4007-b4dd-e2524ec07325"],
16821674

1683-
Cell[CellGroupData[{
1684-
16851675
Cell[BoxData[
16861676
RowBox[{
1687-
RowBox[{
1688-
RowBox[{"var", "=",
1689-
RowBox[{"Listener", "[", "\"\<b\>\"", "]"}]}], ";"}], "//",
1690-
"AbsoluteTiming"}]], "Input",
1691-
CellLabel->"In[54]:=",ExpressionUUID->"172272c7-1d8f-4fad-b479-e9bdba3502e6"],
1692-
1693-
Cell[BoxData[
1694-
RowBox[{"{",
1695-
RowBox[{"0.000108`", ",", "Null"}], "}"}]], "Output",
1696-
CellLabel->
1697-
"Out[54]=",ExpressionUUID->"77a5fd57-fc54-4c99-8a34-4ea910be4e48"]
1698-
}, Open ]],
1699-
1700-
Cell[CellGroupData[{
1701-
1702-
Cell[BoxData[
1703-
RowBox[{"var", "=",
1704-
RowBox[{"2", "//", "AbsoluteTiming"}]}]], "Input",
1705-
CellLabel->"In[55]:=",ExpressionUUID->"16c797be-78c3-4a56-83b2-babf97dcf246"],
1706-
1707-
Cell[BoxData[
1708-
RowBox[{"{",
1709-
RowBox[{"1.`*^-6", ",", "2"}], "}"}]], "Output",
1710-
CellLabel->
1711-
"Out[55]=",ExpressionUUID->"f1da6de5-1a88-4d83-b6b8-2c8f48c46b09"]
1712-
}, Open ]],
1677+
RowBox[{"var", "=",
1678+
RowBox[{"Listener", "[", "\"\<b\>\"", "]"}]}], ";"}]], "Input",
1679+
CellLabel->"In[69]:=",ExpressionUUID->"172272c7-1d8f-4fad-b479-e9bdba3502e6"],
17131680

17141681
Cell[CellGroupData[{
17151682

17161683
Cell[BoxData[
17171684
RowBox[{"var", "//", "InputForm"}]], "Input",
1718-
CellLabel->"In[57]:=",ExpressionUUID->"c7d4739a-f5cc-4eb4-a557-7f8f362cbc7a"],
1685+
CellLabel->"In[70]:=",ExpressionUUID->"90ad8742-afd8-4f85-a499-a12b58fa012a"],
17191686

17201687
Cell["Listener[\"b\"]", "Output",
17211688
CellLabel->
1722-
"Out[57]//InputForm=",ExpressionUUID->"51bc755f-4197-468a-b1d8-993fd455a75e"]
1689+
"Out[70]//InputForm=",ExpressionUUID->"61bebb46-3ecb-4e8c-b950-a220b8ee5d16"]
17231690
}, Open ]],
17241691

17251692
Cell[BoxData[
@@ -1733,17 +1700,17 @@ Cell[BoxData[
17331700
RowBox[{"BoxData", "@",
17341701
RowBox[{"ToBoxes", "@", "expr"}]}], "]"}], ",", " ",
17351702
"\"\<PlainText\>\""}], "]"}]}]}]}]], "Input",
1736-
CellLabel->"In[50]:=",ExpressionUUID->"252b6b75-1e26-40e5-8d58-22cb6bcb11e8"],
1703+
CellLabel->"In[63]:=",ExpressionUUID->"252b6b75-1e26-40e5-8d58-22cb6bcb11e8"],
17371704

17381705
Cell[CellGroupData[{
17391706

17401707
Cell[BoxData[
17411708
RowBox[{"displayedForm", "@", "var"}]], "Input",
1742-
CellLabel->"In[51]:=",ExpressionUUID->"9ec3414a-1f76-40d5-86df-2752faac0304"],
1709+
CellLabel->"In[71]:=",ExpressionUUID->"9ec3414a-1f76-40d5-86df-2752faac0304"],
17431710

17441711
Cell[BoxData["\<\"Null\"\>"], "Output",
17451712
CellLabel->
1746-
"Out[51]=",ExpressionUUID->"a2eb885d-4ecb-40d6-8333-f5bd4490b18d"]
1713+
"Out[71]=",ExpressionUUID->"c31e1e7b-50bb-4a8d-b111-281275282f8a"]
17471714
}, Open ]],
17481715

17491716
Cell[CellGroupData[{

0 commit comments

Comments
 (0)