okwolf/hyperapp-fx

Custom Effects (prev: Promise Effect)

ForsakenHarmony opened this issue · 4 comments

Calls the action with the resolved value

@ForsakenHarmony let's think about what an API could look like for allowing custom effects that can accomplish the same thing.

One thing I can think of is replacing the switch with an extendable object so you can register new effects that way, the creator would still live in user code ig

@ForsakenHarmony One thing I can think of is replacing the switch with an extendable object so you can register new effects that way, the creator would still live in user code ig

This is exactly along the lines I was thinking myself. I haven't started implementing it yet, but in my head, here's what it might look like for a couple of the existing effects:

effects: {
  action(props, getAction) {
    getAction(props.name)(props.data);
  },
  delay(props, getAction) {
    setTimeout(() => {
      getAction(props.action)(props.data);
    }, props.duration);
  },
  http(props, getAction) {
    props.options = props.options || {};
    props.options.response = props.options.response || "json";
    fetch(props.url, props.options)
      .then(response => response[props.options.response]())
      .then(result => getAction(props.action)(result));
  }
}

Each effect would get the props that were passed when the effect was created and a way to get actions for firing when appropriate. Custom effects would be merged in after the core ones in case a user wanted to override their behavior for some reason.

What do you think?