Skip to content

Commit a066ff9

Browse files
committed
refactor convert proxy object
1 parent 9da80e3 commit a066ff9

File tree

10 files changed

+67
-64
lines changed

10 files changed

+67
-64
lines changed

amplitude.js

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,21 @@ Amplitude.prototype.clearUserProperties = function clearUserProperties(){
737737
this.identify(identify);
738738
};
739739

740+
/**
741+
* Applies the proxied functions on the proxied object to an instance of the real object.
742+
* Used to convert proxied Identify and Revenue objects.
743+
* @private
744+
*/
745+
var _convertProxyObjectToRealObject = function _convertProxyObjectToRealObject(instance, proxy) {
746+
for (var i = 0; i < proxy._q.length; i++) {
747+
var fn = instance[proxy._q[i][0]];
748+
if (type(fn) === 'function') {
749+
fn.apply(instance, proxy._q[i].slice(1));
750+
}
751+
}
752+
return instance;
753+
};
754+
740755
/**
741756
* Send an identify call containing user property operations to Amplitude servers.
742757
* See [Readme]{@link https://github.com/amplitude/Amplitude-Javascript#user-properties-and-user-property-operations}
@@ -758,14 +773,7 @@ Amplitude.prototype.identify = function(identify_obj, opt_callback) {
758773

759774
// if identify input is a proxied object created by the async loading snippet, convert it into an identify object
760775
if (type(identify_obj) === 'object' && identify_obj.hasOwnProperty('_q')) {
761-
var instance = new Identify();
762-
for (var i = 0; i < identify_obj._q.length; i++) {
763-
var fn = instance[identify_obj._q[i][0]];
764-
if (type(fn) === 'function') {
765-
fn.apply(instance, identify_obj._q[i].slice(1));
766-
}
767-
}
768-
identify_obj = instance;
776+
identify_obj = _convertProxyObjectToRealObject(new Identify(), identify_obj);
769777
}
770778

771779
if (identify_obj instanceof Identify) {
@@ -934,14 +942,7 @@ Amplitude.prototype.logRevenueV2 = function logRevenueV2(revenue_obj) {
934942

935943
// if revenue input is a proxied object created by the async loading snippet, convert it into an revenue object
936944
if (type(revenue_obj) === 'object' && revenue_obj.hasOwnProperty('_q')) {
937-
var instance = new Revenue();
938-
for (var i = 0; i < revenue_obj._q.length; i++) {
939-
var fn = instance[revenue_obj._q[i][0]];
940-
if (type(fn) === 'function') {
941-
fn.apply(instance, revenue_obj._q[i].slice(1));
942-
}
943-
}
944-
revenue_obj = instance;
945+
revenue_obj = _convertProxyObjectToRealObject(new Revenue(), revenue_obj);
945946
}
946947

947948
if (revenue_obj instanceof Revenue) {

amplitude.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

documentation/Amplitude.html

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ <h4 class="name" id="__VERSION__"><span class="type-signature"></span>__VERSION_
184184

185185
<dt class="tag-source">Source:</dt>
186186
<dd class="tag-source"><ul class="dummy"><li>
187-
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line1048">line 1048</a>
187+
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line1049">line 1049</a>
188188
</li></ul></dd>
189189

190190

@@ -533,7 +533,7 @@ <h5>Parameters:</h5>
533533

534534
<dt class="tag-source">Source:</dt>
535535
<dd class="tag-source"><ul class="dummy"><li>
536-
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line645">line 645</a>
536+
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line660">line 660</a>
537537
</li></ul></dd>
538538

539539

@@ -1028,7 +1028,7 @@ <h5>Parameters:</h5>
10281028

10291029
<dt class="tag-source">Source:</dt>
10301030
<dd class="tag-source"><ul class="dummy"><li>
1031-
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line797">line 797</a>
1031+
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line805">line 805</a>
10321032
</li></ul></dd>
10331033

10341034

@@ -1212,7 +1212,7 @@ <h5>Parameters:</h5>
12121212

12131213
<dt class="tag-source">Source:</dt>
12141214
<dd class="tag-source"><ul class="dummy"><li>
1215-
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line860">line 860</a>
1215+
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line861">line 861</a>
12161216
</li></ul></dd>
12171217

12181218

@@ -1349,7 +1349,7 @@ <h5>Parameters:</h5>
13491349

13501350
<dt class="tag-source">Source:</dt>
13511351
<dd class="tag-source"><ul class="dummy"><li>
1352-
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line824">line 824</a>
1352+
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line832">line 832</a>
13531353
</li></ul></dd>
13541354

13551355

@@ -1713,7 +1713,7 @@ <h4 class="name" id="setGlobalUserProperties"><span class="type-signature"></spa
17131713

17141714
<dt class="tag-source">Source:</dt>
17151715
<dd class="tag-source"><ul class="dummy"><li>
1716-
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line1038">line 1038</a>
1716+
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line1039">line 1039</a>
17171717
</li></ul></dd>
17181718

17191719

@@ -2271,7 +2271,7 @@ <h5>Parameters:</h5>
22712271

22722272
<dt class="tag-source">Source:</dt>
22732273
<dd class="tag-source"><ul class="dummy"><li>
2274-
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line685">line 685</a>
2274+
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line693">line 693</a>
22752275
</li></ul></dd>
22762276

22772277

@@ -2437,7 +2437,7 @@ <h5>Parameters:</h5>
24372437

24382438
<dt class="tag-source">Source:</dt>
24392439
<dd class="tag-source"><ul class="dummy"><li>
2440-
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line780">line 780</a>
2440+
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line788">line 788</a>
24412441
</li></ul></dd>
24422442

24432443

@@ -2483,7 +2483,7 @@ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Amplitude
24832483
<br class="clear">
24842484

24852485
<footer>
2486-
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Tue Apr 19 2016 16:37:04 GMT-0700 (PDT)
2486+
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Tue Apr 19 2016 17:19:27 GMT-0700 (PDT)
24872487
</footer>
24882488

24892489
<script> prettyPrint(); </script>

documentation/Identify.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1295,7 +1295,7 @@ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Amplitude
12951295
<br class="clear">
12961296

12971297
<footer>
1298-
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Tue Apr 19 2016 16:37:04 GMT-0700 (PDT)
1298+
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Tue Apr 19 2016 17:19:27 GMT-0700 (PDT)
12991299
</footer>
13001300

13011301
<script> prettyPrint(); </script>

documentation/Revenue.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -965,7 +965,7 @@ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Amplitude
965965
<br class="clear">
966966

967967
<footer>
968-
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Tue Apr 19 2016 16:37:04 GMT-0700 (PDT)
968+
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Tue Apr 19 2016 17:19:27 GMT-0700 (PDT)
969969
</footer>
970970

971971
<script> prettyPrint(); </script>

documentation/amplitude.js.html

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,21 @@ <h1 class="page-title">Source: amplitude.js</h1>
659659
this.identify(identify);
660660
};
661661

662+
/**
663+
* Applies the proxied functions on the proxied object to an instance of the real object.
664+
* Used to convert proxied Identify and Revenue objects.
665+
* @private
666+
*/
667+
var _convertProxyObjectToRealObject = function _convertProxyObjectToRealObject(instance, proxy) {
668+
for (var i = 0; i &lt; proxy._q.length; i++) {
669+
var fn = instance[proxy._q[i][0]];
670+
if (type(fn) === 'function') {
671+
fn.apply(instance, proxy._q[i].slice(1));
672+
}
673+
}
674+
return instance;
675+
};
676+
662677
/**
663678
* Send an identify call containing user property operations to Amplitude servers.
664679
* See [Readme]{@link https://github.com/amplitude/Amplitude-Javascript#user-properties-and-user-property-operations}
@@ -680,14 +695,7 @@ <h1 class="page-title">Source: amplitude.js</h1>
680695

681696
// if identify input is a proxied object created by the async loading snippet, convert it into an identify object
682697
if (type(identify_obj) === 'object' &amp;&amp; identify_obj.hasOwnProperty('_q')) {
683-
var instance = new Identify();
684-
for (var i = 0; i &lt; identify_obj._q.length; i++) {
685-
var fn = instance[identify_obj._q[i][0]];
686-
if (type(fn) === 'function') {
687-
fn.apply(instance, identify_obj._q[i].slice(1));
688-
}
689-
}
690-
identify_obj = instance;
698+
identify_obj = _convertProxyObjectToRealObject(new Identify(), identify_obj);
691699
}
692700

693701
if (identify_obj instanceof Identify) {
@@ -856,14 +864,7 @@ <h1 class="page-title">Source: amplitude.js</h1>
856864

857865
// if revenue input is a proxied object created by the async loading snippet, convert it into an revenue object
858866
if (type(revenue_obj) === 'object' &amp;&amp; revenue_obj.hasOwnProperty('_q')) {
859-
var instance = new Revenue();
860-
for (var i = 0; i &lt; revenue_obj._q.length; i++) {
861-
var fn = instance[revenue_obj._q[i][0]];
862-
if (type(fn) === 'function') {
863-
fn.apply(instance, revenue_obj._q[i].slice(1));
864-
}
865-
}
866-
revenue_obj = instance;
867+
revenue_obj = _convertProxyObjectToRealObject(new Revenue(), revenue_obj);
867868
}
868869

869870
if (revenue_obj instanceof Revenue) {
@@ -1092,7 +1093,7 @@ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Amplitude
10921093
<br class="clear">
10931094

10941095
<footer>
1095-
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Tue Apr 19 2016 16:37:04 GMT-0700 (PDT)
1096+
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Tue Apr 19 2016 17:19:27 GMT-0700 (PDT)
10961097
</footer>
10971098

10981099
<script> prettyPrint(); </script>

documentation/identify.js.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Amplitude
226226
<br class="clear">
227227

228228
<footer>
229-
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Tue Apr 19 2016 16:37:04 GMT-0700 (PDT)
229+
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Tue Apr 19 2016 17:19:27 GMT-0700 (PDT)
230230
</footer>
231231

232232
<script> prettyPrint(); </script>

documentation/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Amplitude
5656
<br class="clear">
5757

5858
<footer>
59-
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Tue Apr 19 2016 16:37:04 GMT-0700 (PDT)
59+
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Tue Apr 19 2016 17:19:27 GMT-0700 (PDT)
6060
</footer>
6161

6262
<script> prettyPrint(); </script>

documentation/revenue.js.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Amplitude
200200
<br class="clear">
201201

202202
<footer>
203-
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Tue Apr 19 2016 16:37:04 GMT-0700 (PDT)
203+
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Tue Apr 19 2016 17:19:27 GMT-0700 (PDT)
204204
</footer>
205205

206206
<script> prettyPrint(); </script>

src/amplitude.js

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,21 @@ Amplitude.prototype.clearUserProperties = function clearUserProperties(){
631631
this.identify(identify);
632632
};
633633

634+
/**
635+
* Applies the proxied functions on the proxied object to an instance of the real object.
636+
* Used to convert proxied Identify and Revenue objects.
637+
* @private
638+
*/
639+
var _convertProxyObjectToRealObject = function _convertProxyObjectToRealObject(instance, proxy) {
640+
for (var i = 0; i < proxy._q.length; i++) {
641+
var fn = instance[proxy._q[i][0]];
642+
if (type(fn) === 'function') {
643+
fn.apply(instance, proxy._q[i].slice(1));
644+
}
645+
}
646+
return instance;
647+
};
648+
634649
/**
635650
* Send an identify call containing user property operations to Amplitude servers.
636651
* See [Readme]{@link https://github.com/amplitude/Amplitude-Javascript#user-properties-and-user-property-operations}
@@ -652,14 +667,7 @@ Amplitude.prototype.identify = function(identify_obj, opt_callback) {
652667

653668
// if identify input is a proxied object created by the async loading snippet, convert it into an identify object
654669
if (type(identify_obj) === 'object' && identify_obj.hasOwnProperty('_q')) {
655-
var instance = new Identify();
656-
for (var i = 0; i < identify_obj._q.length; i++) {
657-
var fn = instance[identify_obj._q[i][0]];
658-
if (type(fn) === 'function') {
659-
fn.apply(instance, identify_obj._q[i].slice(1));
660-
}
661-
}
662-
identify_obj = instance;
670+
identify_obj = _convertProxyObjectToRealObject(new Identify(), identify_obj);
663671
}
664672

665673
if (identify_obj instanceof Identify) {
@@ -828,14 +836,7 @@ Amplitude.prototype.logRevenueV2 = function logRevenueV2(revenue_obj) {
828836

829837
// if revenue input is a proxied object created by the async loading snippet, convert it into an revenue object
830838
if (type(revenue_obj) === 'object' && revenue_obj.hasOwnProperty('_q')) {
831-
var instance = new Revenue();
832-
for (var i = 0; i < revenue_obj._q.length; i++) {
833-
var fn = instance[revenue_obj._q[i][0]];
834-
if (type(fn) === 'function') {
835-
fn.apply(instance, revenue_obj._q[i].slice(1));
836-
}
837-
}
838-
revenue_obj = instance;
839+
revenue_obj = _convertProxyObjectToRealObject(new Revenue(), revenue_obj);
839840
}
840841

841842
if (revenue_obj instanceof Revenue) {

0 commit comments

Comments
 (0)