@vope/utils 1.4.11 → 1.4.13

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/cjs/index.js CHANGED
@@ -527,6 +527,7 @@ var css = (...args) => {
527
527
  // src/subscriber.ts
528
528
  var Subscriber = class {
529
529
  #listeners = {};
530
+ #once = {};
530
531
  $on(name, listener) {
531
532
  const listeners = this.#listeners;
532
533
  if (name in listeners) {
@@ -536,23 +537,52 @@ var Subscriber = class {
536
537
  }
537
538
  return this;
538
539
  }
540
+ $once(name, listener) {
541
+ this.$on(name, listener);
542
+ const index = this.#listeners[name].length - 1;
543
+ const once2 = this.#once;
544
+ if (name in once2) {
545
+ once2[name].push(index);
546
+ } else {
547
+ once2[name] = [index];
548
+ }
549
+ return this;
550
+ }
539
551
  $emit(name, ...args) {
540
- this.#listeners[name]?.forEach((listener) => listener(...args));
552
+ const listeners = this.#listeners[name];
553
+ if (!listeners)
554
+ return this;
555
+ const once2 = hash8strings(this.#once[name] || []);
556
+ this.#listeners[name] = listeners.reduce((list, listener, index) => {
557
+ listener(...args);
558
+ index in once2 || list.push(listener);
559
+ return list;
560
+ }, []);
561
+ delete this.#once[name];
541
562
  return this;
542
563
  }
543
564
  $off(name, listener) {
544
565
  const listeners = this.#listeners;
566
+ const once2 = this.#once;
545
567
  if (name in listeners === false)
546
568
  return this;
547
569
  if (listener) {
570
+ let count;
548
571
  listeners[name].some((value, index) => {
549
572
  if (value === listener) {
573
+ count = index;
574
+ listeners[name].splice(index, 1);
575
+ return true;
576
+ }
577
+ }) && once2[name].some((value, index) => {
578
+ if (value === count) {
550
579
  listeners[name].splice(index, 1);
551
580
  return true;
552
581
  }
553
582
  });
554
583
  } else {
555
584
  delete listeners[name];
585
+ delete this.#once[name];
556
586
  }
557
587
  return this;
558
588
  }
@@ -690,12 +720,12 @@ var Union = class {
690
720
  /**
691
721
  * 从已有的联合中拷贝数据
692
722
  */
693
- clone(union, key) {
694
- const { data, options } = union;
723
+ clone(target, key) {
724
+ const { data, options } = target;
695
725
  const keys = Array.isArray(key) ? key : [key];
696
726
  keys.forEach((key2) => {
697
727
  if (key2 in data) {
698
- this.#raws[key2] = union.getRaw(key2);
728
+ this.#raws[key2] = target.getRaw(key2);
699
729
  this.#data[key2] = data[key2];
700
730
  this.#options[key2] = options[key2];
701
731
  }
package/lib/subscriber.js CHANGED
@@ -1,5 +1,7 @@
1
+ import { hash8strings } from "./convert/strings";
1
2
  class Subscriber {
2
3
  #listeners = {};
4
+ #once = {};
3
5
  $on(name, listener) {
4
6
  const listeners = this.#listeners;
5
7
  if (name in listeners) {
@@ -9,23 +11,52 @@ class Subscriber {
9
11
  }
10
12
  return this;
11
13
  }
14
+ $once(name, listener) {
15
+ this.$on(name, listener);
16
+ const index = this.#listeners[name].length - 1;
17
+ const once = this.#once;
18
+ if (name in once) {
19
+ once[name].push(index);
20
+ } else {
21
+ once[name] = [index];
22
+ }
23
+ return this;
24
+ }
12
25
  $emit(name, ...args) {
13
- this.#listeners[name]?.forEach((listener) => listener(...args));
26
+ const listeners = this.#listeners[name];
27
+ if (!listeners)
28
+ return this;
29
+ const once = hash8strings(this.#once[name] || []);
30
+ this.#listeners[name] = listeners.reduce((list, listener, index) => {
31
+ listener(...args);
32
+ index in once || list.push(listener);
33
+ return list;
34
+ }, []);
35
+ delete this.#once[name];
14
36
  return this;
15
37
  }
16
38
  $off(name, listener) {
17
39
  const listeners = this.#listeners;
40
+ const once = this.#once;
18
41
  if (name in listeners === false)
19
42
  return this;
20
43
  if (listener) {
44
+ let count;
21
45
  listeners[name].some((value, index) => {
22
46
  if (value === listener) {
47
+ count = index;
48
+ listeners[name].splice(index, 1);
49
+ return true;
50
+ }
51
+ }) && once[name].some((value, index) => {
52
+ if (value === count) {
23
53
  listeners[name].splice(index, 1);
24
54
  return true;
25
55
  }
26
56
  });
27
57
  } else {
28
58
  delete listeners[name];
59
+ delete this.#once[name];
29
60
  }
30
61
  return this;
31
62
  }
package/lib/union.js CHANGED
@@ -29,12 +29,12 @@ class Union {
29
29
  /**
30
30
  * 从已有的联合中拷贝数据
31
31
  */
32
- clone(union, key) {
33
- const { data, options } = union;
32
+ clone(target, key) {
33
+ const { data, options } = target;
34
34
  const keys = Array.isArray(key) ? key : [key];
35
35
  keys.forEach((key2) => {
36
36
  if (key2 in data) {
37
- this.#raws[key2] = union.getRaw(key2);
37
+ this.#raws[key2] = target.getRaw(key2);
38
38
  this.#data[key2] = data[key2];
39
39
  this.#options[key2] = options[key2];
40
40
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vope/utils",
3
- "version": "1.4.11",
3
+ "version": "1.4.13",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "publishConfig": {
@@ -1,8 +1,8 @@
1
- import { _Point } from './rad';
1
+ import { Point2Tuple } from '@vope/types';
2
2
  /**
3
3
  * 返回点被旋转指定角度后新的位置
4
4
  * @param from 旋转点
5
5
  * @param angle 旋转角度
6
6
  * @param visit 旋转原点
7
7
  */
8
- export declare const rotateAngle: (from: _Point, angle: number, visit?: _Point) => _Point;
8
+ export declare const rotateAngle: (from: Point2Tuple, angle: number, visit?: Point2Tuple) => Point2Tuple;
@@ -1,8 +1,8 @@
1
- export type _Point = [number, number];
1
+ import { Point2Tuple } from '@vope/types';
2
2
  /**
3
3
  * 返回点被旋转指定弧度后新的位置
4
4
  * @param from 旋转点
5
5
  * @param rad 旋转弧度
6
6
  * @param visit 旋转原点
7
7
  */
8
- export declare const rotateRad: (from: _Point, rad: number, visit?: _Point) => _Point;
8
+ export declare const rotateRad: (from: Point2Tuple, rad: number, visit?: Point2Tuple) => Point2Tuple;
@@ -1,9 +1,10 @@
1
1
  /**
2
2
  * 订阅
3
3
  */
4
- export default class Subscriber<T extends Record<string, (...args: any[]) => void>> {
4
+ export default class Subscriber<T extends Record<keyof any, (...args: any[]) => void>> {
5
5
  #private;
6
6
  $on<K extends keyof T>(name: K, listener: T[K]): this;
7
+ $once<K extends keyof T>(name: K, listener: T[K]): this;
7
8
  $emit<K extends keyof T>(name: K, ...args: Parameters<T[K]>): this;
8
9
  $off<K extends keyof T>(name: K, listener?: T[K]): this;
9
10
  }
package/types/union.d.ts CHANGED
@@ -29,6 +29,6 @@ export declare class Union<Primary extends string, Member extends string, Origin
29
29
  /**
30
30
  * 从已有的联合中拷贝数据
31
31
  */
32
- clone<T extends Union<Primary, string, Origin, Data, Option>, P extends GetStatus<T> & Member>(union: T, key: P | P[]): Union<Primary, Member, Origin, Data, Option, Status | P> & Omit<this, keyof Union<Primary, Member, Origin, Data, Option, Status | P>>;
32
+ clone<T extends Union<Primary, string, Origin, Data, Option>, const P extends Exclude<GetStatus<T> & Member, Status>>(target: T, key: P | P[]): Union<Primary, Member, Origin, Data, Option, Status | P> & Omit<this, keyof Union<Primary, Member, Origin, Data, Option, Status | P>>;
33
33
  }
34
34
  export {};