Git Product home page Git Product logo

Comments (1)

zoomclub avatar zoomclub commented on July 23, 2024

I'm not comfortable with this integration of RxJS in Vue, I depend on RxJS far to much for toying around. Automatic subscription behind the scenes is magical and limiting and leading to untested errors such as these. I think I'm just going to use the RxJS API where and when I want.

I always liked this approach where everything is in the open:

import Vue from 'vue';
import {Observable, Subject} from 'rxjs';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/delay';
import axios from 'axios';

// This function is a typical Observable.fromEvent
// type of thing. Though specific to Vue JS and making state changes
function createObservable(cb) {
  let state = null;
  const subject = new Subject();
  cb(subject).subscribe(function (change) {
    change(state);
  });
  return function (arg) {
    state = this;
    subject.next(arg);
  }
}

new Vue({
  el: '#app',
  data: {
    posts: [],
    isLoading: false,
    error: null
  },
  methods: {

    // Instead of normal methods we make them expose an observable instead.
    // The returned observable(s) has to map to a function that receives the state
    // that can be changed. Pretty cool!
    fetchClick: createObservable((observable) => {
      const getPosts$ = observable
        .flatMap(() => Observable.fromPromise(axios.get('http://jsonplaceholder.typicode.com/posts')))
        .map(result => result.data)
        .share()

      const resetPosts$ = observable.map(() => state => state.posts = []);
      const startFetching$ = observable.map(() => state => state.isLoading = true);
      const stopFetching$ = getPosts$.map(() => state => state.isLoading = false);
      const setNewPosts$ = getPosts$
        .map(posts => state => state.posts = posts)
        .catch(err => state => state.error = err.message);

      return Observable.merge(
        resetPosts$,
        setNewPosts$,
        startFetching$,
        stopFetching$
      );
    })
  }
})

from vue-rx.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.