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 · 2 comments
Open

Log action name + params #197

HTMHell opened this issue Jan 16, 2025 · 2 comments
Labels
needs repro A reproduction is needed

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?

@charkour
Copy link
Owner

Hi @HTMHell,

Thanks for the note. Right now, I don't have enough information to help solve your issue. I've created a Stackblitz. Could you fork the Stackblitz to provide a reproduction of the issue? Thank you

@charkour charkour added the needs repro A reproduction is needed label Jan 19, 2025
@HTMHell
Copy link
Author

HTMHell commented Jan 19, 2025

@charkour thanks for your reply!

https://stackblitz.com/edit/vitejs-vite-6cv3ufwy?file=src%2FApp.tsx

If you quickly click the button multiple times, you'll see it doesn't log it as much - which is exactly what I need, but when I try to do it like so, the log doesn't look like I need it to.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs repro A reproduction is needed
Projects
None yet
Development

No branches or pull requests

2 participants