@wxhccc/es-util 1.7.0 → 2.0.2

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.
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * @wxhccc/es-util v1.7.0
2
+ * @wxhccc/es-util v2.0.2
3
3
  * (c) 2022 wxhccc
4
4
  * @license MIT
5
5
  */
@@ -7,6 +7,8 @@
7
7
 
8
8
  Object.defineProperty(exports, '__esModule', { value: true });
9
9
 
10
+ var utils = require('./utils.js');
11
+
10
12
  const isStr = (val) => typeof val === 'string';
11
13
  const NOMETHOD = Symbol('NOMETHOD');
12
14
  /**
@@ -17,7 +19,9 @@ const NOMETHOD = Symbol('NOMETHOD');
17
19
  function eventTargetEmitter(options) {
18
20
  const watchers = new Map([[NOMETHOD, new Set()]]);
19
21
  const { name: currentName, customHanlderCreator } = options || {};
20
- const customHanlder = customHanlderCreator instanceof Function ? customHanlderCreator(watchers) : null;
22
+ const customHanlder = utils.isFn(customHanlderCreator)
23
+ ? customHanlderCreator(watchers)
24
+ : null;
21
25
  const splitRegExp = /\|-[a-z]+-\|/;
22
26
  const listenerSafeRun = (listener, ...args) => {
23
27
  try {
@@ -33,25 +37,28 @@ function eventTargetEmitter(options) {
33
37
  const matchListeners = [];
34
38
  watchers.forEach((value, key) => {
35
39
  if (typeof key === 'string' && key.startsWith(method)) {
36
- const [, eFrom,] = key.split(splitRegExp);
40
+ const [, eFrom] = key.split(splitRegExp);
37
41
  if (!eFrom || !from || eFrom === from) {
38
42
  matchListeners.push(...value);
39
43
  }
40
44
  }
41
45
  });
42
- if (matchListeners.length && (!currentName || !to || currentName === to)) {
43
- matchListeners.forEach(listener => listenerSafeRun(listener, data));
46
+ if (matchListeners.length &&
47
+ (!currentName || !to || currentName === to)) {
48
+ matchListeners.forEach((listener) => listenerSafeRun(listener, data));
44
49
  }
45
50
  }
46
51
  const noMethodListeners = watchers.get(NOMETHOD);
47
52
  if (noMethodListeners) {
48
- noMethodListeners.forEach(listener => listenerSafeRun(listener, data, payload));
53
+ noMethodListeners.forEach((listener) => listenerSafeRun(listener, data, payload));
49
54
  }
50
55
  };
51
- const rootEventListener = customHanlder instanceof Function ? customHanlder : defaultHanlder;
56
+ const rootEventListener = utils.isFn(customHanlder) ? customHanlder : defaultHanlder;
52
57
  const getFullMethodName = (method, options) => {
53
58
  const { limitFrom = '', namespace = '' } = options || {};
54
- return (limitFrom || namespace) && typeof method === 'string' ? `${method}|-from-|${limitFrom}|-ns-|${namespace}` : method;
59
+ return (limitFrom || namespace) && typeof method === 'string'
60
+ ? `${method}|-from-|${limitFrom}|-ns-|${namespace}`
61
+ : method;
55
62
  };
56
63
  const on = (method, listener, options) => {
57
64
  if (typeof method === 'symbol' && method !== NOMETHOD) {
@@ -68,7 +75,7 @@ function eventTargetEmitter(options) {
68
75
  if (typeof method === 'symbol' && method !== NOMETHOD) {
69
76
  return;
70
77
  }
71
- const { limitFrom, namespace } = options || {};
78
+ const { namespace } = options || {};
72
79
  if (namespace && !method) {
73
80
  watchers.forEach((_value, key) => {
74
81
  if (typeof key === 'string' && key.endsWith(namespace)) {
@@ -88,12 +95,16 @@ function eventTargetEmitter(options) {
88
95
  watchers.clear();
89
96
  };
90
97
  const createPayload = (methodOrPayload, data, target) => {
91
- return isStr(methodOrPayload) ? {
92
- method: methodOrPayload,
93
- data,
94
- ...(target && isStr(target) ? { targetEmitterName: target } : {}),
95
- ...(currentName && isStr(currentName) ? { originEmitterName: currentName } : {})
96
- } : methodOrPayload;
98
+ return isStr(methodOrPayload)
99
+ ? {
100
+ method: methodOrPayload,
101
+ data,
102
+ ...(target && isStr(target) ? { targetEmitterName: target } : {}),
103
+ ...(currentName && isStr(currentName)
104
+ ? { originEmitterName: currentName }
105
+ : {})
106
+ }
107
+ : methodOrPayload;
97
108
  };
98
109
  const emit = (...args) => {
99
110
  const payload = createPayload(...args);
@@ -118,9 +129,19 @@ function eventTargetEmitter(options) {
118
129
  const listeners = watchers.get(trueMethod);
119
130
  return listeners ? Array.from(listeners) : [];
120
131
  };
121
- return { name: currentName, rootEventListener, on, off, emit, createPayload, removeAllListeners, getMethodNames, getListeners };
132
+ return {
133
+ name: currentName,
134
+ rootEventListener,
135
+ on,
136
+ off,
137
+ emit,
138
+ createPayload,
139
+ removeAllListeners,
140
+ getMethodNames,
141
+ getListeners
142
+ };
122
143
  }
123
144
 
124
145
  exports.NOMETHOD = NOMETHOD;
125
- exports.default = eventTargetEmitter;
146
+ exports["default"] = eventTargetEmitter;
126
147
  exports.eventTargetEmitter = eventTargetEmitter;
package/dist/index.d.ts CHANGED
@@ -6,3 +6,5 @@ export * from './promise';
6
6
  export * from './optional';
7
7
  export * from './event-target-emitter';
8
8
  export * from './page-communicate';
9
+ export * from './raf-timer';
10
+ export * from './date-time';