js-shared-session 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +53 -2
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -7,13 +7,14 @@ A lightweight, zero-dependency Proxy wrapper around sessionStorage that automati
7
7
  - Drop-in Storage API: Supports standard methods (getItem, setItem, removeItem, clear, key, length) as well as direct property access (store.foo = "bar", delete store.foo, Object.keys(store)).
8
8
  - Namespace Isolation: Prefixes keys internally so multiple instances or regular sessionStorage entries never collide.
9
9
  - Async Hydration (store.ready): Automatically requests existing state from already-open tabs upon creation and resolves when synced.
10
+ - Custom Broadcast Listeners. Attach event listeners to respond to updates from other document instances.
10
11
  - Lifecycle Control: Includes .close() to disconnect a tab's channel and .destroy() to wipe shared state across all open tabs.
11
12
  - TypeScript Ready: Includes full .d.ts type definitions.
12
13
 
13
14
  ## Quick Start
14
15
 
15
16
  ```javascript
16
- import SharedSession from "./SharedSession.js";
17
+ import SharedSession from "js-shared-session";
17
18
  // Create or retrieve a namespaced instance (defaults to "shared")
18
19
  const store = SharedSession("myApp", (event) => {
19
20
  console.log("Synced from another tab:", event.data);
@@ -45,9 +46,59 @@ Creates (or returns an existing) Proxy-wrapped sessionStorage instance for names
45
46
  ### Static Listener Helpers
46
47
 
47
48
  - SharedSession.addBroadcastListener(namespace?, listener): Attaches an additional listener to an active namespace. Returns true if the namespace exists.
48
- - SharedSession.removeBroadcastListener(namespace?, listener): Detaches a previously registered listener.
49
+
50
+ "- SharedSession.removeBroadcastListener(namespace?, listener): Detaches a previously registered listener.
51
+
52
+ Use these helpers to react to cross-tab sync events without holding a reference to the proxy instance. Both accept an optional `namespace` (defaults to `"shared"` when omitted) and require a `listener` function. The listener receives a `MessageEvent` whose `event.data` is `{ name, key, newValue, oldValue }`.
53
+
54
+ > Note: the helpers act on the namespace **within the current tab**, so a `SharedSession` for that namespace must already exist here first. If it does not, `addBroadcastListener` returns `false` (it does not throw).
55
+
56
+ ```javascript
57
+ import SharedSession from "./SharedSession.js";
58
+
59
+ // A SharedSession for "myApp" must exist before adding another listener to it.
60
+ const store = SharedSession("myApp");
61
+ await store.ready;
62
+
63
+ // Named handler so it can be removed later.
64
+ function onSync(event) {
65
+ const { name, key, newValue, oldValue } = event.data;
66
+ // name is one of: "init_response", "setItem", "removeItem", "clear"
67
+ if (name === "setItem") {
68
+ console.log(`"${key}" updated elsewhere: ${oldValue} -> ${newValue}`);
69
+ }
70
+ }
71
+
72
+ // Attach the listener. Returns true because the "myApp" namespace is active.
73
+ const attached = SharedSession.addBroadcastListener("myApp", onSync); // true
74
+
75
+ // Now any setItem/removeItem/clear from OTHER tabs fires onSync in this tab.
76
+
77
+ // Detach when finished. Returns true if the listener was registered.
78
+ SharedSession.removeBroadcastListener("myApp", onSync); // true
79
+ ```
80
+
81
+ **Default namespace.** Pass the function as the _first_ argument to target the default `"shared"` namespace:
82
+
83
+ ```javascript
84
+ SharedSession(); // creates/gets the default "shared" instance
85
+
86
+ const ok = SharedSession.addBroadcastListener((event) => {
87
+ console.log("shared namespace changed:", event.data);
88
+ });
89
+ ```
90
+
91
+ **Namespace not yet created.** Without an active instance in this tab the helper returns `false` instead of throwing:
92
+
93
+ ````javascript
94
+ const attached = SharedSession.addBroadcastListener("cart", () => {}); // false
95
+ if (!attached) {
96
+ console.warn('Create SharedSession("cart") before attaching a listener.');
97
+ }
98
+ ```"
49
99
 
50
100
  ## Security Notes
51
101
 
52
102
  - Data stays strictly in sessionStorage and in-memory BroadcastChannel messages (never written to localStorage or cookies).
53
103
  - Restricted by browser same-origin policy, though any script running on the same origin can read or broadcast to the channel—avoid storing high-privilege secrets if untrusted third-party scripts run on your page.
104
+ ````
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "js-shared-session",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "Zero-dependency Proxy wrapper around sessionStorage that synchronizes namespaced entries across same-origin tabs, windows, and iframes via BroadcastChannel.",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",