Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/jolly-states-train.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@solid-primitives/storage": patch
---

sync API: allow for url specification
8 changes: 4 additions & 4 deletions packages/storage/src/persisted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,22 +229,22 @@ export const storageSync: PersistenceSyncAPI = [
/**
* messageSync - synchronize over post message or broadcast channel API
*/
export const messageSync = (channel: Window | BroadcastChannel = window): PersistenceSyncAPI => [
export const messageSync = (channel: Window | BroadcastChannel = window, url = globalThis.location?.href): PersistenceSyncAPI => [
(subscriber: PersistenceSyncCallback) =>
channel.addEventListener("message", ev => {
subscriber((ev as MessageEvent).data);
}),
(key, newValue) =>
channel.postMessage(
{ key, newValue, timeStamp: Date.now(), url: location.href },
{ key, newValue, timeStamp: Date.now(), url },
location.origin,
),
];

/**
* wsSync - syncronize persisted storage via web socket
*/
export const wsSync = (ws: WebSocket, warnOnError: boolean = !!DEV): PersistenceSyncAPI => [
export const wsSync = (ws: WebSocket, warnOnError: boolean = !!DEV, url = globalThis.location?.href): PersistenceSyncAPI => [
(subscriber: PersistenceSyncCallback) =>
ws.addEventListener("message", (ev: MessageEvent) => {
try {
Expand All @@ -263,7 +263,7 @@ export const wsSync = (ws: WebSocket, warnOnError: boolean = !!DEV): Persistence
key,
newValue,
timeStamp: +new Date(),
...(globalThis.window ? { url: location.href } : {}),
...(url ? { url } : {}),
}),
),
];
Expand Down
56 changes: 55 additions & 1 deletion packages/storage/test/persisted.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
latest,
refresh,
} from "solid-js";
import { makePersisted } from "../src/persisted.js";
import { makePersisted, messageSync, storageSync, wsSync, type PersistenceSyncData } from "../src/persisted.js";
import { type AsyncStorage } from "../src/index.js";

describe("makePersisted", () => {
Expand Down Expand Up @@ -209,3 +209,57 @@ describe("makePersisted", () => {
expect(signal()).toBe("init");
});
});

describe("storageSync", () => {
it("receives messages", () => {
const [message, subscriber] = createSignal<PersistenceSyncData>();
storageSync[0](subscriber);
const event = new StorageEvent(
"storage",
{ key: "test9", newValue: "received", timeStamp: Date.now(), url: "https://storage.solid-primitives.org" }
);
window.dispatchEvent(event);
flush();
expect(message()).toEqual(event);
});
});

describe("messageSync", () => {
it("sends and receives messages", async () => {
const [message, subscriber] = createSignal<PersistenceSyncData>();
const sync = messageSync(window, "https://storage.solid-primitives.org");
sync[0](subscriber);
sync[1]("test10", "sent and received");
await new Promise(r => setTimeout(r, 100));
flush();
expect(message()).toEqual({
key: "test10",
newValue: "sent and received",
timeStamp: expect.any(Number),
url: "https://storage.solid-primitives.org",
});
});
});

describe("wsSync", () => {
it("sends and receives messages", async () => {
const mockWs = {
addEventListener: (name: string, handler: (ev: unknown) => void) => {
expect(name).toBe("message");
mockWs.send = (data) => handler(new MessageEvent("message", { data }));
},
};
const [message, subscriber] = createSignal<PersistenceSyncData>();
const sync = wsSync(mockWs, true, "https://storage.solid-primitives.org");
sync[0](subscriber);
sync[1]("test11", "sent and received");
await new Promise(r => setTimeout(r, 100));
flush();
expect(message()).toEqual({
key: "test11",
newValue: "sent and received",
timeStamp: expect.any(Number),
url: "https://storage.solid-primitives.org",
});
});
});
Loading