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

Design qEffect API #63

Closed
Tracked by #59
mhevery opened this issue Oct 15, 2021 · 0 comments · Fixed by #165
Closed
Tracked by #59

Design qEffect API #63

mhevery opened this issue Oct 15, 2021 · 0 comments · Fixed by #165
Labels
TYPE: enhancement New feature or request

Comments

@mhevery
Copy link
Contributor

mhevery commented Oct 15, 2021

Components have a need for composability. qEffect should provide such a solution.

Example

Code example is worth a thousand words!

Imagine you have a piece of code that reports mouse position. A pseudo-code may look like this:

interface MousePosition {
  x: number;
  y: number;
}

const useMousePosition = qEffect<{}, MousePosition>({
  onMount: () => ({ x: 0, y: 0 }),
  listen: {
    ['on:document:mousemove']: qHook((_, state: MousePosition) => {
      const mEvent = useEvent<MouseEvent>();
      state.x = mEvent.x;
      state.y = mEvent.y;
    }),
  },
});

Such code can then be used like so:

export const Header = qComponent<{}, { mouse: MousePosition }>({
  onMount: qHook(() => ({ mouse: useMousePosition({}) })),
  onRender: qHook((_, { mouse }) => (
    <span>
      {mouse.x}, {mouse.y}
    </span>
  )),
});

Goals

  • The effect should be initialized on server and survive serialization to the client.
  • The effect should be hydrated independently from the component using it.
  • The effect should be composable (used by other effects)
@mhevery mhevery closed this as completed Oct 15, 2021
@mhevery mhevery reopened this Oct 15, 2021
@mhevery mhevery changed the title Design API Design useEffect API Oct 15, 2021
@mhevery mhevery changed the title Design useEffect API Design qEffect API Oct 15, 2021
@mhevery mhevery added the TYPE: enhancement New feature or request label Jan 31, 2022
mhevery added a commit to mhevery/qwik that referenced this issue Feb 1, 2022
mhevery added a commit to mhevery/qwik that referenced this issue Feb 4, 2022
mhevery added a commit to mhevery/qwik that referenced this issue Feb 9, 2022
mhevery added a commit to mhevery/qwik that referenced this issue Feb 9, 2022
mhevery added a commit to mhevery/qwik that referenced this issue Feb 10, 2022
mhevery added a commit that referenced this issue Feb 10, 2022
Fix #63

Co-authored-by: Manu MA <manu.mtza@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
TYPE: enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant