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

splitMap - added targets field #336

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

earthspacon
Copy link
Contributor

@earthspacon earthspacon commented Aug 1, 2024

Description

This PR partially resolves this issue.

Often we need the same behavior as in split — react to derived events received from cases and trigger the corresponding units. In targets field we can pass object with the same keys as in cases and values with units to trigger on the corresponding event.

Maybe we should think about targets naming

type WSEvent =
  | { type: 'init'; key: string }
  | { type: 'increment'; count: number }
  | { type: 'reset' };

export const websocketEventReceived = createEvent<WSEvent>();

const $isInitialized = createStore(false);
const $count = createStore<number | null>(null);

const getInitialDataFx = createEffect<string, void>();

splitMap({
  source: websocketEventReceived,
  cases: {
    init: (event) => {
      if (event.type === 'init') return { init: true, dataId: event.key };
    },
    increment: (payload) => {
      if (payload.type === 'increment') return payload.count;
    },
    reset: ({ type }) => {
      if (type === 'reset') return null;
    },
  },
  targets: {
    // EventCallable<{ init?: boolean; dataId?: string }>
    init: spread({ init: $isInitialized, dataId: getInitialDataFx }),
    increment: $count,
    reset: [$count.reinit, $isInitialized.reinit],
  },
});

Checklist

  • Add tests to src/method-name/method-name.test.ts
  • Add fork tests to src/method-name/method-name.fork.test.ts
  • Add type tests to test-typings/method-name.ts
    • Use // @ts-expect-error to mark expected type error
    • import { expectType } from 'tsd' to check expected return type
  • Add documentation in src/method-name/readme.md
    • Add header Patronum/MethodName
    • Add section with overloads, if have
    • Add Motivation, Formulae, Arguments and Return sections for each overload
    • Add useful examples in Example section for each overload
  • Fill frontmatter in src/method-name/readme.md
    • Add title. Make sure it uses camelCase syntax just like the method itself
    • Add slug. Use param-case to write it. In most cases it will be just like title
    • Add desription with one short sentence describing what method useful for
    • Add group. To categorize method on /operators page. Full list of available groups, you can see in documentation/src/content/config.ts

@earthspacon earthspacon changed the title feat(splitMap): added targets field splitMap - added targets field Aug 1, 2024
@earthspacon
Copy link
Contributor Author

@sergeysova @AlexandrHoroshih 😃

@sergeysova sergeysova self-assigned this Sep 25, 2024
@sergeysova sergeysova added the enhancement Improvement in existing feature label Sep 25, 2024
@sergeysova
Copy link
Member

Added new section to a checklist to help integrating with new documentation website

@earthspacon
Copy link
Contributor Author

earthspacon commented Oct 30, 2024

@sergeysova can you help plz with tests in src/split-map/split-map.fork.test.ts, old tests are failing, maybe because new ones added and snapshot updated?
image

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

Successfully merging this pull request may close these issues.

Add splitMap support match syntax
2 participants