Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export function connect(options = {}) {
const id = generateId(options.instanceId);
const opts = {
...options,
instanceId: id,
name: options.name || id,
actionCreators: JSON.stringify(getActionsArray(options.actionCreators || {})),
};
start();
return {
init(state, action) {
send(action || {}, state, 'INIT', opts);
},
subscribe(listener) {
if (!listener) return undefined;
if (!listeners[id]) listeners[id] = [];
listeners[id].push(listener);
relay(type, state, action, nextActionId) {
const message = {
type,
id: this.socket.id,
name: this.instanceName,
instanceId: this.appInstanceId,
};
if (state) {
message.payload = type === 'ERROR' ? state :
stringify(filterState(state, type, this.filters, this.stateSanitizer, this.actionSanitizer, nextActionId));
}
if (type === 'ACTION') {
message.action = stringify(
!this.actionSanitizer ? action : this.actionSanitizer(action.action, nextActionId - 1)
);
message.isExcess = this.isExcess;
message.nextActionId = nextActionId;
} else if (action) {
message.action = action;
}
this.socket.emit(this.socket.id ? 'log' : 'log-noid', message);
}
export function connect(options = {}) {
const id = generateId(options.instanceId);
const opts = {
...options,
instanceId: id,
name: options.name || id,
actionCreators: JSON.stringify(getActionsArray(options.actionCreators || {})),
};
start();
return {
init(state, action) {
send(action || {}, state, 'INIT', opts);
},
subscribe(listener) {
if (!listener) return undefined;
if (!listeners[id]) listeners[id] = [];
listeners[id].push(listener);
return function unsubscribe() {
const index = listeners[id].indexOf(listener);
listeners[id].splice(index, 1);
};
},
init(options) {
this.instanceName = options.name;
this.appInstanceId = getRandomId();
const { blacklist, whitelist } = options.filters || {};
this.filters = getLocalFilter({
actionsBlacklist: blacklist || options.actionsBlacklist,
actionsWhitelist: whitelist || options.actionsWhitelist
});
if (options.port) {
this.socketOptions = {
port: options.port,
hostname: options.hostname || 'localhost',
secure: options.secure
};
} else this.socketOptions = defaultSocketOptions;
this.suppressConnectErrors = options.suppressConnectErrors !== undefined ? options.suppressConnectErrors : true;
this.startOn = str2array(options.startOn);
this.stopOn = str2array(options.stopOn);
this.sendOn = str2array(options.sendOn);
getLiftedState() {
return filterStagedActions(this.getLiftedStateRaw(), this.filters);
}
const liftedAction = liftedState.actionsById[nextActionId - 1];
if (isFiltered(liftedAction.action, this.filters)) return;
this.relay('ACTION', state, liftedAction, nextActionId);
if (!this.isExcess && maxAge) this.isExcess = liftedState.stagedActionIds.length >= maxAge;
} else {
if (this.lastAction === 'JUMP_TO_STATE') return;
if (this.lastAction === 'PAUSE_RECORDING') {
this.paused = liftedState.isPaused;
} else if (this.lastAction === 'LOCK_CHANGES') {
this.locked = liftedState.isLocked;
}
if (this.paused || this.locked) {
if (this.lastAction) this.lastAction = undefined;
else return;
}
this.relay('STATE', filterStagedActions(liftedState, this.filters));
}
}
function getLiftedState(store, filters) {
return filterStagedActions(store.liftedStore.getState(), filters);
}
function checkForReducerErrors(liftedState, instance) {
if (liftedState.computedStates[liftedState.currentStateIndex].error) {
relay('STATE', filterStagedActions(liftedState, instance.filters), instance);
return true;
}
return false;
}
if (isFiltered(liftedAction.action, filters)) return;
if (predicate && !predicate(state, liftedAction.action)) return;
relay('ACTION', state, instance, liftedAction, nextActionId);
if (!isExcess && maxAge) isExcess = liftedState.stagedActionIds.length >= maxAge;
} else {
if (lastAction === 'JUMP_TO_STATE') return;
if (lastAction === 'PAUSE_RECORDING') {
paused = liftedState.isPaused;
} else if (lastAction === 'LOCK_CHANGES') {
locked = liftedState.isLocked;
}
if (paused || locked) {
if (lastAction) lastAction = undefined;
else return;
}
relay('STATE', filterStagedActions(liftedState, filters), instance);
}
}
? state
: stringify(
filterState(
state,
type,
filters,
stateSanitizer,
actionSanitizer,
nextActionId,
predicate
),
serializeState
);
}
if (type === 'ACTION') {
message.action = stringify(
!actionSanitizer ? action : actionSanitizer(action.action, nextActionId - 1),
serializeAction
);
message.isExcess = isExcess;
message.nextActionId = nextActionId;
} else if (instance) {
message.libConfig = {
type: 'redux',
actionCreators: stringify(instance.actionCreators),
serialize: !!instance.serialize,
};
}
postMessage({ __IS_REDUX_NATIVE_MESSAGE__: true, content: message });
}