Skip to content

Commit

Permalink
Add prefix to accessor
Browse files Browse the repository at this point in the history
  • Loading branch information
chuntaro committed Apr 25, 2020
1 parent 02c470b commit 2a8e714
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 53 deletions.
68 changes: 34 additions & 34 deletions promise-core.el
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,12 @@
promise--is-error)))

(defclass promise-class ()
((_deferred-state :accessor _deferred-state :initform 0)
(_state :accessor _state :initform 0)
(_value :accessor _value :initform nil)
(_deferreds :accessor _deferreds :initform nil)
((_deferred-state :accessor promise-_deferred-state :initform 0)
(_state :accessor promise-_state :initform 0)
(_value :accessor promise-_value :initform nil)
(_deferreds :accessor promise-_deferreds :initform nil)
;; for rejection-tracking
(_rejection-id :accessor _rejection-id :initform nil)))
(_rejection-id :accessor promise-_rejection-id :initform nil)))
(defvar promise--on-handle nil)
(defvar promise--on-reject nil)

Expand Down Expand Up @@ -153,34 +153,34 @@
res))))))

(defun promise--handle (self deferred)
(while (= (_state self) 3)
(setf self (_value self)))
(while (= (promise-_state self) 3)
(setf self (promise-_value self)))
(when promise--on-handle
(funcall promise--on-handle self))
(if (= (_state self) 0)
(if (= (promise-_state self) 0)
(cond
((= (_deferred-state self) 0)
(setf (_deferred-state self) 1
(_deferreds self) deferred))
((= (_deferred-state self) 1)
(setf (_deferred-state self) 2
(_deferreds self) (list (_deferreds self)
deferred)))
((= (promise-_deferred-state self) 0)
(setf (promise-_deferred-state self) 1
(promise-_deferreds self) deferred))
((= (promise-_deferred-state self) 1)
(setf (promise-_deferred-state self) 2
(promise-_deferreds self) (list (promise-_deferreds self)
deferred)))
(t
(setf (_deferreds self) (nconc (_deferreds self)
(list deferred)))))
(setf (promise-_deferreds self) (nconc (promise-_deferreds self)
(list deferred)))))
(promise--handle-resolved self deferred)))

(defun promise--handle-resolved (self deferred)
(promise--asap
(lambda ()
(let-alist deferred
(let ((cb (if (= (_state self) 1) .on-fulfilled .on-rejected)))
(let ((cb (if (= (promise-_state self) 1) .on-fulfilled .on-rejected)))
(if (not cb)
(if (= (_state self) 1)
(promise--resolve .promise (_value self))
(promise--reject .promise (_value self)))
(let ((ret (promise--try-call-one cb (_value self))))
(if (= (promise-_state self) 1)
(promise--resolve .promise (promise-_value self))
(promise--reject .promise (promise-_value self)))
(let ((ret (promise--try-call-one cb (promise-_value self))))
(if (eq ret promise--is-error)
(promise--reject .promise promise--last-error)
(promise--resolve .promise ret)))))))))
Expand All @@ -202,34 +202,34 @@ See: https://github.com/promises-aplus/promises-spec#the-promise-resolution-proc
(cond
((and (eq then (ignore-errors (promise--find-then-function self)))
(promise-class-p new-value))
(setf (_state self) 3
(_value self) new-value)
(setf (promise-_state self) 3
(promise-_value self) new-value)
(promise--finale self)
(cl-return))
((functionp then)
(promise--do-resolve (lambda (resolve reject)
(promise-then new-value resolve reject))
self)
(cl-return)))))
(setf (_state self) 1
(_value self) new-value)
(setf (promise-_state self) 1
(promise-_value self) new-value)
(promise--finale self)))

(defun promise--reject (self new-value)
(setf (_state self) 2
(_value self) new-value)
(setf (promise-_state self) 2
(promise-_value self) new-value)
(when promise--on-reject
(funcall promise--on-reject self new-value))
(promise--finale self))

(defun promise--finale (self)
(when (= (_deferred-state self) 1)
(promise--handle self (_deferreds self))
(setf (_deferreds self) nil))
(when (= (_deferred-state self) 2)
(dolist (deferred (_deferreds self))
(when (= (promise-_deferred-state self) 1)
(promise--handle self (promise-_deferreds self))
(setf (promise-_deferreds self) nil))
(when (= (promise-_deferred-state self) 2)
(dolist (deferred (promise-_deferreds self))
(promise--handle self deferred))
(setf (_deferreds self) nil))
(setf (promise-_deferreds self) nil))
nil)

(defun promise--handler-new (on-fulfilled on-rejected promise)
Expand Down
16 changes: 8 additions & 8 deletions promise-es6-extensions.el
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@

(defun promise--value (value)
(let ((p (promise-new #'ignore)))
(setf (_state p) 1
(_value p) value)
(setf (promise-_state p) 1
(promise-_value p) value)
p))

(defconst promise--t (promise--value t))
Expand Down Expand Up @@ -113,12 +113,12 @@
((and (promise-class-p val)
(eq (promise--find-then-function val)
(promise--then-function)))
(while (= (_state val) 3)
(setf val (_value val)))
(when (= (_state val) 1)
(cl-return (res i (_value val))))
(when (= (_state val) 2)
(funcall reject (_value val)))
(while (= (promise-_state val) 3)
(setf val (promise-_value val)))
(when (= (promise-_state val) 1)
(cl-return (res i (promise-_value val))))
(when (= (promise-_state val) 2)
(funcall reject (promise-_value val)))
(promise-then val
(lambda (val)
(res i val))
Expand Down
20 changes: 10 additions & 10 deletions promise-rejection-tracking.el
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,17 @@
(rejections id 'display-id)))))))
(setf promise--on-handle
(lambda (promise)
(when (and (= (_state promise) 2) ; IS REJECTED
(gethash (_rejection-id promise) rejections))
(if (rejections (_rejection-id promise) 'logged)
(on-handled (_rejection-id promise))
(cancel-timer (rejections (_rejection-id promise) 'timeout)))
(remhash (_rejection-id promise) rejections)))
(when (and (= (promise-_state promise) 2) ; IS REJECTED
(gethash (promise-_rejection-id promise) rejections))
(if (rejections (promise-_rejection-id promise) 'logged)
(on-handled (promise-_rejection-id promise))
(cancel-timer (rejections (promise-_rejection-id promise) 'timeout)))
(remhash (promise-_rejection-id promise) rejections)))
promise--on-reject
(lambda (promise err)
(when (zerop (_deferred-state promise)) ; not yet handled
(setf (_rejection-id promise) (cl-incf id))
(puthash (_rejection-id promise)
(when (zerop (promise-_deferred-state promise)) ; not yet handled
(setf (promise-_rejection-id promise) (cl-incf id))
(puthash (promise-_rejection-id promise)
`((display-id . nil)
(error . ,err)
(timeout . ,(run-at-time
Expand All @@ -131,7 +131,7 @@
2)
nil
(lambda ()
(on-unhandled (_rejection-id promise)))))
(on-unhandled (promise-_rejection-id promise)))))
(logged . nil))
rejections)))))))

Expand Down
2 changes: 1 addition & 1 deletion promise.el
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ Timeout:

(defun promise-wait-value (promise)
"Return orignal value form PROMISE return value of `promise-wait'."
(seq-let (state value) (_value promise)
(seq-let (state value) (promise-_value promise)
(cond
((eq :fullfilled state) value)
((eq :rejected state) (error "Rejected: %s" (prin1-to-string value)))
Expand Down

0 comments on commit 2a8e714

Please sign in to comment.