@whitesev/utils 2.5.6 → 2.5.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/Utils.ts CHANGED
@@ -29,7 +29,7 @@ class Utils {
29
29
  this.windowApi = new WindowApi(option);
30
30
  }
31
31
  /** 版本号 */
32
- version = "2024.12.25";
32
+ version = "2025.1.1";
33
33
 
34
34
  /**
35
35
  * 在页面中增加style元素,如果html节点存在子节点,添加子节点第一个,反之,添加到html节点的子节点最后一个
@@ -4095,6 +4095,113 @@ class Utils {
4095
4095
  })
4096
4096
  );
4097
4097
  }
4098
+ /**
4099
+ * 等待任意事件成立
4100
+ *
4101
+ * 运行方式为根据页面元素的改变而触发回调
4102
+ * @param checkFn 检测的函数
4103
+ * @param timeout 超时时间,默认0
4104
+ * @param parent (可选)父元素,默认document
4105
+ * @example
4106
+ * Utils.wait(()=> {
4107
+ * let $test = document.querySelector("#test");
4108
+ * return {
4109
+ * success: $test !== null,
4110
+ * data: $test
4111
+ * }
4112
+ * })
4113
+ */
4114
+ wait<T extends any>(
4115
+ checkFn: (...args: any[]) => {
4116
+ /**
4117
+ * 是否检测成功
4118
+ */
4119
+ success: boolean;
4120
+ /**
4121
+ * 返回的值
4122
+ */
4123
+ data: T;
4124
+ },
4125
+ timeout?: null | undefined,
4126
+ parent?: Node | Element | Document | HTMLElement
4127
+ ): Promise<T>;
4128
+ wait<T extends any>(
4129
+ checkFn: (...args: any[]) => {
4130
+ /**
4131
+ * 是否检测成功
4132
+ */
4133
+ success: boolean;
4134
+ /**
4135
+ * 返回的值
4136
+ */
4137
+ data: T;
4138
+ },
4139
+ timeout?: number,
4140
+ parent?: Node | Element | Document | HTMLElement
4141
+ ): Promise<T | null>;
4142
+ wait<T extends any>(
4143
+ checkFn: (...args: any[]) => {
4144
+ /**
4145
+ * 是否检测成功
4146
+ */
4147
+ success: boolean;
4148
+ /**
4149
+ * 返回的值
4150
+ */
4151
+ data: T;
4152
+ },
4153
+ timeout?: number | null | undefined,
4154
+ parent?: Node | Element | Document | HTMLElement
4155
+ ): Promise<T | null> {
4156
+ const UtilsContext = this;
4157
+ let __timeout__ = typeof timeout === "number" ? timeout : 0;
4158
+ return new Promise((resolve) => {
4159
+ let observer = UtilsContext.mutationObserver(
4160
+ parent || UtilsContext.windowApi.document,
4161
+ {
4162
+ config: {
4163
+ subtree: true,
4164
+ childList: true,
4165
+ attributes: true,
4166
+ },
4167
+ immediate: true,
4168
+ callback(mutations, __observer__) {
4169
+ let result = checkFn();
4170
+ if (result.success) {
4171
+ // 取消观察器
4172
+ if (typeof __observer__?.disconnect === "function") {
4173
+ __observer__.disconnect();
4174
+ }
4175
+ resolve(result.data);
4176
+ }
4177
+ },
4178
+ }
4179
+ );
4180
+ if (__timeout__ > 0) {
4181
+ setTimeout(() => {
4182
+ // 取消观察器
4183
+ if (typeof observer?.disconnect === "function") {
4184
+ observer.disconnect();
4185
+ }
4186
+ resolve(null as T);
4187
+ }, __timeout__);
4188
+ }
4189
+ });
4190
+ }
4191
+ /**
4192
+ * 等待元素出现
4193
+ * @param selectorFn 获取元素的函数
4194
+ * @param timeout 超时时间,默认0
4195
+ * @example
4196
+ * Utils.waitNode(()=>document.querySelector("div"), 1000).then( $div =>{
4197
+ * console.log($div); // $div => HTMLDivELement | null
4198
+ * })
4199
+ */
4200
+ waitNode<K extends any>(selectorFn: () => K | null | undefined): Promise<K>;
4201
+ waitNode<K extends any>(
4202
+ selectorFn: () => K | null | undefined,
4203
+ timeout: number
4204
+ ): Promise<K | null | undefined>;
4098
4205
  /**
4099
4206
  * 等待元素出现
4100
4207
  * @param selector CSS选择器
@@ -4103,7 +4210,7 @@ class Utils {
4103
4210
  * Utils.waitNode("div").then( $div =>{
4104
4211
  * console.log($div); // div => HTMLDivELement
4105
4212
  * })
4106
- * Utils.waitNode("div",document).then( $div =>{
4213
+ * Utils.waitNode("div", document).then( $div =>{
4107
4214
  * console.log($div); // div => HTMLDivELement
4108
4215
  * })
4109
4216
  */
