1
- 'use strict' ;
2
- /*!
1
+ /*!
3
2
* it-depends - v{{ version }}
4
3
* https://github.com/gerich-home/it-depends
5
4
* Copyright (c) 2016 Sergey Gerasimov; Licensed MSPL
9
8
( function ( rootObject , factory ) {
10
9
if ( typeof require === 'function' && typeof exports === 'object' && typeof module === 'object' ) {
11
10
// CommonJS or Node
12
- factory ( exports ) ;
11
+ module . exports = factory ( ) ;
13
12
} else if ( typeof define === 'function' && define [ 'amd' ] ) {
14
13
// AMD anonymous module
15
14
define ( [ ] , factory ) ;
16
15
} else {
17
16
// <script> tag: define the global `itDepends` object
18
- var exports = { } ;
19
- factory ( exports ) ;
20
- rootObject . itDepends = exports ;
17
+ rootObject . itDepends = factory ( ) ;
21
18
}
22
- } ( this , function ( exports ) {
19
+ } ( this , function ( ) {
20
+ 'use strict' ;
21
+
23
22
var nop = function ( ) { } ;
24
23
var trackers = [ nop ] ;
25
24
var nextId = 0 ;
29
28
trackers [ trackers . length - 1 ] ( id , observableValue , currentValue ) ;
30
29
} ;
31
30
32
- exports . value = function ( initialValue ) {
33
- var currentValue = initialValue ;
34
- var id = ++ nextId ;
35
-
36
- var self = function ( ) {
37
- notifyCurrentTracker ( id , self , currentValue ) ;
38
- return currentValue ;
39
- } ;
40
-
41
- self . write = function ( newValue ) {
42
- if ( currentValue !== newValue ) {
43
- currentValue = newValue ;
44
- lastWriteVersion ++ ;
45
- }
46
- } ;
47
-
48
- return self ;
49
- } ;
50
-
51
- exports . computed = function ( calculator ) {
52
- var currentValue ;
53
- var dependencies ;
54
- var id = ++ nextId ;
55
- var lastReadVersion = - 1 ;
56
-
57
- var atLeastOneDependencyChanged = function ( ) {
58
- for ( var dependencyId in dependencies ) {
59
- if ( ! dependencies . hasOwnProperty ( dependencyId ) )
60
- continue ;
61
-
62
- var dependency = dependencies [ dependencyId ] ;
63
-
64
- if ( dependency . observableValue ( ) !== dependency . capturedValue ) {
65
- return true ;
66
- }
67
- }
68
-
69
- return false ;
70
- } ;
71
-
72
- var needRecalcCache ;
73
- var needRecalc = function ( ) {
74
- if ( lastReadVersion !== lastWriteVersion ) {
75
- needRecalcCache = ! dependencies || atLeastOneDependencyChanged ( ) ;
76
- lastReadVersion = lastWriteVersion ;
77
- }
78
-
79
- return needRecalcCache ;
80
- } ;
81
-
82
- var self = function ( ) {
83
- if ( needRecalc ( ) ) {
84
- dependencies = { } ;
85
-
86
- trackers . push ( function ( dependencyId , observableValue , capturedValue ) {
87
- if ( dependencies [ dependencyId ] )
88
- return ;
89
-
90
- dependencies [ dependencyId ] = {
91
- observableValue : observableValue ,
92
- capturedValue : capturedValue
93
- } ;
94
- } ) ;
95
-
96
- try {
97
- currentValue = calculator ( ) ;
98
- } finally {
99
- trackers . pop ( ) ;
100
- }
101
-
102
- needRecalcCache = false ;
103
- }
104
-
105
- notifyCurrentTracker ( id , self , currentValue ) ;
106
-
107
- return currentValue ;
108
- } ;
109
-
110
- return self ;
111
- } ;
112
-
113
- exports . promiseValue = function ( promise , initialValue ) {
114
- var currentValue = exports . value ( initialValue ) ;
115
-
116
- promise . then ( currentValue . write ) ;
117
-
118
- return exports . computed ( currentValue ) ;
119
- } ;
31
+ var library = {
32
+ value : function ( initialValue ) {
33
+ var currentValue = initialValue ;
34
+ var id = ++ nextId ;
35
+
36
+ var self = function ( ) {
37
+ notifyCurrentTracker ( id , self , currentValue ) ;
38
+ return currentValue ;
39
+ } ;
40
+
41
+ self . write = function ( newValue ) {
42
+ if ( currentValue !== newValue ) {
43
+ currentValue = newValue ;
44
+ lastWriteVersion ++ ;
45
+ }
46
+ } ;
47
+
48
+ return self ;
49
+ } ,
50
+ computed : function ( calculator ) {
51
+ var currentValue ;
52
+ var dependencies ;
53
+ var id = ++ nextId ;
54
+ var lastReadVersion = - 1 ;
55
+
56
+ var atLeastOneDependencyChanged = function ( ) {
57
+ for ( var dependencyId in dependencies ) {
58
+ if ( ! dependencies . hasOwnProperty ( dependencyId ) )
59
+ continue ;
60
+
61
+ var dependency = dependencies [ dependencyId ] ;
62
+
63
+ if ( dependency . observableValue ( ) !== dependency . capturedValue ) {
64
+ return true ;
65
+ }
66
+ }
67
+
68
+ return false ;
69
+ } ;
70
+
71
+ var needRecalcCache ;
72
+ var needRecalc = function ( ) {
73
+ if ( lastReadVersion !== lastWriteVersion ) {
74
+ needRecalcCache = ! dependencies || atLeastOneDependencyChanged ( ) ;
75
+ lastReadVersion = lastWriteVersion ;
76
+ }
77
+
78
+ return needRecalcCache ;
79
+ } ;
80
+
81
+ var self = function ( ) {
82
+ if ( needRecalc ( ) ) {
83
+ dependencies = { } ;
84
+
85
+ trackers . push ( function ( dependencyId , observableValue , capturedValue ) {
86
+ if ( dependencies [ dependencyId ] )
87
+ return ;
88
+
89
+ dependencies [ dependencyId ] = {
90
+ observableValue : observableValue ,
91
+ capturedValue : capturedValue
92
+ } ;
93
+ } ) ;
94
+
95
+ try {
96
+ currentValue = calculator ( ) ;
97
+ } finally {
98
+ trackers . pop ( ) ;
99
+ }
100
+
101
+ needRecalcCache = false ;
102
+ }
103
+
104
+ notifyCurrentTracker ( id , self , currentValue ) ;
105
+
106
+ return currentValue ;
107
+ } ;
108
+
109
+ return self ;
110
+ } ,
111
+ promiseValue : function ( promise , initialValue ) {
112
+ var currentValue = library . value ( initialValue ) ;
113
+
114
+ promise . then ( currentValue . write ) ;
115
+
116
+ return library . computed ( currentValue ) ;
117
+ }
118
+ } ;
119
+
120
+ return library ;
120
121
} ) ) ;
0 commit comments