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

Log action name + params #197

Open
HTMHell opened this issue Jan 16, 2025 · 0 comments
Open

Log action name + params #197

HTMHell opened this issue Jan 16, 2025 · 0 comments

Comments

@HTMHell
Copy link

HTMHell commented Jan 16, 2025

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant