Aller au contenu principal

@w5s/shadow-storage

W5S Shadow storage (@w5s/shadow-storage)

NPM Version License

Attach a Map storage to any host object (e.g. globalThis) through a well-known symbol ($shadowStorage). Values survive hot module reload and are shared across modules and package copies.

Installation​

npm install @w5s/shadow-storage

Usage​

import { globalStorage, useShadowStorage } from '@w5s/shadow-storage';

export const moduleStorage = useShadowStorage({});

export function main(): void {
const key = Symbol('someId');

function nextId() {
const id = (globalStorage.get(key) as number) ?? 0;
globalStorage.set(key, id + 1);
return id;
}

console.log(nextId()); // 0
console.log(nextId()); // 1
console.log(nextId()); // 2
}

License​

MIT © Julien Polo julien.polo@gmail.com

Index

Variables

const$shadowStorage

$shadowStorage: typeof $shadowStorage = ...

Symbol for property hosting the shadow storage

constglobalStorage

globalStorage: ShadowStorage = ...

Get or create a storage Map at globalThis[Symbol.for('@w5s/shadow-storage')]. This storage is persistent through hot module reload and shared across modules.

const key = Symbol('someKey');
if (!globalStorage.has(key)) {
globalStorage.set(key, ['foo', 'bar']);
}
globalStorage.get();// ['foo', 'bar']

constmeta

meta: Readonly<{ name: string; version: string }> = ...
Page Options