Giter VIP home page Giter VIP logo

Comments (7)

foxnewsnetwork avatar foxnewsnetwork commented on May 19, 2024

Not sure if you mean what I think you mean, but I really needed this feature and ember-cpm's computedPromise just wasn't cutting it (due to an infinite loop bug and a state sharing bug).

Here's what I did:

computedTask = (depKeys..., fn) ->
  computed (key) ->
    unless @[key + "Task"]?
      @[key + "Task"] = task =>
        @[key + "TaskCurrentResult"] = yield fn.call @
        @notifyPropertyChange key
      .restartable()
      for depKey in depKeys
        @addObserver depKey, @, -> 
          @get(key + "Task").perform()
      @get(key + "Task").perform()
    @[key + "TaskCurrentResult"]

Then, I use this like so:

DS.Model.extend
  comments: hasMany "comment", async: true
  isApproved: computedTask "comments.lastObject", ->
    @get "comments"
    .then (comments) ->
      comments.get("lastObject")
    .then (comment) ->
      comment.get("message") is "lgtm"

But yeah, it'd be great if ember-concurrency could have observable computed property feature. Also, if my implementation isn't too much cancer, I can do a pr if no one else is working on this already.

from ember-concurrency.

machty avatar machty commented on May 19, 2024

@foxnewsnetwork I was actually referring to RxJS Observables, of which I have (an unpublished) spike here: http://ember-concurrency.com/#/docs/examples/observables

I'm not sure I totally follow but are you saying you want a task to be performed when some observed property changes?

from ember-concurrency.

foxnewsnetwork avatar foxnewsnetwork commented on May 19, 2024

Yes, exactly that. The Task api already has an on method to listen to events, but I wound up needing an observes method to handle a lot of the ember-data async logic.

But lol, I guess that's another feature for another day.

from ember-concurrency.

machty avatar machty commented on May 19, 2024

@foxnewsnetwork yeah, I'm not ready to add that feature just yet until we've all had time to absorb the current API. There might just be another task-friendly approach to what you're trying to do that avoids the ugliness of observers.

from ember-concurrency.

opsb avatar opsb commented on May 19, 2024

@machty I'd love to see the observable integration in. Our ember app needs to poll our backend and an Observable would be perfect for this. We're also aiming to get a release of orbit.js out soon which uses Observables for realtime queries(rxjs under the hood), would be awesome if those could be unsubscribed automatically in components.

from ember-concurrency.

20v100 avatar 20v100 commented on May 19, 2024

I have a similar need. I often use this pattern:

  myPromiseComputedProperty: computed('a', 'b', function() {
    const _this = this;
    let prom = new Ember.RSVP.Promise(function(resolve, reject) {
      let myVar = null; 
      myVar = _this.get('myTask').perform();
      if(!myVar) {
        reject("error");
      } else {
        resolve(myVar);
      }
    });
    return DS.PromiseObject.create({ promise: prom });
  }),

  myTask: task(function * () {
     ...
  })

It does not work with an Ember-Concurrency task. Maybe I have the wrong approach/antipattern. Happy to hear your take on it.

from ember-concurrency.

jasimon avatar jasimon commented on May 19, 2024

@20v100 you shouldn't need to wrap in a promise

let prom = this.get('myTask').perform();
return DS.PromiseObject.create({ promise: prom });

from ember-concurrency.

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.