@vope/utils 1.4.12 → 1.4.14

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
@@ -24,6 +24,7 @@ __export(src_exports, {
24
24
  MemoryBasic: () => MemoryBasic,
25
25
  MemoryList: () => MemoryList,
26
26
  MemoryPower: () => MemoryPower,
27
+ MemorySimple: () => MemorySimple,
27
28
  Subscriber: () => Subscriber,
28
29
  Union: () => Union,
29
30
  clamp: () => clamp,
@@ -331,11 +332,15 @@ var MemoryBasic = class {
331
332
  push(...list) {
332
333
  const { memorys } = this;
333
334
  list.forEach((data) => {
334
- const type = this.getType(data);
335
- const memory = memorys.get(type);
336
- const index = this.list.push(data) - 1;
337
- const result = this.#mount(index, memory);
338
- memory === result || memorys.set(type, result);
335
+ if (Array.isArray(data)) {
336
+ this.push(...data);
337
+ } else {
338
+ const type = this.getType(data);
339
+ const memory = memorys.get(type);
340
+ const index = this.list.push(data) - 1;
341
+ const result = this.#mount(index, memory);
342
+ memory === result || memorys.set(type, result);
343
+ }
339
344
  });
340
345
  return this;
341
346
  }
@@ -480,6 +485,79 @@ var MemoryPower = class extends MemoryBasic {
480
485
  }
481
486
  };
482
487
 
488
+ // src/memory/simple.ts
489
+ var MemorySimple = class {
490
+ #memory;
491
+ #key;
492
+ get size() {
493
+ return this.#memory.size;
494
+ }
495
+ constructor(key) {
496
+ this.#key = key;
497
+ this.#memory = new MemoryBasic(key, {
498
+ mount: (index) => index,
499
+ reflex: (reactor) => [reactor]
500
+ });
501
+ }
502
+ #getType(data) {
503
+ const key = this.#key;
504
+ return typeof key === "function" ? key(data) : data[key];
505
+ }
506
+ push(...list) {
507
+ const memory = this.#memory;
508
+ list.forEach((data) => {
509
+ if (Array.isArray(data)) {
510
+ this.push(...data);
511
+ } else if (!memory.includes(this.#getType(data))) {
512
+ memory.push(data);
513
+ }
514
+ });
515
+ return this;
516
+ }
517
+ /**
518
+ * 获取指定类型存储数据
519
+ */
520
+ get(type) {
521
+ const value = this.#memory.get(type);
522
+ if (value === null)
523
+ return value;
524
+ return value.length ? value[0] : null;
525
+ }
526
+ /**
527
+ * 遍历所有存储数据(按入栈顺序)
528
+ */
529
+ forEach(callback) {
530
+ this.#memory.forEach(callback);
531
+ return this;
532
+ }
533
+ /**
534
+ * 获取指定类型存储数据
535
+ */
536
+ includes(type) {
537
+ return this.#memory.includes(type);
538
+ }
539
+ /**
540
+ * 删除指定存储数据
541
+ */
542
+ delete(data) {
543
+ this.#memory.delete(data);
544
+ return this;
545
+ }
546
+ /**
547
+ * 删除指定类型存储数据
548
+ */
549
+ deleteType(type) {
550
+ this.#memory.deleteType(type);
551
+ return this;
552
+ }
553
+ /**
554
+ * 清理存储
555
+ */
556
+ clear() {
557
+ this.#memory.clear();
558
+ }
559
+ };
560
+
483
561
  // src/once.ts
