NullVoxPopuli / ember-resources

An implementation of Resources. Supports ember 3.28+
https://github.com/NullVoxPopuli/ember-resources/blob/main/docs/docs/README.md
MIT License
91 stars 37 forks source link

Q: nested factory #1044

Closed basz closed 8 months ago

basz commented 8 months ago

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 commented 8 months ago

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 commented 8 months ago

repl

NullVoxPopuli commented 8 months ago

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 commented 8 months ago

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 commented 8 months ago

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 commented 8 months ago

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 commented 8 months ago

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

repl

thx again!

NullVoxPopuli commented 8 months ago

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: https://github.com/NullVoxPopuli/ember-resources/issues/1012