Skip to content

Commit

Permalink
added runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
JerryI committed Oct 18, 2024
1 parent 6691346 commit c40f75f
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wljs-editor",
"version": "1.1.8",
"version": "1.1.9",
"description": "WLJS Code editor",
"scripts": {
"build": "node --max-old-space-size=8192 ./node_modules/.bin/rollup --config rollup.config.mjs",
Expand Down
19 changes: 12 additions & 7 deletions src/FrontendRuntime.wl
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ BeginPackage["Notebook`Editor`FrontEndRuntime`", {
"JerryI`Misc`Events`",
"JerryI`Misc`Events`Promise`",
"JerryI`Misc`WLJS`Transport`",
"JerryI`Notebook`Kernel`",
"JerryI`Notebook`",
"JerryI`Notebook`AppExtensions`"
"JerryI`Notebook`AppExtensions`",
"Notebook`Editor`",
"JerryI`WLX`WebUI`"
}]


Expand All @@ -17,32 +20,34 @@ frontEndRuntime = <|
|>

rebuild := (
stringTemplate = {
compiledString = {
StringRiffle[StringJoin["<script type=\"module\">", #, "</script>"] &/@ frontEndRuntime[{"Modules", "js"}] ],
StringRiffle[StringJoin["<style>", #, "</style>"] &/@ frontEndRuntime[{"Modules", "css"}] ]
} // StringRiffle;
);

rebuild;

AppExtensions`TemplateInjection["AppHead"] = Function[Null, ""];
component[__] := compiledString

injectInRuntime[{"Modules", "js"}, data_List] := With[{notebooks},
AppExtensions`TemplateInjection["AppHead"] = component

injectInRuntime[{"Modules", "js"}, data_List] := With[{notebooks = Values[Notebook`HashMap]},
WebUISubmit[ Global`UIHeadInject["js", data ], #["Socket"] ] &/@ notebooks;
]

EventHandler[NotebookEditorChannel // EventClone,
{
"RequestRuntimeExtensions" -> Function[assoc,
With[{result = frontEndRuntime, kernel = assoc["Kernel"], promise = assoc["Promise"]},
With[{result = frontEndRuntime, kernel = Kernel`HashMap[assoc["Kernel"] ], promise = assoc["Promise"]},
Kernel`Async[kernel, EventFire[promise, Resolve, result] ];
]
],

"UpdateRuntimeExtensions" -> Function[assoc,
With[{promise = assoc["Promise"], data = assoc["Data"], kernel = assoc["Kernel"], key = assoc["Key"]},
With[{promise = assoc["Promise"], data = assoc["Data"], kernel = Kernel`HashMap[assoc["Kernel"] ], key = assoc["Key"]},

With[{new = Complement[data, frontEndRuntime[key] ] // DeleteDuplicates},
With[{new = Complement[data, frontEndRuntime[key] // DeleteDuplicates ] // DeleteDuplicates},
frontEndRuntime[key] = data // DeleteDuplicates;
injectInRuntime[key, new];
];
Expand Down
20 changes: 17 additions & 3 deletions src/FrontendRuntimeKernel.wl
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,29 @@ FrontEndRuntime::usage = "FrontEndRuntime[] gives a list of properties possible

Begin["`Internal`"]

FrontEndRuntime[any_List] := With[{promise = Promise[]},
FrontEndRuntime[any_List] := With[{r = FrontEndRuntime[]},
If[FailureQ[r],
$Failed
,
r[any]
]
]

FrontEndRuntime[] := With[{promise = Promise[]},
EventFire[Internal`Kernel`CommunicationChannel, "RequestRuntimeExtensions", <|"Promise" -> (promise), "Kernel"->Internal`Kernel`Hash|>];
promise // WaitAll
With[{r = (promise // WaitAll)},
r
]
]

FrontEndRuntime /: Set[FrontEndRuntime[key_], data_List] := With[{promise = Promise[]},
EventFire[Internal`Kernel`CommunicationChannel, "UpdateRuntimeExtensions", <|"Promise" -> (promise), "Kernel"->Internal`Kernel`Hash, "Data"->data, "Key"->key|>];
With[{
list = Select[data /. {File[path_String] :> Import[path, "Text"]}, StringQ]
},
EventFire[Internal`Kernel`CommunicationChannel, "UpdateRuntimeExtensions", <|"Promise" -> (promise), "Kernel"->Internal`Kernel`Hash, "Data"->list, "Key"->key|>];
promise // WaitAll
]
]

End[]
EndPackage[]
26 changes: 26 additions & 0 deletions src/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,32 @@ core['Notebook`Editor`Rasterize`Internal`OverlayView'].Create = async (args, env
}, 1000);
}

core.UIHeadInject = async (args, env) => {
const type = await interpretate(args[0], env);
await core.UIHeadInject[type](args.slice(1), env);
}

core.UIHeadInject.js = async (args, env) => {
const data = await interpretate(args[0], env);

data.forEach((el) => {
const script = document.createElement('script');
script.type = "module";
script.textContent = el;
document.head.appendChild(script);
})
}

core.UIHeadInject.css = async (args, env) => {
const data = await interpretate(args[0], env);

data.forEach((el) => {
const style = document.createElement('style');
style.textContent = el;
document.head.appendChild(style);
})
}

core.SystemOpen = async (args, env) => {
const type = await interpretate(args[1], env);
await core.SystemOpen[type](args[0], env);
Expand Down

0 comments on commit c40f75f

Please sign in to comment.