484
562
  var once = (func, thisArg) => {
485
563
  let local;
@@ -527,6 +605,7 @@ var css = (...args) => {
527
605
  // src/subscriber.ts
528
606
  var Subscriber = class {
529
607
  #listeners = {};
608
+ #once = {};
530
609
  $on(name, listener) {
531
610
  const listeners = this.#listeners;
532
611
  if (name in listeners) {
@@ -536,23 +615,52 @@ var Subscriber = class {
536
615
  }
537
616
  return this;
538
617
  }
618
+ $once(name, listener) {
619
+ this.$on(name, listener);
620
+ const index = this.#listeners[name].length - 1;
621
+ const once2 = this.#once;
622
+ if (name in once2) {
623
+ once2[name].push(index);
624
+ } else {
625
+ once2[name] = [index];
626
+ }
627
+ return this;
628
+ }
539
629
  $emit(name, ...args) {
540
- this.#listeners[name]?.forEach((listener) => listener(...args));
630
+ const listeners = this.#listeners[name];
631
+ if (!listeners)
632
+ return this;
633
+ const once2 = hash8strings(this.#once[name] || []);
634
+ this.#listeners[name] = listeners.reduce((list, listener, index) => {
635
+ listener(...args);
636
+ index in once2 || list.push(listener);
637
+ return list;
638
+ }, []);
639
+ delete this.#once[name];
541
640
  return this;
542
641
  }
543
642
  $off(name, listener) {
544
643
  const listeners = this.#listeners;
644
+ const once2 = this.#once;
545
645
  if (name in listeners === false)
546
646
  return this;
547
647
  if (listener) {
648
+ let count;
548
649
  listeners[name].some((value, index) => {
549
650
  if (value === listener) {
651
+ count = index;
652
+ listeners[name].splice(index, 1);
653
+ return true;
654
+ }
655
+ }) && once2[name].some((value, index) => {
656
+ if (value === count) {
550
657
  listeners[name].splice(index, 1);
551
658
  return true;
552
659
  }
553
660
  });
554
661
  } else {
555
662
  delete listeners[name];
663
+ delete this.#once[name];
556
664
  }
557
665
  return this;
558
666
  }
package/lib/index.js CHANGED
@@ -16,6 +16,7 @@ import { loadImage } from "./load/image";
16
16
  import { MemoryBasic } from "./memory/basic";
17
17
  import { MemoryList } from "./memory/list";
18
18
  import { MemoryPower } from "./memory/power";
19
+ import { MemorySimple } from "./memory/simple";
19
20
  import { once } from "./once";
20
21
  import { deconPowersSum } from "./power/decon";
21
22
  import { rotateAngle } from "./rotate/angle";
@@ -32,6 +33,7 @@ export {
32
33
  MemoryBasic,
33
34
  MemoryList,
34
35
  MemoryPower,
36
+ MemorySimple,
35
37
  default2 as Subscriber,
36
38
  Union,
37
39
  clamp,
@@ -26,11 +26,15 @@ class MemoryBasic {
26
26
  push(...list) {
27
27
  const { memorys } = this;
28
28
  list.forEach((data) => {
29
- const type = this.getType(data);
30
- const memory = memorys.get(type);
31
- const index = this.list.push(data) - 1;
32
- const result = this.#mount(index, memory);
33
- memory === result || memorys.set(type, result);
29
+ if (Array.isArray(data)) {
30
+ this.push(...data);
31
+ } else {
32
+ const type = this.getType(data);
33
+ const memory = memorys.get(type);
34
+ const index = this.list.push(data) - 1;
35
+ const result = this.#mount(index, memory);
36
+ memory === result || memorys.set(type, result);
37
+ }
34
38
  });
35
39
  return this;
36
40
  }
@@ -0,0 +1,75 @@
1
+ import { MemoryBasic } from "./basic";
2
+ class MemorySimple {
3
+ #memory;
4
+ #key;
5
+ get size() {
6
+ return this.#memory.size;
7
+ }
8
+ constructor(key) {
9
+ this.#key = key;
10
+ this.#memory = new MemoryBasic(key, {
11
+ mount: (index) => index,
12
+ reflex: (reactor) => [reactor]
13
+ });
14
+ }
15
+ #getType(data) {
16
+ const key = this.#key;
17
+ return typeof key === "function" ? key(data) : data[key];
18
+ }
19
+ push(...list) {
20
+ const memory = this.#memory;
21
+ list.forEach((data) => {
22
+ if (Array.isArray(data)) {
23
+ this.push(...data);
24
+ } else if (!memory.includes(this.#getType(data))) {
25
+ memory.push(data);
26
+ }
27
+ });
28
+ return this;
29
+ }
30
+ /**
31
+ * 获取指定类型存储数据
32
+ */
33
+ get(type) {
34
+ const value = this.#memory.get(type);
35
+ if (value === null)
36
+ return value;
37
+ return value.length ? value[0] : null;
38
+ }
39
+ /**
40
+ * 遍历所有存储数据(按入栈顺序)
41
+ */
42
+ forEach(callback) {
43
+ this.#memory.forEach(callback);
44
+ return this;
45
+ }
46
+ /**
47
+ * 获取指定类型存储数据
48
+ */
49
+ includes(type) {
50
+ return this.#memory.includes(type);
51
+ }
52
+ /**
53
+ * 删除指定存储数据
54
+ */
55
+ delete(data) {
56
+ this.#memory.delete(data);
57
+ return this;
58
+ }
59
+ /**
60
+ * 删除指定类型存储数据
61
+ */
62
+ deleteType(type) {
63
+ this.#memory.deleteType(type);
64
+ return this;
65
+ }
66
+ /**
67
+ * 清理存储
68
+ */
69
+ clear() {
70
+ this.#memory.clear();
71
+ }
72
+ }
73
+ export {
74
+ MemorySimple
75
+ };
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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vope/utils",
3
- "version": "1.4.12",
3
+ "version": "1.4.14",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "publishConfig": {
package/types/index.d.ts CHANGED
@@ -16,6 +16,7 @@ export { loadImage } from './load/image';
16
16
  export { MemoryBasic } from './memory/basic';
17
17
  export { MemoryList } from './memory/list';
18
18
  export { MemoryPower } from './memory/power';
19
+ export { MemorySimple } from './memory/simple';
19
20
  export { once } from './once';
20
21
  export { deconPowersSum } from './power/decon';
21
22
  export { rotateAngle } from './rotate/angle';
@@ -12,6 +12,7 @@ interface Config<Reactor> {
12
12
  */
13
13
  reflex(reactor: Reactor): number[];
14
14
  }
15
+ export type _LV<T> = (T | _LV<T>)[];
15
16
  /**
16
17
  * 分类存储(通过继承,可用于拓展存储方式)
17
18
  */
@@ -30,7 +31,7 @@ export declare class MemoryBasic<V, Reactor, Type extends Memory.Type> {
30
31
  */
31
32
  constructor(key: Memory.Pitch<V, Type>, config: Config<Reactor>);
32
33
  protected getType(data: V): Type;
33
- push(...list: V[]): this;
34
+ push(...list: _LV<V>): this;
34
35
  /**
35
36
  * 遍历所有存储数据(按入栈顺序)
36
37
  */
@@ -0,0 +1,35 @@
1
+ import { _LV } from './basic';
2
+ import { Memory } from './type';
3
+ /**
4
+ * 简易存储(不分类)
5
+ */
6
+ export declare class MemorySimple<T, Type extends Memory.Type> {
7
+ #private;
8
+ get size(): number;
9
+ constructor(key: Memory.Pitch<T, Type>);
10
+ push(...list: _LV<T>): this;
11
+ /**
12
+ * 获取指定类型存储数据
13
+ */
14
+ get(type: Type): T | null;
15
+ /**
16
+ * 遍历所有存储数据(按入栈顺序)
17
+ */
18
+ forEach(callback: (data: T, index: number) => void): this;
19
+ /**
20
+ * 获取指定类型存储数据
21
+ */
22
+ includes(type: Type): boolean;
23
+ /**
24
+ * 删除指定存储数据
25
+ */
26
+ delete(data: T): this;
27
+ /**
28
+ * 删除指定类型存储数据
29
+ */
30
+ deleteType(type: Type): this;
31
+ /**
32
+ * 清理存储
33
+ */
34
+ clear(): void;
35
+ }
@@ -1,7 +1,7 @@
1
1
  import { GetGivenProps } from '@vope/types'
2
2
 
3
3
  export namespace Memory {
4
- type Type = string | number
4
+ type Type = string | number | symbol
5
5
 
6
6
  type Pitch<Data, Type> = (
7
7
  Data extends object
@@ -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
  }