checkNeedRemoteRefresh.js 857 B

12345678910111213141516171819202122232425262728293031323334353637
  1. /**
  2. * @author oldj
  3. * @blog https://oldj.net
  4. */
  5. 'use strict'
  6. const saveHosts = require('./saveHosts')
  7. const checkOne = require('./checkOneRemoteHosts')
  8. function isHostsEqual(hosts1, hosts2) {
  9. return hosts1.id === hosts2.id && hosts1.content === hosts2.content && hosts1.last_refresh === hosts2.last_refresh
  10. }
  11. function isListEqual (list1, list2) {
  12. let len = list1.length
  13. if (len !== list2.length) return false
  14. for (let i = 0; i < len; i ++) {
  15. if (!isHostsEqual(list1[i] ,list2[i])) {
  16. return false
  17. }
  18. }
  19. return true
  20. }
  21. module.exports = (svr, list, hosts = null) => {
  22. return Promise.all(list.map(i => checkOne(svr, i, hosts && hosts.id === i.id)))
  23. .then(list2 => {
  24. if (!isListEqual(list, list2)) {
  25. // 仅在 list 的内容发生变化时才再次保存
  26. return saveHosts(svr, list2)
  27. }
  28. })
  29. }