1
0

behavior.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. (function() {
  2. var Behavior, PropertyAccessors, Signal, helpers, isEqual,
  3. __hasProp = {}.hasOwnProperty,
  4. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
  5. __slice = [].slice;
  6. isEqual = require('underscore-plus').isEqual;
  7. PropertyAccessors = require('property-accessors');
  8. Signal = require('./signal');
  9. module.exports = Behavior = (function(_super) {
  10. __extends(Behavior, _super);
  11. PropertyAccessors.includeInto(Behavior);
  12. function Behavior() {
  13. var args, subscribeCallback, _ref;
  14. args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
  15. if (typeof ((_ref = args[0]) != null ? _ref.call : void 0) !== 'function') {
  16. this.value = args.shift();
  17. }
  18. Behavior.__super__.constructor.call(this, subscribeCallback = args.shift());
  19. }
  20. Behavior.prototype.retained = function() {
  21. var _this = this;
  22. this.subscribe(this, 'value-internal', function(value) {
  23. return _this.value = value;
  24. });
  25. this.subscribe(this, 'value-subscription-added', function(handler) {
  26. return handler(_this.value);
  27. });
  28. return typeof this.subscribeCallback === "function" ? this.subscribeCallback() : void 0;
  29. };
  30. Behavior.prototype.emit = function() {
  31. var args, name;
  32. name = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
  33. if (name === 'value') {
  34. this.emit.apply(this, ['value-internal'].concat(__slice.call(args)));
  35. }
  36. return Behavior.__super__.emit.apply(this, arguments);
  37. };
  38. Behavior.prototype.getValue = function() {
  39. if (!(this.retainCount > 0)) {
  40. throw new Error("Subscribe to or retain this behavior before calling getValue");
  41. }
  42. return this.value;
  43. };
  44. Behavior.prototype.and = function(right) {
  45. return helpers.combine(this, right, (function(leftValue, rightValue) {
  46. return leftValue && rightValue;
  47. })).distinctUntilChanged();
  48. };
  49. Behavior.prototype.or = function(right) {
  50. return helpers.combine(this, right, (function(leftValue, rightValue) {
  51. return leftValue || rightValue;
  52. })).distinctUntilChanged();
  53. };
  54. Behavior.prototype.toBehavior = function() {
  55. return this;
  56. };
  57. Behavior.prototype.lazyAccessor('changes', function() {
  58. var source;
  59. source = this;
  60. return new Signal(function() {
  61. var gotFirst,
  62. _this = this;
  63. gotFirst = false;
  64. return this.subscribe(source, 'value', function() {
  65. var metadata, value;
  66. value = arguments[0], metadata = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
  67. if (gotFirst) {
  68. _this.emitValue.apply(_this, [value].concat(__slice.call(metadata)));
  69. }
  70. return gotFirst = true;
  71. });
  72. });
  73. });
  74. Behavior.prototype.becomes = function(predicateOrTargetValue) {
  75. var predicate, targetValue;
  76. if (typeof predicateOrTargetValue !== 'function') {
  77. targetValue = predicateOrTargetValue;
  78. return this.becomes(function(value) {
  79. return isEqual(value, targetValue);
  80. });
  81. }
  82. predicate = predicateOrTargetValue;
  83. return this.map(function(value) {
  84. return !!predicate(value);
  85. }).distinctUntilChanged().changes;
  86. };
  87. Behavior.prototype.becomesLessThan = function(targetValue) {
  88. return this.becomes(function(value) {
  89. return value < targetValue;
  90. });
  91. };
  92. Behavior.prototype.becomesGreaterThan = function(targetValue) {
  93. return this.becomes(function(value) {
  94. return value > targetValue;
  95. });
  96. };
  97. return Behavior;
  98. })(Signal);
  99. helpers = require('./helpers');
  100. }).call(this);