Skip to content

Commit a9a41be

Browse files
authored
Merge pull request #9 from 2214962083/dev
更新基础包,新增内置 EndAwait 函数以支持末尾顶部 await
2 parents fb48d3d + 7c59f90 commit a9a41be

File tree

13 files changed

+639
-116
lines changed

13 files changed

+639
-116
lines changed

docs/questions.md

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@
4040

4141
这种状况是因为 `Scriptable` 内部处理多个小部件异步渲染时没处理好。导致部分小部件渲染为空。([在此可以看到相关讨论](https://talk.automators.fm/t/hellp-call-script-setwidget-to-set-the-content-of-the-widget-run-in-widget/9615/7))
4242

43-
解决方法也很简单,在渲染时最外层加个 await。由于打包器不支持 `top-level-await`,建议在编译好后手动添加上去。
44-
45-
开发时不用加,毕竟是偶尔情况,在生产环境打包发布给别人用时,再手动加。
43+
解决方法也很简单,在渲染时最外层加个 await。由于打包器不支持 `top-level-await`,所以本框架内置了一个末尾底部等待的函数 `EndAwait` ,使用方法见下
4644

4745
```tsx
4846
Class HelloWorld {
@@ -54,17 +52,29 @@ Class HelloWorld {
5452
}
5553
}
5654

57-
// 在这加,手动往打包后的文件加,因为打包器不会把 await 打包进去,而且 tsconfig 原因,编辑器也会报错
58-
await new HelloWorld().init()
59-
```
55+
// 使用前
56+
// new HelloWorld().init()
6057

61-
如果有更好的解决方法可以 `issues` 提案。
58+
// 使用后,这样就不会出现以上状况了
59+
EndAwait(() => new HelloWorld().init())
60+
```
6261

6362
<br/>
6463

6564
<br/>
6665

6766
**E、为什么 jsx widget 是异步渲染?**
6867

69-
为了方便引入网络资源,比如图片,实现 `wimage` 的 src 填写网络连接就能自动加载图片,是需要异步等待的。所以渲染小部件,返回 ListWidget 实例也是异步。
68+
为了方便引入网络资源,比如图片,实现 `wimage` 的 src 填写网络连接就能自动加载图片,是需要异步等待的。所以渲染小部件,返回 ListWidget 实例也是异步。
69+
70+
<br/>
71+
72+
<br/>
73+
74+
**F、基础包同步运行时报错,而同步后,直接运行所同步的脚本却没报错?**
75+
76+
可能是基础包版本过旧原因,再扫二维码进引导页重新安装一下基础包即可。
77+
78+
<br/>
7079

80+
<br/>

src/lib/basic.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,9 @@ class Basic {
246246
*/
247247
async runCode(syncScriptName: string, scriptText: string) {
248248
try {
249-
const runRemoteCode = new Function(`${scriptText}`)
249+
const runRemoteCode = new Function(`(async () => {
250+
${scriptText}
251+
})()`)
250252
// 执行远程代码
251253
runRemoteCode()
252254
} catch (err) {

src/lib/compile.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,14 @@ async function compile(options: CompileOptions) {
165165
banner: `${header}
166166
// @编译时间 ${Date.now()}
167167
const MODULE = module;
168+
let __topLevelAwait__ = () => Promise.resolve();
169+
function EndAwait(promiseFunc) {
170+
__topLevelAwait__ = promiseFunc
171+
};
168172
`,
173+
footer: `
174+
await __topLevelAwait__();
175+
`,
169176
jsxFactory: 'h',
170177
jsxFragment: 'Fragment',
171178
define,

src/lib/static/基础包.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
/**
22
* 作者: 小明
33
* 版本: 1.0.0
4-
* 更新时间:2020-12-11
5-
* github: https://github.com/2214962083/scriptable.git
4+
* 更新时间:2020-12-14
5+
* github: https://github.com/2214962083/ios-scriptable-tsx
66
*/
77

8-
// @编译时间 1607657751861
8+
// @编译时间 1607924203272
99
const MODULE = module
10+
let __topLevelAwait__ = () => Promise.resolve()
11+
function EndAwait(promiseFunc) {
12+
__topLevelAwait__ = promiseFunc
13+
}
1014

1115
// src/lib/constants.ts
1216
var URLSchemeFrom
@@ -187,7 +191,7 @@ async function showNotification(args2) {
187191
notification.subtitle = subtitle
188192
notification.body = body
189193
openURL && (notification.openURL = openURL)
190-
sound && notification.sound
194+
sound && (notification.sound = sound)
191195
notification = Object.assign(notification, others)
192196
return await notification.schedule()
193197
}
@@ -516,7 +520,7 @@ function runOnClick(instance, onClick) {
516520
}
517521
}
518522

519-
// src/lib/baisc.ts
523+
// src/lib/basic.ts
520524
var {setStorage: setStorage2, getStorage: getStorage2} = useStorage('basic-storage')
521525
var runScriptDate = Date.now()
522526
setStorage2('runScriptDate', runScriptDate)
@@ -678,7 +682,9 @@ ${scriptText}`
678682
}
679683
async runCode(syncScriptName, scriptText) {
680684
try {
681-
const runRemoteCode = new Function(`${scriptText}`)
685+
const runRemoteCode = new Function(`(async () => {
686+
${scriptText}
687+
})()`)
682688
runRemoteCode()
683689
} catch (err) {
684690
console.log('同步的代码执行失败')
@@ -736,3 +742,5 @@ console.__rewrite__ = true;
736742
}
737743
}
738744
new Basic().init()
745+
746+
await __topLevelAwait__()
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,4 @@ class BiliFans {
159159
}
160160
}
161161

