forked from MrTreasure/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mobx.ts
234 lines (205 loc) · 4.8 KB
/
mobx.ts
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
class NPromise {
private status: STATUS = STATUS.PENDING
private value: any
private reason: any
private onFulFillCallback: Function[] = []
private onRejectCllback: Function[] = []
constructor (exector) {
const self = this
const resolve = res => {
setImmediate(() => {
self.status = STATUS.FULFILLED
self.onFulFillCallback.forEach(fun => {
fun(self.value)
})
})
}
const reject = res => {
setImmediate(() => {
self.status = STATUS.REJECTED
self.onRejectCllback.forEach(fun => {
fun(self.reason)
})
})
}
}
static race (promiseList: NPromise[]) {
return new NPromise((resolve, reject) => {
for (let p of promiseList) {
p.then(resolve, reject)
}
})
}
static all (promiseList: NPromise[]) {
return new NPromise((resolve, reject) => {
let i = 0
let len = promiseList.length
let count = len
const result = []
const resolveAll = (index, value) => {
result[index] = value
if (--count === 0) {
resolve(result)
}
}
const resolver = index => value => resolveAll(index, value)
const rejecter = reason => reject(reason)
for (; i < len; i++) {
promiseList[i].then(resolver(i), rejecter)
}
})
}
public then (onFulFilled, onRject) {
const self = this
let promise: NPromise
if (this.status === STATUS.FULFILLED) {
promise = new NPromise((resolve, reject) => {
try {
let res = onFulFilled(self.value)
resolvePromise(promise, res, resolve, reject)
} catch (error) {
reject(error)
}
})
}
if (this.status === STATUS.REJECTED) {
promise = new NPromise((resolve, reject) => {
try {
let res = onRject(self.reason)
resolvePromise(promise, res, resolve, reject)
} catch (error) {
reject(error)
}
})
}
if (this.status === STATUS.PENDING) {
promise = new NPromise((resolve, reject) => {
self.onFulFillCallback.push(value => {
try {
let res = onFulFilled(self.value)
resolvePromise(promise, res, resolve, reject)
} catch (error) {
reject(error)
}
})
self.onRejectCllback.push(reason => {
try {
let res = onRject(self.reason)
resolvePromise(promise, res, resolve, reject)
} catch (error) {
reject(error)
}
})
})
}
return promise
}
}
function resolvePromise (promise, res, resolve, reject) {
let then
let called = false
if (res && (typeof res === 'object' || typeof res === 'function')) {
then = res.then
if (typeof then === 'function') {
try {
then.call(res, (value) => {
if (called) return
called = true
resolvePromise(promise, value, resolve, reject)
}, err => {
if (called) return
called = true
reject(err)
})
} catch (error) {
reject(error)
}
} else {
resolve(res)
}
} else {
resolve(res)
}
}
enum STATUS {
PENDING,
FULFILLED,
REJECTED
}
const inOrder = (root, callback) => {
const stack = []
let current = root
while (current || stack.length > 0) {
while (current) {
stack.push(current)
current = current.left
}
if (stack.length > 0) {
current = stack.pop()
callback(current.val)
current = current.right
}
}
}
const preOrder = (root, callback) => {
const stack = []
let current
stack.push(root)
while (stack.length > 0) {
current = stack.pop()
callback(current.val)
if (current.right) {
stack.push(current.right)
}
if (current.left) {
stack.push(current.left)
}
}
}
const postOrder = (root, callback) => {
const stack = []
let current = root
let prev
while (current || stack.length > 0) {
if (current) {
stack.push(current.left)
current = current.left
} else {
current = stack[stack.length - 1]
if (current.rihgt && current.right !== prev) {
stack.push(current.right)
current = current.left
} else {
current = stack.pop()
callback(current.val)
prev = current
current = null
}
}
}
}
const insert = (root, val) => {
const node = { val }
let parent
let current = root
if (!root) {
root = node
return
}
while (current) {
parent = current
if (current.left.val > val) {
current = current.left
if (!current) {
parent.left = node
break
}
} else {
current = current.right
if (!current) {
parent.right = node
break
}
}
}
}