1+ 'use strict' ;
2+
3+ Object . defineProperty ( exports , "__esModule" , {
4+ value : true
5+ } ) ;
6+
7+ var _typeof = typeof Symbol === "function" && typeof Symbol . iterator === "symbol" ? function ( obj ) { return typeof obj ; } : function ( obj ) { return obj && typeof Symbol === "function" && obj . constructor === Symbol && obj !== Symbol . prototype ? "symbol" : typeof obj ; } ;
8+
9+ exports . callIn = callIn ;
10+
11+ var _proxyequal = require ( 'proxyequal' ) ;
12+
13+ var _cache = require ( './cache' ) ;
14+
15+ function _toConsumableArray ( arr ) { if ( Array . isArray ( arr ) ) { for ( var i = 0 , arr2 = Array ( arr . length ) ; i < arr . length ; i ++ ) { arr2 [ i ] = arr [ i ] ; } return arr2 ; } else { return Array . from ( arr ) ; } }
16+
17+ var nothing = 'PROXY_EQUAL_NOTHING' ;
18+ var emptyArray = [ ] ;
19+
20+ function addAffected ( affected , object ) {
21+ var key = ( 0 , _proxyequal . getProxyKey ) ( object ) ;
22+ affected [ key . fingerPrint ] . resultAffected . push ( key . suffix ) ;
23+ }
24+
25+ function deproxifyResult ( result , affected , returnPureValue ) {
26+
27+ var isInProxy = ( 0 , _proxyequal . isProxyfied ) ( result ) ;
28+ if ( isInProxy ) {
29+ addAffected ( affected , result ) ;
30+ return ( 0 , _proxyequal . deproxify ) ( result ) ;
31+ }
32+
33+ if ( ( typeof result === 'undefined' ? 'undefined' : _typeof ( result ) ) === 'object' ) {
34+ var sub = Array . isArray ( result ) ? [ ] : { } ;
35+ var altered = false ;
36+
37+ if ( result && Object . getOwnPropertyDescriptor ( result , '__proxyequal_scanEnd' ) ) {
38+ Object . defineProperty ( result , '__proxyequal_scanEnd' , {
39+ value : 'here was spread guard' ,
40+ configurable : true ,
41+ enumerable : false
42+ } ) ;
43+ }
44+
45+ for ( var i in result ) {
46+ if ( result . hasOwnProperty ( i ) ) {
47+ var data = result [ i ] ;
48+ var newResult = deproxifyResult ( data , affected , false ) ;
49+ if ( data && newResult !== nothing ) {
50+ altered = true ;
51+ sub [ i ] = newResult ;
52+ } else {
53+ sub [ i ] = data ;
54+ }
55+ }
56+ }
57+ if ( altered ) {
58+ return sub ;
59+ }
60+ return returnPureValue ? result : nothing ;
61+ }
62+
63+ return returnPureValue ? result : nothing ;
64+ }
65+
66+ function callIn ( that , cache , args , func , memoizationDepth ) {
67+ var proxyMap = arguments . length > 5 && arguments [ 5 ] !== undefined ? arguments [ 5 ] : [ ] ;
68+
69+ var proxies = args . map ( function ( arg , index ) {
70+ if ( arg && ( typeof arg === 'undefined' ? 'undefined' : _typeof ( arg ) ) === 'object' ) {
71+ var map = proxyMap [ index ] ;
72+ if ( ! map ) {
73+ return proxyMap [ index ] = ( 0 , _proxyequal . proxyState ) ( arg , index ) ;
74+ }
75+ map . reset ( ) ;
76+ return map . replaceState ( arg ) ;
77+ }
78+ return undefined ;
79+ } ) ;
80+ var callArgs = args . map ( function ( arg , index ) {
81+ return proxies [ index ] ? proxies [ index ] . state : arg ;
82+ } ) ;
83+
84+ //call the real function
85+ var preResult = func . call . apply ( func , [ that ] . concat ( _toConsumableArray ( callArgs ) ) ) ;
86+ proxies . forEach ( function ( proxy ) {
87+ return proxy && proxy . seal ( ) ;
88+ } ) ;
89+
90+ var spreadDetected = [ ] ;
91+ var affected = proxies . map ( function ( proxy ) {
92+ if ( proxy ) {
93+ if ( proxy . spreadDetected !== false ) {
94+ spreadDetected . push ( proxy . spreadDetected ) ;
95+ }
96+ var _affected = proxy . affected || emptyArray ;
97+
98+ return {
99+ useAffected : [ ] . concat ( _toConsumableArray ( _affected ) ) ,
100+ resultAffected : [ ]
101+ } ;
102+ }
103+ return undefined ;
104+ } ) ;
105+ var result = deproxifyResult ( preResult , affected , true ) ;
106+ var cacheLine = { args : args , affected : affected , result : result , callArgs : callArgs } ;
107+ if ( cache . length < memoizationDepth ) {
108+ cache . push ( cacheLine ) ;
109+ } else {
110+ ( 0 , _cache . updateCacheLine ) ( cache , 0 , cacheLine ) ;
111+ }
112+
113+ if ( spreadDetected . length > 0 ) {
114+ if ( process . env . NODE_ENV !== 'production' ) {
115+ console . warn ( 'memoize-state: object spread detected in ' , func , '. Keys affected: ' , spreadDetected . map ( function ( key ) {
116+ return key ? key : 'root' ;
117+ } ) , '. This is no-op.' ) ;
118+ }
119+ }
120+
121+ return result ;
122+ }
0 commit comments