NullVoxPopuli/ember-resources

Q: nested factory

Closed this issue · 8 comments

basz commented

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}°` : '-';
    };
  });
});

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?

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

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

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

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

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

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

repl

thx again!

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