Subscribe on changes!

Export RefImpl

avatar
Sep 1st 2020

What problem does this feature solve?

I would like to extend RefImpl to create a Ref which can hold more state. Currently I would have to use customRef because I can't create a valid Ref object by myself without knowing RefSymbol, which is not exported either.

// Use the provided getters/setters
class MyRef<T> extends RefImpl<T>{
    get value(){
       //... do stuff
       return super.value;
    }
    //...
}

// Override the tracking
class MyRef2<T> extends RefImpl<T>{
    get value(){
       track(toRaw(this), "get" /* GET */, 'value');
       //... do stuff
       return this._value;
    }
    //...
}

What does the proposed API look like?

Just export the class and maybe look into setting properties as private or protected.

avatar
Sep 1st 2020

I just found out I can use track and trigger to get everything I need to create a custom reactive class.

avatar
Sep 1st 2020

For now, RefImpl is an implementation detail, and this implementation can change in future. In fact, refs weren't implemented as class instances until RC.7.

It seems like it's not being leaked to public API intentionally.

avatar
Sep 1st 2020

Closing since track and trigger solve the original problem

avatar
Sep 1st 2020

This is intentional. The only reason class being used internally is performance. It's not meant to be extended. Use customRef.

avatar
Sep 1st 2020

The best solution I found is to create a customRef<void> in the constructor and store the given track and trigger callbacks as instance properties, so I can use them whenever I want.

Storing the returned Ref<void> as a property gives me the ability to do watch(myinstance.ref).