you need to get the handle each time
This commit is contained in:
parent
32ea38177e
commit
7a54689f76
24
app.js
24
app.js
@ -27,15 +27,25 @@ async function PickHandle()
|
|||||||
}
|
}
|
||||||
async function LoadHandleFiles()
|
async function LoadHandleFiles()
|
||||||
{
|
{
|
||||||
try
|
console.log("fetching room.js", handle);
|
||||||
|
if(handle)
|
||||||
{
|
{
|
||||||
const module = await import("./fsx/data/room.js"+"");
|
try
|
||||||
/** @type {Record<string, TYPES.GraphParts>} */
|
{
|
||||||
const read = module.default();
|
const module = await import("./graph/room.js"+"?bust="+Math.random());
|
||||||
rooms.val = read;
|
/** @type {Record<string, TYPES.GraphParts>} */
|
||||||
|
const read = module.default();
|
||||||
|
rooms.val = read;
|
||||||
|
}
|
||||||
|
catch(_e)
|
||||||
|
{
|
||||||
|
console.log("the handle exists, but the request failed. the service work must not be ready yet.")
|
||||||
|
rooms.val = {};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch(_e)
|
else
|
||||||
{
|
{
|
||||||
|
console.log("no fs handle has been set, cannot get room graph")
|
||||||
rooms.val = {};
|
rooms.val = {};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -49,7 +59,7 @@ await LoadHandleFiles();
|
|||||||
/** @type {(path:string[], part:string, time:number, data:string)=>void} */
|
/** @type {(path:string[], part:string, time:number, data:string)=>void} */
|
||||||
async function WRITE(path, part, time, data)
|
async function WRITE(path, part, time, data)
|
||||||
{
|
{
|
||||||
const fileHandle = await FSHandle.drilldown(handle, path, true);
|
const fileHandle = await FSHandle.Dig(handle, path, true);
|
||||||
if(fileHandle)
|
if(fileHandle)
|
||||||
{
|
{
|
||||||
const file = await fileHandle.getFile();
|
const file = await fileHandle.getFile();
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
/** @import * as TYPES from "./types.ts" */
|
/** @import * as TYPES from "./types.ts" */
|
||||||
|
import * as FSAccess from "../store-directory-handle.js";
|
||||||
|
|
||||||
/** @type {TYPES.GraphBuilder} */
|
/** @type {TYPES.GraphBuilder} */
|
||||||
export function Room({user, role, part, desk, pass})
|
export function Room({user, role, part, desk, pass})
|
||||||
@ -118,10 +119,12 @@ export function Room({user, role, part, desk, pass})
|
|||||||
{
|
{
|
||||||
const [userID, userObject] = userData[i];
|
const [userID, userObject] = userData[i];
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
const resp = await fetch(`./fsx/room/${context.Path}/${passID}/${userID}`);
|
const handle = await FSAccess.getDirectoryHandle();
|
||||||
|
const text = await FSAccess.Read(handle, ["store", context.Path, passID, userID+".json"]);
|
||||||
|
console.log("json loaded", text);
|
||||||
/** @type {TYPES.UserPassFile} */
|
/** @type {TYPES.UserPassFile} */
|
||||||
const json = await resp.json();
|
const json = JSON.parse(text);
|
||||||
|
|
||||||
Object.entries(json).forEach(([partID, payload])=>{
|
Object.entries(json).forEach(([partID, payload])=>{
|
||||||
|
|
||||||
|
|||||||
@ -1,9 +1,19 @@
|
|||||||
import * as FSAccess from "./store-directory-handle.js";
|
import * as FSAccess from "./store-directory-handle.js";
|
||||||
|
|
||||||
self.addEventListener('install', ()=> self.skipWaiting()); // Activate worker immediately);
|
self.addEventListener('install', ()=>{console.log("SW INSTALL"); return self.skipWaiting()}); // Activate worker immediately);
|
||||||
self.addEventListener('activate', ()=> self.clients.claim()); // Become available to all pages);
|
self.addEventListener('activate', ()=>{
|
||||||
|
console.log("SW ACTIVATE");
|
||||||
|
return self.clients.claim();
|
||||||
|
}); // Become available to all pages);
|
||||||
self.addEventListener('fetch', (event) =>event.respondWith(Interceptor(event)));
|
self.addEventListener('fetch', (event) =>event.respondWith(Interceptor(event)));
|
||||||
|
|
||||||
|
const options = {
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/javascript',
|
||||||
|
'Cache-Control': 'no-cache'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** @type {(event:{request:Request})=>Promise<Response>} */
|
/** @type {(event:{request:Request})=>Promise<Response>} */
|
||||||
async function Interceptor(event)
|
async function Interceptor(event)
|
||||||
{
|
{
|
||||||
@ -11,65 +21,19 @@ async function Interceptor(event)
|
|||||||
const pathname = url.pathname.substring(1);
|
const pathname = url.pathname.substring(1);
|
||||||
let parts = pathname.split("/");
|
let parts = pathname.split("/");
|
||||||
|
|
||||||
if(parts[0] == "fsx")
|
if(parts[0] == "graph")
|
||||||
{
|
{
|
||||||
parts = parts.slice(1);
|
|
||||||
console.log("intercept:", pathname);
|
console.log("intercept:", pathname);
|
||||||
|
|
||||||
if(parts[0] == "room")
|
|
||||||
{
|
|
||||||
parts[parts.length-1] = parts[parts.length-1]+".json";
|
|
||||||
}
|
|
||||||
|
|
||||||
const handle = await FSAccess.getDirectoryHandle();
|
const handle = await FSAccess.getDirectoryHandle();
|
||||||
if(handle)
|
const text = await FSAccess.Read(handle, parts);
|
||||||
|
if(text)
|
||||||
{
|
{
|
||||||
if(event.request.method == "PUT")
|
return new Response(text, options);
|
||||||
{
|
|
||||||
const body = await event.request.text();
|
|
||||||
console.log("PUT intercept", parts, body);
|
|
||||||
const fileHandle = await FSAccess.drilldown(handle, parts, true);
|
|
||||||
|
|
||||||
if(fileHandle)
|
|
||||||
{
|
|
||||||
|
|
||||||
const writeable = await fileHandle.createWritable();
|
|
||||||
await writeable.write(body);
|
|
||||||
await writeable.close();
|
|
||||||
return new Response("OK", {
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/javascript',
|
|
||||||
'Cache-Control': 'no-cache'
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return new Response("404", {status:404});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const fileHandle = await FSAccess.drilldown(handle, parts);
|
|
||||||
if(fileHandle)
|
|
||||||
{
|
|
||||||
const file = await fileHandle.getFile();
|
|
||||||
const content = await file.text();
|
|
||||||
|
|
||||||
return new Response(content, {
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/javascript',
|
|
||||||
'Cache-Control': 'no-cache'
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log("couldnt find:", pathname);
|
|
||||||
return new Response("404", {status:404});
|
|
||||||
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return fetch(event.request);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return fetch(event.request);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -17,17 +17,22 @@ export async function setDirectoryHandle(handle) {
|
|||||||
const db = await openDB();
|
const db = await openDB();
|
||||||
const tx = db.transaction('handles', 'readwrite');
|
const tx = db.transaction('handles', 'readwrite');
|
||||||
tx.objectStore('handles').put(handle, 'user-folder');
|
tx.objectStore('handles').put(handle, 'user-folder');
|
||||||
|
console.log("handle set", handle);
|
||||||
await tx.done;
|
await tx.done;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// 📂 Retrieve a directory handle
|
// 📂 Retrieve a directory handle
|
||||||
/** @type {()=>Promise<FileSystemDirectoryHandle|false>} */
|
/** @type {()=>Promise<FileSystemDirectoryHandle|false>} */
|
||||||
export async function getDirectoryHandle() {
|
export async function getDirectoryHandle() {
|
||||||
|
|
||||||
const db = await openDB();
|
const db = await openDB();
|
||||||
const tx = db.transaction('handles', 'readonly');
|
const tx = db.transaction('handles', 'readonly');
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const getRequest = tx.objectStore('handles').get('user-folder');
|
const getRequest = tx.objectStore('handles').get('user-folder');
|
||||||
getRequest.onsuccess = () => resolve(getRequest.result);
|
getRequest.onsuccess = () => {
|
||||||
|
return resolve(getRequest.result);
|
||||||
|
}
|
||||||
getRequest.onerror = () => {
|
getRequest.onerror = () => {
|
||||||
console.error('Error retrieving directory handle:', getRequest.error);
|
console.error('Error retrieving directory handle:', getRequest.error);
|
||||||
return resolve(false);
|
return resolve(false);
|
||||||
@ -36,7 +41,7 @@ export async function getDirectoryHandle() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** @type {(handle:FileSystemDirectoryHandle, parts:string[], create?:boolean)=>Promise<FileSystemFileHandle|false>} */
|
/** @type {(handle:FileSystemDirectoryHandle, parts:string[], create?:boolean)=>Promise<FileSystemFileHandle|false>} */
|
||||||
export async function drilldown(handle, parts, create=false)
|
export async function Dig(handle, parts, create=false)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@ -54,6 +59,36 @@ export async function drilldown(handle, parts, create=false)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @type {(handle:FileSystemDirectoryHandle, parts:string[])=>Promise<string|false>} */
|
||||||
|
export async function Read(handle, parts)
|
||||||
|
{
|
||||||
|
|
||||||
|
const fileHandle = await Dig(handle, parts);
|
||||||
|
if(fileHandle)
|
||||||
|
{
|
||||||
|
const file = await fileHandle.getFile();
|
||||||
|
return await file.text();
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @type {(handle:FileSystemDirectoryHandle, parts:string[], text:string)=>Promise<boolean>} */
|
||||||
|
export async function Write(handle, parts, text)
|
||||||
|
{
|
||||||
|
const fileHandle = await Dig(handle, parts);
|
||||||
|
if(fileHandle)
|
||||||
|
{
|
||||||
|
const writeable = await fileHandle.createWritable();
|
||||||
|
await writeable.write(text);
|
||||||
|
await writeable.close();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// // 🔐 Check or request permission
|
// // 🔐 Check or request permission
|
||||||
// async function verifyPermission(handle, mode = 'readwrite') {
|
// async function verifyPermission(handle, mode = 'readwrite') {
|
||||||
// const opts = { mode };
|
// const opts = { mode };
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user