Skip to content

Commit 8c3cf4a

Browse files
Merge pull request #105 from pyscript/storage
Added a storage utility
2 parents a1b57c7 + 0b43495 commit 8c3cf4a

File tree

15 files changed

+122
-12
lines changed

15 files changed

+122
-12
lines changed

.eslintrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
},
66
"extends": "eslint:recommended",
77
"parserOptions": {
8-
"ecmaVersion": 12,
8+
"ecmaVersion": "latest",
99
"sourceType": "module"
1010
},
1111
"ignorePatterns": ["__template.js", "xworker.js", "esm/python/*.js", "esm/3rd-party/*"],

docs/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,7 @@ The module is registered within the interpreter as *JS* module and it offers var
379379
| currentScript | `from polyscript import currentScript` | it's an explicit, always correct, reference to the current node running the generic script code. |
380380
| js_modules | `from polyscript import js_modules` | described in the [Extra config Features](#extra-config-features) part. |
381381
| lazy_py_modules | `from polyscript import lazy_py_modules` | allows, only in *Python* related interpreters, and without needing static config entries, to import lazily any available module.
382+
| storage | `from polyscript import storage` | a utility to instantiate a named [idb-map](https://github.com/WebReflection/idb-map/#readme) that can be consumed synchronously.
382383
383384
384385
#### lazy_py_modules
@@ -393,6 +394,26 @@ The module is registered within the interpreter as *JS* module and it offers var
393394
</script>
394395
```
395396
397+
#### storage
398+
399+
```html
400+
<script type="micropython" async>
401+
from polyscript import storage
402+
403+
# await its loading
404+
map = await storage("my-user-persistent-storage")
405+
406+
# just use it synchronously
407+
map.set("key", "value")
408+
print(map.get("key"))
409+
410+
# after set, delete, or clear
411+
# it is possible to sync operations
412+
await map.sync()
413+
</script>
414+
```
415+
416+
396417
397418
### Worker exports
398419

docs/index.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

esm/interpreter/_python.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { dedent } from '../utils.js';
22
import { io } from './_io.js';
3+
import Storage from '../storage.js';
34

45
export const loader = new WeakMap();
56

@@ -11,6 +12,11 @@ export const registerJSModule = (interpreter, name, value) => {
1112
await loader.get(interpreter)(packages);
1213
return packages.map(name => interpreter.pyimport(name));
1314
};
15+
value.storage = async (name) => {
16+
const storage = new Storage(name);
17+
await storage.sync();
18+
return storage;
19+
};
1420
}
1521
interpreter.registerJsModule(name, value);
1622
};

esm/interpreter/micropython.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const mkdir = (FS, path) => {
2323

2424
export default {
2525
type,
26-
module: (version = '1.23.0') =>
26+
module: (version = '1.24.0-preview-44') =>
2727
`https://cdn.jsdelivr.net/npm/@micropython/micropython-webassembly-pyscript@${version}/micropython.mjs`,
2828
async engine({ loadMicroPython }, config, url, baseURL) {
2929
const { stderr, stdout, get } = stdio({

esm/storage.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import IDBMap from '@webreflection/idb-map';
2+
3+
export default class Storage extends Map {
4+
#map;
5+
#queue;
6+
constructor(name) {
7+
super();
8+
this.#map = new IDBMap(name);
9+
this.#queue = this.#map.entries().then(entries => {
10+
for (const [key, value] of entries)
11+
this.set(key, value);
12+
});
13+
}
14+
async sync() {
15+
await this.#queue;
16+
}
17+
clear() {
18+
this.#queue = this.#queue.then(() => this.#map.clear());
19+
return super.clear();
20+
}
21+
delete(key) {
22+
this.#queue = this.#queue.then(() => this.#map.delete(key));
23+
return super.delete(key);
24+
}
25+
set(key, value) {
26+
this.#queue = this.#queue.then(() => this.#map.set(key, value));
27+
return super.set(key, value);
28+
}
29+
}

node.importmap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"imports": {
33
"http://pyodide": "./test/mocked/pyodide.mjs",
44
"https://cdn.jsdelivr.net/pyodide/v0.26.1/full/pyodide.mjs": "./test/mocked/pyodide.mjs",
5-
"https://cdn.jsdelivr.net/npm/@micropython/micropython-webassembly-pyscript@1.23.0/micropython.mjs": "./test/mocked/micropython.mjs",
5+
"https://cdn.jsdelivr.net/npm/@micropython/micropython-webassembly-pyscript@1.24.0-preview-44/micropython.mjs": "./test/mocked/micropython.mjs",
66
"./3rd-party/toml.js": "./test/mocked/toml.mjs"
77
}
88
}

package-lock.json

Lines changed: 9 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "polyscript",
3-
"version": "0.12.15",
3+
"version": "0.13.1",
44
"description": "PyScript single core to rule them all",
55
"main": "./cjs/index.js",
66
"types": "./types/polyscript/esm/index.d.ts",
@@ -80,6 +80,7 @@
8080
"@ungap/structured-clone": "^1.2.0",
8181
"@ungap/with-resolvers": "^0.1.0",
8282
"@webreflection/fetch": "^0.1.5",
83+
"@webreflection/idb-map": "^0.2.0",
8384
"basic-devtools": "^0.1.6",
8485
"codedent": "^0.1.2",
8586
"coincident": "^1.2.3",
@@ -90,6 +91,6 @@
9091
"to-json-callback": "^0.1.1"
9192
},
9293
"worker": {
93-
"blob": "sha256-pWERQSHp3qDvCx7LVz9I3QMz9+k3IwDLVMixPMRxxI4="
94+
"blob": "sha256-QYGr9ma1eAghLgOKOxC9Tz8PvKkbO4gurbzJ1bac/xM="
9495
}
9596
}

0 commit comments

Comments
 (0)