Browse Source

some samples and comments

Ivan Asmer 2 years ago
parent
commit
084e036e75
2 changed files with 44 additions and 20 deletions
  1. 24 9
      package-lock.json
  2. 20 11
      src/reducers/index.js

+ 24 - 9
package-lock.json

@@ -7022,7 +7022,8 @@
             },
             "ansi-regex": {
               "version": "2.1.1",
-              "bundled": true
+              "bundled": true,
+              "optional": true
             },
             "aproba": {
               "version": "1.2.0",
@@ -7059,7 +7060,8 @@
             },
             "code-point-at": {
               "version": "1.1.0",
-              "bundled": true
+              "bundled": true,
+              "optional": true
             },
             "concat-map": {
               "version": "0.0.1",
@@ -7068,7 +7070,8 @@
             },
             "console-control-strings": {
               "version": "1.1.0",
-              "bundled": true
+              "bundled": true,
+              "optional": true
             },
             "core-util-is": {
               "version": "1.0.2",
@@ -7171,7 +7174,8 @@
             },
             "inherits": {
               "version": "2.0.3",
-              "bundled": true
+              "bundled": true,
+              "optional": true
             },
             "ini": {
               "version": "1.3.5",
@@ -7181,6 +7185,7 @@
             "is-fullwidth-code-point": {
               "version": "1.0.0",
               "bundled": true,
+              "optional": true,
               "requires": {
                 "number-is-nan": "^1.0.0"
               }
@@ -7200,11 +7205,13 @@
             },
             "minimist": {
               "version": "0.0.8",
-              "bundled": true
+              "bundled": true,
+              "optional": true
             },
             "minipass": {
               "version": "2.3.5",
               "bundled": true,
+              "optional": true,
               "requires": {
                 "safe-buffer": "^5.1.2",
                 "yallist": "^3.0.0"
@@ -7221,6 +7228,7 @@
             "mkdirp": {
               "version": "0.5.1",
               "bundled": true,
+              "optional": true,
               "requires": {
                 "minimist": "0.0.8"
               }
@@ -7293,7 +7301,8 @@
             },
             "number-is-nan": {
               "version": "1.0.1",
-              "bundled": true
+              "bundled": true,
+              "optional": true
             },
             "object-assign": {
               "version": "4.1.1",
@@ -7303,6 +7312,7 @@
             "once": {
               "version": "1.4.0",
               "bundled": true,
+              "optional": true,
               "requires": {
                 "wrappy": "1"
               }
@@ -7378,7 +7388,8 @@
             },
             "safe-buffer": {
               "version": "5.1.2",
-              "bundled": true
+              "bundled": true,
+              "optional": true
             },
             "safer-buffer": {
               "version": "2.1.2",
@@ -7408,6 +7419,7 @@
             "string-width": {
               "version": "1.0.2",
               "bundled": true,
+              "optional": true,
               "requires": {
                 "code-point-at": "^1.0.0",
                 "is-fullwidth-code-point": "^1.0.0",
@@ -7425,6 +7437,7 @@
             "strip-ansi": {
               "version": "3.0.1",
               "bundled": true,
+              "optional": true,
               "requires": {
                 "ansi-regex": "^2.0.0"
               }
@@ -7463,11 +7476,13 @@
             },
             "wrappy": {
               "version": "1.0.2",
-              "bundled": true
+              "bundled": true,
+              "optional": true
             },
             "yallist": {
               "version": "3.0.3",
-              "bundled": true
+              "bundled": true,
+              "optional": true
             }
           }
         }

+ 20 - 11
src/reducers/index.js

@@ -9,7 +9,6 @@ const gql = new GraphQLClient('http://shop-roles.asmer.fs.a-level.com.ua/graphql
 
 const sagaMiddleware = createSagaMiddleware()
 
-
 let store = createStore((state={}, {type, ...params}) => { //единственный редьюсер данного хранилища
     if (type === 'SEARCH_RESULT'){
         return {searchResult: {...params}}
@@ -20,20 +19,20 @@ let store = createStore((state={}, {type, ...params}) => { //единствен
 const delay = ms => new Promise(ok => setTimeout(() => ok(ms), ms))
 
 function* aWorker({timeout, message}){
+    console.log('НАЧАЛО A WORKER ', timeout, message)
     yield put({type: 'LOG', text: `aWorker started with ${timeout}`})
     yield delay(timeout)
     console.log(message)
 }
 
 function* actionCheck(){
-    yield takeLeading('ACTION_A', aWorker)
+    yield takeLatest('ACTION_A', aWorker)
 }
 
 function* searchWorker({text}){
-    yield put(actionSearchResult({payload: null}))
-    yield delay(2000)
-//    let payload = yield delay(2000) //типа fetch
-    let payload = yield gql.request(`
+    yield put(actionSearchResult({payload: null})) //аналог dispatch(actionSearchResult({payload:null}))
+    yield delay(2000) //аналог await
+    let payload = yield gql.request( `
             query gf($query: String){
                 GoodFind(query: $query){
                     _id, name, description, price, images{
@@ -47,26 +46,36 @@ function* searchWorker({text}){
                         {
                             sort: [{name: 1}]} //сортируем по title алфавитно
                         ])
-            }) 
-    yield put(actionSearchResult({payload}))
+            }) //аналог того же await 
+    yield put(actionSearchResult({payload})) //аналог dispatch()
     console.log('search end' , text)
 }
 
 function* searchCheck(){
-    yield takeLatest('SEARCH', searchWorker)
+    yield takeLeading('SEARCH', searchWorker)
+}
+
+function* logoutWorker(){
+    yield put({type: 'PROMISE_CLEAR'})
+}
+
+function* logoutWatcher(){
+    yield takeEvery('AUTH_LOGOUT', logoutWorker)
 }
 
 
 function* rootSaga(){
     yield all([
         actionCheck(),
-        searchCheck()
+        searchCheck(),
+        logoutWatcher()
+        //еще вотчеры если надо
     ])
 }
 
 sagaMiddleware.run(rootSaga)
 
-//store.subscribe(()=> console.log(store.getState())) // подписка на обновления store
+store.subscribe(()=> console.log(store.getState())) // подписка на обновления store
 
 store.dispatch({type: 'ACTION_A', timeout: 5000, message: 'HELLO WORLD 5sec'})
 store.dispatch({type: 'ACTION_A', timeout: 2000, message: 'WORLD HELLO 2sec'})