Skip to content

Commit ca138aa

Browse files
author
Jean-Baptiste Doderlein
committed
Replace Service Worker with mini coi
1 parent 0bb34a5 commit ca138aa

File tree

3 files changed

+86
-18
lines changed

3 files changed

+86
-18
lines changed

README.md

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,46 @@
22
WIP : A small but efficient, intuitive and responsive Python IDE right in your browser! Ships Micro Python, interpreter by your browser (so it works offline!), compiled with pyscript.
33

44
## TODO
5-
- Write a good README
6-
- Test on real programm (=debug)
7-
- Change icon and name from BetterOCaml
8-
- Change XtermJS look to better match BetterEditor design
9-
- Make script to build from the sources the libs in the repo
10-
- Update PWA settings
11-
- Clean up JS to remove every useless functions
12-
13-
## Use of Web Worker / Python Interpreter
14-
We now use Micro Python but we could use original python interpreter if the toplevel was in a web worker.
15-
To accept the toplevel in web worker, we would need these headers :
16-
```
17-
Cross-Origin-Opener-Policy: same-origin
18-
Cross-Origin-Embedder-Policy: require-corp
19-
```
20-
But this is not possible on github pages.
5+
### Write a good README
6+
Take exampel from BetterOCaml, with gifs, screenshot and usecases
7+
8+
### Test on real programm (=debug)
9+
Make test on real world program = teaching material, see if the editor is capable
10+
11+
### Change icon and name from BetterOCaml
12+
13+
### Change XtermJS look to better match BetterEditor design
14+
15+
Need to integrate termjs plugins (for autosizing) + see if we can integrate matplotlib inside
16+
17+
### Make script to build from the sources the libs in the repo
18+
19+
Track all libs used and write how to update each files
20+
21+
### Update PWA + serviceWorker settings
22+
23+
Need to make mini-coi work with existing service worker to cache all fiels needed to the editor
24+
25+
### Clean up JS to remove every useless functions
26+
27+
## BetterPython + BetterPythonLight ?
28+
29+
The choice of interpreter is important.
30+
31+
### MicroPython
32+
- lightweight
33+
- fast to load + to execute
34+
- no package = no matplotlib
35+
- work without worker if needed
36+
37+
### Pyoide
38+
- real cpython implementation
39+
- can import package = can use matplotlib
40+
- heavy to load
41+
- package are heavy
42+
- need worker to work
43+
- import package is slow
44+
45+
2 solutions : choose one of the interpreter or make 2 versions
46+
47+
Need advice and feedbacks to decide

src/index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,14 +331,15 @@ <h4>Graphics main window</h4>
331331
r.style.setProperty('--toplevel-font-size', (localStorage.getItem("betterocaml-text-toplevel") || "1.2em"));
332332

333333
// Service Worker
334+
/*
334335
if ('serviceWorker' in navigator) {
335336
try {
336337
navigator.serviceWorker.register('serviceWorker.js');
337338
console.log("Service Worker Registered");
338339
} catch (error) {
339340
console.log("Service Worker Registration Failed");
340341
}
341-
}
342+
}*/
342343

343344
// Init navbar size
344345
navbar_resize()

src/mini-coi.js

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
/*! coi-serviceworker v0.1.7 - Guido Zuidhof and contributors, licensed under MIT */
22
/*! mini-coi - Andrea Giammarchi and contributors, licensed under MIT */
3+
4+
const staticAssets = [
5+
// './',
6+
'./manifest.json',
7+
'./index.html',
8+
'./favicon.ico'
9+
];
10+
11+
312
(({ document: d, navigator: { serviceWorker: s } }) => {
413
if (d) {
514
const { currentScript: c } = d;
@@ -9,10 +18,24 @@
918
});
1019
}
1120
else {
12-
addEventListener('install', () => skipWaiting());
21+
addEventListener('install', async event => {
22+
skipWaiting();
23+
const cache = await caches.open('static-cache');
24+
cache.addAll(staticAssets);
25+
});
1326
addEventListener('activate', e => e.waitUntil(clients.claim()));
1427
addEventListener('fetch', e => {
28+
const req = e.request;
29+
const url = new URL(req.url);
1530
const { request: r } = e;
31+
let cached_req;
32+
/*
33+
if(url.origin === location.url){
34+
cached_req = cacheFirst(req);
35+
} else {
36+
cached_req = newtorkFirst(req);
37+
}
38+
*/
1639
if (r.cache === 'only-if-cached' && r.mode !== 'same-origin') return;
1740
e.respondWith(fetch(r).then(r => {
1841
const { body, status, statusText } = r;
@@ -26,4 +49,21 @@
2649
});
2750
}
2851
})(self);
52+
53+
async function cacheFirst(req){
54+
const cachedResponse = caches.match(req);
55+
return cachedResponse || fetch(req);
56+
}
57+
58+
async function newtorkFirst(req){
59+
const cache = await caches.open('dynamic-cache');
60+
61+
try {
62+
const res = await fetch(req);
63+
cache.put(req, res.clone());
64+
return res;
65+
} catch (error) {
66+
return await cache.match(req);
67+
}
68+
}
2969

0 commit comments

Comments
 (0)