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

Methods like provide are lost when implementing Higher Level Actor Logic #4868

Open
sourcec0de opened this issue Apr 25, 2024 · 1 comment · May be fixed by #4871
Open

Methods like provide are lost when implementing Higher Level Actor Logic #4868

sourcec0de opened this issue Apr 25, 2024 · 1 comment · May be fixed by #4871

Comments

@sourcec0de
Copy link

Bug or feature request?

It would be great to have a more robust way of extending existing machines/actors.

Description:

Hello 👋 , something we noticed in the doc for higher-level-actor-logic

The spread operator is used to preserve the original actor logic.
Doing this seems to drop the original prototype information so methods like provide go missing.

(Bug) Expected result:

When using a higher-order function to extend a machine, I would expect all types and original prototype methods to pass through.

i.e this should type correctly, and the function should be present.

withExtendedLogic(machine).provide({})

(Bug) Actual result:

provide is not a function

(Bug) Potential fix:

Instead of this

function withLogic(logic) {
  return { 
    ...logic, 
    transition() {}
  }
}

We ended up with this to preserve those methods.
Also, shout out to @wfischer42 on our team at Discern.
He's the one who implemented this fix.

export const cloneInstance = <T extends object>(input: T): T => {
  const prototypeDescriptors = Object.getOwnPropertyDescriptors(
    Object.getPrototypeOf(input),
  )
  const protoClone = Object.create(null, prototypeDescriptors)
  return Object.create(protoClone, Object.getOwnPropertyDescriptors(input)) as T
}

function withLogic(logic) {
  const logicClone = cloneInstance(logic)
  logicClone.transition = () => {}
  
  return logicClone
}
@davidkpiano davidkpiano linked a pull request Apr 26, 2024 that will close this issue
@sourcec0de
Copy link
Author

sourcec0de commented Apr 29, 2024

@davidkpiano we ran across another issue that seems related.
It appears that calling provide after extending with a higher-level machine function reverts to the previous machines' logic.

const machine = createMachine()
const machineWithLogging = withLogging(machine)
machineWithLogging.provide() // returns to old behavior without logging

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.

1 participant