-
Notifications
You must be signed in to change notification settings - Fork 1
/
Thunk.tsx
195 lines (159 loc) · 5.43 KB
/
Thunk.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import React, {useState, useEffect, useLayoutEffect, useRef} from 'react';
import {Provider, useDispatch, useSelector} from 'react-redux';
import {createAsyncActionHelper, createNotAsyncActionHelper} from './asyncReducer';
import {
View,
Text,
TouchableOpacity,
} from 'react-native';
let asyncCount = 0;
let notAsyncCount = 0;
interface stateObj {
asyncDataThunk: number
}
let xxx: boolean = true;
const Thunk = (props: any) => {
const [stateData, setStateData] = useState(0);
const dispatch = useDispatch();
const thunkData = useSelector((state: stateObj): number => state.asyncDataThunk);
const refData = useRef(null);
const {navigation} = props;
useEffect(() => {
console.log('render thunk', thunkData);
});
useLayoutEffect(() => {
console.log('render thunk useLayoutEffect', thunkData);
if(!xxx){
setStateData(20);
}
xxx = false;
}, [thunkData]);
const actionClick = () => {
navigation.push('Thunk');
// promiseAll([
// Promise.resolve(true),
// Promise.resolve(1),
// Promise.resolve('string'),
// Promise.resolve({}),
// // ...
// ]).then((result) => {
// console.log('-------', result);
// });
// asyncCount += 10;
// dispatch(createAsyncActionHelper(asyncCount));
};
function promiseAll(promises: object[]) {
// TODO: implement function body
return Promise.all(promises).then((resolveData) => {
return resolveData;
});
}
function sum(a: number, b: number) {
return a + b;
}
let memoizeFunc = memoize(sum);
// console.log(memoizeFunc(1,2)); // return 3
// console.log(memoizeFunc(1,2)); // return memoize 3
// console.log(memoizeFunc(2,2)); // return 4
// console.log(memoizeFunc(1,2)); // return memoize 3
function memoize(func: (...args: number[]) => number) {
// TODO: implement function
const cache = new Map();
return function (...args: number[]) {
let key = JSON.stringify(args);
if (cache.has(key)) {
return cache.get(key);
}
const result = func(...args);
cache.set(key, result);
return result;
}
}
interface IActionReducer {
type?: string,
action?: number | string | object
}
interface IInitialState {
[key: string]: number | string | object
}
interface IReducer {
(state: {} | undefined, action: IActionReducer): IInitialState
}
type listenerType = () => void;
const createStore = (reducer: IReducer, initialState: IInitialState) => {
let state = initialState || reducer(undefined, {});
let listeners: listenerType[] = [];
return {
getStore: () => state,
dispatch: (action: IActionReducer) => {
state = reducer(state, action);
listeners.forEach((listener: listenerType) => {
listener()
});
},
subscribe: (newListener: listenerType) => {
listeners.push(newListener);
return () => {
listeners = listeners.filter((l: listenerType) => l !== newListener)
}
}
}
};
const actionNotAsyncClick = () => {
notAsyncCount += 20;
dispatch(createNotAsyncActionHelper(notAsyncCount));
};
class HttpError extends Error {
response: any;
constructor(response: any) {
super(`${response.status} for ${response.url}`);
this.name = 'HttpError';
this.response = response;
}
}
const loadJson = async (url: string) => {
const response: any = await fetch(url);
if (response.status == 200) {
return await response.json();
} else {
throw new Error(response.status);
}
};
const func23 = (func: (...args: number[]) => number) => {
let cache = new Map();
return (...args: number[]) => {
const key = JSON.stringify(args);
if (cache.has(key)) {
return Promise.resolve(cache.get(key)); // (*)
}
return new Promise((resolve, reject) => {
const result = func(...args);
cache.set(key, result);
resolve(result);
})
}
};
const sum2 = (a: number, b: number) => a + b;
const func23Helper = func23(sum2);
const func23Click = async () => {
const result = await func23Helper(2, 3);
func23Helper(2, 3).then((data) => {
console.log(data);
});
};
return (
<View style={{marginVertical: 20, borderWidth: 1, borderColor: 'blue'}}>
<Text ref={refData}>Thunk - {thunkData}; stateData - {stateData}</Text>
<TouchableOpacity onPress={actionClick}>
<Text style={{textAlign: 'center'}}>Send async Thunk</Text>
</TouchableOpacity>
<TouchableOpacity onPress={actionNotAsyncClick}>
<Text style={{textAlign: 'center'}}>Send not async Thunk</Text>
</TouchableOpacity>
<TouchableOpacity onPress={func23Click}>
<Text style={{textAlign: 'center'}}>func23Click</Text>
</TouchableOpacity>
</View>
)
};
export {Thunk}