Skip to content

Commit

Permalink
jazz: add file watching and make sure library works in window and worker
Browse files Browse the repository at this point in the history
  • Loading branch information
progrium committed Mar 9, 2024
1 parent de1f296 commit 7f80f78
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
2 changes: 1 addition & 1 deletion external/jazz/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
build:
npm install
esbuild jazz.ts --bundle --outfile=jazz.min.js --format=esm --minify
echo "var window = globalThis; var localStorage = {};" | cat - jazz.min.js > ../../kernel/jazz/jazz.min.js
echo "var window = globalThis; if (typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope) { var localStorage = {}; } else { var localStorage = globalThis.localStorage; }" | cat - jazz.min.js > ../../kernel/jazz/jazz.min.js
38 changes: 35 additions & 3 deletions external/jazz/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
createBinaryStreamFromBlob,
readBlobFromBinaryStream,
autoSubResolution,
autoSub
} from "jazz-browser";
import { CoMap, BinaryCoStream } from "cojson";

Expand Down Expand Up @@ -200,6 +201,8 @@ export async function nodeRemove(n, name) {
await nodeTouch(n);
}



export async function walk(path: string): any {
path = cleanPath(path);
let cur = await window.jazz.root();
Expand All @@ -211,9 +214,6 @@ export async function walk(path: string): any {
if (!name) {
continue;
}
if (!cur.isDir) {
break;
}
let names = await nodeDir(cur);
if (!names.includes(name)) {
return null;
Expand All @@ -223,6 +223,20 @@ export async function walk(path: string): any {
return undefined;
}
}
if (!cur.isDir && !mtimes[cur.id]) {
// setup change tracking
autoSub(cur.id, globalThis.node, (file) => {
if (!mtimes[file.id]) {
mtimes[file.id] = file?.meta.coValue.get("mtime");
return;
}
if (mtimes[file.id] < file?.meta.coValue.get("mtime")) {
const event = new CustomEvent("change", {detail: {path, ...file?.meta.coValue.toJSON()}});
watches.dispatchEvent(event);
mtimes[file.id] = file?.meta.coValue.get("mtime");
}
});
}
return cur;
}

Expand Down Expand Up @@ -325,3 +339,21 @@ export async function writeFile(path, content) {
return true;
}

// file watching

const mtimes = {};
const watches = new EventTarget();

export function watch(path, cb) {
const listener = (e) => {
if (e.detail.path.startsWith(path)) {
cb(e);
}
}
watches.addEventListener("change", listener);
return listener;
}

export function unwatch(listener) {
watches.removeEventListener("change", listener);
}
8 changes: 6 additions & 2 deletions external/jazz/jazz.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,16 @@ export async function initNode(name, domain, clientID, accessToken, migration) {
}

export async function initJazz(globalObj) {
let setupFn = setupFrameSpace;
let getItem = async (key) => localStorage.getItem(key);
let setupFn = undefined;
let getItem = undefined;
if (globalObj.hostURL) {
// in worker
setupFn = setupWorkerSpace;
getItem = async (key) => {return (await globalObj.sys.call("host.getItem", [key])).value};
} else {
// in frame
setupFn = setupFrameSpace
getItem = async (key) => globalObj.localStorage.getItem(key);
}

const jazzEnabled = await getItem("jazz:enabled");
Expand Down

0 comments on commit 7f80f78

Please sign in to comment.