@@ -10,48 +10,9 @@ const DEFAULT = Symbol.for('default');
10
10
let inProgress = DEFAULT ;
11
11
let inVector ;
12
12
13
- const v3ValueOf = new Map ( ) ;
14
- v3ValueOf [ X ] = function ( ) {
15
- if ( ! inVector ) {
16
- inVector = this ;
17
- }
18
- return this [ inProgress ] ;
19
- } ;
20
- v3ValueOf [ Y ] = function ( ) {
21
- return this [ inProgress ] ;
22
- } ;
23
- v3ValueOf [ Z ] = v3ValueOf [ Y ] ;
24
- v3ValueOf [ DEFAULT ] = function ( org ) {
25
- return org . call ( this ) ;
26
- } ;
27
-
28
13
let resultCacheIndex = - 1 ;
29
14
let handlingCache = false ;
30
15
const resultCache = [ ] ;
31
- const v3resultCache = new Map ( ) ;
32
- v3resultCache [ X ] = function ( org , args ) {
33
- if ( handlingCache ) {
34
- return org . apply ( this , args ) ;
35
- }
36
- try {
37
- handlingCache = true ;
38
-
39
- resultCacheIndex += 1 ;
40
- const res = org . apply ( this , args ) ;
41
- resultCache [ resultCacheIndex ] = res ;
42
- return res ;
43
- } finally {
44
- handlingCache = false ;
45
- }
46
- } ;
47
- v3resultCache [ Y ] = function ( ) {
48
- resultCacheIndex += 1 ;
49
- return resultCache [ resultCacheIndex ] ;
50
- } ;
51
- v3resultCache [ Z ] = v3resultCache [ Y ] ;
52
- v3resultCache [ DEFAULT ] = function ( org , args ) {
53
- return org . apply ( this , args ) ;
54
- } ;
55
16
56
17
export function operatorCalc ( alg , result ) {
57
18
if ( typeof alg !== 'function' ) {
@@ -97,15 +58,54 @@ export function cachedValueOf(Vector) {
97
58
const name = 'valueOf' ;
98
59
const org = Vector . prototype [ name ] ;
99
60
Vector . prototype [ name ] = function ( ) {
100
- return v3ValueOf [ inProgress ] . call ( this , org ) ;
61
+ if ( inProgress === X ) {
62
+ if ( ! inVector ) {
63
+ inVector = this ;
64
+ }
65
+ return this . x ;
66
+ }
67
+ if ( inProgress === Y ) {
68
+ return this . y ;
69
+ }
70
+ if ( inProgress === Z ) {
71
+ return this . z ;
72
+ }
73
+ return org . call ( this ) ;
74
+ } ;
75
+ }
76
+
77
+ function bindCache ( org ) {
78
+ return function ( ...args ) {
79
+ if ( inProgress === X ) {
80
+ if ( handlingCache ) {
81
+ return org . apply ( this , args ) ;
82
+ }
83
+ try {
84
+ handlingCache = true ;
85
+
86
+ resultCacheIndex += 1 ;
87
+ const res = org . apply ( this , args ) ;
88
+ resultCache [ resultCacheIndex ] = res ;
89
+ return res ;
90
+ } finally {
91
+ handlingCache = false ;
92
+ }
93
+ }
94
+ if ( inProgress === Y ) {
95
+ resultCacheIndex += 1 ;
96
+ return resultCache [ resultCacheIndex ] ;
97
+ }
98
+ if ( inProgress === Z ) {
99
+ resultCacheIndex += 1 ;
100
+ return resultCache [ resultCacheIndex ] ;
101
+ }
102
+ return org . apply ( this , args ) ;
101
103
} ;
102
104
}
103
105
104
106
export function cachedMethod ( Vector , name ) {
105
107
const org = Vector . prototype [ name ] ;
106
- Vector . prototype [ name ] = function ( ...args ) {
107
- return v3resultCache [ inProgress ] . call ( this , org , args ) ;
108
- } ;
108
+ Vector . prototype [ name ] = bindCache ( org ) ;
109
109
}
110
110
111
111
export function cachedGetter ( Vector , name ) {
@@ -115,8 +115,6 @@ export function cachedGetter(Vector, name) {
115
115
} ;
116
116
117
117
Object . defineProperty ( Vector . prototype , name , {
118
- get ( ) {
119
- return v3resultCache [ inProgress ] . call ( this , org ) ;
120
- }
118
+ get : bindCache ( org )
121
119
} ) ;
122
120
}
0 commit comments