55
66'use strict' ;
77
8- /*global define*/
8+ // @ts -check
99
10- // This module can be loaded in an amd and commonjs-context.
11- // Because we want both instances to use the same perf-data
12- // we store them globally
13- // stores data as: 'name','timestamp'
14-
15- if ( typeof define !== "function" && typeof module === "object" && typeof module . exports === "object" ) {
16- // this is commonjs, fake amd
17- global . define = function ( _dep , callback ) {
18- module . exports = callback ( ) ;
19- global . define = undefined ;
20- } ;
21- }
10+ function _factory ( sharedObj ) {
2211
23- define ( [ ] , function ( ) {
24-
25- global . _performanceEntries = global . _performanceEntries || [ ] ;
12+ sharedObj . _performanceEntries = sharedObj . _performanceEntries || [ ] ;
2613
2714 const _dataLen = 2 ;
2815 const _timeStamp = typeof console . timeStamp === 'function' ? console . timeStamp . bind ( console ) : ( ) => { } ;
2916
3017 function importEntries ( entries ) {
31- global . _performanceEntries . splice ( 0 , 0 , ...entries ) ;
18+ sharedObj . _performanceEntries . splice ( 0 , 0 , ...entries ) ;
3219 }
3320
3421 function exportEntries ( ) {
35- return global . _performanceEntries . slice ( 0 ) ;
22+ return sharedObj . _performanceEntries . slice ( 0 ) ;
3623 }
3724
3825 function getEntries ( ) {
3926 const result = [ ] ;
40- const entries = global . _performanceEntries ;
27+ const entries = sharedObj . _performanceEntries ;
4128 for ( let i = 0 ; i < entries . length ; i += _dataLen ) {
4229 result . push ( {
4330 name : entries [ i ] ,
@@ -48,7 +35,7 @@ define([], function () {
4835 }
4936
5037 function getEntry ( name ) {
51- const entries = global . _performanceEntries ;
38+ const entries = sharedObj . _performanceEntries ;
5239 for ( let i = 0 ; i < entries . length ; i += _dataLen ) {
5340 if ( entries [ i ] === name ) {
5441 return {
@@ -60,7 +47,7 @@ define([], function () {
6047 }
6148
6249 function getDuration ( from , to ) {
63- const entries = global . _performanceEntries ;
50+ const entries = sharedObj . _performanceEntries ;
6451 let target = to ;
6552 let endIndex = 0 ;
6653 for ( let i = entries . length - _dataLen ; i >= 0 ; i -= _dataLen ) {
@@ -79,11 +66,11 @@ define([], function () {
7966 }
8067
8168 function mark ( name ) {
82- global . _performanceEntries . push ( name , Date . now ( ) ) ;
69+ sharedObj . _performanceEntries . push ( name , Date . now ( ) ) ;
8370 _timeStamp ( name ) ;
8471 }
8572
86- var exports = {
73+ const exports = {
8774 mark : mark ,
8875 getEntries : getEntries ,
8976 getEntry : getEntry ,
@@ -93,4 +80,29 @@ define([], function () {
9380 } ;
9481
9582 return exports ;
96- } ) ;
83+ }
84+
85+ // This module can be loaded in an amd and commonjs-context.
86+ // Because we want both instances to use the same perf-data
87+ // we store them globally
88+
89+ let sharedObj ;
90+ if ( typeof global === 'object' ) {
91+ // nodejs
92+ sharedObj = global ;
93+ } else if ( typeof self === 'object' ) {
94+ // browser
95+ sharedObj = self ;
96+ } else {
97+ sharedObj = { } ;
98+ }
99+
100+ if ( typeof define === 'function' ) {
101+ // amd
102+ define ( [ ] , function ( ) { return _factory ( sharedObj ) ; } ) ;
103+ } else if ( typeof module === "object" && typeof module . exports === "object" ) {
104+ // commonjs
105+ module . exports = _factory ( sharedObj ) ;
106+ } else {
107+ // invalid context...
108+ }
0 commit comments