Skip to content
This repository was archived by the owner on Aug 21, 2024. It is now read-only.
This repository was archived by the owner on Aug 21, 2024. It is now read-only.

Remove redundant Action/Receptor pattern for state services #8305

@HexaField

Description

@HexaField

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)
  }
})

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions