Skip to content

Commit

Permalink
Breaking change - starting from now you should not write require('it-…
Browse files Browse the repository at this point in the history
…depends').itDepends in NodeJS - just write require('it-depends')
  • Loading branch information
gerich-home committed Feb 15, 2016
1 parent 5db6a90 commit d24de38
Show file tree
Hide file tree
Showing 11 changed files with 106 additions and 105 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ npm install it-depends

In your application include the module and use it:
```javascript
var itDepends = require('it-depends').itDepends;
var itDepends = require('it-depends');

// your code goes here:
var firstName = itDepends.value('James');
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "it-depends",
"version": "2.0.4",
"version": "3.0.0",
"description": "Lightweight dependency tracking library for JavaScript",
"files": [
"src"
Expand Down
2 changes: 1 addition & 1 deletion specs/computedWith2ValueDependencies.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var expect = require('chai').expect;
var itDepends = require('../src/it-depends.js').itDepends;
var itDepends = require('../src/it-depends.js');

describe('computed with 2 value dependencies', function () {
var callCount;
Expand Down
2 changes: 1 addition & 1 deletion specs/computedWithComputedDependency.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var expect = require('chai').expect;
var itDepends = require('../src/it-depends.js').itDepends;
var itDepends = require('../src/it-depends.js');

describe('computed with computed dependency', function () {
var callCount;
Expand Down
2 changes: 1 addition & 1 deletion specs/computedWithNoDependencies.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var expect = require('chai').expect;
var itDepends = require('../src/it-depends.js').itDepends;
var itDepends = require('../src/it-depends.js');

describe('computed with no dependencies', function () {
var callCount;
Expand Down
2 changes: 1 addition & 1 deletion specs/computedWithValueDependency.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var expect = require('chai').expect;
var itDepends = require('../src/it-depends.js').itDepends;
var itDepends = require('../src/it-depends.js');

describe('computed with single value dependency', function () {
var callCount;
Expand Down
2 changes: 1 addition & 1 deletion specs/promiseValue.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var expect = require('chai').expect;
var itDepends = require('../src/it-depends.js').itDepends;
var itDepends = require('../src/it-depends.js');

describe('promise value', function () {
var callbacks = [];
Expand Down
2 changes: 1 addition & 1 deletion specs/supportsRequire.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var expect = require('chai').expect;
var itDepends = require('../src/it-depends.js').itDepends;
var itDepends = require('../src/it-depends.js');

describe('itDepends library', function () {

Expand Down
2 changes: 1 addition & 1 deletion specs/unrelatedComputeds.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var expect = require('chai').expect;
var itDepends = require('../src/it-depends.js').itDepends;
var itDepends = require('../src/it-depends.js');

describe('computed unrelated to other computed and value', function () {
var callCount;
Expand Down
2 changes: 1 addition & 1 deletion specs/value.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var expect = require('chai').expect;
var itDepends = require('../src/it-depends.js').itDepends;
var itDepends = require('../src/it-depends.js');

describe('value', function () {
it('should store initial value', function () {
Expand Down
191 changes: 96 additions & 95 deletions src/it-depends.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
'use strict';
/*!
/*!
* it-depends - v{{ version }}
* https://github.com/gerich-home/it-depends
* Copyright (c) 2016 Sergey Gerasimov; Licensed MSPL
Expand All @@ -9,17 +8,17 @@
(function(rootObject, factory) {
if (typeof require === 'function' && typeof exports === 'object' && typeof module === 'object') {
// CommonJS or Node
factory(exports);
module.exports = factory();
} else if (typeof define === 'function' && define['amd']) {
// AMD anonymous module
define([], factory);
} else {
// <script> tag: define the global `itDepends` object
var exports = {};
factory(exports);
rootObject.itDepends = exports;
rootObject.itDepends = factory();
}
}(this, function(exports) {
}(this, function() {
'use strict';

var nop = function() {};
var trackers = [nop];
var nextId = 0;
Expand All @@ -29,92 +28,94 @@
trackers[trackers.length - 1](id, observableValue, currentValue);
};

exports.value = function(initialValue) {
var currentValue = initialValue;
var id = ++nextId;

var self = function() {
notifyCurrentTracker(id, self, currentValue);
return currentValue;
};

self.write = function(newValue) {
if (currentValue !== newValue) {
currentValue = newValue;
lastWriteVersion++;
}
};

return self;
};

exports.computed = function(calculator) {
var currentValue;
var dependencies;
var id = ++nextId;
var lastReadVersion = -1;

var atLeastOneDependencyChanged = function() {
for (var dependencyId in dependencies) {
if (!dependencies.hasOwnProperty(dependencyId))
continue;

var dependency = dependencies[dependencyId];

if (dependency.observableValue() !== dependency.capturedValue) {
return true;
}
}

return false;
};

var needRecalcCache;
var needRecalc = function() {
if (lastReadVersion !== lastWriteVersion) {
needRecalcCache = !dependencies || atLeastOneDependencyChanged();
lastReadVersion = lastWriteVersion;
}

return needRecalcCache;
};

var self = function() {
if (needRecalc()) {
dependencies = {};

trackers.push(function(dependencyId, observableValue, capturedValue) {
if (dependencies[dependencyId])
return;

dependencies[dependencyId] = {
observableValue: observableValue,
capturedValue: capturedValue
};
});

try {
currentValue = calculator();
} finally {
trackers.pop();
}

needRecalcCache = false;
}

notifyCurrentTracker(id, self, currentValue);

return currentValue;
};

return self;
};

exports.promiseValue = function(promise, initialValue) {
var currentValue = exports.value(initialValue);

promise.then(currentValue.write);

return exports.computed(currentValue);
};
var library = {
value: function(initialValue) {
var currentValue = initialValue;
var id = ++nextId;

var self = function() {
notifyCurrentTracker(id, self, currentValue);
return currentValue;
};

self.write = function(newValue) {
if (currentValue !== newValue) {
currentValue = newValue;
lastWriteVersion++;
}
};

return self;
},
computed: function(calculator) {
var currentValue;
var dependencies;
var id = ++nextId;
var lastReadVersion = -1;

var atLeastOneDependencyChanged = function() {
for (var dependencyId in dependencies) {
if (!dependencies.hasOwnProperty(dependencyId))
continue;

var dependency = dependencies[dependencyId];

if (dependency.observableValue() !== dependency.capturedValue) {
return true;
}
}

return false;
};

var needRecalcCache;
var needRecalc = function() {
if (lastReadVersion !== lastWriteVersion) {
needRecalcCache = !dependencies || atLeastOneDependencyChanged();
lastReadVersion = lastWriteVersion;
}

return needRecalcCache;
};

var self = function() {
if (needRecalc()) {
dependencies = {};

trackers.push(function(dependencyId, observableValue, capturedValue) {
if (dependencies[dependencyId])
return;

dependencies[dependencyId] = {
observableValue: observableValue,
capturedValue: capturedValue
};
});

try {
currentValue = calculator();
} finally {
trackers.pop();
}

needRecalcCache = false;
}

notifyCurrentTracker(id, self, currentValue);

return currentValue;
};

return self;
},
promiseValue: function(promise, initialValue) {
var currentValue = library.value(initialValue);

promise.then(currentValue.write);

return library.computed(currentValue);
}
};

return library;
}));

0 comments on commit d24de38

Please sign in to comment.