Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Tips] Update the cache file automatically in the background #23

Open
masutaka opened this issue Dec 30, 2017 · 2 comments
Open

[Tips] Update the cache file automatically in the background #23

masutaka opened this issue Dec 30, 2017 · 2 comments

Comments

@masutaka
Copy link

helm-github-stars.el can update the cache file helm-github-stars-cache-file automatically. But it's a synchronous process and takes time.

(setq helm-github-stars-refetch-time 0.5)

I can do it asynchronously using async.el. async.el evaluates S-expression in child emacs process.

(require 'helm-github-stars)

(defvar my-helm-github-stars-interval (* 3 60 60)
  "Number of seconds to call `my-helm-github-stars-async-generate-cache-file'.")

(defvar my-helm-github-stars-timer nil
  "Timer object for GitHub Stars caching will be stored here.
DO NOT SET VALUE MANUALLY.")

(defun my-helm-github-stars-async-generate-cache-file ()
  "Generate `helm-github-stars-cache-file' in the child emacs process"
  (async-start
   ;; START-FUNC
   `(lambda ()
      (let ((start-time (current-time)))
	(require 'package)
	(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/") t)
	(package-initialize)
	(require 'helm-github-stars)
	(setq helm-github-stars-token "<GitHub Personal Access Token>""))
	(hgs/generate-cache-file)
	start-time))
   ;; FINISH-FUNC
   (lambda (start-time)
     (let ((now (current-time)))
       (message "[GH] Success to GET my GitHub Stars and Repos (%0.1fsec) at %s."
		(time-to-seconds (time-subtract now start-time))
		(format-time-string "%Y-%m-%d %H:%M:%S" now))))))

(defun my-helm-github-stars-set-timer ()
  "Set helm-github-stars timer."
  (setq my-helm-github-stars-timer
	(run-at-time "0 sec"
		     my-helm-github-stars-interval
		     #'my-helm-github-stars-async-generate-cache-file)))

(defun my-helm-github-stars-cancel-timer ()
  "Cancel helm-github-stars timer."
  (when my-helm-github-stars-timer
    (cancel-timer my-helm-github-stars-timer)
    (setq my-helm-github-stars-timer nil)))

(my-helm-github-stars-set-timer)

I recommend to remove the below setting.

(setq helm-github-stars-refetch-time 0.5)

See also;

Thanks.

@Sliim
Copy link
Owner

Sliim commented Jan 2, 2018

Hello @masutaka
Thanks for your suggestion! I think this could be a good improvement 👍

Maybe we could use the helm-github-stars-refetch-time variable for the function run-at-time (in your my-helm-github-stars-set-timer) no?

@masutaka
Copy link
Author

masutaka commented Jan 3, 2018

Of course, It's okay 👌

When I wrote the code, I thought helm-github-stars-refetch-time should be only used for the control of hgs/clear-cache-file-by-time(). However it's a just idea. It's no problem to use helm-github-stars-refetch-time instead of the my-helm-github-stars-interval.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants