8000 实现简易版 promise · HashCoding/boxbot@b42e0c8 · GitHub
[go: up one dir, main page]

Skip to content

Commit b42e0c8

Browse files
committed
实现简易版 promise
1 parent fa26e34 commit b42e0c8

File tree

4 files changed

+56
-21
lines changed

4 files changed

+56
-21
lines changed

app.js

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ var Application = function () {
55

66
this.$image = document.querySelector('#image')
77
this.$random = document.querySelector('#random')
8-
this.$run= document.querySelector('#run')
9-
this.$reset= document.querySelector('#reset')
10-
this.$duration= document.querySelector('#duration')
11-
this.$resolution= document.querySelector('#resolution')
8+
this.$run = document.querySelector('#run')
9+
this.$reset = document.querySelector('#reset')
10+
this.$duration = document.querySelector('#duration')
11+
this.$resolution = document.querySelector('#resolution')
1212

1313
this.init()
1414
this.reset()
@@ -24,10 +24,16 @@ Application.prototype.init = function () {
2424
this.$image.addEventListener('change', this.loadImage.bind(this))
2525
}
2626

27+
/**
28+
* 设置运行速度
29+
*/
2730
Application.prototype.setDuration = function () {
2831
this.boxbot.setDuration(parseInt(this.$duration.value))
2932
}
3033

34+
/**
35+
* 设置地图尺寸
36+
*/
3137
Application.prototype.setResolution = function () {
3238
this.boxbot.setResolution(parseInt(this.$resolution.value))
3339
this.reset()
@@ -76,7 +82,7 @@ Application.prototype.loadImage = function () {
7682

7783
/**
7884
* 键盘事件处理
79-
*
85+
*
8086
* @param {Event} event
8187
*/
8288
Application.prototype.hotkey = function (event) {
@@ -114,18 +120,20 @@ Application.prototype.run = function () {
114120
var prev = 0
115121
codes.forEach((function (code, i) {
116122
if (code) {
117-
this.boxbot.exec(code).then((function () {
118-
if (i % 37 == 0) {
119-
this.editor.scrollTo(i)
120-
}
121-
this.editor.clearFlag(prev)
122-
this.editor.setFlag(i, 'success')
123-
prev = i
124-
}).bind(this)).catch((function (e) {
125-
console.log(e)
126-
this.editor.clearFlag(prev)
127-
this.editor.setFlag(i, 'warning')
128-
}).bind(this))
123+
this.boxbot.exec(code).then(
124+
(function () {
125+
if (i % 37 == 0) {
126+
this.editor.scrollTo(i)
127+
}
128+
this.editor.clearFlag(prev)
129+
this.editor.setFlag(i, 'success')
130+
prev = i
131+
}).bind(this),
132+
(function (e) {
133+
console.log(e)
134+
this.editor.clearFlag(prev)
135+
this.editor.setFlag(i, 'warning')
136+
}).bind(this))
129137
}
130138
}).bind(this))
131139
}

boxbot.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ var Boxbot = function () {
1515
Boxbot.prototype.directions = {bot: BOTTOM, lef: LEFT, top: TOP, rig: RIGHT}
1616
Boxbot.prototype.commands = [
1717
{
18-
pattern: /^go(\s+)?(\w+)?$/i,
18+
pattern: /^go(\s+)?(\d+)?$/i,
1919
handler: function (step) {
2020
return this.run(this.go, [arguments[1]])
2121
}
@@ -125,7 +125,7 @@ Boxbot.prototype.setResolution = function (size) {
125125
/**
126126
* 修墙
127127
*
128-
* @param {[int]} [position]
128+
* @param {[int]} [position] 默认为前方
129129
*/
130130
Boxbot.prototype.build = function (position) {
131131
position = position || this.bot.getPosition(null, 1)
@@ -140,7 +140,7 @@ Boxbot.prototype.build = function (position) {
140140
* 涂色
141141
*
142142 341A
* @param {string} color
143-
* @param {[int]} [position]
143+
* @param {[int]} [position] 默认为前方
144144
*/
145145
Boxbot.prototype.setColor = function (color, position) {
146146
position = position || this.bot.getPosition(null, 1)
@@ -291,7 +291,7 @@ Boxbot.prototype.search = function (target, algorithm) {
291291
var path = this.finder.search(algorithm || 'dfs', this.bot.getCurrentPosition(), target)
292292
if (path) {
293293
if (path.length > 1) {
294-
path.shift()
294+
path.shift() // 忽略掉起点
295295
path.forEach((function (item) {
296296
this.run(this.goto, [item, true])
297297
}).bind(this))

index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
</article>
5252
</article>
5353
<p class="note"><em>注:选择图片文件后,程序会读取图片并生成绘图命令,接着运行命令,机器人将一步一步地把图形画出来!</em></p>
54+
<script src="promise.js"></script>
5455
<script src="boxbot-bot.js"></script>
5556
<script src="boxbot-map.js"></script>
5657
<script src="boxbot-finder.js"></script>

promise.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* 简易版 Promise,非标准实现,只兼容一小部分用法
3+
*
4+
* @param {function} executor
5+
* @constructor
6+
*/
7+
var Promise = function (executor) {
8+
executor(this.resolve.bind(this), this.reject.bind(this))
9+
}
10+
11+
Promise.prototype.resolve = function (value) {
12+
if (this.onResolve) {
13+
this.onResolve(value)
14+
}
15+
}
16+
17+
Promise.prototype.reject = function (reason) {
18+
if (this.onReject) {
19+
this.onReject(reason)
20+
}
21+
}
22+
23+
Promise.prototype.then = function (onResolve, onReject) {
24+
this.onResolve = onResolve
25+
this.onReject = onReject
26+
}

0 commit comments

Comments
 (0)
0