rxjs-native-bridge 1.0.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.
- package/README.md +87 -0
- package/dist/es/index.js +184 -0
- package/dist/index-BpknkqY0.cjs +3 -0
- package/dist/index-HOABx4tn.js +2989 -0
- package/dist/index.d.ts +77 -0
- package/dist/lib/index.js +1 -0
- package/package.json +49 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { BehaviorSubject } from 'rxjs';
|
|
2
|
+
import { Observable } from 'rxjs';
|
|
3
|
+
import { Subject } from 'rxjs';
|
|
4
|
+
|
|
5
|
+
declare type CallInterceptor<R = unknown> = (res: R, ctx: {
|
|
6
|
+
handlerName: string;
|
|
7
|
+
params: unknown;
|
|
8
|
+
}) => unknown;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* WebView JSBridge 单例
|
|
12
|
+
*/
|
|
13
|
+
export declare class JSBridge {
|
|
14
|
+
protected static instance: JSBridge;
|
|
15
|
+
private bridge?;
|
|
16
|
+
private isReady;
|
|
17
|
+
private readyCallbacks;
|
|
18
|
+
readonly isAndroid: boolean;
|
|
19
|
+
readonly isiOS: boolean;
|
|
20
|
+
protected constructor();
|
|
21
|
+
/** 获取单例 */
|
|
22
|
+
static getInstance(): JSBridge;
|
|
23
|
+
/** 是否在 Native WebView 中 */
|
|
24
|
+
isNative(): boolean;
|
|
25
|
+
/** 初始化 bridge */
|
|
26
|
+
private setup;
|
|
27
|
+
private setupAndroid;
|
|
28
|
+
private setupIOS;
|
|
29
|
+
/** bridge ready 统一入口 */
|
|
30
|
+
private onReady;
|
|
31
|
+
/** 等待 bridge 可用 */
|
|
32
|
+
private withBridge;
|
|
33
|
+
/** JS 调用 Native */
|
|
34
|
+
callHandler<T = unknown, R = unknown>(name: string, data?: T, callback?: (response: R) => void): void;
|
|
35
|
+
/** Native 调用 JS */
|
|
36
|
+
registerHandler<T = unknown, R = unknown>(name: string, callback: (data: T, responseCallback: (response: R) => void) => void): void;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export declare const jsBridge: JSBridge;
|
|
40
|
+
|
|
41
|
+
export declare class RxJsBridge extends JSBridge {
|
|
42
|
+
protected static instance: RxJsBridge;
|
|
43
|
+
protected _callInterceptor?: CallInterceptor;
|
|
44
|
+
protected _stateMap: Map<string, BehaviorSubject<unknown>>;
|
|
45
|
+
protected _event: Subject<RxJSBridgeEvent<unknown>> | undefined;
|
|
46
|
+
protected _registered: Set<string>;
|
|
47
|
+
protected constructor();
|
|
48
|
+
static getInstance(): RxJsBridge;
|
|
49
|
+
useCallInterceptor<R = unknown>(interceptor: CallInterceptor<R>): this;
|
|
50
|
+
call<T = unknown, R = unknown>(handlerName: string, params: T): Observable<R>;
|
|
51
|
+
setState<T = unknown>(handlerName: string, value: T): void;
|
|
52
|
+
delState(handlerName: string): void;
|
|
53
|
+
clearState(): void;
|
|
54
|
+
register<T = unknown>(handlerName: string, defaultState?: unknown): this;
|
|
55
|
+
on<T = unknown>(handlerName: string): Observable< {
|
|
56
|
+
params: T;
|
|
57
|
+
reply: ((res: unknown) => void) | undefined;
|
|
58
|
+
}>;
|
|
59
|
+
event<T = unknown>(handlerName: string, defaultState?: unknown): Observable< {
|
|
60
|
+
params: T;
|
|
61
|
+
reply: ((res: unknown) => void) | undefined;
|
|
62
|
+
}>;
|
|
63
|
+
once<T = unknown>(handlerName: string, defaultState?: unknown): Observable< {
|
|
64
|
+
params: T;
|
|
65
|
+
reply: ((res: unknown) => void) | undefined;
|
|
66
|
+
}>;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export declare const rxJsBridge: RxJsBridge;
|
|
70
|
+
|
|
71
|
+
declare interface RxJSBridgeEvent<T = unknown> {
|
|
72
|
+
key: string;
|
|
73
|
+
params: T;
|
|
74
|
+
reply?: (res: unknown) => void;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export { }
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var w=Object.create;var h=Object.defineProperty;var y=Object.getOwnPropertyDescriptor;var f=Object.getOwnPropertyNames;var v=Object.getPrototypeOf,b=Object.prototype.hasOwnProperty;var _=(s,e,t,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of f(e))!b.call(s,r)&&r!==t&&h(s,r,{get:()=>e[r],enumerable:!(i=y(e,r))||i.enumerable});return s};var B=(s,e,t)=>(t=s!=null?w(v(s)):{},_(e||!s||!s.__esModule?h(t,"default",{value:s,enumerable:!0}):t,s));Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});class n{static instance;bridge;isReady=!1;readyCallbacks=[];isAndroid=navigator.userAgent.includes("Android")||navigator.userAgent.includes("Adr");isiOS=/\(i[^;]+;( U;)? CPU.+Mac OS X/.test(navigator.userAgent);constructor(){this.setup()}static getInstance(){return n.instance||(n.instance=new n),n.instance}isNative(){return!!window.WebViewJavascriptBridge}setup(){this.isAndroid?this.setupAndroid():this.isiOS&&this.setupIOS()}setupAndroid(){window.WebViewJavascriptBridge?this.onReady(window.WebViewJavascriptBridge):document.addEventListener("WebViewJavascriptBridgeReady",()=>{this.onReady(window.WebViewJavascriptBridge)},!1)}setupIOS(){if(window.WebViewJavascriptBridge){this.onReady(window.WebViewJavascriptBridge);return}if(window.WVJBCallbacks){window.WVJBCallbacks.push(t=>this.onReady(t));return}window.WVJBCallbacks=[t=>this.onReady(t)];const e=document.createElement("iframe");e.style.display="none",e.src="https://__bridge_loaded__",document.documentElement.appendChild(e),setTimeout(()=>{document.documentElement.removeChild(e)},0)}onReady(e){this.isReady||(this.bridge=e,this.isReady=!0,this.isAndroid&&e.init((t,i)=>{i("JS init response")}),this.readyCallbacks.forEach(t=>t(e)),this.readyCallbacks.length=0)}withBridge(e){this.isReady&&this.bridge?e(this.bridge):this.readyCallbacks.push(e)}callHandler(e,t,i){this.withBridge(r=>{r.callHandler(e,t,i)})}registerHandler(e,t){this.withBridge(i=>{i.registerHandler(e,t)})}}const S=n.getInstance();let p,d,u,o;(async()=>{try{const s=await import("rxjs");p=s.Observable,d=s.Subject,o=await Promise.resolve().then(()=>require("../index-BpknkqY0.cjs")),u=s.BehaviorSubject}catch(s){throw new Error("RxJS 未安装,RxJsBridge 功能不可用"+s)}})();class a extends n{static instance;_callInterceptor;_stateMap=new Map;_event=d?new d:void 0;_registered=new Set;constructor(){super()}static getInstance(){return a.instance||(a.instance=new a),a.instance}useCallInterceptor(e){return this._callInterceptor=e,this}call(e,t){return new p(i=>{try{this.callHandler(e,t,r=>{try{let c=r;this._callInterceptor&&(c=this._callInterceptor(r,{handlerName:e,params:t})),i.next(c),i.complete()}catch(c){i.error(c)}})}catch(r){i.error(r)}})}setState(e,t){if(!this._registered.has(e))return;let i=this._stateMap.get(e);i?i.next(t):(i=new u(t),this._stateMap.set(e,i))}delState(e){const t=this._stateMap.get(e);t&&(t.complete(),this._stateMap.delete(e))}clearState(){this._stateMap.forEach(e=>e.complete()),this._stateMap.clear()}register(e,t){return this._registered.has(e)?this:(this._registered.add(e),t!==void 0&&this.setState(e,t),this.registerHandler(e,(i,r)=>{const c=g=>{console.log("扩展方法,暂时无用",g)};this._event.next({key:e,params:i,reply:c});const l=this._stateMap.get(e);r(l?l.getValue():null)}),this)}on(e){return this._event.pipe(o.filter(t=>t.key===e),o.map(t=>({params:t.params,reply:t.reply})))}event(e,t){return this.register(e,t),this.on(e)}once(e,t){return this.register(e,t),this.on(e).pipe(o.take(1))}}const m=a.getInstance();exports.JSBridge=n;exports.RxJsBridge=a;exports.jsBridge=S;exports.rxJsBridge=m;
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "rxjs-native-bridge",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/ztf-git/rxjs-native-bridge"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"dist"
|
|
11
|
+
],
|
|
12
|
+
"main": "dist/es/index.js",
|
|
13
|
+
"module": "dist/lib/index.js",
|
|
14
|
+
"types": "dist/index.d.ts",
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"import": "./dist/es/index.js",
|
|
19
|
+
"require": "./dist/lib/index.js"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"scripts": {
|
|
23
|
+
"dev": "vite",
|
|
24
|
+
"build": "vite build",
|
|
25
|
+
"format": "prettier --write . && eslint . --fix",
|
|
26
|
+
"preview": "vite preview"
|
|
27
|
+
},
|
|
28
|
+
"optionalDependencies": {
|
|
29
|
+
"rxjs": "^7.8.2"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@eslint/js": "^9.39.2",
|
|
33
|
+
"@types/node": "^24.10.1",
|
|
34
|
+
"dts": "^0.1.1",
|
|
35
|
+
"eslint": "^9.39.2",
|
|
36
|
+
"eslint-config-prettier": "^10.1.8",
|
|
37
|
+
"eslint-plugin-prettier": "^5.5.4",
|
|
38
|
+
"from": "^0.1.7",
|
|
39
|
+
"globals": "^16.5.0",
|
|
40
|
+
"import": "^0.0.6",
|
|
41
|
+
"jiti": "^2.6.1",
|
|
42
|
+
"prettier": "^3.7.4",
|
|
43
|
+
"rxjs": "^7.8.2",
|
|
44
|
+
"typescript": "~5.9.3",
|
|
45
|
+
"typescript-eslint": "^8.50.0",
|
|
46
|
+
"vite": "^7.2.4",
|
|
47
|
+
"vite-plugin-dts": "^4.5.4"
|
|
48
|
+
}
|
|
49
|
+
}
|