mirror of
https://github.com/pyscript/pyscript.git
synced 2026-02-15 04:01:02 -05:00
* Remove duplicate LICENSE. * Remove un-userd pyscript.sw directory and its content. * Remove ReadTheDocs settings (unused). * Remove un-used pyproject.toml * Remove now unused CHANGELOG. Changes now tracked via release notes on GitHub. * Updated / cleaned release page template and associated GH actions. * Update prettierignore to remove un-needed refs. * Move troubleshooting into correct README. * Add reason for the index.html * Rename the "pyscript.core" directory to "core". * Update PR template because CHANGELOG is no longer used. * Codespell configuration in pyproject.toml. * Update pyscript.core -> core in .githubignore * Remove test-results/.last-run.json. This should be ignored by git. * Pin nodejs version. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
73 lines
2.2 KiB
JavaScript
73 lines
2.2 KiB
JavaScript
import { ArrayBuffer, TypedArray } from "sabayon/shared";
|
|
import IDBMapSync from "@webreflection/idb-map/sync";
|
|
import { parse, stringify } from "flatted";
|
|
|
|
const to_idb = (value) => {
|
|
if (value == null) return stringify(["null", 0]);
|
|
/* eslint-disable no-fallthrough */
|
|
switch (typeof value) {
|
|
case "object": {
|
|
if (value instanceof TypedArray)
|
|
return stringify(["memoryview", [...value]]);
|
|
if (value instanceof ArrayBuffer)
|
|
return stringify(["bytearray", [...new Uint8Array(value)]]);
|
|
}
|
|
case "string":
|
|
case "number":
|
|
case "boolean":
|
|
return stringify(["generic", value]);
|
|
default:
|
|
throw new TypeError(`Unexpected value: ${String(value)}`);
|
|
}
|
|
};
|
|
|
|
const from_idb = (value) => {
|
|
const [kind, result] = parse(value);
|
|
if (kind === "null") return null;
|
|
if (kind === "generic") return result;
|
|
if (kind === "bytearray") return new Uint8Array(value).buffer;
|
|
if (kind === "memoryview") return new Uint8Array(value);
|
|
return value;
|
|
};
|
|
|
|
// this export simulate pyscript.storage exposed in the Python world
|
|
export const storage = async (name) => {
|
|
if (!name) throw new SyntaxError("The storage name must be defined");
|
|
|
|
const store = new IDBMapSync(`@pyscript/${name}`);
|
|
const map = new Map();
|
|
await store.sync();
|
|
for (const [k, v] of store.entries()) map.set(k, from_idb(v));
|
|
|
|
const clear = () => {
|
|
map.clear();
|
|
store.clear();
|
|
};
|
|
|
|
const sync = async () => {
|
|
await store.sync();
|
|
};
|
|
|
|
return new Proxy(map, {
|
|
ownKeys: (map) => [...map.keys()],
|
|
has: (map, name) => map.has(name),
|
|
get: (map, name) => {
|
|
if (name === "clear") return clear;
|
|
if (name === "sync") return sync;
|
|
return map.get(name);
|
|
},
|
|
set: (map, name, value) => {
|
|
map.set(name, value);
|
|
store.set(name, to_idb(value));
|
|
return true;
|
|
},
|
|
deleteProperty: (map, name) => {
|
|
if (map.has(name)) {
|
|
map.delete(name);
|
|
store.delete(name);
|
|
}
|
|
return true;
|
|
},
|
|
});
|
|
};
|