Skip to content

Commit

Permalink
wip: Use JSON serialization when in-browser #14
Browse files Browse the repository at this point in the history
  • Loading branch information
lo5 committed Jun 22, 2022
1 parent d9ee5b9 commit 1223515
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 12 deletions.
36 changes: 24 additions & 12 deletions py/h2o_nitro/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,32 @@
import random
from pathlib import Path
from collections import OrderedDict
import msgpack
from enum import Enum, IntEnum
from .version import __version__
from .version import __version__, __nitride__

web_directory = str(Path(__file__).parent / 'www')

if __nitride__:
import json


def _marshal(d: dict):
return json.dumps(d)


def _unmarshal(b) -> dict:
return json.loads(b)
else:
import msgpack


def _marshal(d: dict):
return msgpack.packb(d)


def _unmarshal(b) -> dict:
return msgpack.unpackb(b)

__xid = 0


Expand Down Expand Up @@ -71,14 +91,6 @@ def __init__(self):
super().__init__('Interrupted')


def _marshal(d: dict):
return msgpack.packb(d)


def _unmarshal(b) -> dict:
return msgpack.unpackb(b)


def _dump(x): # recursive
if isinstance(x, OrderedDict):
return _dump([(k, v) for k, v in x.items()])
Expand Down Expand Up @@ -607,7 +619,7 @@ def _unwrap_input(x):
return None if x is None else x[1]


def _marshal_error(code: int, text: str) -> dict:
def _marshal_error(code: int, text: str):
return _marshal(dict(t=_MsgType.Error, code=code, text=text))


Expand All @@ -619,7 +631,7 @@ def _marshal_set(
theme: Optional[Theme] = None,
plugins: Optional[Iterable[Plugin]] = None,
mode: Optional[str] = None,
) -> dict:
):
return _marshal(dict(
t=_MsgType.Set,
xid=_xid(),
Expand Down
1 change: 1 addition & 0 deletions py/h2o_nitro/version.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
__version__ = "0.10.1"
__nitride__ = False
34 changes: 34 additions & 0 deletions web/public/nitride.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

const pollInterval = 100

let _poller = 0;

async function init(runtime, program, autoload) {
importScripts(runtime);
self.pyodide = await loadPyodide();
await self.pyodide.loadPackage(["micropip"]);
if (autoload) await self.pyodide.loadPackagesFromImports(program);
await self.pyodide.runPythonAsync(program);
clearInterval(_poller);
_poller = setInterval(() => {
const message = self.pyodide.globals.get('_nitro_io').read();
if (message) self.postMessage({ t: 1, message });
}, pollInterval);
}

self.onmessage = async (event) => {
const c = event.data;
try {
switch (c.t) {
case 1:
self.pyodide.globals.get('_nitro_io').write(c.message);
break;
case 2:
await init(c.runtime, c.program, c.autoload);
self.postMessage({ t: 3 });
break;
}
} catch (error) {
self.postMessage({ t: 0, error: error.message });
}
}

0 comments on commit 1223515

Please sign in to comment.