1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
|
;;; An asynchronous data model sample for ctable.el
(require 'ctable)
(require 'deferred)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; synchronous version
(defun ctbl:sync-demo1 ()
(interactive)
(ctbl:open-table-buffer-easy
(loop with lim = 4000
for i from 0 upto lim
for d = (/ (random 1000) 1000.0)
collect
(list i d (exp (- (/ i 1.0 lim))) (exp (* (- (/ i 1.0 lim)) d))))))
;; (ctbl:sync-demo1) ; 5 seconds to display!
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; asynchronous version
(defun ctbl:async-demo ()
"Sample code for implementation for async data model table."
(interactive)
(let ((param (copy-ctbl:param ctbl:default-rendering-param)))
(setf (ctbl:param-fixed-header param) t)
(let* ((async-model ; define async-data-model
(make-ctbl:async-model
:request 'ctbl:async-demo-request
:cancel 'ctbl:async-demo-cancel
:reset 'ctbl:async-demo-reset
:init-num 40 :more-num 20))
(cp
(ctbl:create-table-component-buffer
:model
(make-ctbl:model
:column-model
(list (make-ctbl:cmodel :title "row")
(make-ctbl:cmodel :title "delta")
(make-ctbl:cmodel :title "exp")
(make-ctbl:cmodel :title "exp-delta"))
:data async-model) ; here!
:param param)))
(ctbl:cp-add-click-hook
cp (lambda () (message "CTable : Click Hook [%S]"
(ctbl:cp-get-selected-data-row cp))))
(pop-to-buffer (ctbl:cp-get-buffer cp)))))
(defvar ctbl:async-demo-timer nil)
(defun ctbl:async-demo-request (row-num len responsef errorf &rest)
(lexical-let
((row-num row-num) (len len)
(responsef responsef) (errorf errorf))
(setq ctbl:async-demo-timer
(deferred:$
(deferred:wait 500)
(deferred:nextc it
(lambda (x)
(setq ctbl:async-demo-timer nil)
(funcall responsef
(if (< 500 row-num) nil
(loop with lim = 100
for i from row-num below (+ row-num len)
for d = (/ (random 1000) 1000.0)
collect
(list i d (exp (- (/ i 1.0 lim)))
(exp (* (- (/ i 1.0 lim)) d))))))))))))
(defun ctbl:async-demo-reset (&rest)
(message "RESET async data!!"))
(defun ctbl:async-demo-cancel (&rest)
(when ctbl:async-demo-timer
(deferred:cancel ctbl:async-demo-timer)))
;; (progn (eval-current-buffer) (ctbl:async-demo))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; async wrapper version
(defun ctbl:sync-demo2 ()
(interactive)
(let* ((async-model ; wrapping a huge data in async-data-model
(ctbl:async-model-wrapper
(loop with lim = 4000
for i from 0 upto lim
for d = (/ (random 1000) 1000.0)
collect
(list i d (exp (- (/ i 1.0 lim))) (exp (* (- (/ i 1.0 lim)) d))))))
(cp
(ctbl:create-table-component-buffer
:model
(make-ctbl:model
:column-model
(list (make-ctbl:cmodel :title "row")
(make-ctbl:cmodel :title "delta")
(make-ctbl:cmodel :title "exp")
(make-ctbl:cmodel :title "exp-delta"))
:data async-model))))
(pop-to-buffer (ctbl:cp-get-buffer cp))))
;; (progn (eval-current-buffer) (ctbl:sync-demo2))
|