hw18.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. // Светофор =======================================================================
  2. const delay = ms => new Promise(ok => setTimeout(() => ok(ms), ms))
  3. async function trafficLight(){
  4. const trafficLights = document.createElement('div')
  5. document.body.appendChild(trafficLights)
  6. Object.assign(document.body.style, {display: 'flex', justifyContent: 'center'})
  7. trafficLights.innerHTML = `<img src="trafficLight/empty.png" style="width:200px">`
  8. await delay(1000)
  9. while (true){
  10. trafficLights.innerHTML = `<img src="trafficLight/red.png" style="width:200px">`
  11. await delay(3000)
  12. trafficLights.innerHTML = `<img src="trafficLight/redYellow.png" style="width:200px">`
  13. await delay(1250)
  14. trafficLights.innerHTML = `<img src="trafficLight/green.png" style="width:200px">`
  15. await delay(3000)
  16. for (let i = 0; i < 3; i ++) {
  17. trafficLights.innerHTML = `<img src="trafficLight/empty.png" style="width:200px">`
  18. await delay(500)
  19. trafficLights.innerHTML = `<img src="trafficLight/green.png" style="width:200px">`
  20. await delay(500)
  21. }
  22. trafficLights.innerHTML = `<img src="trafficLight/yellow.png" style="width:200px">`
  23. await delay(1250)
  24. }
  25. }
  26. trafficLight()
  27. // Stage 2 =======================================================================
  28. const delay = ms => new Promise(ok => setTimeout(() => ok(ms), ms))
  29. async function trafficLight(parent, redTime, yellowTime, greenTime, prepare){
  30. const trafficLights = document.createElement('div')
  31. parent.appendChild(trafficLights)
  32. Object.assign(document.body.style, {display: 'flex', justifyContent: 'center'})
  33. trafficLights.innerHTML = `<img src="trafficLight/empty.png" style="width:200px">`
  34. await delay(1000)
  35. while (true){
  36. trafficLights.innerHTML = `<img src="trafficLight/red.png" style="width:200px">`
  37. await delay(redTime)
  38. trafficLights.innerHTML = `<img src="trafficLight/redYellow.png" style="width:200px">`
  39. await delay(yellowTime)
  40. trafficLights.innerHTML = `<img src="trafficLight/green.png" style="width:200px">`
  41. await delay(greenTime)
  42. for (let i = 0; i < 3; i ++) {
  43. trafficLights.innerHTML = `<img src="trafficLight/empty.png" style="width:200px">`
  44. await delay(prepare)
  45. trafficLights.innerHTML = `<img src="trafficLight/green.png" style="width:200px">`
  46. await delay(prepare)
  47. }
  48. trafficLights.innerHTML = `<img src="trafficLight/yellow.png" style="width:200px">`
  49. await delay(yellowTime)
  50. }
  51. }
  52. trafficLight(document.body, 3000, 1250, 3000, 500)
  53. // PedestrianTrafficLight =======================================================================
  54. function domEventPromise (element, eventName) {
  55. return new Promise ( (resolve) => {
  56. const event = event => {
  57. element.removeEventListener(eventName, event)
  58. resolve(event)
  59. }
  60. element.addEventListener(eventName, event)
  61. })
  62. }
  63. async function padestrianTrafficLight (parent, time, buttonTime) {
  64. const trafficLights = document.createElement('div')
  65. parent.appendChild(trafficLights)
  66. Object.assign(document.body.style, {display: 'flex', justifyContent: 'center', flexDirection: 'column', alignItems: 'center'})
  67. const goButton = document.createElement('button')
  68. parent.appendChild(goButton)
  69. goButton.innerText = 'Go!'
  70. Object.assign(goButton.style, {backgroundColor: '#313131', borderRadius: '50%', width: '50px', height: '50px', color: 'white', cursor: 'pointer'})
  71. trafficLights.innerHTML = `<img src="trafficLight/stop.png" style="width:200px">`
  72. while (true) {
  73. goButton.disabled = true
  74. Object.assign(goButton.style, {borderColor:'#e50201', color: '#e50201'})
  75. await delay(buttonTime)
  76. goButton.disabled = false
  77. Object.assign(goButton.style, {borderColor:'#bdd909', color: '#bdd909'})
  78. await Promise.race([domEventPromise(goButton, 'click'), delay(time)])
  79. trafficLights.innerHTML = `<img src="trafficLight/go.png" style="width:200px">`
  80. await delay (time)
  81. trafficLights.innerHTML = `<img src="trafficLight/stop.png" style="width:200px">`
  82. }
  83. }
  84. padestrianTrafficLight(document.body, 10000, 3000)
  85. // speedtest =======================================================================
  86. const delay = ms => new Promise(ok => setTimeout(() => ok(ms), ms))
  87. async function speedtest(getPromise, count, parallel){
  88. const start = performance.now()
  89. for (let countRepeat = 0; countRepeat < count; countRepeat ++) {
  90. const promiseArray = []
  91. for (let countPromise = 0; countPromise < parallel; countPromise ++) {
  92. promiseArray.push(getPromise())
  93. }
  94. const result = await Promise.all(promiseArray)
  95. console.log(result)
  96. }
  97. const duration = performance.now() - start
  98. return {
  99. 'duration': duration,
  100. 'parallelDuration': duration/(count*parallel),
  101. 'paralledSpeed': count*parallel/duration,
  102. 'queryDuration': duration/count,
  103. 'querySpeed': (count*parallel/duration)/count
  104. }
  105. }
  106. speedtest(() => fetch('http://swapi.dev/api/people/1').then(res => res.json()), 10, 5).then(result => console.log(result))
  107. speedtest(() => delay(1000), 10, 10 ).then(result => console.log(result))
  108. // gql =======================================================================
  109. async function gql (endpoint, query, variables) {
  110. const querys = await fetch(endpoint,
  111. {
  112. method: 'POST',
  113. headers:
  114. {
  115. 'Content-Type': 'application/json',
  116. 'Accept': 'application/json',
  117. },
  118. body: JSON.stringify(
  119. {
  120. 'query': query,
  121. 'variables': variables,
  122. }
  123. )
  124. }
  125. )
  126. .then (data => data.json())
  127. return querys
  128. }
  129. ;(async () => {
  130. const catQuery = `query cats($q: String){
  131. CategoryFind(query: $q){
  132. _id name
  133. }
  134. }`
  135. const cats = await gql("http://shop-roles.node.ed.asmer.org.ua/graphql", catQuery, {q: "[{}]"})
  136. console.log(cats) //список категорий с _id name и всем таким прочим
  137. const loginQuery = `query login($login:String, $password:String){
  138. login(login:$login, password:$password)
  139. }`
  140. const token = await gql("http://shop-roles.node.ed.asmer.org.ua/graphql", loginQuery ,{login: "test457", password: "123123"})
  141. console.log(token)
  142. })()
  143. // jwtDecode =======================================================================
  144. function jwtDecode (token) {
  145. try {
  146. const tokenArr = token.split('.')
  147. const data = atob(tokenArr[1])
  148. const dataObj = JSON.parse(data)
  149. return dataObj
  150. }
  151. catch {
  152. return undefined
  153. }
  154. }
  155. const token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOnsiaWQiOiI2MzIyMDVhZWI3NGUxZjVmMmVjMWEzMjAiLCJsb2dpbiI6InRlc3Q0NTciLCJhY2wiOlsiNjMyMjA1YWViNzRlMWY1ZjJlYzFhMzIwIiwidXNlciJdfSwiaWF0IjoxNjY4MjcyMTYzfQ.rxV1ki9G6LjT2IPWcqkMeTi_1K9sb3Si8vLB6UDAGdw"
  156. console.log(jwtDecode(token))
  157. //{
  158. // "sub": {
  159. // "id": "632205aeb74e1f5f2ec1a320",
  160. // "login": "test457",
  161. // "acl": [
  162. // "632205aeb74e1f5f2ec1a320",
  163. // "user"
  164. // ]
  165. // },
  166. // "iat": 1668272163
  167. //}
  168. try {
  169. console.log(jwtDecode()) //undefined
  170. console.log(jwtDecode("дичь")) //undefined
  171. console.log(jwtDecode("ey.ey.ey")) //undefined
  172. console.log('до сюда доработало, а значит jwtDecode не матерился в консоль красным цветом')
  173. }
  174. finally{
  175. console.log('ДЗ, видимо, окончено')
  176. }