162-
new BiliFans().init()
162+
EndAwait(() => new BiliFans().init())

src/scripts/funds.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,4 +228,4 @@ class Funds {
228228
}
229229
}
230230

231-
new Funds().init()
231+
EndAwait(() => new Funds().init())

src/scripts/music163.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,4 +314,4 @@ class Music163 {
314314
}
315315
}
316316

317-
new Music163().init()
317+
EndAwait(() => new Music163().init())
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,4 @@ class YiyanWidget {
8080
}
8181
}
8282

83-
new YiyanWidget().init()
83+
EndAwait(() => new YiyanWidget().init())

src/types/index.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,9 @@ declare const MODULE: Module
1515
declare namespace Scriptable {
1616
class Widget {}
1717
}
18+
19+
/**
20+
* 结尾生成顶部等待(top-level-await)渲染
21+
* @param promiseFunc 渲染函数,如: () => render()
22+
*/
23+
declare const EndAwait: <T>(promiseFunc: () => Promise<T>) => Promise<T>

打包好的成品/一言.js

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
// @编译时间 1607657381586
1+
// @编译时间 1607924347523
22
const MODULE = module
3+
let __topLevelAwait__ = () => Promise.resolve()
4+
function EndAwait(promiseFunc) {
5+
__topLevelAwait__ = promiseFunc
6+
}
37

48
// src/lib/constants.ts
59
var URLSchemeFrom
@@ -174,26 +178,32 @@ function hash(string) {
174178
function isLaunchInsideApp() {
175179
return !config.runsInWidget && args.queryParameters.from !== URLSchemeFrom.WIDGET
176180
}
177-
async function showPreviewOptions(widget) {
181+
async function showPreviewOptions(render) {
178182
const selectIndex = await showActionSheet({
179183
title: '预览组件',
180184
desc: '测试桌面组件在各种尺寸下的显示效果',
181185
itemList: ['小尺寸', '中尺寸', '大尺寸', '全部尺寸'],
182186
})
183187
switch (selectIndex) {
184188
case 0:
185-
await widget.presentSmall()
189+
config.widgetFamily = 'small'
190+
await (await render()).presentSmall()
186191
break
187192
case 1:
188-
await widget.presentMedium()
193+
config.widgetFamily = 'medium'
194+
await (await render()).presentMedium()
189195
break
190196
case 2:
191-
await widget.presentLarge()
197+
config.widgetFamily = 'large'
198+
await (await render()).presentLarge()
192199
break
193200
case 3:
194-
await widget.presentSmall()
195-
await widget.presentMedium()
196-
await widget.presentLarge()
201+
config.widgetFamily = 'small'
202+
await (await render()).presentSmall()
203+
config.widgetFamily = 'medium'
204+
await (await render()).presentMedium()
205+
config.widgetFamily = 'large'
206+
await (await render()).presentLarge()
197207
break
198208
}
199209
return selectIndex
@@ -419,7 +429,7 @@ var listWidget = new ListWidget()
419429
GenrateView.setListWidget(listWidget)
420430
function h(type, props, ...children) {
421431
props = props || {}
422-
const _children = [].concat(...children)
432+
const _children = flatteningArr(children)
423433
switch (type) {
424434
case 'wbox':
425435
return GenrateView.wbox(props, ..._children)
@@ -444,6 +454,13 @@ function h(type, props, ...children) {
444454
break
445455
}
446456
}
457+
function flatteningArr(arr) {
458+
return [].concat(
459+
...arr.map(item => {
460+
return Array.isArray(item) ? flatteningArr(item) : item
461+
}),
462+
)
463+
}
447464
function getColor(color) {
448465
return typeof color === 'string' ? new Color(color, 1) : color
449466
}
@@ -495,10 +512,10 @@ function runOnClick(instance, onClick) {
495512
// src/scripts/tsx-yiyan.tsx
496513
var YiyanWidget = class {
497514
async init() {
498-
this.widget = await this.render()
499515
if (isLaunchInsideApp()) {
500-
return await showPreviewOptions(this.widget)
516+
return await showPreviewOptions(this.render.bind(this))
501517
}
518+
this.widget = await this.render()
502519
Script.setWidget(this.widget)
503520
Script.complete()
504521
}
@@ -560,7 +577,6 @@ var YiyanWidget = class {
560577
})
561578
}
562579
async menu() {
563-
const optionFunc = [this.selectPreviewSize]
564580
const selectIndex = await showActionSheet({
565581
title: '菜单',
566582
itemList: [
@@ -569,34 +585,13 @@ var YiyanWidget = class {
569585
},
570586
],
571587
})
572-
optionFunc[selectIndex].apply(this)
573-
}
574-
async selectPreviewSize() {
575-
const selectIndex = await showActionSheet({
576-
title: '选择预览尺寸',
577-
itemList: [
578-
{
579-
text: '小组件',
580-
},
581-
{
582-
text: '中组件',
583-
},
584-
{
585-
text: '大组件',
586-
},
587-
],
588-
})
589588
switch (selectIndex) {
590589
case 0:
591-
await this.widget.presentSmall()
592-
break
593-
case 1:
594-
await this.widget.presentMedium()
595-
break
596-
case 2:
597-
await this.widget.presentLarge()
590+
await showPreviewOptions(this.render.bind(this))
598591
break
599592
}
600593
}
601594
}
602-
new YiyanWidget().init()
595+
EndAwait(() => new YiyanWidget().init())
596+
597+
await __topLevelAwait__()

0 commit comments

Comments
 (0)