Skip to content

Commit

Permalink
added localstorage support for workers
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarotero committed Oct 5, 2024
1 parent ec70590 commit 60972eb
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 4 deletions.
16 changes: 14 additions & 2 deletions cli/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ export function build(
}

worker = new Worker(workerUrl, { type: "module" });
worker.postMessage({
type: "localStorage",
data: { ...localStorage },
});

worker.postMessage({
type,
Expand All @@ -31,8 +35,16 @@ export function build(
});

worker.onmessage = (event) => {
if (event.data.type === "reload") {
init();
switch (event.data.type) {
case "reload":
init();
break;

case "localStorage": {
const { method, args } = event.data;
localStorage[method](...args);
break;
}
}
};
}
Expand Down
1 change: 1 addition & 0 deletions cli/build_worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import noCors from "../middlewares/no_cors.ts";
import notFound from "../middlewares/not_found.ts";
import reload from "../middlewares/reload.ts";
import { buildSite } from "./utils.ts";
import "./missing_worker_apis.ts";

interface BuildOptions {
type: "build" | "rebuild";
Expand Down
16 changes: 14 additions & 2 deletions cli/cms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,27 @@ export function runCms(
}

worker = new Worker(workerUrl, { type: "module" });
worker.postMessage({
type: "localStorage",
data: { ...localStorage },
});

worker.postMessage({
type,
config,
});

worker.onmessage = (event) => {
if (event.data.type === "reload") {
init();
switch (event.data.type) {
case "reload":
init();
break;

case "localStorage": {
const { method, args } = event.data;
localStorage[method](...args);
break;
}
}
};
}
Expand Down
1 change: 1 addition & 0 deletions cli/cms_worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { normalizePath } from "../core/utils/path.ts";
import { fromFileUrl } from "../deps/path.ts";
import { setEnv } from "../core/utils/env.ts";
import { createSite } from "./utils.ts";
import "./missing_worker_apis.ts";

interface CMSOptions {
type: "build" | "rebuild";
Expand Down
67 changes: 67 additions & 0 deletions cli/missing_worker_apis.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// deno-lint-ignore-file no-explicit-any

class S implements Storage {
#length: number = 0;
#type?: string | undefined;
[key: string]: any;

constructor(type?: string) {
this.#type = type;
}

get length() {
return this.#length;
}

clear() {
this.#length = 0;
Object.keys(this).forEach((key) => {
delete this[key];
});
this.#sync("clear");
}

getItem(key: string): string | null {
return this[key] ?? null;
}

key(index: number): string | null {
return Object.keys(this)[index] ?? null;
}

removeItem(key: string) {
delete this[key];
this.#length--;
this.#sync("removeItem", key);
}

setItem(key: string, value: any) {
this[key] = value;
this.#length++;
this.#sync("setItem", key, value);
}

#sync(method: string, ...args: unknown[]) {
if (this.#type) {
globalThis.postMessage({
type: this.#type,
method,
args,
});
}
}
}

globalThis.addEventListener("message", (event) => {
if (event.data.type === "localStorage") {
const storage = new S("localStorage");

for (const [key, value] of Object.entries(event.data.data)) {
storage.setItem(key, value);
}

globalThis.localStorage = storage;
}
});

globalThis.sessionStorage = new S();

0 comments on commit 60972eb

Please sign in to comment.