Open mistyharsh opened 6 years ago
Here is another attempt at segregation:
@Component({})
export default class QuickSearch extends Vue {
@Prop()
public placeholder: string;
private searchQuery: string = '';
@Observe('click$')
public observable1(x: Subject<any>): Observable<any> {
// Do entire observable pipeline here .pipe()
// This function will be executed once during the creation of component
// Output of this function should be a stream that will be registered by name passed to the decorator
// NOT SETTER. CANNOT MUTATE STATE OF THE COMPONENT
return x.pipe(
map((_) => Math.random())
);
}
@Subscribe('click$')
public subscription1(subscribedData: any) {
// Subscribe to click$ automatically with this function
// Each time click$ emits, this function is executed
// MUTATIONS TO COMPONENT STATE ARE ALLOWED
}
@Subscribe('click$', { observer: true })
public subscription2(obs: Observable<any>): Subscription {
// Subscribe to click$ and get observer in case you wish to handle errors
// This function will be executed only once to register observer.
// Or directly return observer from the function.
// It is mandatory to return subscription, otherwise memory leakage
// MUTATIONS TO COMPONENT STATE ARE ALLOWED
return obs.subscribe({
next: () => {},
error: () => {},
complete: () => {}
});
}
}
Bad things:
Strings - vue-streams
happened because Andre challenged me to avoid Strings. The only solution I found was the sources
object (which is essentially a "config" which is read by the library to set up sources. I'm not claiming the "sources config" is the best solution, but I think it conveys the intent of "setup sources" and offers a nice developer experience compared to anything else I came up with.
Big Subscriptions Function - subscriptions
mirrors what data
does in Vue
. It's a function that returns an object where the props are rendered in the template. I'm happy with this approach because it's a natural pair to the Vue way of doing things. If you think the function is getting too big, maybe the solution is to create more, smaller components instead.
Avoiding Custom Directives - I initially tried to copy vue-rx
's approach of fromEvent
, but directives are not supported on non-dom elements like transition
which fire custom events. So instead I just allow my sources to be called as methods (bound to .next
) so that it supports anything that can be calleg from a Vue template.
Avoiding this
- Love it or hate it, this
plays a huge part of Vue's internal reactivity and lifecycle system. Evan's approach (the creator of Vue) in vue-rx
was very similar to what I did. I think trying to avoid this
in Vue is fighting against the framework.
Hi @johnlindquist,
Thanks for creating vue-streams. I am following your Vue-Rx experiments as I am also investigating approaches to bring reactive extensions to Vue.js in an idiomatic way. My first approach was to harness class-based component approach but keep Cycle.js like MVI semantics:
However, I am currently stuck on how to handle event from the component
$emit(eventName, data)
. I could not find an elegant way.I played with vue-streams. Here are my observations.
Great things:
<button @click="click$">Click me</button>
. Compared tovue-rx
, we don't have to writedomStreams
andv-stream
. The syntax for events using@
annotation makes it clear between what is a prop, event, and directive.Things that can be improved/added:
|
operator)this
pointer and restrictthis
to purely act as aViewModel
forView
Vue Template.Please let me know how I can help or contribute in any way!!!