@hsl-fi/shared-local-storage
v0.4.0
Published
## 1. Listening: Mount `SharedLocalStorageObserver` in some file that will be mounted on every page
Downloads
127
Keywords
Readme
Shared-local-storage
1. Listening: Mount SharedLocalStorageObserver
in some file that will be mounted on every page
import SharedLocalStorageObserver from '@hsl-fi/shared-local-storage';
const keys = ['saved-searches', 'favouriteStore']; // LocalStorage-keys you want to observe
const url = "https://www.hsl.fi/local-storage-emitter"; // Url you want to receive the messages from
<SharedLocalStorageObserver
keys={['saved-searches', 'favouriteStore']}
url={url}
/>
2. Sending: Create local-storage-emitter
-page with following component:
import {useSharedLocalStorageEmitter} from '@hsl-fi/shared-local-storage';
const keys = ['saved-searches', 'favouriteStore'];
const targetOrigin = 'https://www.hsl.fi';
const LocalStorageEmitter = () => {
useSharedLocalStorageEmitter(keys, targetOrigin);
return <div aria-hidden="true">LocalStorageEmitter</div>;
};
export default LocalStorageEmitter;
How it works
Example:
User selects a destination from autosuggest-list in HSL.fi
- HSL.fi:
- Selected destination is saved to localStorage using
save
-function fromOldSearchesStore
saved-searches-updated-at
-timestamp is updatedsaved-searches
-value is updated
- Selected destination is saved to localStorage using
- HSL.fi:
User is redirected to Reittiopas
- HSL.fi/local-storage-emitter (in iframe):
- Sends a postMessage: current
saved-searches
in HSL.fi
- Sends a postMessage: current
- Reittiopas:
- Detects the postMessage using
SharedLocalStorageObserver
saved-searches
will be replaced with the one from the postMessage, sincesaved-searches-updated-at
is greater than the current value
- Detects the postMessage using
- HSL.fi/local-storage-emitter (in iframe):
User selects a starting point or a destination in the autosuggest-list of the route search in Reittiopas
- Reittiopas:
- Selected destination is saved to localStorage using
save
-function fromOldSearchesStore
saved-searches-updated-at
-timestamp is updatedsaved-searches
-value is updated
- Selected destination is saved to localStorage using
- Reittiopas:
User reloads the Reittiopas page
- HSL.fi/local-storage-emitter (in iframe):
- Sends a postMessage: current
saved-searches
in HSL.fi
- Sends a postMessage: current
- Reittiopas:
- Detects the postMessage using
SharedLocalStorageObserver
saved-searches
will NOT be replaced with the one from the postMessage, sincesaved-searches-updated-at
is not greater than the current value
- Detects the postMessage using
- HSL.fi/local-storage-emitter (in iframe):