@@ -4142,6 +4142,49 @@ type TPlatform = 'ios' | 'android' | 'windows' | 'mac'
4142
4142
} )
4143
4143
}
4144
4144
4145
+ // Test case from `wx.createVKSession`
4146
+ {
4147
+ const gl : WechatMiniprogram . WebGLRenderingContext = { }
4148
+ const canvasWidth = 300
4149
+ const canvasHeight = 300
4150
+ // 以下 demo 以 v2 为例
4151
+ // 创建 session 对象
4152
+ const session = wx . createVKSession ( {
4153
+ track : {
4154
+ plane : { mode : 3 } ,
4155
+ } ,
4156
+ version : 'v2' ,
4157
+ gl,
4158
+ } )
4159
+ // 逐帧分析
4160
+ const onFrame = ( timestamp : number ) => {
4161
+ // 开发者可以自己控制帧率
4162
+ const frame = session . getVKFrame ( canvasWidth , canvasHeight )
4163
+ if ( frame ) {
4164
+ // 分析完毕,可以拿到帧对象
4165
+ doRender ( frame )
4166
+ }
4167
+
4168
+ expectType < number > ( timestamp )
4169
+
4170
+ session . requestAnimationFrame ( onFrame )
4171
+ }
4172
+ session . start ( err => {
4173
+ if ( ! err ) session . requestAnimationFrame ( onFrame )
4174
+ } )
4175
+
4176
+ // 渲染函数
4177
+ const doRender = ( frame : WechatMiniprogram . VKFrame ) => {
4178
+ expectType < WechatMiniprogram . VKFrame > ( frame )
4179
+ }
4180
+ }
4181
+
4182
+ // Test case from `wx.isVKSupport`
4183
+ {
4184
+ const isSupportV2 = wx . isVKSupport ( 'v2' )
4185
+ expectType < boolean > ( isSupportV2 )
4186
+ }
4187
+
4145
4188
// Test case from `FileSystemManager.readCompressedFile`
4146
4189
{
4147
4190
const fs = wx . getFileSystemManager ( )
@@ -4503,3 +4546,139 @@ type TPlatform = 'ios' | 'android' | 'windows' | 'mac'
4503
4546
disableNormalization : true ,
4504
4547
} )
4505
4548
}
4549
+
4550
+ // Test case from `wx.preloadAssets`
4551
+ {
4552
+ let imgUrl = ''
4553
+ wx . preloadAssets ( {
4554
+ data : [
4555
+ {
4556
+ type : 'image' ,
4557
+ src : imgUrl ,
4558
+ } ,
4559
+ ] ,
4560
+ success ( resp ) {
4561
+ console . log ( 'preloadAssets success' , resp )
4562
+ } ,
4563
+ fail ( err ) {
4564
+ console . log ( 'preloadAssets fail' , err )
4565
+ } ,
4566
+ } )
4567
+ }
4568
+
4569
+ // Test case from `wx.editImage`
4570
+ {
4571
+ wx . editImage ( {
4572
+ src : '' , // 图片路径
4573
+ } )
4574
+ }
4575
+
4576
+ // Test case from `wx.createCacheManager`
4577
+ {
4578
+ const cacheManager = wx . createCacheManager ( { } )
4579
+ cacheManager . addRule ( / h t t p s : \/ \/ (?: .* ) / ig) // 表示所有 https 请求都匹配
4580
+
4581
+ cacheManager . on ( 'request' , evt => {
4582
+ // 在弱网时接收到 wx.request 请求
4583
+ return new Promise ( ( resolve , reject ) => {
4584
+ const matchRes = cacheManager . match ( evt )
4585
+ if ( matchRes && matchRes . data ) {
4586
+ // 有缓存,返回
4587
+ resolve ( matchRes . data )
4588
+ } else {
4589
+ // 没缓存,抛错
4590
+ reject ( { errMsg : 'no cache' } )
4591
+ }
4592
+ } )
4593
+ } )
4594
+ }
4595
+
4596
+ // Test case from `CacheManager.addRule`
4597
+ {
4598
+ const cacheManager = wx . createCacheManager ( { } )
4599
+ const ruleId = cacheManager . addRule ( {
4600
+ id : 'haha-rule' ,
4601
+ method : 'GET' ,
4602
+ url : '/haha' ,
4603
+ maxAge : 123455 ,
4604
+ dataSchema : [
4605
+ // data 字段的匹配,默认为空,表示不匹配
4606
+ // 类型可以是:string、number、boolean、null、object、any(表示任意类型均可),以及这些类型的数组表示方式
4607
+ { name : 'aaa' , schema : { type : 'string' } } , // 类型为 string
4608
+ { name : 'bbb' , schema : [ { type : 'number' } , { type : 'string' } ] } , // 类型为 number, string
4609
+ { name : 'ccc' , schema : { type : 'string' , value : 'abc' } } , // 值为 abc
4610
+ { name : 'ddd' , schema : { type : 'string' , value : / ( a b c | c b a ) / ig} } , // 值符合该正则匹配,如果该值不是字符串类型,则会被尝试转成字符串后再进行比较
4611
+ { name : 'ddd' , schema : { type : 'string' , value : ( val : string ) => val === '123' } } , // 传入函数来校验值
4612
+ { name : 'eee' , schema : { type : 'object' , value : [ { // 类型为对象,则通过嵌套的方式来逐层校验
4613
+ name : 'aaa' , schema : { type : 'string' } ,
4614
+ // ...
4615
+ // 嵌套 dataSchema,同上面的方式一样来匹配嵌套的对象
4616
+ } ] } } ,
4617
+ { name : 'fff' , schema : { type : 'string[]' } } , // 类型为 string 数组
4618
+ { name : 'ggg' , schema : { type : 'any' } } , // 类型为任意类型
4619
+ { name : 'hhh' , schema : { type : 'any[]' } } , // 类型为任意类型的数组
4620
+ ] ,
4621
+ } )
4622
+ expectType < string > ( ruleId )
4623
+ }
4624
+
4625
+ // Test case from `CacheManager.on`
4626
+ {
4627
+ const cacheManager = wx . createCacheManager ( { } )
4628
+ cacheManager . on ( 'request' , async function ( ) {
4629
+ // evt.url - 请求 url
4630
+ // evt.data - 请求参数
4631
+ // evt.method - 请求方法
4632
+ // evt.request - 原始 request 方法,返回一个 promise
4633
+
4634
+ // if (evt.url === '/xxx') {
4635
+ // // 如果有些请求仍然希望走到网络,则可以如下处理
4636
+ // const res = await evt.request()
4637
+ // // res 即为网络请求返回
4638
+ // }
4639
+
4640
+ return new Promise ( ( resolve , reject ) => {
4641
+ // do sth
4642
+ let data = { }
4643
+ if ( data ) {
4644
+ // 这里 resolve 的 data 就会作为 wx.request 的 success 回调结果返回
4645
+ resolve ( data )
4646
+ } else {
4647
+ // 这里 reject 的错误信息就会作为 wx.request 的 fail 回调结果返回
4648
+ reject ( 'no data' )
4649
+ }
4650
+ } )
4651
+ } )
4652
+ }
4653
+
4654
+ // Test case from `CacheManager.match`
4655
+ {
4656
+ const cacheManager = wx . createCacheManager ( { } )
4657
+ cacheManager . on ( 'request' , ( evt ) => {
4658
+ const cache = cacheManager . match ( evt )
4659
+ // 若有重复监听,则取第一个 handler 返回的 promise
4660
+ return new Promise ( ( resolve , reject ) => {
4661
+ if ( cache . data ) {
4662
+ resolve ( cache . data )
4663
+ } else {
4664
+ reject ( 'no cache' )
4665
+ }
4666
+ } )
4667
+ } )
4668
+ }
4669
+
4670
+ // Test case from `wx.requestPluginPayment`
4671
+ {
4672
+ wx . requestPluginPayment ( {
4673
+ version : 'release' ,
4674
+ fee : 1 ,
4675
+ paymentArgs : { } ,
4676
+ currencyType : 'CNY' ,
4677
+ success ( res ) {
4678
+ expectType < string > ( res . errMsg )
4679
+ } ,
4680
+ fail ( res ) {
4681
+ expectType < string > ( res . errMsg )
4682
+ }
4683
+ } )
4684
+ }
0 commit comments