Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Verbose flag on methods that console.log #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "redux-iframe",
"description": "Build stateful micro frontends by sharing Redux state and actions between iframe modules and container applications",
"version": "1.1.0",
"version": "1.1.1",
"license": "MIT",
"keywords": [
"redux",
Expand Down
8 changes: 6 additions & 2 deletions src/events/event-listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ type EventListenerOptions = {
* or if the arrays <code>actionsToSend</code> and <code>acceptedActions</code> are disjoint.
*/
addMarker: boolean
/**
* If true, log all received messages from iFrame and their event.data.
*/
verbose: boolean
}

/**
Expand All @@ -23,9 +27,9 @@ type EventListenerOptions = {
* @param acceptedActions array of types (strings) of accepted actions
* @param options parameters to control the event listener
*/
export const installEventListener = (store: Store, acceptedActions: Array<string>, options: EventListenerOptions = { addMarker: true }) => {
export const installEventListener = (store: Store, acceptedActions: Array<string>, options: EventListenerOptions = { addMarker: true, verbose: false }) => {
window.addEventListener('message', event => {
console.log('Received message', event.data)
if (options.verbose) console.log('Received message', event.data)
try {
const action: AnyAction = JSON.parse(event.data)
if (action.type && acceptedActions.includes(action.type)) {
Expand Down
5 changes: 3 additions & 2 deletions src/events/event-sender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ export const createModuleEventSender = (actionsToSend: Array<string>, iFrameId:
* @param windowSelector function that returns the target window for the message
* @param actionsToSend array of types (strings) of actions to be sent to the target window
* @param targetOrigin the target origin (URL), defaults to the URL of the current window
* @param verbose boolean that logs sent message to console if true
*/
const createEventSenderMiddleware = (windowSelector: WindowSelector, actionsToSend: Array<string>, targetOrigin: string): Middleware => {
const createEventSenderMiddleware = (windowSelector: WindowSelector, actionsToSend: Array<string>, targetOrigin: string, verbose: boolean = false): Middleware => {
return (store: MiddlewareAPI<Dispatch<AnyAction>>) => {
return (next: Dispatch<AnyAction>) => {
return (action: AnyAction) => {
Expand All @@ -43,7 +44,7 @@ const createEventSenderMiddleware = (windowSelector: WindowSelector, actionsToSe
const targetWindow = windowSelector()
if (targetWindow) { // Target window (iframe) may not be loaded at the moment
const message = JSON.stringify(action)
console.log('Sending message', message)
if (verbose) console.log('Sending message', message)
targetWindow.postMessage(message, targetOrigin)
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/global/parent-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ import { Store } from 'redux'
*
* @param keys array of Redux top-level keys to copy from
* @param globalName name of the global variable holding the Redux store
* @param verbose boolean that logs loaded state to console if true
* @return a slice of the store of the parent window or undefined if no parent window exists
*/
export const getParentState = (keys: Array<string>, globalName: string = 'ReduxStore'): Object | undefined => {
export const getParentState = (keys: Array<string>, globalName: string = 'ReduxStore', verbose: boolean = false): Object | undefined => {
if (window.parent && typeof (window.parent as any)[globalName] === 'object') {
const parentStore: Store = (window.parent as any)[globalName]
if (typeof parentStore.getState === 'function') {
const parentState = parentStore.getState()
const filteredState = filterState(parentState, keys)
// TODO: Remove or configure log output
console.log('Loaded state from parent window:', filteredState)
if (verbose) console.log('Loaded state from parent window:', filteredState)
return filteredState
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/storage/web-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ const DEFAULT_STORAGE_OPTIONS : StorageOptions = {
*
* @param keys array of Redux top-level keys to load
* @param options storage options (which default to rootKey: 'state' and storage: SESSION)
* @param verbose boolean that logs loaded state to console if true
*/
export const getStoredState = (keys: Array<string>, options: StorageOptions = DEFAULT_STORAGE_OPTIONS): Object | undefined => {
export const getStoredState = (keys: Array<string>, options: StorageOptions = DEFAULT_STORAGE_OPTIONS, verbose: boolean = false): Object | undefined => {
const storage = getStorage(options.storageType || StorageType.SESSION)
if (storage) {
try {
Expand All @@ -52,8 +53,7 @@ export const getStoredState = (keys: Array<string>, options: StorageOptions = DE
return undefined
}
const filteredState = filterState(JSON.parse(serializedState), keys)
// TODO: Remove or configure log output
console.log('Loaded state from storage:', filteredState)
if (verbose) console.log('Loaded state from storage:', filteredState)
return filteredState
} catch (err) {
console.warn('Cannot read from storage:', err)
Expand Down Expand Up @@ -102,15 +102,15 @@ const getStorage = (storageType: StorageType) => {
*
* @param state the Redux state object
* @param options storage options (which default to rootKey: 'state' and storage: SESSION)
* @param verbose boolean that logs saved state to console if true
* @return true if storing succeeded, false otherwise
*/
const saveState = (state: Object, options: StorageOptions): boolean => {
const saveState = (state: Object, options: StorageOptions, verbose: boolean = false): boolean => {
const storage = getStorage(options.storageType || StorageType.SESSION)
if (storage) {
try {
if (state) {
// TODO: Remove or configure log output
console.log('Saved state to storage:', state)
if (verbose) console.log('Saved state to storage:', state)
storage.setItem(options.rootKey || ROOT_KEY, JSON.stringify(state))
return true
}
Expand Down