@ks-web/use 0.0.9 → 0.1.0

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.
@@ -0,0 +1,2 @@
1
+ export type AnyFn = (...args: any[]) => any;
2
+ export declare function createGlobalState<Fn extends AnyFn>(stateFactory: Fn): Fn;
package/dist/index.cjs.js CHANGED
@@ -2331,6 +2331,7 @@ var src_exports = {};
2331
2331
  __export(src_exports, {
2332
2332
  CUSTOM_FIELD_INJECTION_KEY: () => CUSTOM_FIELD_INJECTION_KEY,
2333
2333
  cancelRaf: () => cancelRaf,
2334
+ createGlobalState: () => createGlobalState,
2334
2335
  customWatermark: () => customWatermark,
2335
2336
  debounce: () => debounce,
2336
2337
  doubleRaf: () => doubleRaf,
@@ -2348,6 +2349,7 @@ __export(src_exports, {
2348
2349
  useCountDown: () => useCountDown,
2349
2350
  useCustomFieldValue: () => useCustomFieldValue,
2350
2351
  useError: () => useError,
2352
+ useEventBus: () => useEventBus,
2351
2353
  useEventListener: () => useEventListener,
2352
2354
  useHttp: () => useHttp,
2353
2355
  usePageVisibility: () => usePageVisibility,
@@ -5549,6 +5551,64 @@ function throttle(func, wait, options) {
5549
5551
  trailing
5550
5552
  });
5551
5553
  }
5554
+
5555
+ // src/useEventBus/index.ts
5556
+ var import_vue13 = require("vue");
5557
+
5558
+ // src/useEventBus/internal.ts
5559
+ var events = /* @__PURE__ */ new Map();
5560
+
5561
+ // src/useEventBus/index.ts
5562
+ function useEventBus(key) {
5563
+ const scope = (0, import_vue13.getCurrentScope)();
5564
+ function on(listener) {
5565
+ var _a;
5566
+ const listeners = events.get(key) || /* @__PURE__ */ new Set();
5567
+ listeners.add(listener);
5568
+ events.set(key, listeners);
5569
+ const _off = () => off(listener);
5570
+ (_a = scope == null ? void 0 : scope.cleanups) == null ? void 0 : _a.push(_off);
5571
+ return _off;
5572
+ }
5573
+ function once(listener) {
5574
+ function _listener(...args) {
5575
+ off(_listener);
5576
+ listener(...args);
5577
+ }
5578
+ return on(_listener);
5579
+ }
5580
+ function off(listener) {
5581
+ const listeners = events.get(key);
5582
+ if (!listeners)
5583
+ return;
5584
+ listeners.delete(listener);
5585
+ if (!listeners.size)
5586
+ reset();
5587
+ }
5588
+ function reset() {
5589
+ events.delete(key);
5590
+ }
5591
+ function emit(event, payload) {
5592
+ var _a;
5593
+ (_a = events.get(key)) == null ? void 0 : _a.forEach((v) => v(event, payload));
5594
+ }
5595
+ return { on, once, off, emit, reset };
5596
+ }
5597
+
5598
+ // src/createGlobalState/index.ts
5599
+ var import_vue14 = require("vue");
5600
+ function createGlobalState(stateFactory) {
5601
+ let initialized = false;
5602
+ let state;
5603
+ const scope = (0, import_vue14.effectScope)(true);
5604
+ return (...args) => {
5605
+ if (!initialized) {
5606
+ state = scope.run(() => stateFactory(...args));
5607
+ initialized = true;
5608
+ }
5609
+ return state;
5610
+ };
5611
+ }
5552
5612
  /*! Bundled license information:
5553
5613
 
5554
5614
  js-md5/src/md5.js:
package/dist/index.d.ts CHANGED
@@ -15,3 +15,5 @@ export * from './useHttp';
15
15
  export * from './useError';
16
16
  export * from './useWaterMark';
17
17
  export * from './useDebounceThrottle';
18
+ export * from './useEventBus';
19
+ export * from './createGlobalState';
@@ -5531,9 +5531,68 @@ function throttle(func, wait, options) {
5531
5531
  trailing
5532
5532
  });
5533
5533
  }
5534
+
5535
+ // src/useEventBus/index.ts
5536
+ import { getCurrentScope } from "vue";
5537
+
5538
+ // src/useEventBus/internal.ts
5539
+ var events = /* @__PURE__ */ new Map();
5540
+
5541
+ // src/useEventBus/index.ts
5542
+ function useEventBus(key) {
5543
+ const scope = getCurrentScope();
5544
+ function on(listener) {
5545
+ var _a;
5546
+ const listeners = events.get(key) || /* @__PURE__ */ new Set();
5547
+ listeners.add(listener);
5548
+ events.set(key, listeners);
5549
+ const _off = () => off(listener);
5550
+ (_a = scope == null ? void 0 : scope.cleanups) == null ? void 0 : _a.push(_off);
5551
+ return _off;
5552
+ }
5553
+ function once(listener) {
5554
+ function _listener(...args) {
5555
+ off(_listener);
5556
+ listener(...args);
5557
+ }
5558
+ return on(_listener);
5559
+ }
5560
+ function off(listener) {
5561
+ const listeners = events.get(key);
5562
+ if (!listeners)
5563
+ return;
5564
+ listeners.delete(listener);
5565
+ if (!listeners.size)
5566
+ reset();
5567
+ }
5568
+ function reset() {
5569
+ events.delete(key);
5570
+ }
5571
+ function emit(event, payload) {
5572
+ var _a;
5573
+ (_a = events.get(key)) == null ? void 0 : _a.forEach((v) => v(event, payload));
5574
+ }
5575
+ return { on, once, off, emit, reset };
5576
+ }
5577
+
5578
+ // src/createGlobalState/index.ts
5579
+ import { effectScope } from "vue";
5580
+ function createGlobalState(stateFactory) {
5581
+ let initialized = false;
5582
+ let state;
5583
+ const scope = effectScope(true);
5584
+ return (...args) => {
5585
+ if (!initialized) {
5586
+ state = scope.run(() => stateFactory(...args));
5587
+ initialized = true;
5588
+ }
5589
+ return state;
5590
+ };
5591
+ }
5534
5592
  export {
5535
5593
  CUSTOM_FIELD_INJECTION_KEY,
5536
5594
  cancelRaf,
5595
+ createGlobalState,
5537
5596
  customWatermark,
5538
5597
  debounce,
5539
5598
  doubleRaf,
@@ -5551,6 +5610,7 @@ export {
5551
5610
  useCountDown,
5552
5611
  useCustomFieldValue,
5553
5612
  useError,
5613
+ useEventBus,
5554
5614
  useEventListener,
5555
5615
  useHttp,
5556
5616
  usePageVisibility,
@@ -0,0 +1,35 @@
1
+ export type Fn = () => void;
2
+ export type EventBusListener<T = unknown, P = any> = (event: T, payload?: P) => void;
3
+ export type EventBusEvents<T, P = any> = Set<EventBusListener<T, P>>;
4
+ export interface EventBusKey<T> extends Symbol {
5
+ }
6
+ export type EventBusIdentifier<T = unknown> = EventBusKey<T> | string | number;
7
+ export interface UseEventBusReturn<T, P> {
8
+ /**
9
+ * 消息订阅
10
+ * @param listener
11
+ * @returns 停止函数,用于删除当前回调。
12
+ */
13
+ on: (listener: EventBusListener<T, P>) => Fn;
14
+ /**
15
+ * 和on一样,但是只监听一次
16
+ * @param listener watch listener.
17
+ * @returns 停止函数,用于删除当前回调。
18
+ */
19
+ once: (listener: EventBusListener<T, P>) => Fn;
20
+ /**
21
+ * 派发消息
22
+ * @param event data sent.
23
+ */
24
+ emit: (event?: T, payload?: P) => void;
25
+ /**
26
+ * 移除事件监听
27
+ * @param listener watch listener.
28
+ */
29
+ off: (listener: EventBusListener<T>) => void;
30
+ /**
31
+ * 重置所有事件订阅
32
+ */
33
+ reset: () => void;
34
+ }
35
+ export declare function useEventBus<T = unknown, P = any>(key: EventBusIdentifier<T>): UseEventBusReturn<T, P>;
@@ -0,0 +1,2 @@
1
+ import type { EventBusEvents, EventBusIdentifier } from '.';
2
+ export declare const events: Map<EventBusIdentifier<any>, EventBusEvents<any>>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ks-web/use",
3
- "version": "0.0.9",
3
+ "version": "0.1.0",
4
4
  "description": "Vant Composition API",
5
5
  "main": "dist/index.cjs.js",
6
6
  "module": "dist/index.esm.mjs",
@@ -16,15 +16,6 @@
16
16
  "files": [
17
17
  "dist"
18
18
  ],
19
- "scripts": {
20
- "clean": "rimraf ./dist",
21
- "dev": "node ./build.js -w",
22
- "build:types": "tsc -p ./tsconfig.json --emitDeclarationOnly",
23
- "build:bundle": "node ./build.js",
24
- "build": "pnpm clean && pnpm build:bundle && pnpm build:types",
25
- "release": "ks-cli release",
26
- "prepare": "pnpm build"
27
- },
28
19
  "publishConfig": {
29
20
  "access": "public",
30
21
  "registry": "https://registry.npmjs.org/"
@@ -52,5 +43,13 @@
52
43
  },
53
44
  "peerDependencies": {
54
45
  "vue": "^3.0.0"
46
+ },
47
+ "scripts": {
48
+ "clean": "rimraf ./dist",
49
+ "dev": "node ./build.js -w",
50
+ "build:types": "tsc -p ./tsconfig.json --emitDeclarationOnly",
51
+ "build:bundle": "node ./build.js",
52
+ "build": "pnpm clean && pnpm build:bundle && pnpm build:types",
53
+ "release": "ks-cli release"
55
54
  }
56
- }
55
+ }