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

Remove redundant Action/Receptor pattern for state services #8305

Closed
HexaField opened this issue Jul 19, 2023 · 0 comments · Fixed by #9066
Closed

Remove redundant Action/Receptor pattern for state services #8305

HexaField opened this issue Jul 19, 2023 · 0 comments · Fixed by #9066
Assignees
Milestone

Comments

@HexaField
Copy link
Member

HexaField commented Jul 19, 2023

Across the whole client, we have all these State/Receptor/Service/Action patterns, they need to be refactored to just State/Service

The idea here originally, was that a service function is called, which dispatches an action, that a receptor receives, and the state mutates. We wanted to control exactly when & how state was updated, but in practice, we only need this for networked actions (of which none of these client ones are, only ones with a specified $topic field are networked)

So we can simplify this a LOT by just mutating state directly inside the service. This should make a lot of code easier to read, as well as spam chat a whole lot less.

// State
export const MyState = defineState({
  name: 'MyState',
  initial: () => ({
    text: ''
  })
})

// Receptor
export const MyServiceReceptor = (action) => {
  const s = getMutableState(MyState)
  matches(action)
    .when(MyAction.setText.matches, (action) => {
      return s.merge({ text: action.text })
    })
}

// Service
export const MyService = {
  setText: () => {
    dispatchAction(MyAction.setText({ text: true }))
  }
}

// Actions
export class MyAction {
  static setText = defineAction({
    type: 'ee.client.me.SET_TEXT' as const,
    text: matches.string
  })
}

becomes

export const MyState = defineState({
  name: 'MyState',

  initial: () => ({
    text: ''
  })

  setText: () => {
    const myState = getMutableState(MyState)
    myState.text.set(true)
  }
})
@aditya-mitra aditya-mitra self-assigned this Jul 20, 2023
This was referenced Jul 20, 2023
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

Successfully merging a pull request may close this issue.

3 participants