Skip to content

Commit 1366da8

Browse files
committed
更新基础包,加入内置 EndAwait 函数以修复小部件显示出错的 bug
1 parent d89148f commit 1366da8

File tree

14 files changed

+640
-117
lines changed

14 files changed

+640
-117
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/>

scriptable.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const outputDir = path.resolve(rootPath, './dist')
1818
const minify = process.env.NODE_ENV === 'production'
1919

2020
/**是否加密代码*/
21-
const encrypt = process.env.NODE_ENV === 'production'
21+
const encrypt = false //process.env.NODE_ENV === 'production'
2222

2323
/**往编译后的代码头部插入的代码*/
2424
const header = `

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>

0 commit comments

Comments
 (0)