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

Q: nested factory #1044

Closed
basz opened this issue Nov 29, 2023 · 8 comments
Closed

Q: nested factory #1044

basz opened this issue Nov 29, 2023 · 8 comments

Comments

@basz
Copy link

basz commented Nov 29, 2023

Are composited factories allowed?

Can't get them to work... Displayes 5° (default value) nothing else, but when i use the ExtruderResource directly it does work...

const ExtruderResource = resourceFactory((printerManager: PrinterManager) => {
  return resource(({ on }) => {
    const lastMessage = cell<number>(5);

    printerManager.on('statusUpdate', (status) => {
      const { extruder } = status;

      if (extruder) {
        lastMessage.set(extruder.temperature);
      }
    });

    on.cleanup(() => {});

    return () => {
      return lastMessage.current;
    };
  });
});

export const ExtruderCurrentTemperature = resourceFactory((printerManager: PrinterManager) => {
  return resource(({ on, use }) => {
    const extruder = use(ExtruderResource(printerManager));

    console.log(extruder.current);

    // const temperature = extruder.current;
    // const temp = cell<number>(temperature);

    //on.cleanup(() => {});

    return () => {
      return !isNaN(extruder.current) ? `${extruder.current}°` : '-';
    };
  });
});
@NullVoxPopuli
Copy link
Owner

NullVoxPopuli commented Nov 29, 2023

Are composited factories allowed?

they are, resourceFactory is a pass-through / no-op utility that is a consequence of current limitations in ember's helper-manager system.

ExtruderCurrentTemperature

This looks like an exciting project!!! 3d printer stuff?

Can't get them to work...

I don't immediately see anything fishy.
Are you using at least 6.4.0? the release notes have an example similar to what you're trying to achieve:

https://github.com/NullVoxPopuli/ember-resources/releases/tag/ember-resources%406.4.0

Any chance you can simulate this example in the REPL? / https://limber.glimdown.com?

@basz
Copy link
Author

basz commented Nov 29, 2023

repl

@NullVoxPopuli
Copy link
Owner

ah!

.current can't be accessed within the resource main body because it would invalidate the whole resource each change (which is why it's safe to access in the Formula arrow fn):

fixed

@basz
Copy link
Author

basz commented Nov 29, 2023

ah i see. makes sense... this is intricate...

one question, why would each resource show a different random number? it's the same resource?

@NullVoxPopuli
Copy link
Owner

NullVoxPopuli commented Nov 29, 2023

one question, why would each resource show a different random number? it's the same resource?

there are two instance of it, so different randomness for each one

this is intricate...

yeah, it get simpler with Starbeam.

the body of the resource can be moved in to an on.sync() or on.setup() call (similar to on.cleanup) (codemod will do this for you), but it makes the situation clearer

@basz
Copy link
Author

basz commented Nov 30, 2023

the body of the resource can be moved in to an on.sync() or on.setup() call (similar to on.cleanup) (codemod will do this for you), but it makes the situation clearer

Could you explain this a little. I do not see any sync or setup method on the ResourceApi (on)?

@basz
Copy link
Author

basz commented Nov 30, 2023

it seems ok to use in it return function... using another cell to 'memorize' the received payload

repl

thx again!

@basz basz closed this as completed Nov 30, 2023
@NullVoxPopuli
Copy link
Owner

I do not see any sync or setup method on the ResourceApi (on)?

Yeah, we can't have it in ember. :(
Have to wait until starbeam apis are available.

Some more info here: #1012

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

No branches or pull requests

2 participants