Skip to content
This repository was archived by the owner on Feb 14, 2024. It is now read-only.

Add xeus-python services #20

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion jupyterlite_xeus_python/env_build_addon.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

PYTHON_VERSION = "3.10"

SERVICE = "xeus_python_service.js"

CHANNELS = [
"https://repo.mamba.pm/emscripten-forge",
"https://repo.mamba.pm/conda-forge"
Expand Down Expand Up @@ -89,9 +91,14 @@ def __init__(self, *args, **kwargs):

def pre_build(self, manager):
"""yield a doit task to create the emscripten-32 env and grab anything we need from it"""
yield dict(
name="setup-service-worker",
actions=[(self.copy_one, [Path(__file__).parent / SERVICE, Path(self.manager.output_dir) / SERVICE])],
)

# Bail early if there is nothing to do
if not self.packages:
return []
return

# Create emscripten env with the given packages
self.create_env()
Expand Down
24 changes: 24 additions & 0 deletions jupyterlite_xeus_python/xeus_python_service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
self.addEventListener("install", (event) => {
console.log('install xeus-python service');
event.waitUntil(self.skipWaiting());
});

self.addEventListener('activate', (event) => {
console.log('activate xeus-python service');
event.waitUntil(self.clients.claim());
});

self.addEventListener('fetch', (event) => {
var url = new URL(event.request.url);
if (url.pathname === '/SLEEP') {
console.log('SLEEP', event);

// wait ?t=X seconds, then return a 304:
event.respondWith(new Promise(resolve => {
var t = Number.parseFloat(new URLSearchParams(url.search).get('t')) * 1000.;
var response = new Response(null, { status: 304 });

setTimeout(resolve, t, response);
}));
}
});
14 changes: 14 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@ const server_kernel: JupyterLiteServerPlugin<void> = {
});
}
});

navigator.serviceWorker.register('/xeus_python_service.js').then(
registration => {
// Registration was successful
console.log(
'ServiceWorker registration successful with scope: ',
registration.scope
);
},
err => {
// registration failed :(
console.log('ServiceWorker registration failed: ', err);
}
);
}
};

Expand Down
13 changes: 13 additions & 0 deletions src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@ const ctx: Worker = self as any;
let raw_xkernel: any;
let raw_xserver: any;

function sleep(seconds: number) {
const xhr = new XMLHttpRequest();
xhr.open('GET', `/SLEEP?t=${seconds}`, false);
try{
xhr.send();
} catch(e) {
console.error(e);
}
}

// sleep(2);
// sleep(3);

async function waitRunDependency() {
const promise = new Promise((r: any) => {
globalThis.Module.monitorRunDependencies = (n: number) => {
Expand Down