Skip to content

Log action name + params #197

@HTMHell

Description

@HTMHell

I'd like to console.log each time an action is being called, with the passed params.

For example:

increment, 1
decrement, 2
setName, foo

etc.

This is my middleware:

const logger: TLoggerImpl = (config, logger) => (set, get, api) => {
  const originalConfig = config(set, get, api);
  const newActions = Object.fromEntries(
    Object.entries(originalConfig).map(([actionName, actionFn]) => {
      let enhancedFn = actionFn;
      if (typeof actionFn === "function") {
        enhancedFn = (...args: unknown[]) => {
          const ret = actionFn(...args);
          logger(actionName, args);
          return ret;
        };
      }
      return [actionName, enhancedFn];
    }),
  );
  return { ...originalConfig, ...newActions };
};

It works when I wrap my store before zundo, but it's not ideal because I'm using debounce with handleSet.

I've seen a suggestion to use wrapTemporal here #184.

So I did:

        wrapTemporal: (storeInitializer) =>
          logger(storeInitializer, (fnName, fnArgs) => {
            console.log(`${fnName} called with ${JSON.stringify(fnArgs)}`);
          }),

But it only catches _handleSet with old state and new state.

Is there a way to do it?

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs reproA reproduction is needed

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions