Skip to content

New functional API #42

@refi64

Description

@refi64

vuejs/rfcs#42 is the new proposed preferred API for Vue 3, and I've been really trying to think hard about how to best apply this to the more traditional OO, nominally typed Dart.

Here's basically the API I have in mind:

@vue
class MyComponentState extends State {
  // Note that there's no explicit Value<...> or Computed wrappers here,
  // because I couldn't think of any way to do that without feeling ugly.
  int count = 0;
  int get plusOne => count + 1;
  void increment() => count++;

  MyComponentState() {
    watch(() => count * 2, (val) => print('count * 2 is $val'));
    onMounted.listen(() => ...);
  }
}

@vue
class MyComponent extends Vue<MyComponentState> {
  final template = '''
    ...
  ''';

  final String prop1;
  final int prop2;

  void setup() => MyComponentState();
}

Merging state

One problem that persist is how we should handle merged state, e.g. the Dart version of this in setup:

const Component = {
  setup() {
    const { x, y } = useMouse()
    const { z } = useOtherLogic()
    return { x, y, z }
  }
}

Most likely this will continue to use Dart's mixins for the best typing support:

class ComponentState extends State with MouseLogic, OtherLogic {
  // MouseLogic, OtherLogic's own constructors initialize their stuff.
  // ...
}

class Component extends Vue<ComponentState> {
  void setup() => ComponentState();
}

However, this does re-introduce the problem of namespace conflicts, which I quite frankly am not sure how to solve yet.

The more basic reactivity API will still be supported (e.g. var v = value(0)), and all State will probably be is some sort of base class with a createVueState method that returns the state in the format Vue wants.

Timeline: I have no idea, this is likely going to be paired with some fundamental backend changes in order to utilize Dart's part system to avoid screwing with line numbers, so it's going to take a bit. Definitely messes with the 0.5/0.6 release plans I was shooting for.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions