Coder Social home page Coder Social logo

Property value about kefir HOT 4 CLOSED

kefirjs avatar kefirjs commented on May 22, 2024
Property value

from kefir.

Comments (4)

rpominov avatar rpominov commented on May 22, 2024

No, there is no way to get current value of property.
If you really need to, you can do something like this:

var notSet = {};
var value = notSet;
var setVal = function(x) {value = x};
obs.onValue(setVal).offValue(setVal);
if (value === notSet) {
  // property has no current value yet
} else {
  // here it is, current value in the `value` variable
}

But don't use ._current it may contain internal special value, or be outdated (updates on activation).

To check if an observable a property, just do obs instanceof Kefir.Property

from kefir.

iofjuupasli avatar iofjuupasli commented on May 22, 2024

It looks like dirty solution. Also it's not clear that onValue on Property calls handler synchronously (and will be sync in future).
Is it possible to add this method to api (getCurrent)? Seems like it's very easy to add now.
Property - is smth that contains value. Do I understand correctly? So, there is value, but we can't get it. A little curiously.

from kefir.

rpominov avatar rpominov commented on May 22, 2024

It looks dirty because it not right thing to do. It not very easy to add such method to the API, but possible (using a hack like one above). But I don't think that method should be in API (I agree with Bacon.js on this https://github.com/baconjs/bacon.js/wiki/FAQ#how-do-i-get-the-latest-value-of-a-property)

from kefir.

rpominov avatar rpominov commented on May 22, 2024

A little more inside on the subject.

As I mentioned before, ._current not always contain current value of a property. It might be outdated and gets updated when you add first subscriber. So first (and all the rest) subscribers always gets correct current value.

This works that way to make possible get current value from any source. For example one can do something like this:

var inputValue = Kefir.fromBinder(function(emitter) {

  // the current value of the property will come from this line
  emitter.emit($input.val()); 

  function emitVal() {emitter.emit($input.val())}
  $input.on('change', emitVal);
  return function() {
    $input.off('change', emitVal);
  }
}).toProperty();

I can't remember now, but there is also another reasons why it works that way. Mostly it important on some edge cases. In early versions of Kefir there was method .getCurrent(), and it was used internally in all places where one observable subscribes to another, but it didn't go well.

Closing it now, I hope I answered your question :)

from kefir.

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.