forked from chuntaro/emacs-promise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
promise-es6-extensions.el
163 lines (140 loc) · 6.43 KB
/
promise-es6-extensions.el
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
;;; promise-es6-extensions.el --- Porting es6-extensions.js -*- lexical-binding: t; -*-
;; Copyright (C) 2016-2017 chuntaro
;; Author: chuntaro <[email protected]>
;; URL: https://github.com/chuntaro/emacs-promise
;; Keywords: async promise convenience
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;; The original JavaScript code is:
;;
;; Copyright (c) 2014 Forbes Lindesay
;;
;; Permission is hereby granted, free of charge, to any person obtaining a copy
;; of this software and associated documentation files (the "Software"), to deal
;; in the Software without restriction, including without limitation the rights
;; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
;; copies of the Software, and to permit persons to whom the Software is
;; furnished to do so, subject to the following conditions:
;;
;; The above copyright notice and this permission notice shall be included in
;; all copies or substantial portions of the Software.
;;
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
;; THE SOFTWARE.
;;; Commentary:
;; This implementation ported the following implementation faithfully.
;; https://github.com/then/promise/blob/master/src/es6-extensions.js
;;
;; This file contains the ES6 extensions to the core Promises/A+ API.
;; (promise-resolve value)
;; (promise-all [values...])
;; (promise-reject value)
;; (promise-race [values...])
;; (promise-catch ((this promise-class) on-rejected))
;;; Code:
(require 'promise-core)
(defsubst promise--then-function ()
(ignore-errors
(cl--generic-method-function (cl-find-method #'promise-then
'()
'(promise-class)))))
;; Static Functions
(defun promise--value (value)
(let ((p (promise-new #'ignore)))
(setf (_state p) 1
(_value p) value)
p))
(defconst promise--t (promise--value t))
(defconst promise--nil (promise--value nil))
(defconst promise--zero (promise--value 0))
(defconst promise--emptystring (promise--value ""))
(defun promise-resolve (value)
(cond
((promise-class-p value) value)
((eq value t) promise--t)
((eq value nil) promise--nil)
((eq value 0) promise--zero)
((eq value "") promise--emptystring)
((promise--is-object value)
(condition-case ex
(let ((then (ignore-errors (promise--find-then-function value))))
(if (functionp then)
(promise-new (lambda (resolve reject)
(promise-then value resolve reject)))
(promise--value value)))
(error (promise-new (lambda (_resolve reject)
(funcall reject ex))))))
(t
(promise--value value))))
(defun promise-all (arr)
(let ((args (cl-coerce arr 'vector)))
(promise-new
(lambda (resolve reject)
(if (zerop (length args))
(funcall resolve [])
(let ((remaining (length args)))
(cl-labels
((res (i val)
(cl-block nil
(when (and val (promise--is-object val))
(cond
((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)))
(promise-then val
(lambda (val)
(res i val))
reject)
(cl-return))
(t
(let ((then (ignore-errors
(promise--find-then-function val))))
(when (functionp then)
(let ((p (promise-new
(lambda (resolve reject)
(promise-then val
resolve
reject)))))
(promise-then p
(lambda (val)
(res i val))
reject)
(cl-return)))))))
(setf (aref args i) val)
(when (zerop (cl-decf remaining))
(funcall resolve args)))))
(cl-loop for i from 0
for arg across args
do (res i arg)))))))))
(defun promise-reject (value)
(promise-new (lambda (_resolve reject)
(funcall reject value))))
(defun promise-race (values)
(promise-new (lambda (resolve reject)
(cl-loop for value across (cl-coerce values 'vector)
do (promise-then (promise-resolve value)
resolve
reject)))))
(cl-defmethod promise-catch ((this promise-class) on-rejected)
(promise-then this nil on-rejected))
(provide 'promise-es6-extensions)
;;; promise-es6-extensions.el ends here