emojis_watcher_darwin.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // SiYuan - Refactor your thinking
  2. // Copyright (c) 2020-present, b3log.org
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. //go:build darwin
  17. package model
  18. import (
  19. "path/filepath"
  20. "time"
  21. "github.com/radovskyb/watcher"
  22. "github.com/siyuan-note/logging"
  23. "github.com/siyuan-note/siyuan/kernel/util"
  24. )
  25. var emojisWatcher *watcher.Watcher
  26. func WatchEmojis() {
  27. go func() {
  28. watchEmojis()
  29. }()
  30. }
  31. func watchEmojis() {
  32. if nil != emojisWatcher {
  33. emojisWatcher.Close()
  34. }
  35. emojisWatcher = watcher.New()
  36. emojisDir := filepath.Join(util.DataDir, "emojis")
  37. go func() {
  38. for {
  39. select {
  40. case event, ok := <-emojisWatcher.Event:
  41. if !ok {
  42. return
  43. }
  44. //logging.LogInfof("emojis changed: %s", event)
  45. util.PushReloadEmojiConf()
  46. case err, ok := <-emojisWatcher.Error:
  47. if !ok {
  48. return
  49. }
  50. logging.LogErrorf("watch emojis failed: %s", err)
  51. case <-emojisWatcher.Closed:
  52. return
  53. }
  54. }
  55. }()
  56. if err := emojisWatcher.Add(emojisDir); nil != err {
  57. logging.LogErrorf("add emojis watcher for folder [%s] failed: %s", emojisDir, err)
  58. return
  59. }
  60. //logging.LogInfof("added file watcher [%s]", emojisDir)
  61. if err := emojisWatcher.Start(10 * time.Second); nil != err {
  62. logging.LogErrorf("start emojis watcher for folder [%s] failed: %s", emojisDir, err)
  63. return
  64. }
  65. }
  66. func CloseWatchEmojis() {
  67. if nil != emojisWatcher {
  68. emojisWatcher.Close()
  69. }
  70. }