@@ -4123,7 +4230,7 @@ class Utils {
4123
4230
  * Utils.waitNode(["div"]).then( ([$div]) =>{
4124
4231
  * console.log($div); // div => HTMLDivELement[]
4125
4232
  * })
4126
- * Utils.waitNode(["div"],document).then( ([$div]) =>{
4233
+ * Utils.waitNode(["div"], document).then( ([$div]) =>{
4127
4234
  * console.log($div); // div => HTMLDivELement[]
4128
4235
  * })
4129
4236
  */
@@ -4141,7 +4248,7 @@ class Utils {
4141
4248
  * @param parent 父元素,默认document
4142
4249
  * @param timeout 超时时间,默认0
4143
4250
  * @example
4144
- * Utils.waitNode("div",document,1000).then( $div =>{
4251
+ * Utils.waitNode("div", document, 1000).then( $div =>{
4145
4252
  * console.log($div); // $div => HTMLDivELement | null
4146
4253
  * })
4147
4254
  */
@@ -4161,7 +4268,7 @@ class Utils {
4161
4268
  * @param parent 父元素,默认document
4162
4269
  * @param timeout 超时时间,默认0
4163
4270
  * @example
4164
- * Utils.waitNode(["div"],document,1000).then( ([$div]) =>{
4271
+ * Utils.waitNode(["div"], document, 1000).then( ([$div]) =>{
4165
4272
  * console.log($div); // $div => HTMLDivELement[] | null
4166
4273
  * })
4167
4274
  */
@@ -4180,7 +4287,7 @@ class Utils {
4180
4287
  * @param selector CSS选择器
4181
4288
  * @param timeout 超时时间,默认0
4182
4289
  * @example
4183
- * Utils.waitNode("div",1000).then( $div =>{
4290
+ * Utils.waitNode("div", 1000).then( $div =>{
4184
4291
  * console.log($div); // $div => HTMLDivELement | null
4185
4292
  * })
4186
4293
  */
@@ -4197,7 +4304,7 @@ class Utils {
4197
4304
  * @param selectorList CSS选择器数组
4198
4305
  * @param timeout 超时时间,默认0
4199
4306
  * @example
4200
- * Utils.waitNode(["div"],1000).then( [$div] =>{
4307
+ * Utils.waitNode(["div"], 1000).then( [$div] =>{
4201
4308
  * console.log($div); // $div => HTMLDivELement[] | null
4202
4309
  * })
4203
4310
  */
@@ -4214,13 +4321,19 @@ class Utils {
4214
4321
  args = args.filter((arg) => arg !== void 0);
4215
4322
  let UtilsContext = this;
4216
4323
  // 选择器
4217
- let selector = args[0] as unknown as string | string[];
4324
+ let selector = args[0] as unknown as string | string[] | Function;
4218
4325
  // 父元素(监听的元素)
4219
4326
  let parent: Element = UtilsContext.windowApi.document as any as Element;
4220
4327
  // 超时时间
4221
4328
  let timeout = 0;
4222
- if (typeof args[0] !== "string" && !Array.isArray(args[0])) {
4223
- throw new TypeError("Utils.waitNode 第一个参数必须是string|string[]");
4329
+ if (
4330
+ typeof args[0] !== "string" &&
4331
+ !Array.isArray(args[0]) &&
4332
+ typeof args[0] !== "function"
4333
+ ) {
4334
+ throw new TypeError(
4335
+ "Utils.waitNode 第一个参数必须是string|string[]|Function"
4336
+ );
4224
4337
  }
4225
4338
  if (args.length === 1) {
4226
4339
  // 上面已做处理
@@ -4257,52 +4370,42 @@ class Utils {
4257
4370
  } else {
4258
4371
  throw new TypeError("Utils.waitNode 参数个数错误");
4259
4372
  }
4260
- return new Promise((resolve) => {
4261
- function getNode() {
4262
- if (Array.isArray(selector)) {
4263
- let result: T[] = [];
4264
- for (let index = 0; index < selector.length; index++) {
4265
- let node = parent.querySelector(selector[index]);
4266
- if (node) {
4267
- result.push(node as any);
4268
- }
4269
- }
4270
- if (result.length === selector.length) {
4271
- return result;
4272
- }
4273
- } else {
4274
- return parent.querySelector(selector);
4275
- }
4276
- }
4277
- var observer = UtilsContext.mutationObserver(parent, {
4278
- config: {
4279
- subtree: true,
4280
- childList: true,
4281
- attributes: true,
4282
- },
4283
- callback() {
4284
- let node = getNode();
4373
+ function getNode() {
4374
+ if (Array.isArray(selector)) {
4375
+ let result: T[] = [];
4376
+ for (let index = 0; index < selector.length; index++) {
4377
+ let node = parent.querySelector(selector[index]);
4285
4378
  if (node) {
4286
- // 取消观察器
4287
- if (typeof observer?.disconnect === "function") {
4288
- observer.disconnect();
4289
- }
4290
- resolve(node as any as T);
4291
- return;
4292
- }
4293
- },
4294
- immediate: true,
4295
- });
4296
- if (timeout > 0) {
4297
- setTimeout(() => {
4298
- // 取消观察器
4299
- if (typeof observer?.disconnect === "function") {
4300
- observer.disconnect();
4379
+ result.push(node as any);
4301
4380
  }
4302
- resolve(null);
4303
- }, timeout);
4381
+ }
4382
+ if (result.length === selector.length) {
4383
+ return result;
4384
+ }
4385
+ } else if (typeof selector === "function") {
4386
+ return selector();
4387
+ } else {
4388
+ return parent.querySelector(selector);
4304
4389
  }
4305
- });
4390
+ }
4391
+ return UtilsContext.wait(
4392
+ () => {
4393
+ let node = getNode();
4394
+ if (node) {
4395
+ return {
4396
+ success: true,
4397
+ data: node,
4398
+ };
4399
+ } else {
4400
+ return {
4401
+ success: false,
4402
+ data: node,
4403
+ };
4404
+ }
4405
+ },
4406
+ timeout,
4407
+ parent
4408
+ );
4306
4409
  }
4307
4410
  /**
4308
4411
  * 等待任意元素出现
@@ -4312,7 +4415,7 @@ class Utils {
4312
4415
  * Utils.waitAnyNode(["div","div"]).then( $div =>{
4313
4416
  * console.log($div); // $div => HTMLDivELement 这里是第一个
4314
4417
  * })
4315
- * Utils.waitAnyNode(["a","div"],document).then( $a =>{
4418
+ * Utils.waitAnyNode(["a","div"], document).then( $a =>{
4316
4419
  * console.log($a); // $a => HTMLAnchorElement 这里是第一个
4317
4420
  * })
4318
4421
  */
@@ -4330,7 +4433,7 @@ class Utils {
4330
4433
  * @param parent 父元素,默认document
4331
4434
  * @param timeout 超时时间,默认0
4332
4435
  * @example
4333
- * Utils.waitAnyNode(["div","div"],document,10000).then( $div =>{
4436
+ * Utils.waitAnyNode(["div","div"], document, 10000).then( $div =>{
4334
4437
  * console.log($div); // $div => HTMLDivELement | null
4335
4438
  * })
4336
4439
  */
@@ -4349,7 +4452,7 @@ class Utils {
4349
4452
  * @param selectorList CSS选择器数组
4350
4453
  * @param timeout 超时时间,默认0
4351
4454
  * @example
4352
- * Utils.waitAnyNode(["div","div"],10000).then( $div =>{
4455
+ * Utils.waitAnyNode(["div","div"], 10000).then( $div =>{
4353
4456
  * console.log($div); // $div => HTMLDivELement | null
4354
4457
  * })
4355
4458
  */
@@ -4414,7 +4517,6 @@ class Utils {
4414
4517
  });
4415
4518
  return Promise.any(promiseList);
4416
4519
  }
4417
-
4418
4520
  /**
4419
4521
  * 等待元素数组出现
4420
4522
  * @param selector CSS选择器
@@ -4423,7 +4525,7 @@ class Utils {
4423
4525
  * Utils.waitNodeList("div").then( $result =>{
4424
4526
  * console.log($result); // $result => NodeListOf<HTMLDivElement>
4425
4527
  * })
4426
- * Utils.waitNodeList("div",document).then( $result =>{
4528
+ * Utils.waitNodeList("div", document).then( $result =>{
4427
4529
  * console.log($result); // $result => NodeListOf<HTMLDivElement>
4428
4530
  * })
4429
4531
  */
@@ -4443,7 +4545,7 @@ class Utils {
4443
4545
  * Utils.waitNodeList(["div"]).then( $result =>{
4444
4546
  * console.log($result); // $result => NodeListOf<HTMLDivElement>[]
4445
4547
  * })
4446
- * Utils.waitNodeList(["div"],document).then( $result =>{
4548
+ * Utils.waitNodeList(["div"], document).then( $result =>{
4447
4549
  * console.log($result); // $result => NodeListOf<HTMLDivElement>[]
4448
4550
  * })
4449
4551
  */
@@ -4461,7 +4563,7 @@ class Utils {
4461
4563
  * @param parent 监听的父元素
4462
4564
  * @param timeout 超时时间,默认0
4463
4565
  * @example
4464
- * Utils.waitNodeList("div",document,10000).then( $result =>{
4566
+ * Utils.waitNodeList("div", document, 10000).then( $result =>{
4465
4567
  * console.log($result); // $result => NodeListOf<HTMLDivElement> | null
4466
4568
  * })
4467
4569
  */
@@ -4481,7 +4583,7 @@ class Utils {
4481
4583
  * @param parent 监听的父元素
4482
4584
  * @param timeout 超时时间,默认0
4483
4585
  * @example
4484
- * Utils.waitNodeList(["div"],document,10000).then( $result =>{
4586
+ * Utils.waitNodeList(["div"], document, 10000).then( $result =>{
4485
4587
  * console.log($result); // $result => NodeListOf<HTMLDivElement>[] | null
4486
4588
  * })
4487
4589
  */
@@ -4500,7 +4602,7 @@ class Utils {
4500
4602
  * @param selector CSS选择器数组
4501
4603
  * @param timeout 超时时间,默认0
4502
4604
  * @example
4503
- * Utils.waitNodeList("div",10000).then( $result =>{
4605
+ * Utils.waitNodeList("div", 10000).then( $result =>{
4504
4606
  * console.log($result); // $result => NodeListOf<HTMLDivElement> | null
4505
4607
  * })
4506
4608
  */
@@ -4517,7 +4619,7 @@ class Utils {
4517
4619
  * @param selectorList CSS选择器数组
4518
4620
  * @param timeout 超时时间,默认0
4519
4621
  * @example
4520
- * Utils.waitNodeList(["div"],10000).then( $result =>{
4622
+ * Utils.waitNodeList(["div"], 10000).then( $result =>{
4521
4623
  * console.log($result); // $result => NodeListOf<HTMLDivElement>[] | null
4522
4624
  * })
4523
4625
  */
@@ -4579,57 +4681,45 @@ class Utils {
4579
4681
  } else {
4580
4682
  throw new TypeError("Utils.waitNodeList 参数个数错误");
4581
4683
  }
4582
- return new Promise((resolve) => {
4583
- function getNodeList() {
4584
- if (Array.isArray(selector)) {
4585
- let result: T[] = [];
4586
- for (let index = 0; index < selector.length; index++) {
4587
- let nodeList = (parent as Element).querySelectorAll(
4588
- selector[index]
4589
- ) as T;
4590
- if (nodeList.length) {
4591
- result.push(nodeList);
4592
- }
4593
- }
4594
- if (result.length === selector.length) {
4595
- return result;
4596
- }
4597
- } else {
4598
- let nodeList = (parent as Element).querySelectorAll(selector) as T;
4684
+ function getNodeList() {
4685
+ if (Array.isArray(selector)) {
4686
+ let result: T[] = [];
4687
+ for (let index = 0; index < selector.length; index++) {
4688
+ let nodeList = (parent as Element).querySelectorAll(
4689
+ selector[index]
4690
+ ) as T;
4599
4691
  if (nodeList.length) {
4600
- return nodeList;
4692
+ result.push(nodeList);
4601
4693
  }
4602
4694
  }
4695
+ if (result.length === selector.length) {
4696
+ return result;
4697
+ }
4698
+ } else {
4699
+ let nodeList = (parent as Element).querySelectorAll(selector) as T;
4700
+ if (nodeList.length) {
4701
+ return nodeList;
4702
+ }
4603
4703
  }
4604
- var observer = UtilsContext.mutationObserver(parent, {
4605
- config: {
4606
- subtree: true,
4607
- childList: true,
4608
- attributes: true,
4609
- },
4610
- callback() {
4611
- let node = getNodeList();
4612
- if (node) {
4613
- // 取消观察器
4614
- try {
4615
- observer.disconnect();
4616
- } catch (error) {}
4617
- resolve(node as T);
4618
- return;
4619
- }
4620
- },
4621
- immediate: true,
4622
- });
4623
- if (timeout > 0) {
4624
- setTimeout(() => {
4625
- // 取消观察器
4626
- if (typeof observer?.disconnect === "function") {
4627
- observer.disconnect();
4628
- }
4629
- resolve(null);
4630
- }, timeout);
4631
- }
4632
- });
4704
+ }
4705
+ return UtilsContext.wait<any>(
4706
+ () => {
4707
+ let node = getNodeList();
4708
+ if (node) {
4709
+ return {
4710
+ success: true,
4711
+ data: node,
4712
+ };
4713
+ } else {
4714
+ return {
4715
+ success: false,
4716
+ data: node,
4717
+ };
4718
+ }
4719
+ },
4720
+ timeout,
4721
+ parent
4722
+ );
4633
4723
  }
4634
4724
  /**
4635
4725
  * 等待任意元素数组出现
@@ -4639,7 +4729,7 @@ class Utils {
4639
4729
  * Utils.waitAnyNodeList(["div","a"]).then( $result =>{
4640
4730
  * console.log($result); // $result => NodeListOf<HTMLDivElement>
4641
4731
  * })
4642
- * Utils.waitAnyNodeList(["div","a"],document).then( $result =>{
4732
+ * Utils.waitAnyNodeList(["div","a"], document).then( $result =>{
4643
4733
  * console.log($result); // $result => NodeListOf<HTMLDivElement>
4644
4734
  * })
4645
4735
  */
@@ -4657,7 +4747,7 @@ class Utils {
4657
4747
  * @param parent 父元素,默认document
4658
4748
  * @param timeout 超时时间,默认0
4659
4749
  * @example
4660
- * Utils.waitAnyNodeList(["div","a"],document,10000).then( $result =>{
4750
+ * Utils.waitAnyNodeList(["div","a"], document, 10000).then( $result =>{
4661
4751
  * console.log($result); // $result => NodeListOf<HTMLDivElement> | null
4662
4752
  * })
4663
4753
  */
@@ -4676,7 +4766,7 @@ class Utils {
4676
4766
  * @param selectorList CSS选择器数组
4677
4767
  * @param timeout 超时时间,默认0
4678
4768
  * @example
4679
- * Utils.waitAnyNodeList(["div","div"],10000).then( $result =>{
4769
+ * Utils.waitAnyNodeList(["div","div"], 10000).then( $result =>{
4680
4770
  * console.log($result); // $result => NodeListOf<HTMLDivElement> | null
4681
4771
  * })
4682
4772
  */
@@ -25,3 +25,4 @@ export declare interface AnyObject {
25
25
  export type PartialKeys<T, K extends keyof T> = {
26
26
  [P in K]?: T[P];
27
27
  };
28
+ export type Values<T> = T[keyof T];