@trimjs/web-app 0.2.4 → 0.2.6
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 +99 -0
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +58 -0
- package/dist/index.iife.js +1 -1
- package/dist/index.js +1 -1
- package/dist/index.umd.cjs +1 -1
- package/package.json +4 -1
package/README.md
CHANGED
|
@@ -544,6 +544,105 @@ const files = await app.pickFile({
|
|
|
544
544
|
|
|
545
545
|
---
|
|
546
546
|
|
|
547
|
+
#### $on
|
|
548
|
+
|
|
549
|
+
```typescript
|
|
550
|
+
async $on(event: string, cb: (...args: any[]) => void): Promise<void>
|
|
551
|
+
```
|
|
552
|
+
|
|
553
|
+
监听系统事件(仅 Web 平台)。
|
|
554
|
+
|
|
555
|
+
**参数:**
|
|
556
|
+
|
|
557
|
+
- `event: string` - 事件名称
|
|
558
|
+
- `cb: (...args: any[]) => void` - 回调函数
|
|
559
|
+
|
|
560
|
+
**示例:**
|
|
561
|
+
|
|
562
|
+
```typescript
|
|
563
|
+
// 监听主题变更
|
|
564
|
+
app.$on("os/theme", (theme) => {
|
|
565
|
+
console.log("主题变更:", theme);
|
|
566
|
+
});
|
|
567
|
+
|
|
568
|
+
// 监听语言变更
|
|
569
|
+
app.$on("os/language", (language) => {
|
|
570
|
+
console.log("语言变更:", language);
|
|
571
|
+
});
|
|
572
|
+
```
|
|
573
|
+
|
|
574
|
+
---
|
|
575
|
+
|
|
576
|
+
#### $off
|
|
577
|
+
|
|
578
|
+
```typescript
|
|
579
|
+
async $off(event: string, cb: (...args: any[]) => void): Promise<void>
|
|
580
|
+
```
|
|
581
|
+
|
|
582
|
+
取消监听系统事件(仅 Web 平台)。
|
|
583
|
+
|
|
584
|
+
**参数:**
|
|
585
|
+
|
|
586
|
+
- `event: string` - 事件名称
|
|
587
|
+
- `cb: (...args: any[]) => void` - 之前注册的回调函数引用
|
|
588
|
+
|
|
589
|
+
**示例:**
|
|
590
|
+
|
|
591
|
+
```typescript
|
|
592
|
+
const onThemeChange = (theme) => {
|
|
593
|
+
console.log("主题变更:", theme);
|
|
594
|
+
};
|
|
595
|
+
|
|
596
|
+
app.$on("os/theme", onThemeChange);
|
|
597
|
+
|
|
598
|
+
// 取消监听
|
|
599
|
+
app.$off("os/theme", onThemeChange);
|
|
600
|
+
```
|
|
601
|
+
|
|
602
|
+
---
|
|
603
|
+
|
|
604
|
+
#### $once
|
|
605
|
+
|
|
606
|
+
```typescript
|
|
607
|
+
async $once(event: string, cb: (...args: any[]) => void): Promise<void>
|
|
608
|
+
```
|
|
609
|
+
|
|
610
|
+
监听一次系统事件,触发后自动取消(仅 Web 平台)。
|
|
611
|
+
|
|
612
|
+
**参数:**
|
|
613
|
+
|
|
614
|
+
- `event: string` - 事件名称
|
|
615
|
+
- `cb: (...args: any[]) => void` - 回调函数
|
|
616
|
+
|
|
617
|
+
**示例:**
|
|
618
|
+
|
|
619
|
+
```typescript
|
|
620
|
+
// 只监听一次激活事件
|
|
621
|
+
app.$once("os/app.activated", () => {
|
|
622
|
+
console.log("应用被激活");
|
|
623
|
+
});
|
|
624
|
+
```
|
|
625
|
+
|
|
626
|
+
---
|
|
627
|
+
|
|
628
|
+
#### 系统事件列表
|
|
629
|
+
|
|
630
|
+
以下是基座平台提供的系统事件:
|
|
631
|
+
|
|
632
|
+
| 事件名 | 说明 |
|
|
633
|
+
|---|---|
|
|
634
|
+
| `os/theme` | 主题色变更(dark / light) |
|
|
635
|
+
| `os/language` | 系统语言变更 |
|
|
636
|
+
| `os/format.date` | 日期格式变更 |
|
|
637
|
+
| `os/app.appMeta` | 应用元数据变更 |
|
|
638
|
+
| `os/app.visibilitychange` | 应用最小化状态变更 |
|
|
639
|
+
| `os/app.activated` | 应用激活态变更 |
|
|
640
|
+
| `os/net.set` | 网络设置变更 |
|
|
641
|
+
| `os/net.ovs` | 网络 OVS 开关变更 |
|
|
642
|
+
| `os/storage.change` | 存储变更 |
|
|
643
|
+
|
|
644
|
+
---
|
|
645
|
+
|
|
547
646
|
#### callAppMethod
|
|
548
647
|
|
|
549
648
|
```typescript
|
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var e=Object.defineProperty,t=Object.defineProperties,n=Object.getOwnPropertyDescriptors,r=Object.getOwnPropertySymbols,o=Object.prototype.hasOwnProperty,i=Object.prototype.propertyIsEnumerable,s=(t,n,r)=>n in t?e(t,n,{enumerable:!0,configurable:!0,writable:!0,value:r}):t[n]=r,l=(e,t)=>{for(var n in t||(t={}))o.call(t,n)&&s(e,n,t[n]);if(r)for(var n of r(t))i.call(t,n)&&s(e,n,t[n]);return e},a=(e,r)=>t(e,n(r)),c=(e,t,n)=>s(e,"symbol"!=typeof t?t+"":t,n),u=(e,t,n)=>new Promise((r,o)=>{var i=e=>{try{l(n.next(e))}catch(t){o(t)}},s=e=>{try{l(n.throw(e))}catch(t){o(t)}},l=e=>e.done?r(e.value):Promise.resolve(e.value).then(i,s);l((n=n.apply(e,t)).next())});Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const p=new class{constructor(){c(this,"debug",!1)}setDebug(e){this.debug=e}isDebug(){return this.debug}log(){if(this.debug){for(var e=arguments.length,t=new Array(e),n=0;n<e;n++)t[n]=arguments[n];console.log("[Trim App]",...t)}}warn(){if(this.debug){for(var e=arguments.length,t=new Array(e),n=0;n<e;n++)t[n]=arguments[n];console.warn("[Trim App]",...t)}}error(){if(this.debug){for(var e=arguments.length,t=new Array(e),n=0;n<e;n++)t[n]=arguments[n];console.error("[Trim App]",...t)}}info(){if(this.debug){for(var e=arguments.length,t=new Array(e),n=0;n<e;n++)t[n]=arguments[n];console.info("[Trim App]",...t)}}};const d=()=>function(e){const t=/FNOS\/([\d.]+)|FNAppType\/(\w+)|FNAppVer\/([\d.]+)/g,n={};let r;for(;null!==(r=t.exec(e));)r[1]&&(n.fnOSVersion=r[1]),r[2]&&(n.fnAppType=r[2]),r[3]&&(n.fnAppVersion=r[3]);return n}(navigator.userAgent).fnAppVersion;let f=!1;window.addEventListener("flutterInAppWebViewPlatformReady",()=>{f=!0});const h=new Map;d()&&(window.fnAppMessage=e=>{p.log("app-message",e);const t=JSON.parse(e),n=h.get(t.reqId);n&&(n(t),h.delete(t.reqId))});var g=(e=>(e.Call="call",e.Reply="reply",e.Syn="syn",e.SynAck="synAck",e.Ack="ack",e))(g||{}),y=(e=>(e.Fulfilled="fulfilled",e.Rejected="rejected",e))(y||{}),v=(e=>(e.ConnectionDestroyed="ConnectionDestroyed",e.ConnectionTimeout="ConnectionTimeout",e.NoIframeSrc="NoIframeSrc",e))(v||{}),m=(e=>(e.DataCloneError="DataCloneError",e))(m||{}),b=(e=>(e.Message="message",e))(b||{});const w=({name:e,message:t,stack:n})=>({name:e,message:t,stack:n});let _=0;const $=e=>e?e.split("."):[],A=(e,t,n)=>{const r=$(t);return r.reduce((e,t,o)=>(void 0===e[t]&&(e[t]={}),o===r.length-1&&(e[t]=n),e[t]),e),e},O=(e,t)=>{const n={};return Object.keys(e).forEach(r=>{const o=e[r],i=((e,t)=>{const n=$(t||"");return n.push(e),(e=>e.join("."))(n)})(r,t);"object"==typeof o&&Object.assign(n,O(o,i)),"function"==typeof o&&(n[i]=o)}),n},E=(e,t,n,r,o)=>{const{localName:i,local:s,remote:l,originForSending:a,originForReceiving:c}=t;let u=!1;o(`${i}: Connecting call sender`);const p=e=>(...t)=>{let n;o(`${i}: Sending ${e}() call`);try{l.closed&&(n=!0)}catch(p){n=!0}if(n&&r(),u){const t=new Error(`Unable to send ${e}() call due to destroyed connection`);throw t.code=v.ConnectionDestroyed,t}return new Promise((n,r)=>{const u=++_,p=t=>{if(t.source!==l||t.data.penpal!==g.Reply||t.data.id!==u)return;if("*"!==c&&t.origin!==c)return void o(`${i} received message from origin ${t.origin} which did not match expected origin ${c}`);const a=t.data;o(`${i}: Received ${e}() reply`),s.removeEventListener(b.Message,p);let d=a.returnValue;a.returnValueIsError&&(d=(e=>{const t=new Error;return Object.keys(e).forEach(n=>t[n]=e[n]),t})(d)),(a.resolution===y.Fulfilled?n:r)(d)};s.addEventListener(b.Message,p);const d={penpal:g.Call,id:u,methodName:e,args:t};l.postMessage(d,a)})},d=n.reduce((e,t)=>(e[t]=p(t),e),{});return Object.assign(e,(e=>{const t={};for(const n in e)A(t,n,e[n]);return t})(d)),()=>{u=!0}},P="0.1.2",S=(e,t,n,r,o)=>{const{destroy:i,onDestroy:s}=r;return r=>{if(!(e instanceof RegExp?e.test(r.origin):"*"===e||e===r.origin))return void o(`Child: Handshake - Received SYN-ACK from origin ${r.origin} which did not match expected origin ${e}`);o("Child: Handshake - Received SYN-ACK, responding with ACK");const l="null"===r.origin?"*":r.origin,a={penpal:g.Ack,methodNames:Object.keys(t),config:n};window.parent.postMessage(a,l);const c={localName:"Child",local:window,remote:window.parent,originForSending:l,originForReceiving:r.origin},u=((e,t,n)=>{const{localName:r,local:o,remote:i,originForSending:s,originForReceiving:l}=e;let a=!1;const c=e=>{if(e.source!==i||e.data.penpal!==g.Call)return;if("*"!==l&&e.origin!==l)return void n(`${r} received message from origin ${e.origin} which did not match expected origin ${l}`);const o=e.data,{methodName:c,args:u,id:p}=o;n(`${r}: Received ${c}() call`);const d=e=>t=>{if(n(`${r}: Sending ${c}() reply`),a)return void n(`${r}: Unable to send ${c}() reply due to destroyed connection`);const o={penpal:g.Reply,id:p,resolution:e,returnValue:t};e===y.Rejected&&t instanceof Error&&(o.returnValue=w(t),o.returnValueIsError=!0);try{i.postMessage(o,s)}catch(l){if(l.name===m.DataCloneError){const e={penpal:g.Reply,id:p,resolution:y.Rejected,returnValue:w(l),returnValueIsError:!0};i.postMessage(e,s)}throw l}};new Promise(e=>e(t[c].apply(t,u))).then(d(y.Fulfilled),d(y.Rejected))};return o.addEventListener(b.Message,c),()=>{a=!0,o.removeEventListener(b.Message,c)}})(c,t,o);s(u);const p={},d=E(p,c,r.data.methodNames,i,o);return s(d),p}};function C(e,t){null==console||console.warn(`[fnApp warn]: ${e}`,t)}function x(e,t){null==console||console.error(`[fnApp error]: ${e}`,t)}const M="_fn_all_event",j="事件订阅数量为空",N=window.__POWERED_BY_FNAPP__?window.__FNAPP.inject.appEventObjMap:new Map;class F{constructor(e,t){c(this,"id"),c(this,"appName"),c(this,"eventObj"),c(this,"getEventCallBackLength",e=>{var t;return(null==(t=this.eventObj[e])?void 0:t.length)||0}),this.id=e,this.appName=t,this.$clear(),N.get(this.id)||N.set(this.id,{}),this.eventObj=N.get(this.id)}$on(e,t){const n=this.eventObj[e];return n?(n.includes(t)||n.push(t),this):(this.eventObj[e]=[t],this)}$onAll(e){return this.$on(M,e)}$once(e,t){t.$__once=!0,this.$on(e,t)}$off(e,t){const n=this.eventObj[e];if(!e||!n||!n.length)return C(`${e} ${j}`),this;let r,o=n.length;for(;o--;)if(r=n[o],r===t){n.splice(o,1);break}return this}$offAll(e){return this.$off(M,e)}$emit(e,...t){return e=`${this.appName}/${e}`,this.$pureEmit(e,...t)}$pureEmit(e,...t){let n=0;return N.forEach(r=>{if(r[e])for(let i=r[e].length-1;i>=0;i--){n++;const s=r[e][i];s.$__once&&r[e].splice(i,1);try{s(...t)}catch(o){x(o)}}if(r[M])for(let e=r[M].length-1;e>=0;e--){n++;const i=r[M][e];i.$__once&&r[M].splice(e,1);try{i(...t)}catch(o){x(o)}}}),e&&0!==n||C(`${e} ${j}`),this}$clear(){const e=N.get(this.id)||{};return Object.keys(e).forEach(t=>delete e[t]),this}}var T=function(e,t){return(T=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n])})(e,t)};function R(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Class extends value "+String(t)+" is not a constructor or null");function n(){this.constructor=e}T(e,t),e.prototype=null===t?Object.create(t):(n.prototype=t.prototype,new n)}function k(e){var t="function"==typeof Symbol&&Symbol.iterator,n=t&&e[t],r=0;if(n)return n.call(e);if(e&&"number"==typeof e.length)return{next:function(){return e&&r>=e.length&&(e=void 0),{value:e&&e[r++],done:!e}}};throw new TypeError(t?"Object is not iterable.":"Symbol.iterator is not defined.")}function V(e,t){var n="function"==typeof Symbol&&e[Symbol.iterator];if(!n)return e;var r,o,i=n.call(e),s=[];try{for(;(void 0===t||t-- >0)&&!(r=i.next()).done;)s.push(r.value)}catch(l){o={error:l}}finally{try{r&&!r.done&&(n=i.return)&&n.call(i)}finally{if(o)throw o.error}}return s}function W(e,t,n){if(n||2===arguments.length)for(var r,o=0,i=t.length;o<i;o++)!r&&o in t||(r||(r=Array.prototype.slice.call(t,0,o)),r[o]=t[o]);return e.concat(r||Array.prototype.slice.call(t))}function I(e){return"function"==typeof e}"function"==typeof SuppressedError&&SuppressedError;var q,D=((q=function(e){return function(t){e(this),this.message=t?t.length+" errors occurred during unsubscription:\n"+t.map(function(e,t){return t+1+") "+e.toString()}).join("\n "):"",this.name="UnsubscriptionError",this.errors=t}}(function(e){Error.call(e),e.stack=(new Error).stack})).prototype=Object.create(Error.prototype),q.prototype.constructor=q,q);function z(e,t){if(e){var n=e.indexOf(t);0<=n&&e.splice(n,1)}}var L=function(){function e(e){this.initialTeardown=e,this.closed=!1,this._parentage=null,this._finalizers=null}var t;return e.prototype.unsubscribe=function(){var e,t,n,r,o;if(!this.closed){this.closed=!0;var i=this._parentage;if(i)if(this._parentage=null,Array.isArray(i))try{for(var s=k(i),l=s.next();!l.done;l=s.next()){l.value.remove(this)}}catch(f){e={error:f}}finally{try{l&&!l.done&&(t=s.return)&&t.call(s)}finally{if(e)throw e.error}}else i.remove(this);var a=this.initialTeardown;if(I(a))try{a()}catch(h){o=h instanceof D?h.errors:[h]}var c=this._finalizers;if(c){this._finalizers=null;try{for(var u=k(c),p=u.next();!p.done;p=u.next()){var d=p.value;try{Y(d)}catch(g){o=null!=o?o:[],g instanceof D?o=W(W([],V(o)),V(g.errors)):o.push(g)}}}catch(y){n={error:y}}finally{try{p&&!p.done&&(r=u.return)&&r.call(u)}finally{if(n)throw n.error}}}if(o)throw new D(o)}},e.prototype.add=function(t){var n;if(t&&t!==this)if(this.closed)Y(t);else{if(t instanceof e){if(t.closed||t._hasParent(this))return;t._addParent(this)}(this._finalizers=null!==(n=this._finalizers)&&void 0!==n?n:[]).push(t)}},e.prototype._hasParent=function(e){var t=this._parentage;return t===e||Array.isArray(t)&&t.includes(e)},e.prototype._addParent=function(e){var t=this._parentage;this._parentage=Array.isArray(t)?(t.push(e),t):t?[t,e]:e},e.prototype._removeParent=function(e){var t=this._parentage;t===e?this._parentage=null:Array.isArray(t)&&z(t,e)},e.prototype.remove=function(t){var n=this._finalizers;n&&z(n,t),t instanceof e&&t._removeParent(this)},e.EMPTY=((t=new e).closed=!0,t),e}();function J(e){return e instanceof L||e&&"closed"in e&&I(e.remove)&&I(e.add)&&I(e.unsubscribe)}function Y(e){I(e)?e():e.unsubscribe()}L.EMPTY;var H=void 0,B=function(e,t){for(var n=[],r=2;r<arguments.length;r++)n[r-2]=arguments[r];return setTimeout.apply(void 0,W([e,t],V(n)))};function K(){}var U=function(e){function t(t){var n=e.call(this)||this;return n.isStopped=!1,t?(n.destination=t,J(t)&&t.add(n)):n.destination=Z,n}return R(t,e),t.create=function(e,t,n){return new G(e,t,n)},t.prototype.next=function(e){this.isStopped||this._next(e)},t.prototype.error=function(e){this.isStopped||(this.isStopped=!0,this._error(e))},t.prototype.complete=function(){this.isStopped||(this.isStopped=!0,this._complete())},t.prototype.unsubscribe=function(){this.closed||(this.isStopped=!0,e.prototype.unsubscribe.call(this),this.destination=null)},t.prototype._next=function(e){this.destination.next(e)},t.prototype._error=function(e){try{this.destination.error(e)}finally{this.unsubscribe()}},t.prototype._complete=function(){try{this.destination.complete()}finally{this.unsubscribe()}},t}(L),Q=function(){function e(e){this.partialObserver=e}return e.prototype.next=function(e){var t=this.partialObserver;if(t.next)try{t.next(e)}catch(n){X(n)}},e.prototype.error=function(e){var t=this.partialObserver;if(t.error)try{t.error(e)}catch(n){X(n)}else X(e)},e.prototype.complete=function(){var e=this.partialObserver;if(e.complete)try{e.complete()}catch(t){X(t)}},e}(),G=function(e){function t(t,n,r){var o,i=e.call(this)||this;return o=I(t)||!t?{next:null!=t?t:void 0,error:null!=n?n:void 0,complete:null!=r?r:void 0}:t,i.destination=new Q(o),i}return R(t,e),t}(U);function X(e){var t;t=e,B(function(){throw t})}var Z={closed:!0,next:K,error:function(e){throw e},complete:K},ee="function"==typeof Symbol&&Symbol.observable||"@@observable";function te(e){return e}var ne=function(){function e(e){e&&(this._subscribe=e)}return e.prototype.lift=function(t){var n=new e;return n.source=this,n.operator=t,n},e.prototype.subscribe=function(e,t,n){var r,o=this,i=(r=e)&&r instanceof U||function(e){return e&&I(e.next)&&I(e.error)&&I(e.complete)}(r)&&J(r)?e:new G(e,t,n);return function(){var e=o,t=e.operator,n=e.source;i.add(t?t.call(i,n):n?o._subscribe(i):o._trySubscribe(i))}(),i},e.prototype._trySubscribe=function(e){try{return this._subscribe(e)}catch(t){e.error(t)}},e.prototype.forEach=function(e,t){var n=this;return new(t=re(t))(function(t,r){var o=new G({next:function(t){try{e(t)}catch(n){r(n),o.unsubscribe()}},error:r,complete:t});n.subscribe(o)})},e.prototype._subscribe=function(e){var t;return null===(t=this.source)||void 0===t?void 0:t.subscribe(e)},e.prototype[ee]=function(){return this},e.prototype.pipe=function(){for(var e,t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];return(0===(e=t).length?te:1===e.length?e[0]:function(t){return e.reduce(function(e,t){return t(e)},t)})(this)},e.prototype.toPromise=function(e){var t=this;return new(e=re(e))(function(e,n){var r;t.subscribe(function(e){return r=e},function(e){return n(e)},function(){return e(r)})})},e.create=function(t){return new e(t)},e}();function re(e){var t;return null!==(t=null!=e?e:H)&&void 0!==t?t:Promise}const oe=(e={})=>{const{parentOrigin:t="*",timeout:n,debug:r=!1,config:o={}}=e,i=(e=>(...t)=>{e&&console.log("[Penpal]",...t)})(r),s=((e,t)=>{const n=[];let r=!1;return{destroy(o){r||(r=!0,t(`${e}: Destroying connection`),n.forEach(e=>{e(o)}))},onDestroy(e){r?e():n.push(e)}}})("App",i),{destroy:c,onDestroy:p}=s,d=new Promise((e,r)=>{const d=((e,t)=>{let n;return void 0!==e&&(n=window.setTimeout(()=>{const n=new Error(`Connection timed out after ${e}ms`);n.code=v.ConnectionTimeout,t(n)},e)),()=>{clearTimeout(n)}})(n,c),f=n=>{if((()=>{try{clearTimeout(void 0)}catch(e){return!1}return!0})()&&n.source===parent&&n.data&&n.data.penpal===g.SynAck){const r=n.data,c=new F(r.id,r.appName),p=new F(r.id+"/api",r.appName),h=r.id,g={},y=e=>"api/"+e,v=e=>function(t){const n=y(e);p.$on(n,function e(r){t.next(r),r.reqid&&"succ"===r.result&&(t.complete(),p.$off(n,e))})},m=O({$emit:(e,...t)=>{c.getEventCallBackLength(e)>0?c.$pureEmit(e,...t):null==g||g.$off(e,()=>{},!0)},onQueryResult:e=>{p.$pureEmit(y(e.reqid),e)},onOsNotify:e=>{}}),w=S(t,m,a(l({},o),{postmateVersion:P}),s,i)(n),_={},$=w.bus;if($){const e=(e,t,n)=>{n?(t.$__once=!0,c.$once(e,t)):c.$on(e,t),_[e]?_[e].push(t):(_[e]=[t],$.$on(e,h))};Object.assign(g,$,{$on:e,$off:(e,t,n)=>{var r;if(_[e]){const n=_[e].indexOf(t);n>-1&&(_[e].splice(n,1),c.$off(e,t))}n&&delete _[e],(null==(r=_[e])?void 0:r.length)||($.$off(e,h),delete _[e])},$once:(t,n)=>{e(t,n,!0)}})}const A=a(l({},w),{bus:g,query:(e,t)=>u(null,null,function*(){const n=(null==e?void 0:e.reqid)||(yield w.genReqId());return new Promise((r,o)=>{w.query(a(l({},e),{reqid:n}),t).then(e=>{"succ"===(null==e?void 0:e.result)?r(e):o(e)})})}),createObservableQuery:(e,...t)=>u(null,[e,...t],function*(e,t={}){const n=(null==e?void 0:e.reqid)||(yield w.genReqId()),r=new ne(v(n));return w.query(a(l({},e),{reqid:n}),a(l({},t),{observable:!0})),r})});w&&(window.removeEventListener(b.Message,f),d(),e({methods:A,config:n.data.config}))}};window.addEventListener(b.Message,f),(()=>{i("Child: Handshake - Sending SYN");const e={penpal:g.Syn},n=t instanceof RegExp?"*":t;window.parent.postMessage(e,n)})(),p(e=>{window.removeEventListener(b.Message,f),e&&r(e)})});return{promise:d,destroy(){c()}}};let ie=null;class se{constructor(){c(this,"options"),c(this,"flutterInAppWebView",null),c(this,"osConnector",null),c(this,"initPromise"),c(this,"isWeb",!0);let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};this.options=e,e.debug&&(p.setDebug(!0),p.log("Trim App initialized with debug mode")),p.log("Options:",e),this.initPromise=this.init()}init(){return u(this,null,function*(){return p.log("Starting initialization..."),new Promise(e=>{var t;d()?(this.isWeb=!1,p.log("Initializing mobile platform..."),new Promise(e=>{var t;f||(null==(t=window.flutter_inappwebview)?void 0:t._platformReady)?(p.log("flutter_inappwebview is ready"),e(window.flutter_inappwebview)):window.addEventListener("flutterInAppWebViewPlatformReady",()=>{p.log("flutter_inappwebview is ready"),e(window.flutter_inappwebview)})}).then(t=>{this.flutterInAppWebView=t,p.log("Mobile platform initialized"),e()})):(this.isWeb=!0,p.log("Initializing web platform..."),(ie||(ie=oe({debug:null==t?void 0:t.debug})),ie.promise).then(t=>{this.osConnector=t,p.log("Web platform initialized"),e()}))})})}callAppMethod(e,t){return u(this,null,function*(){return this.flutterInAppWebView?((e,t,n)=>u(null,null,function*(){p.log(`callHandler: ${t}, params: ${n}`);const r=yield e.callHandler(t,n);if(r)try{const e=JSON.parse(r);p.log(`messageData: ${JSON.stringify(e)}`);const t=JSON.parse(e.result);return p.log(`appMessage: ${JSON.stringify(t)}`),t}catch(o){return p.error(`error: ${o}`),null}return null}))(this.flutterInAppWebView,e,t):null})}ready(){return u(this,null,function*(){return this.initPromise})}getOptions(){return this.options}setOptions(e){this.options=l(l({},this.options),e),void 0!==e.debug&&p.setDebug(e.debug),p.log("Options updated:",this.options)}getPlatformConfig(){return u(this,null,function*(){var e;if(yield this.initPromise,this.isWeb){const t=yield null==(e=this.osConnector)?void 0:e.methods.getPlatformConfig();if(!t)throw new Error("Failed to get platform config");return t}{const e=yield this.callAppMethod("getAppMessage");if(!e)throw new Error("Failed to get platform config");return{theme:e.nightMode,language:e.language,appVersion:e.appVersion,systemVersion:e.systemVersion,format:{date:e.dateFormat,time:e.timeFormat}}}})}setTitle(e){return u(this,null,function*(){var t;return yield this.initPromise,p.log("Setting title:",e),this.isWeb?null==(t=this.osConnector)?void 0:t.methods.setTitle(e):this.callAppMethod("updateTitle",e)})}setExitPageTips(e){return u(this,null,function*(){var t;return yield this.initPromise,this.isWeb?null==(t=this.osConnector)?void 0:t.methods.setExitPageTips(e):this.callAppMethod("setExitPageTips",e?JSON.stringify(e):"")})}openFile(e){return u(this,null,function*(){var t;return yield this.initPromise,p.log("Opening file:",e),this.isWeb?null==(t=this.osConnector)?void 0:t.methods.openFile(e):this.callAppMethod("openFile",e)})}openFileManager(e){return u(this,null,function*(){var t;return yield this.initPromise,p.log("Opening file manager:",e),this.isWeb?null==(t=this.osConnector)?void 0:t.methods.openFileManagerApp(e):this.callAppMethod("openFileManager",e)})}openApp(e){return u(this,null,function*(){var t;if(yield this.initPromise,p.log("Opening app:",e),this.isWeb)return null==(t=this.osConnector)?void 0:t.methods.openApp(e);throw new Error("openApp is not supported on mobile platform")})}close(){return u(this,null,function*(){var e;return yield this.initPromise,this.isWeb?null==(e=this.osConnector)?void 0:e.methods.close():this.callAppMethod("exitPage")})}query(e,t){return u(this,null,function*(){var n;if(yield this.initPromise,this.isWeb){const r=yield null==(n=this.osConnector)?void 0:n.methods.query(e,t);if(!r)throw new Error("Failed to query");return r}throw new Error("query is not supported on mobile platform")})}pickFile(e){return u(this,null,function*(){var t;return yield this.initPromise,this.isWeb?null==(t=this.osConnector)?void 0:t.methods.pickFile(e):this.callAppMethod("openFolder",JSON.stringify(e))})}}exports.TrimApp=se,exports.default=se;
|
|
1
|
+
"use strict";var e=Object.defineProperty,t=Object.defineProperties,n=Object.getOwnPropertyDescriptors,r=Object.getOwnPropertySymbols,o=Object.prototype.hasOwnProperty,i=Object.prototype.propertyIsEnumerable,s=(t,n,r)=>n in t?e(t,n,{enumerable:!0,configurable:!0,writable:!0,value:r}):t[n]=r,l=(e,t)=>{for(var n in t||(t={}))o.call(t,n)&&s(e,n,t[n]);if(r)for(var n of r(t))i.call(t,n)&&s(e,n,t[n]);return e},a=(e,r)=>t(e,n(r)),c=(e,t,n)=>s(e,"symbol"!=typeof t?t+"":t,n),u=(e,t,n)=>new Promise((r,o)=>{var i=e=>{try{l(n.next(e))}catch(t){o(t)}},s=e=>{try{l(n.throw(e))}catch(t){o(t)}},l=e=>e.done?r(e.value):Promise.resolve(e.value).then(i,s);l((n=n.apply(e,t)).next())});Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const p=new class{constructor(){c(this,"debug",!1)}setDebug(e){this.debug=e}isDebug(){return this.debug}log(){if(this.debug){for(var e=arguments.length,t=new Array(e),n=0;n<e;n++)t[n]=arguments[n];console.log("[Trim App]",...t)}}warn(){if(this.debug){for(var e=arguments.length,t=new Array(e),n=0;n<e;n++)t[n]=arguments[n];console.warn("[Trim App]",...t)}}error(){if(this.debug){for(var e=arguments.length,t=new Array(e),n=0;n<e;n++)t[n]=arguments[n];console.error("[Trim App]",...t)}}info(){if(this.debug){for(var e=arguments.length,t=new Array(e),n=0;n<e;n++)t[n]=arguments[n];console.info("[Trim App]",...t)}}};const d=()=>function(e){const t=/FNOS\/([\d.]+)|FNAppType\/(\w+)|FNAppVer\/([\d.]+)/g,n={};let r;for(;null!==(r=t.exec(e));)r[1]&&(n.fnOSVersion=r[1]),r[2]&&(n.fnAppType=r[2]),r[3]&&(n.fnAppVersion=r[3]);return n}(navigator.userAgent).fnAppVersion;let h=!1;window.addEventListener("flutterInAppWebViewPlatformReady",()=>{h=!0});const f=new Map;d()&&(window.fnAppMessage=e=>{p.log("app-message",e);const t=JSON.parse(e),n=f.get(t.reqId);n&&(n(t),f.delete(t.reqId))});var g=(e=>(e.Call="call",e.Reply="reply",e.Syn="syn",e.SynAck="synAck",e.Ack="ack",e))(g||{}),y=(e=>(e.Fulfilled="fulfilled",e.Rejected="rejected",e))(y||{}),m=(e=>(e.ConnectionDestroyed="ConnectionDestroyed",e.ConnectionTimeout="ConnectionTimeout",e.NoIframeSrc="NoIframeSrc",e))(m||{}),v=(e=>(e.DataCloneError="DataCloneError",e))(v||{}),b=(e=>(e.Message="message",e))(b||{});const w=({name:e,message:t,stack:n})=>({name:e,message:t,stack:n});let $=0;const _=e=>e?e.split("."):[],A=(e,t,n)=>{const r=_(t);return r.reduce((e,t,o)=>(void 0===e[t]&&(e[t]={}),o===r.length-1&&(e[t]=n),e[t]),e),e},E=(e,t)=>{const n={};return Object.keys(e).forEach(r=>{const o=e[r],i=((e,t)=>{const n=_(t||"");return n.push(e),(e=>e.join("."))(n)})(r,t);"object"==typeof o&&Object.assign(n,E(o,i)),"function"==typeof o&&(n[i]=o)}),n},O=(e,t,n,r,o)=>{const{localName:i,local:s,remote:l,originForSending:a,originForReceiving:c}=t;let u=!1;o(`${i}: Connecting call sender`);const p=e=>(...t)=>{let n;o(`${i}: Sending ${e}() call`);try{l.closed&&(n=!0)}catch(p){n=!0}if(n&&r(),u){const t=new Error(`Unable to send ${e}() call due to destroyed connection`);throw t.code=m.ConnectionDestroyed,t}return new Promise((n,r)=>{const u=++$,p=t=>{if(t.source!==l||t.data.penpal!==g.Reply||t.data.id!==u)return;if("*"!==c&&t.origin!==c)return void o(`${i} received message from origin ${t.origin} which did not match expected origin ${c}`);const a=t.data;o(`${i}: Received ${e}() reply`),s.removeEventListener(b.Message,p);let d=a.returnValue;a.returnValueIsError&&(d=(e=>{const t=new Error;return Object.keys(e).forEach(n=>t[n]=e[n]),t})(d)),(a.resolution===y.Fulfilled?n:r)(d)};s.addEventListener(b.Message,p);const d={penpal:g.Call,id:u,methodName:e,args:t};l.postMessage(d,a)})},d=n.reduce((e,t)=>(e[t]=p(t),e),{});return Object.assign(e,(e=>{const t={};for(const n in e)A(t,n,e[n]);return t})(d)),()=>{u=!0}},P="0.1.2",S=(e,t,n,r,o)=>{const{destroy:i,onDestroy:s}=r;return r=>{if(!(e instanceof RegExp?e.test(r.origin):"*"===e||e===r.origin))return void o(`Child: Handshake - Received SYN-ACK from origin ${r.origin} which did not match expected origin ${e}`);o("Child: Handshake - Received SYN-ACK, responding with ACK");const l="null"===r.origin?"*":r.origin,a={penpal:g.Ack,methodNames:Object.keys(t),config:n};window.parent.postMessage(a,l);const c={localName:"Child",local:window,remote:window.parent,originForSending:l,originForReceiving:r.origin},u=((e,t,n)=>{const{localName:r,local:o,remote:i,originForSending:s,originForReceiving:l}=e;let a=!1;const c=e=>{if(e.source!==i||e.data.penpal!==g.Call)return;if("*"!==l&&e.origin!==l)return void n(`${r} received message from origin ${e.origin} which did not match expected origin ${l}`);const o=e.data,{methodName:c,args:u,id:p}=o;n(`${r}: Received ${c}() call`);const d=e=>t=>{if(n(`${r}: Sending ${c}() reply`),a)return void n(`${r}: Unable to send ${c}() reply due to destroyed connection`);const o={penpal:g.Reply,id:p,resolution:e,returnValue:t};e===y.Rejected&&t instanceof Error&&(o.returnValue=w(t),o.returnValueIsError=!0);try{i.postMessage(o,s)}catch(l){if(l.name===v.DataCloneError){const e={penpal:g.Reply,id:p,resolution:y.Rejected,returnValue:w(l),returnValueIsError:!0};i.postMessage(e,s)}throw l}};new Promise(e=>e(t[c].apply(t,u))).then(d(y.Fulfilled),d(y.Rejected))};return o.addEventListener(b.Message,c),()=>{a=!0,o.removeEventListener(b.Message,c)}})(c,t,o);s(u);const p={},d=O(p,c,r.data.methodNames,i,o);return s(d),p}};function C(e,t){null==console||console.warn(`[fnApp warn]: ${e}`,t)}function M(e,t){null==console||console.error(`[fnApp error]: ${e}`,t)}const x="_fn_all_event",j="事件订阅数量为空",N=window.__POWERED_BY_FNAPP__?window.__FNAPP.inject.appEventObjMap:new Map;class F{constructor(e,t){c(this,"id"),c(this,"appName"),c(this,"eventObj"),c(this,"getEventCallBackLength",e=>{var t;return(null==(t=this.eventObj[e])?void 0:t.length)||0}),this.id=e,this.appName=t,this.$clear(),N.get(this.id)||N.set(this.id,{}),this.eventObj=N.get(this.id)}$on(e,t){const n=this.eventObj[e];return n?(n.includes(t)||n.push(t),this):(this.eventObj[e]=[t],this)}$onAll(e){return this.$on(x,e)}$once(e,t){t.$__once=!0,this.$on(e,t)}$off(e,t){const n=this.eventObj[e];if(!e||!n||!n.length)return C(`${e} ${j}`),this;let r,o=n.length;for(;o--;)if(r=n[o],r===t){n.splice(o,1);break}return this}$offAll(e){return this.$off(x,e)}$emit(e,...t){return e=`${this.appName}/${e}`,this.$pureEmit(e,...t)}$pureEmit(e,...t){let n=0;return N.forEach(r=>{if(r[e])for(let i=r[e].length-1;i>=0;i--){n++;const s=r[e][i];s.$__once&&r[e].splice(i,1);try{s(...t)}catch(o){M(o)}}if(r[x])for(let e=r[x].length-1;e>=0;e--){n++;const i=r[x][e];i.$__once&&r[x].splice(e,1);try{i(...t)}catch(o){M(o)}}}),e&&0!==n||C(`${e} ${j}`),this}$clear(){const e=N.get(this.id)||{};return Object.keys(e).forEach(t=>delete e[t]),this}}var T=function(e,t){return(T=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n])})(e,t)};function R(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Class extends value "+String(t)+" is not a constructor or null");function n(){this.constructor=e}T(e,t),e.prototype=null===t?Object.create(t):(n.prototype=t.prototype,new n)}function W(e){var t="function"==typeof Symbol&&Symbol.iterator,n=t&&e[t],r=0;if(n)return n.call(e);if(e&&"number"==typeof e.length)return{next:function(){return e&&r>=e.length&&(e=void 0),{value:e&&e[r++],done:!e}}};throw new TypeError(t?"Object is not iterable.":"Symbol.iterator is not defined.")}function k(e,t){var n="function"==typeof Symbol&&e[Symbol.iterator];if(!n)return e;var r,o,i=n.call(e),s=[];try{for(;(void 0===t||t-- >0)&&!(r=i.next()).done;)s.push(r.value)}catch(l){o={error:l}}finally{try{r&&!r.done&&(n=i.return)&&n.call(i)}finally{if(o)throw o.error}}return s}function V(e,t,n){if(n||2===arguments.length)for(var r,o=0,i=t.length;o<i;o++)!r&&o in t||(r||(r=Array.prototype.slice.call(t,0,o)),r[o]=t[o]);return e.concat(r||Array.prototype.slice.call(t))}function I(e){return"function"==typeof e}"function"==typeof SuppressedError&&SuppressedError;var q,D=((q=function(e){return function(t){e(this),this.message=t?t.length+" errors occurred during unsubscription:\n"+t.map(function(e,t){return t+1+") "+e.toString()}).join("\n "):"",this.name="UnsubscriptionError",this.errors=t}}(function(e){Error.call(e),e.stack=(new Error).stack})).prototype=Object.create(Error.prototype),q.prototype.constructor=q,q);function L(e,t){if(e){var n=e.indexOf(t);0<=n&&e.splice(n,1)}}var z=function(){function e(e){this.initialTeardown=e,this.closed=!1,this._parentage=null,this._finalizers=null}var t;return e.prototype.unsubscribe=function(){var e,t,n,r,o;if(!this.closed){this.closed=!0;var i=this._parentage;if(i)if(this._parentage=null,Array.isArray(i))try{for(var s=W(i),l=s.next();!l.done;l=s.next()){l.value.remove(this)}}catch(h){e={error:h}}finally{try{l&&!l.done&&(t=s.return)&&t.call(s)}finally{if(e)throw e.error}}else i.remove(this);var a=this.initialTeardown;if(I(a))try{a()}catch(f){o=f instanceof D?f.errors:[f]}var c=this._finalizers;if(c){this._finalizers=null;try{for(var u=W(c),p=u.next();!p.done;p=u.next()){var d=p.value;try{Y(d)}catch(g){o=null!=o?o:[],g instanceof D?o=V(V([],k(o)),k(g.errors)):o.push(g)}}}catch(y){n={error:y}}finally{try{p&&!p.done&&(r=u.return)&&r.call(u)}finally{if(n)throw n.error}}}if(o)throw new D(o)}},e.prototype.add=function(t){var n;if(t&&t!==this)if(this.closed)Y(t);else{if(t instanceof e){if(t.closed||t._hasParent(this))return;t._addParent(this)}(this._finalizers=null!==(n=this._finalizers)&&void 0!==n?n:[]).push(t)}},e.prototype._hasParent=function(e){var t=this._parentage;return t===e||Array.isArray(t)&&t.includes(e)},e.prototype._addParent=function(e){var t=this._parentage;this._parentage=Array.isArray(t)?(t.push(e),t):t?[t,e]:e},e.prototype._removeParent=function(e){var t=this._parentage;t===e?this._parentage=null:Array.isArray(t)&&L(t,e)},e.prototype.remove=function(t){var n=this._finalizers;n&&L(n,t),t instanceof e&&t._removeParent(this)},e.EMPTY=((t=new e).closed=!0,t),e}();function J(e){return e instanceof z||e&&"closed"in e&&I(e.remove)&&I(e.add)&&I(e.unsubscribe)}function Y(e){I(e)?e():e.unsubscribe()}z.EMPTY;var H=void 0,U=function(e,t){for(var n=[],r=2;r<arguments.length;r++)n[r-2]=arguments[r];return setTimeout.apply(void 0,V([e,t],k(n)))};function B(){}var K=function(e){function t(t){var n=e.call(this)||this;return n.isStopped=!1,t?(n.destination=t,J(t)&&t.add(n)):n.destination=Z,n}return R(t,e),t.create=function(e,t,n){return new G(e,t,n)},t.prototype.next=function(e){this.isStopped||this._next(e)},t.prototype.error=function(e){this.isStopped||(this.isStopped=!0,this._error(e))},t.prototype.complete=function(){this.isStopped||(this.isStopped=!0,this._complete())},t.prototype.unsubscribe=function(){this.closed||(this.isStopped=!0,e.prototype.unsubscribe.call(this),this.destination=null)},t.prototype._next=function(e){this.destination.next(e)},t.prototype._error=function(e){try{this.destination.error(e)}finally{this.unsubscribe()}},t.prototype._complete=function(){try{this.destination.complete()}finally{this.unsubscribe()}},t}(z),Q=function(){function e(e){this.partialObserver=e}return e.prototype.next=function(e){var t=this.partialObserver;if(t.next)try{t.next(e)}catch(n){X(n)}},e.prototype.error=function(e){var t=this.partialObserver;if(t.error)try{t.error(e)}catch(n){X(n)}else X(e)},e.prototype.complete=function(){var e=this.partialObserver;if(e.complete)try{e.complete()}catch(t){X(t)}},e}(),G=function(e){function t(t,n,r){var o,i=e.call(this)||this;return o=I(t)||!t?{next:null!=t?t:void 0,error:null!=n?n:void 0,complete:null!=r?r:void 0}:t,i.destination=new Q(o),i}return R(t,e),t}(K);function X(e){var t;t=e,U(function(){throw t})}var Z={closed:!0,next:B,error:function(e){throw e},complete:B},ee="function"==typeof Symbol&&Symbol.observable||"@@observable";function te(e){return e}var ne=function(){function e(e){e&&(this._subscribe=e)}return e.prototype.lift=function(t){var n=new e;return n.source=this,n.operator=t,n},e.prototype.subscribe=function(e,t,n){var r,o=this,i=(r=e)&&r instanceof K||function(e){return e&&I(e.next)&&I(e.error)&&I(e.complete)}(r)&&J(r)?e:new G(e,t,n);return function(){var e=o,t=e.operator,n=e.source;i.add(t?t.call(i,n):n?o._subscribe(i):o._trySubscribe(i))}(),i},e.prototype._trySubscribe=function(e){try{return this._subscribe(e)}catch(t){e.error(t)}},e.prototype.forEach=function(e,t){var n=this;return new(t=re(t))(function(t,r){var o=new G({next:function(t){try{e(t)}catch(n){r(n),o.unsubscribe()}},error:r,complete:t});n.subscribe(o)})},e.prototype._subscribe=function(e){var t;return null===(t=this.source)||void 0===t?void 0:t.subscribe(e)},e.prototype[ee]=function(){return this},e.prototype.pipe=function(){for(var e,t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];return(0===(e=t).length?te:1===e.length?e[0]:function(t){return e.reduce(function(e,t){return t(e)},t)})(this)},e.prototype.toPromise=function(e){var t=this;return new(e=re(e))(function(e,n){var r;t.subscribe(function(e){return r=e},function(e){return n(e)},function(){return e(r)})})},e.create=function(t){return new e(t)},e}();function re(e){var t;return null!==(t=null!=e?e:H)&&void 0!==t?t:Promise}const oe=(e={})=>{const{parentOrigin:t="*",timeout:n,debug:r=!1,config:o={}}=e,i=(e=>(...t)=>{e&&console.log("[Penpal]",...t)})(r),s=((e,t)=>{const n=[];let r=!1;return{destroy(o){r||(r=!0,t(`${e}: Destroying connection`),n.forEach(e=>{e(o)}))},onDestroy(e){r?e():n.push(e)}}})("App",i),{destroy:c,onDestroy:p}=s,d=new Promise((e,r)=>{const d=((e,t)=>{let n;return void 0!==e&&(n=window.setTimeout(()=>{const n=new Error(`Connection timed out after ${e}ms`);n.code=m.ConnectionTimeout,t(n)},e)),()=>{clearTimeout(n)}})(n,c),h=n=>{if((()=>{try{clearTimeout(void 0)}catch(e){return!1}return!0})()&&n.source===parent&&n.data&&n.data.penpal===g.SynAck){const r=n.data,c=new F(r.id,r.appName),p=new F(r.id+"/api",r.appName),f=r.id,g={},y=e=>"api/"+e,m=e=>function(t){const n=y(e);p.$on(n,function e(r){t.next(r),r.reqid&&"succ"===r.result&&(t.complete(),p.$off(n,e))})},v=E({$emit:(e,...t)=>{c.getEventCallBackLength(e)>0?c.$pureEmit(e,...t):null==g||g.$off(e,()=>{},!0)},onQueryResult:e=>{p.$pureEmit(y(e.reqid),e)},onOsNotify:e=>{}}),w=S(t,v,a(l({},o),{postmateVersion:P}),s,i)(n),$={},_=w.bus;if(_){const e=(e,t,n)=>{n?(t.$__once=!0,c.$once(e,t)):c.$on(e,t),$[e]?$[e].push(t):($[e]=[t],_.$on(e,f))};Object.assign(g,_,{$on:e,$off:(e,t,n)=>{var r;if($[e]){const n=$[e].indexOf(t);n>-1&&($[e].splice(n,1),c.$off(e,t))}n&&delete $[e],(null==(r=$[e])?void 0:r.length)||(_.$off(e,f),delete $[e])},$once:(t,n)=>{e(t,n,!0)}})}const A=a(l({},w),{bus:g,query:(e,t)=>u(null,null,function*(){const n=(null==e?void 0:e.reqid)||(yield w.genReqId());return new Promise((r,o)=>{w.query(a(l({},e),{reqid:n}),t).then(e=>{"succ"===(null==e?void 0:e.result)?r(e):o(e)})})}),createObservableQuery:(e,...t)=>u(null,[e,...t],function*(e,t={}){const n=(null==e?void 0:e.reqid)||(yield w.genReqId()),r=new ne(m(n));return w.query(a(l({},e),{reqid:n}),a(l({},t),{observable:!0})),r})});w&&(window.removeEventListener(b.Message,h),d(),e({methods:A,config:n.data.config}))}};window.addEventListener(b.Message,h),(()=>{i("Child: Handshake - Sending SYN");const e={penpal:g.Syn},n=t instanceof RegExp?"*":t;window.parent.postMessage(e,n)})(),p(e=>{window.removeEventListener(b.Message,h),e&&r(e)})});return{promise:d,destroy(){c()}}};let ie=null;class se{constructor(){c(this,"options"),c(this,"flutterInAppWebView",null),c(this,"osConnector",null),c(this,"initPromise"),c(this,"isWeb",!0);let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};this.options=e,e.debug&&(p.setDebug(!0),p.log("Trim App initialized with debug mode")),p.log("Options:",e),this.initPromise=this.init()}init(){return u(this,null,function*(){return p.log("Starting initialization..."),new Promise(e=>{var t;d()?(this.isWeb=!1,p.log("Initializing mobile platform..."),new Promise(e=>{var t;h||(null==(t=window.flutter_inappwebview)?void 0:t._platformReady)?(p.log("flutter_inappwebview is ready"),e(window.flutter_inappwebview)):window.addEventListener("flutterInAppWebViewPlatformReady",()=>{p.log("flutter_inappwebview is ready"),e(window.flutter_inappwebview)})}).then(t=>{this.flutterInAppWebView=t,p.log("Mobile platform initialized"),e()})):(this.isWeb=!0,p.log("Initializing web platform..."),(ie||(ie=oe({debug:null==t?void 0:t.debug})),ie.promise).then(t=>{this.osConnector=t,p.log("Web platform initialized"),e()}))})})}callAppMethod(e,t){return u(this,null,function*(){return this.flutterInAppWebView?((e,t,n)=>u(null,null,function*(){p.log(`callHandler: ${t}, params: ${n}`);const r=yield e.callHandler(t,n);if(r)try{const e=JSON.parse(r);p.log(`messageData: ${JSON.stringify(e)}`);const t=JSON.parse(e.result);return p.log(`appMessage: ${JSON.stringify(t)}`),t}catch(o){return p.error(`error: ${o}`),null}return null}))(this.flutterInAppWebView,e,t):null})}ready(){return u(this,null,function*(){return this.initPromise})}getOptions(){return this.options}setOptions(e){this.options=l(l({},this.options),e),void 0!==e.debug&&p.setDebug(e.debug),p.log("Options updated:",this.options)}getPlatformConfig(){return u(this,null,function*(){var e;if(yield this.initPromise,this.isWeb){const t=yield null==(e=this.osConnector)?void 0:e.methods.getPlatformConfig();if(!t)throw new Error("Failed to get platform config");return t}{const e=yield this.callAppMethod("getAppMessage");if(!e)throw new Error("Failed to get platform config");return{theme:e.nightMode,language:e.language,appVersion:e.appVersion,systemVersion:e.systemVersion,format:{date:e.dateFormat,time:e.timeFormat}}}})}setTitle(e){return u(this,null,function*(){var t;return yield this.initPromise,p.log("Setting title:",e),this.isWeb?null==(t=this.osConnector)?void 0:t.methods.setTitle(e):this.callAppMethod("updateTitle",e)})}setExitPageTips(e){return u(this,null,function*(){var t;return yield this.initPromise,this.isWeb?null==(t=this.osConnector)?void 0:t.methods.setExitPageTips(e):this.callAppMethod("setExitPageTips",e?JSON.stringify(e):"")})}openFile(e){return u(this,null,function*(){var t;return yield this.initPromise,p.log("Opening file:",e),this.isWeb?null==(t=this.osConnector)?void 0:t.methods.openFile(e):this.callAppMethod("openFile",e)})}openFileManager(e){return u(this,null,function*(){var t;return yield this.initPromise,p.log("Opening file manager:",e),this.isWeb?null==(t=this.osConnector)?void 0:t.methods.openFileManagerApp(e):this.callAppMethod("openFileManager",e)})}openApp(e){return u(this,null,function*(){var t;if(yield this.initPromise,p.log("Opening app:",e),this.isWeb)return null==(t=this.osConnector)?void 0:t.methods.openApp(e);throw new Error("openApp is not supported on mobile platform")})}openCustomApp(e,t){return u(this,null,function*(){var n;if(yield this.initPromise,p.log("Opening custom app:",e,t),this.isWeb)return null==(n=this.osConnector)?void 0:n.methods.openCustomApp(e,t);throw new Error("openCustomApp is not supported on mobile platform")})}openURL(e,t,n){return u(this,null,function*(){if(yield this.initPromise,p.log("Opening URL:",e,t,n),!this.isWeb)return this.callAppMethod("openSystemBrowser",e);if(!n)return void window.open(e,t,n);const r=window.open("",t,n);r&&(r.location.href=e)})}close(){return u(this,null,function*(){var e;return yield this.initPromise,this.isWeb?null==(e=this.osConnector)?void 0:e.methods.close():this.callAppMethod("exitPage")})}query(e,t){return u(this,null,function*(){var n;if(yield this.initPromise,this.isWeb){const r=yield null==(n=this.osConnector)?void 0:n.methods.query(e,t);if(!r)throw new Error("Failed to query");return r}throw new Error("query is not supported on mobile platform")})}$on(e,t){return u(this,null,function*(){var n;if(yield this.initPromise,!this.isWeb)throw new Error("$on is not supported on mobile platform");(null==(n=this.osConnector)?void 0:n.methods.bus).$on(e,t)})}$off(e,t){return u(this,null,function*(){var n;if(yield this.initPromise,!this.isWeb)throw new Error("$off is not supported on mobile platform");(null==(n=this.osConnector)?void 0:n.methods.bus).$off(e,t)})}$once(e,t){return u(this,null,function*(){var n;if(yield this.initPromise,!this.isWeb)throw new Error("$once is not supported on mobile platform");(null==(n=this.osConnector)?void 0:n.methods.bus).$once(e,t)})}pickFile(e){return u(this,null,function*(){var t;return yield this.initPromise,this.isWeb?null==(t=this.osConnector)?void 0:t.methods.pickFile(e):this.callAppMethod("openFolder",JSON.stringify(e))})}}exports.TrimApp=se,exports.default=se;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,30 @@
|
|
|
1
|
+
declare interface App {
|
|
2
|
+
name: string; // 应用名(展示名)
|
|
3
|
+
icon: string; // 图标
|
|
4
|
+
headerIconVisible?: boolean; // 应用头部是否显示图标
|
|
5
|
+
appName: string; // 应用名标志: 系统应用唯一标志
|
|
6
|
+
serviceName?: string; // 应用名标志:服务名
|
|
7
|
+
alias?: string; // 别名, 获取应用设置时优先采用别名,用于多开等的情况
|
|
8
|
+
type: AppType; // 应用类型
|
|
9
|
+
componentUrl?: string;
|
|
10
|
+
iframe?: boolean; // 是否是iframe
|
|
11
|
+
microApp?: boolean; // 与飞牛基座适配的微应用
|
|
12
|
+
url?: string; // 应用的url(非内置应用的类型)
|
|
13
|
+
description?: string; // 应用描述
|
|
14
|
+
multiple?: boolean; // 是否支持多开
|
|
15
|
+
hiddenDesktopIcon?: boolean; // 是否隐藏桌面图标
|
|
16
|
+
enableSettings?: boolean; // 是否开启设置,默认为false
|
|
17
|
+
enableMaximize?: boolean; // 是否允许最大化
|
|
18
|
+
width?: number;
|
|
19
|
+
style?: any;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
declare enum AppType {
|
|
23
|
+
INBUILT = "inbuilt", // 内置应用
|
|
24
|
+
THIRD_PARTY = "third-party", // 第三方
|
|
25
|
+
TEMPORARY = "temporary", // 临时的应用,非常规应用,不会出现在应用列表中,关闭后会被销毁
|
|
26
|
+
}
|
|
27
|
+
|
|
1
28
|
declare interface FilePickerParams {
|
|
2
29
|
/**
|
|
3
30
|
* 是否多选
|
|
@@ -87,6 +114,14 @@ declare interface FilePickerParams {
|
|
|
87
114
|
okText?: string;
|
|
88
115
|
}
|
|
89
116
|
|
|
117
|
+
declare interface OpenAppParams {
|
|
118
|
+
custom?: {
|
|
119
|
+
name?: string;
|
|
120
|
+
};
|
|
121
|
+
app?: App; // 自定义的应用
|
|
122
|
+
params?: { [key: string]: any }; // 打开应用时带入的data参数,
|
|
123
|
+
}
|
|
124
|
+
|
|
90
125
|
declare interface PlatformConfig {
|
|
91
126
|
theme: "dark" | "light";
|
|
92
127
|
language: string;
|
|
@@ -183,6 +218,17 @@ declare class TrimApp {
|
|
|
183
218
|
* @param anchor 锚点
|
|
184
219
|
*/
|
|
185
220
|
openApp(anchor: string): Promise<void | undefined>;
|
|
221
|
+
/**
|
|
222
|
+
* 打开自定义应用
|
|
223
|
+
* @param appName 自定义应用标识
|
|
224
|
+
* @param options 自定义应用配置和打开参数
|
|
225
|
+
*/
|
|
226
|
+
openCustomApp(appName: string, options: OpenAppParams): Promise<void | undefined>;
|
|
227
|
+
/**
|
|
228
|
+
* 打开外部链接
|
|
229
|
+
* App 端通过系统浏览器打开,Web 端沿用 window.open 行为
|
|
230
|
+
*/
|
|
231
|
+
openURL(url: string, target?: string, features?: string): Promise<unknown>;
|
|
186
232
|
/**
|
|
187
233
|
* 关闭应用
|
|
188
234
|
*/
|
|
@@ -191,6 +237,18 @@ declare class TrimApp {
|
|
|
191
237
|
* 请求接口
|
|
192
238
|
*/
|
|
193
239
|
query<T>(params: any, config?: QueryConfig): Promise<ResponseData<T>>;
|
|
240
|
+
/**
|
|
241
|
+
* 监听事件(仅 Web 平台)
|
|
242
|
+
*/
|
|
243
|
+
$on(event: string, cb: (...args: any[]) => void): Promise<void>;
|
|
244
|
+
/**
|
|
245
|
+
* 取消监听事件(仅 Web 平台)
|
|
246
|
+
*/
|
|
247
|
+
$off(event: string, cb: (...args: any[]) => void): Promise<void>;
|
|
248
|
+
/**
|
|
249
|
+
* 监听一次事件(仅 Web 平台)
|
|
250
|
+
*/
|
|
251
|
+
$once(event: string, cb: (...args: any[]) => void): Promise<void>;
|
|
194
252
|
/**
|
|
195
253
|
* 选择文件
|
|
196
254
|
*/
|
package/dist/index.iife.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var TrimApp=function(e){"use strict";var t=Object.defineProperty,n=Object.defineProperties,r=Object.getOwnPropertyDescriptors,o=Object.getOwnPropertySymbols,i=Object.prototype.hasOwnProperty,s=Object.prototype.propertyIsEnumerable,l=(e,n,r)=>n in e?t(e,n,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[n]=r,a=(e,t)=>{for(var n in t||(t={}))i.call(t,n)&&l(e,n,t[n]);if(o)for(var n of o(t))s.call(t,n)&&l(e,n,t[n]);return e},c=(e,t)=>n(e,r(t)),u=(e,t,n)=>l(e,"symbol"!=typeof t?t+"":t,n),p=(e,t,n)=>new Promise((r,o)=>{var i=e=>{try{l(n.next(e))}catch(t){o(t)}},s=e=>{try{l(n.throw(e))}catch(t){o(t)}},l=e=>e.done?r(e.value):Promise.resolve(e.value).then(i,s);l((n=n.apply(e,t)).next())});const d=new class{constructor(){u(this,"debug",!1)}setDebug(e){this.debug=e}isDebug(){return this.debug}log(){if(this.debug){for(var e=arguments.length,t=new Array(e),n=0;n<e;n++)t[n]=arguments[n];console.log("[Trim App]",...t)}}warn(){if(this.debug){for(var e=arguments.length,t=new Array(e),n=0;n<e;n++)t[n]=arguments[n];console.warn("[Trim App]",...t)}}error(){if(this.debug){for(var e=arguments.length,t=new Array(e),n=0;n<e;n++)t[n]=arguments[n];console.error("[Trim App]",...t)}}info(){if(this.debug){for(var e=arguments.length,t=new Array(e),n=0;n<e;n++)t[n]=arguments[n];console.info("[Trim App]",...t)}}};const f=()=>function(e){const t=/FNOS\/([\d.]+)|FNAppType\/(\w+)|FNAppVer\/([\d.]+)/g,n={};let r;for(;null!==(r=t.exec(e));)r[1]&&(n.fnOSVersion=r[1]),r[2]&&(n.fnAppType=r[2]),r[3]&&(n.fnAppVersion=r[3]);return n}(navigator.userAgent).fnAppVersion;let h=!1;window.addEventListener("flutterInAppWebViewPlatformReady",()=>{h=!0});const g=new Map;f()&&(window.fnAppMessage=e=>{d.log("app-message",e);const t=JSON.parse(e),n=g.get(t.reqId);n&&(n(t),g.delete(t.reqId))});var y=(e=>(e.Call="call",e.Reply="reply",e.Syn="syn",e.SynAck="synAck",e.Ack="ack",e))(y||{}),v=(e=>(e.Fulfilled="fulfilled",e.Rejected="rejected",e))(v||{}),m=(e=>(e.ConnectionDestroyed="ConnectionDestroyed",e.ConnectionTimeout="ConnectionTimeout",e.NoIframeSrc="NoIframeSrc",e))(m||{}),b=(e=>(e.DataCloneError="DataCloneError",e))(b||{}),w=(e=>(e.Message="message",e))(w||{});const _=({name:e,message:t,stack:n})=>({name:e,message:t,stack:n});let $=0;const A=e=>e?e.split("."):[],O=(e,t,n)=>{const r=A(t);return r.reduce((e,t,o)=>(void 0===e[t]&&(e[t]={}),o===r.length-1&&(e[t]=n),e[t]),e),e},E=(e,t)=>{const n={};return Object.keys(e).forEach(r=>{const o=e[r],i=((e,t)=>{const n=A(t||"");return n.push(e),(e=>e.join("."))(n)})(r,t);"object"==typeof o&&Object.assign(n,E(o,i)),"function"==typeof o&&(n[i]=o)}),n},P=(e,t,n,r,o)=>{const{localName:i,local:s,remote:l,originForSending:a,originForReceiving:c}=t;let u=!1;o(`${i}: Connecting call sender`);const p=e=>(...t)=>{let n;o(`${i}: Sending ${e}() call`);try{l.closed&&(n=!0)}catch(p){n=!0}if(n&&r(),u){const t=new Error(`Unable to send ${e}() call due to destroyed connection`);throw t.code=m.ConnectionDestroyed,t}return new Promise((n,r)=>{const u=++$,p=t=>{if(t.source!==l||t.data.penpal!==y.Reply||t.data.id!==u)return;if("*"!==c&&t.origin!==c)return void o(`${i} received message from origin ${t.origin} which did not match expected origin ${c}`);const a=t.data;o(`${i}: Received ${e}() reply`),s.removeEventListener(w.Message,p);let d=a.returnValue;a.returnValueIsError&&(d=(e=>{const t=new Error;return Object.keys(e).forEach(n=>t[n]=e[n]),t})(d)),(a.resolution===v.Fulfilled?n:r)(d)};s.addEventListener(w.Message,p);const d={penpal:y.Call,id:u,methodName:e,args:t};l.postMessage(d,a)})},d=n.reduce((e,t)=>(e[t]=p(t),e),{});return Object.assign(e,(e=>{const t={};for(const n in e)O(t,n,e[n]);return t})(d)),()=>{u=!0}},S="0.1.2",C=(e,t,n,r,o)=>{const{destroy:i,onDestroy:s}=r;return r=>{if(!(e instanceof RegExp?e.test(r.origin):"*"===e||e===r.origin))return void o(`Child: Handshake - Received SYN-ACK from origin ${r.origin} which did not match expected origin ${e}`);o("Child: Handshake - Received SYN-ACK, responding with ACK");const l="null"===r.origin?"*":r.origin,a={penpal:y.Ack,methodNames:Object.keys(t),config:n};window.parent.postMessage(a,l);const c={localName:"Child",local:window,remote:window.parent,originForSending:l,originForReceiving:r.origin},u=((e,t,n)=>{const{localName:r,local:o,remote:i,originForSending:s,originForReceiving:l}=e;let a=!1;const c=e=>{if(e.source!==i||e.data.penpal!==y.Call)return;if("*"!==l&&e.origin!==l)return void n(`${r} received message from origin ${e.origin} which did not match expected origin ${l}`);const o=e.data,{methodName:c,args:u,id:p}=o;n(`${r}: Received ${c}() call`);const d=e=>t=>{if(n(`${r}: Sending ${c}() reply`),a)return void n(`${r}: Unable to send ${c}() reply due to destroyed connection`);const o={penpal:y.Reply,id:p,resolution:e,returnValue:t};e===v.Rejected&&t instanceof Error&&(o.returnValue=_(t),o.returnValueIsError=!0);try{i.postMessage(o,s)}catch(l){if(l.name===b.DataCloneError){const e={penpal:y.Reply,id:p,resolution:v.Rejected,returnValue:_(l),returnValueIsError:!0};i.postMessage(e,s)}throw l}};new Promise(e=>e(t[c].apply(t,u))).then(d(v.Fulfilled),d(v.Rejected))};return o.addEventListener(w.Message,c),()=>{a=!0,o.removeEventListener(w.Message,c)}})(c,t,o);s(u);const p={},d=P(p,c,r.data.methodNames,i,o);return s(d),p}};function M(e,t){null==console||console.warn(`[fnApp warn]: ${e}`,t)}function j(e,t){null==console||console.error(`[fnApp error]: ${e}`,t)}const x="_fn_all_event",N="事件订阅数量为空",T=window.__POWERED_BY_FNAPP__?window.__FNAPP.inject.appEventObjMap:new Map;class F{constructor(e,t){u(this,"id"),u(this,"appName"),u(this,"eventObj"),u(this,"getEventCallBackLength",e=>{var t;return(null==(t=this.eventObj[e])?void 0:t.length)||0}),this.id=e,this.appName=t,this.$clear(),T.get(this.id)||T.set(this.id,{}),this.eventObj=T.get(this.id)}$on(e,t){const n=this.eventObj[e];return n?(n.includes(t)||n.push(t),this):(this.eventObj[e]=[t],this)}$onAll(e){return this.$on(x,e)}$once(e,t){t.$__once=!0,this.$on(e,t)}$off(e,t){const n=this.eventObj[e];if(!e||!n||!n.length)return M(`${e} ${N}`),this;let r,o=n.length;for(;o--;)if(r=n[o],r===t){n.splice(o,1);break}return this}$offAll(e){return this.$off(x,e)}$emit(e,...t){return e=`${this.appName}/${e}`,this.$pureEmit(e,...t)}$pureEmit(e,...t){let n=0;return T.forEach(r=>{if(r[e])for(let i=r[e].length-1;i>=0;i--){n++;const s=r[e][i];s.$__once&&r[e].splice(i,1);try{s(...t)}catch(o){j(o)}}if(r[x])for(let e=r[x].length-1;e>=0;e--){n++;const i=r[x][e];i.$__once&&r[x].splice(e,1);try{i(...t)}catch(o){j(o)}}}),e&&0!==n||M(`${e} ${N}`),this}$clear(){const e=T.get(this.id)||{};return Object.keys(e).forEach(t=>delete e[t]),this}}var R=function(e,t){return(R=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n])})(e,t)};function k(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Class extends value "+String(t)+" is not a constructor or null");function n(){this.constructor=e}R(e,t),e.prototype=null===t?Object.create(t):(n.prototype=t.prototype,new n)}function V(e){var t="function"==typeof Symbol&&Symbol.iterator,n=t&&e[t],r=0;if(n)return n.call(e);if(e&&"number"==typeof e.length)return{next:function(){return e&&r>=e.length&&(e=void 0),{value:e&&e[r++],done:!e}}};throw new TypeError(t?"Object is not iterable.":"Symbol.iterator is not defined.")}function W(e,t){var n="function"==typeof Symbol&&e[Symbol.iterator];if(!n)return e;var r,o,i=n.call(e),s=[];try{for(;(void 0===t||t-- >0)&&!(r=i.next()).done;)s.push(r.value)}catch(l){o={error:l}}finally{try{r&&!r.done&&(n=i.return)&&n.call(i)}finally{if(o)throw o.error}}return s}function I(e,t,n){if(n||2===arguments.length)for(var r,o=0,i=t.length;o<i;o++)!r&&o in t||(r||(r=Array.prototype.slice.call(t,0,o)),r[o]=t[o]);return e.concat(r||Array.prototype.slice.call(t))}function q(e){return"function"==typeof e}"function"==typeof SuppressedError&&SuppressedError;var D,z=((D=function(e){return function(t){e(this),this.message=t?t.length+" errors occurred during unsubscription:\n"+t.map(function(e,t){return t+1+") "+e.toString()}).join("\n "):"",this.name="UnsubscriptionError",this.errors=t}}(function(e){Error.call(e),e.stack=(new Error).stack})).prototype=Object.create(Error.prototype),D.prototype.constructor=D,D);function L(e,t){if(e){var n=e.indexOf(t);0<=n&&e.splice(n,1)}}var J=function(){function e(e){this.initialTeardown=e,this.closed=!1,this._parentage=null,this._finalizers=null}var t;return e.prototype.unsubscribe=function(){var e,t,n,r,o;if(!this.closed){this.closed=!0;var i=this._parentage;if(i)if(this._parentage=null,Array.isArray(i))try{for(var s=V(i),l=s.next();!l.done;l=s.next()){l.value.remove(this)}}catch(f){e={error:f}}finally{try{l&&!l.done&&(t=s.return)&&t.call(s)}finally{if(e)throw e.error}}else i.remove(this);var a=this.initialTeardown;if(q(a))try{a()}catch(h){o=h instanceof z?h.errors:[h]}var c=this._finalizers;if(c){this._finalizers=null;try{for(var u=V(c),p=u.next();!p.done;p=u.next()){var d=p.value;try{H(d)}catch(g){o=null!=o?o:[],g instanceof z?o=I(I([],W(o)),W(g.errors)):o.push(g)}}}catch(y){n={error:y}}finally{try{p&&!p.done&&(r=u.return)&&r.call(u)}finally{if(n)throw n.error}}}if(o)throw new z(o)}},e.prototype.add=function(t){var n;if(t&&t!==this)if(this.closed)H(t);else{if(t instanceof e){if(t.closed||t._hasParent(this))return;t._addParent(this)}(this._finalizers=null!==(n=this._finalizers)&&void 0!==n?n:[]).push(t)}},e.prototype._hasParent=function(e){var t=this._parentage;return t===e||Array.isArray(t)&&t.includes(e)},e.prototype._addParent=function(e){var t=this._parentage;this._parentage=Array.isArray(t)?(t.push(e),t):t?[t,e]:e},e.prototype._removeParent=function(e){var t=this._parentage;t===e?this._parentage=null:Array.isArray(t)&&L(t,e)},e.prototype.remove=function(t){var n=this._finalizers;n&&L(n,t),t instanceof e&&t._removeParent(this)},e.EMPTY=((t=new e).closed=!0,t),e}();function Y(e){return e instanceof J||e&&"closed"in e&&q(e.remove)&&q(e.add)&&q(e.unsubscribe)}function H(e){q(e)?e():e.unsubscribe()}J.EMPTY;var B=void 0,K=function(e,t){for(var n=[],r=2;r<arguments.length;r++)n[r-2]=arguments[r];return setTimeout.apply(void 0,I([e,t],W(n)))};function U(){}var Q=function(e){function t(t){var n=e.call(this)||this;return n.isStopped=!1,t?(n.destination=t,Y(t)&&t.add(n)):n.destination=ee,n}return k(t,e),t.create=function(e,t,n){return new X(e,t,n)},t.prototype.next=function(e){this.isStopped||this._next(e)},t.prototype.error=function(e){this.isStopped||(this.isStopped=!0,this._error(e))},t.prototype.complete=function(){this.isStopped||(this.isStopped=!0,this._complete())},t.prototype.unsubscribe=function(){this.closed||(this.isStopped=!0,e.prototype.unsubscribe.call(this),this.destination=null)},t.prototype._next=function(e){this.destination.next(e)},t.prototype._error=function(e){try{this.destination.error(e)}finally{this.unsubscribe()}},t.prototype._complete=function(){try{this.destination.complete()}finally{this.unsubscribe()}},t}(J),G=function(){function e(e){this.partialObserver=e}return e.prototype.next=function(e){var t=this.partialObserver;if(t.next)try{t.next(e)}catch(n){Z(n)}},e.prototype.error=function(e){var t=this.partialObserver;if(t.error)try{t.error(e)}catch(n){Z(n)}else Z(e)},e.prototype.complete=function(){var e=this.partialObserver;if(e.complete)try{e.complete()}catch(t){Z(t)}},e}(),X=function(e){function t(t,n,r){var o,i=e.call(this)||this;return o=q(t)||!t?{next:null!=t?t:void 0,error:null!=n?n:void 0,complete:null!=r?r:void 0}:t,i.destination=new G(o),i}return k(t,e),t}(Q);function Z(e){var t;t=e,K(function(){throw t})}var ee={closed:!0,next:U,error:function(e){throw e},complete:U},te="function"==typeof Symbol&&Symbol.observable||"@@observable";function ne(e){return e}var re=function(){function e(e){e&&(this._subscribe=e)}return e.prototype.lift=function(t){var n=new e;return n.source=this,n.operator=t,n},e.prototype.subscribe=function(e,t,n){var r,o=this,i=(r=e)&&r instanceof Q||function(e){return e&&q(e.next)&&q(e.error)&&q(e.complete)}(r)&&Y(r)?e:new X(e,t,n);return function(){var e=o,t=e.operator,n=e.source;i.add(t?t.call(i,n):n?o._subscribe(i):o._trySubscribe(i))}(),i},e.prototype._trySubscribe=function(e){try{return this._subscribe(e)}catch(t){e.error(t)}},e.prototype.forEach=function(e,t){var n=this;return new(t=oe(t))(function(t,r){var o=new X({next:function(t){try{e(t)}catch(n){r(n),o.unsubscribe()}},error:r,complete:t});n.subscribe(o)})},e.prototype._subscribe=function(e){var t;return null===(t=this.source)||void 0===t?void 0:t.subscribe(e)},e.prototype[te]=function(){return this},e.prototype.pipe=function(){for(var e,t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];return(0===(e=t).length?ne:1===e.length?e[0]:function(t){return e.reduce(function(e,t){return t(e)},t)})(this)},e.prototype.toPromise=function(e){var t=this;return new(e=oe(e))(function(e,n){var r;t.subscribe(function(e){return r=e},function(e){return n(e)},function(){return e(r)})})},e.create=function(t){return new e(t)},e}();function oe(e){var t;return null!==(t=null!=e?e:B)&&void 0!==t?t:Promise}const ie=(e={})=>{const{parentOrigin:t="*",timeout:n,debug:r=!1,config:o={}}=e,i=(e=>(...t)=>{e&&console.log("[Penpal]",...t)})(r),s=((e,t)=>{const n=[];let r=!1;return{destroy(o){r||(r=!0,t(`${e}: Destroying connection`),n.forEach(e=>{e(o)}))},onDestroy(e){r?e():n.push(e)}}})("App",i),{destroy:l,onDestroy:u}=s,d=new Promise((e,r)=>{const d=((e,t)=>{let n;return void 0!==e&&(n=window.setTimeout(()=>{const n=new Error(`Connection timed out after ${e}ms`);n.code=m.ConnectionTimeout,t(n)},e)),()=>{clearTimeout(n)}})(n,l),f=n=>{if((()=>{try{clearTimeout(void 0)}catch(e){return!1}return!0})()&&n.source===parent&&n.data&&n.data.penpal===y.SynAck){const r=n.data,l=new F(r.id,r.appName),u=new F(r.id+"/api",r.appName),h=r.id,g={},y=e=>"api/"+e,v=e=>function(t){const n=y(e);u.$on(n,function e(r){t.next(r),r.reqid&&"succ"===r.result&&(t.complete(),u.$off(n,e))})},m=E({$emit:(e,...t)=>{l.getEventCallBackLength(e)>0?l.$pureEmit(e,...t):null==g||g.$off(e,()=>{},!0)},onQueryResult:e=>{u.$pureEmit(y(e.reqid),e)},onOsNotify:e=>{}}),b=C(t,m,c(a({},o),{postmateVersion:S}),s,i)(n),_={},$=b.bus;if($){const e=(e,t,n)=>{n?(t.$__once=!0,l.$once(e,t)):l.$on(e,t),_[e]?_[e].push(t):(_[e]=[t],$.$on(e,h))};Object.assign(g,$,{$on:e,$off:(e,t,n)=>{var r;if(_[e]){const n=_[e].indexOf(t);n>-1&&(_[e].splice(n,1),l.$off(e,t))}n&&delete _[e],(null==(r=_[e])?void 0:r.length)||($.$off(e,h),delete _[e])},$once:(t,n)=>{e(t,n,!0)}})}const A=c(a({},b),{bus:g,query:(e,t)=>p(null,null,function*(){const n=(null==e?void 0:e.reqid)||(yield b.genReqId());return new Promise((r,o)=>{b.query(c(a({},e),{reqid:n}),t).then(e=>{"succ"===(null==e?void 0:e.result)?r(e):o(e)})})}),createObservableQuery:(e,...t)=>p(null,[e,...t],function*(e,t={}){const n=(null==e?void 0:e.reqid)||(yield b.genReqId()),r=new re(v(n));return b.query(c(a({},e),{reqid:n}),c(a({},t),{observable:!0})),r})});b&&(window.removeEventListener(w.Message,f),d(),e({methods:A,config:n.data.config}))}};window.addEventListener(w.Message,f),(()=>{i("Child: Handshake - Sending SYN");const e={penpal:y.Syn},n=t instanceof RegExp?"*":t;window.parent.postMessage(e,n)})(),u(e=>{window.removeEventListener(w.Message,f),e&&r(e)})});return{promise:d,destroy(){l()}}};let se=null;class le{constructor(){u(this,"options"),u(this,"flutterInAppWebView",null),u(this,"osConnector",null),u(this,"initPromise"),u(this,"isWeb",!0);let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};this.options=e,e.debug&&(d.setDebug(!0),d.log("Trim App initialized with debug mode")),d.log("Options:",e),this.initPromise=this.init()}init(){return p(this,null,function*(){return d.log("Starting initialization..."),new Promise(e=>{var t;f()?(this.isWeb=!1,d.log("Initializing mobile platform..."),new Promise(e=>{var t;h||(null==(t=window.flutter_inappwebview)?void 0:t._platformReady)?(d.log("flutter_inappwebview is ready"),e(window.flutter_inappwebview)):window.addEventListener("flutterInAppWebViewPlatformReady",()=>{d.log("flutter_inappwebview is ready"),e(window.flutter_inappwebview)})}).then(t=>{this.flutterInAppWebView=t,d.log("Mobile platform initialized"),e()})):(this.isWeb=!0,d.log("Initializing web platform..."),(se||(se=ie({debug:null==t?void 0:t.debug})),se.promise).then(t=>{this.osConnector=t,d.log("Web platform initialized"),e()}))})})}callAppMethod(e,t){return p(this,null,function*(){return this.flutterInAppWebView?((e,t,n)=>p(null,null,function*(){d.log(`callHandler: ${t}, params: ${n}`);const r=yield e.callHandler(t,n);if(r)try{const e=JSON.parse(r);d.log(`messageData: ${JSON.stringify(e)}`);const t=JSON.parse(e.result);return d.log(`appMessage: ${JSON.stringify(t)}`),t}catch(o){return d.error(`error: ${o}`),null}return null}))(this.flutterInAppWebView,e,t):null})}ready(){return p(this,null,function*(){return this.initPromise})}getOptions(){return this.options}setOptions(e){this.options=a(a({},this.options),e),void 0!==e.debug&&d.setDebug(e.debug),d.log("Options updated:",this.options)}getPlatformConfig(){return p(this,null,function*(){var e;if(yield this.initPromise,this.isWeb){const t=yield null==(e=this.osConnector)?void 0:e.methods.getPlatformConfig();if(!t)throw new Error("Failed to get platform config");return t}{const e=yield this.callAppMethod("getAppMessage");if(!e)throw new Error("Failed to get platform config");return{theme:e.nightMode,language:e.language,appVersion:e.appVersion,systemVersion:e.systemVersion,format:{date:e.dateFormat,time:e.timeFormat}}}})}setTitle(e){return p(this,null,function*(){var t;return yield this.initPromise,d.log("Setting title:",e),this.isWeb?null==(t=this.osConnector)?void 0:t.methods.setTitle(e):this.callAppMethod("updateTitle",e)})}setExitPageTips(e){return p(this,null,function*(){var t;return yield this.initPromise,this.isWeb?null==(t=this.osConnector)?void 0:t.methods.setExitPageTips(e):this.callAppMethod("setExitPageTips",e?JSON.stringify(e):"")})}openFile(e){return p(this,null,function*(){var t;return yield this.initPromise,d.log("Opening file:",e),this.isWeb?null==(t=this.osConnector)?void 0:t.methods.openFile(e):this.callAppMethod("openFile",e)})}openFileManager(e){return p(this,null,function*(){var t;return yield this.initPromise,d.log("Opening file manager:",e),this.isWeb?null==(t=this.osConnector)?void 0:t.methods.openFileManagerApp(e):this.callAppMethod("openFileManager",e)})}openApp(e){return p(this,null,function*(){var t;if(yield this.initPromise,d.log("Opening app:",e),this.isWeb)return null==(t=this.osConnector)?void 0:t.methods.openApp(e);throw new Error("openApp is not supported on mobile platform")})}close(){return p(this,null,function*(){var e;return yield this.initPromise,this.isWeb?null==(e=this.osConnector)?void 0:e.methods.close():this.callAppMethod("exitPage")})}query(e,t){return p(this,null,function*(){var n;if(yield this.initPromise,this.isWeb){const r=yield null==(n=this.osConnector)?void 0:n.methods.query(e,t);if(!r)throw new Error("Failed to query");return r}throw new Error("query is not supported on mobile platform")})}pickFile(e){return p(this,null,function*(){var t;return yield this.initPromise,this.isWeb?null==(t=this.osConnector)?void 0:t.methods.pickFile(e):this.callAppMethod("openFolder",JSON.stringify(e))})}}return e.TrimApp=le,e.default=le,Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}}),e}({});
|
|
1
|
+
var TrimApp=function(e){"use strict";var t=Object.defineProperty,n=Object.defineProperties,r=Object.getOwnPropertyDescriptors,o=Object.getOwnPropertySymbols,i=Object.prototype.hasOwnProperty,s=Object.prototype.propertyIsEnumerable,l=(e,n,r)=>n in e?t(e,n,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[n]=r,a=(e,t)=>{for(var n in t||(t={}))i.call(t,n)&&l(e,n,t[n]);if(o)for(var n of o(t))s.call(t,n)&&l(e,n,t[n]);return e},c=(e,t)=>n(e,r(t)),u=(e,t,n)=>l(e,"symbol"!=typeof t?t+"":t,n),p=(e,t,n)=>new Promise((r,o)=>{var i=e=>{try{l(n.next(e))}catch(t){o(t)}},s=e=>{try{l(n.throw(e))}catch(t){o(t)}},l=e=>e.done?r(e.value):Promise.resolve(e.value).then(i,s);l((n=n.apply(e,t)).next())});const d=new class{constructor(){u(this,"debug",!1)}setDebug(e){this.debug=e}isDebug(){return this.debug}log(){if(this.debug){for(var e=arguments.length,t=new Array(e),n=0;n<e;n++)t[n]=arguments[n];console.log("[Trim App]",...t)}}warn(){if(this.debug){for(var e=arguments.length,t=new Array(e),n=0;n<e;n++)t[n]=arguments[n];console.warn("[Trim App]",...t)}}error(){if(this.debug){for(var e=arguments.length,t=new Array(e),n=0;n<e;n++)t[n]=arguments[n];console.error("[Trim App]",...t)}}info(){if(this.debug){for(var e=arguments.length,t=new Array(e),n=0;n<e;n++)t[n]=arguments[n];console.info("[Trim App]",...t)}}};const h=()=>function(e){const t=/FNOS\/([\d.]+)|FNAppType\/(\w+)|FNAppVer\/([\d.]+)/g,n={};let r;for(;null!==(r=t.exec(e));)r[1]&&(n.fnOSVersion=r[1]),r[2]&&(n.fnAppType=r[2]),r[3]&&(n.fnAppVersion=r[3]);return n}(navigator.userAgent).fnAppVersion;let f=!1;window.addEventListener("flutterInAppWebViewPlatformReady",()=>{f=!0});const g=new Map;h()&&(window.fnAppMessage=e=>{d.log("app-message",e);const t=JSON.parse(e),n=g.get(t.reqId);n&&(n(t),g.delete(t.reqId))});var y=(e=>(e.Call="call",e.Reply="reply",e.Syn="syn",e.SynAck="synAck",e.Ack="ack",e))(y||{}),m=(e=>(e.Fulfilled="fulfilled",e.Rejected="rejected",e))(m||{}),v=(e=>(e.ConnectionDestroyed="ConnectionDestroyed",e.ConnectionTimeout="ConnectionTimeout",e.NoIframeSrc="NoIframeSrc",e))(v||{}),b=(e=>(e.DataCloneError="DataCloneError",e))(b||{}),w=(e=>(e.Message="message",e))(w||{});const $=({name:e,message:t,stack:n})=>({name:e,message:t,stack:n});let A=0;const _=e=>e?e.split("."):[],E=(e,t,n)=>{const r=_(t);return r.reduce((e,t,o)=>(void 0===e[t]&&(e[t]={}),o===r.length-1&&(e[t]=n),e[t]),e),e},O=(e,t)=>{const n={};return Object.keys(e).forEach(r=>{const o=e[r],i=((e,t)=>{const n=_(t||"");return n.push(e),(e=>e.join("."))(n)})(r,t);"object"==typeof o&&Object.assign(n,O(o,i)),"function"==typeof o&&(n[i]=o)}),n},P=(e,t,n,r,o)=>{const{localName:i,local:s,remote:l,originForSending:a,originForReceiving:c}=t;let u=!1;o(`${i}: Connecting call sender`);const p=e=>(...t)=>{let n;o(`${i}: Sending ${e}() call`);try{l.closed&&(n=!0)}catch(p){n=!0}if(n&&r(),u){const t=new Error(`Unable to send ${e}() call due to destroyed connection`);throw t.code=v.ConnectionDestroyed,t}return new Promise((n,r)=>{const u=++A,p=t=>{if(t.source!==l||t.data.penpal!==y.Reply||t.data.id!==u)return;if("*"!==c&&t.origin!==c)return void o(`${i} received message from origin ${t.origin} which did not match expected origin ${c}`);const a=t.data;o(`${i}: Received ${e}() reply`),s.removeEventListener(w.Message,p);let d=a.returnValue;a.returnValueIsError&&(d=(e=>{const t=new Error;return Object.keys(e).forEach(n=>t[n]=e[n]),t})(d)),(a.resolution===m.Fulfilled?n:r)(d)};s.addEventListener(w.Message,p);const d={penpal:y.Call,id:u,methodName:e,args:t};l.postMessage(d,a)})},d=n.reduce((e,t)=>(e[t]=p(t),e),{});return Object.assign(e,(e=>{const t={};for(const n in e)E(t,n,e[n]);return t})(d)),()=>{u=!0}},S="0.1.2",C=(e,t,n,r,o)=>{const{destroy:i,onDestroy:s}=r;return r=>{if(!(e instanceof RegExp?e.test(r.origin):"*"===e||e===r.origin))return void o(`Child: Handshake - Received SYN-ACK from origin ${r.origin} which did not match expected origin ${e}`);o("Child: Handshake - Received SYN-ACK, responding with ACK");const l="null"===r.origin?"*":r.origin,a={penpal:y.Ack,methodNames:Object.keys(t),config:n};window.parent.postMessage(a,l);const c={localName:"Child",local:window,remote:window.parent,originForSending:l,originForReceiving:r.origin},u=((e,t,n)=>{const{localName:r,local:o,remote:i,originForSending:s,originForReceiving:l}=e;let a=!1;const c=e=>{if(e.source!==i||e.data.penpal!==y.Call)return;if("*"!==l&&e.origin!==l)return void n(`${r} received message from origin ${e.origin} which did not match expected origin ${l}`);const o=e.data,{methodName:c,args:u,id:p}=o;n(`${r}: Received ${c}() call`);const d=e=>t=>{if(n(`${r}: Sending ${c}() reply`),a)return void n(`${r}: Unable to send ${c}() reply due to destroyed connection`);const o={penpal:y.Reply,id:p,resolution:e,returnValue:t};e===m.Rejected&&t instanceof Error&&(o.returnValue=$(t),o.returnValueIsError=!0);try{i.postMessage(o,s)}catch(l){if(l.name===b.DataCloneError){const e={penpal:y.Reply,id:p,resolution:m.Rejected,returnValue:$(l),returnValueIsError:!0};i.postMessage(e,s)}throw l}};new Promise(e=>e(t[c].apply(t,u))).then(d(m.Fulfilled),d(m.Rejected))};return o.addEventListener(w.Message,c),()=>{a=!0,o.removeEventListener(w.Message,c)}})(c,t,o);s(u);const p={},d=P(p,c,r.data.methodNames,i,o);return s(d),p}};function M(e,t){null==console||console.warn(`[fnApp warn]: ${e}`,t)}function j(e,t){null==console||console.error(`[fnApp error]: ${e}`,t)}const x="_fn_all_event",N="事件订阅数量为空",T=window.__POWERED_BY_FNAPP__?window.__FNAPP.inject.appEventObjMap:new Map;class F{constructor(e,t){u(this,"id"),u(this,"appName"),u(this,"eventObj"),u(this,"getEventCallBackLength",e=>{var t;return(null==(t=this.eventObj[e])?void 0:t.length)||0}),this.id=e,this.appName=t,this.$clear(),T.get(this.id)||T.set(this.id,{}),this.eventObj=T.get(this.id)}$on(e,t){const n=this.eventObj[e];return n?(n.includes(t)||n.push(t),this):(this.eventObj[e]=[t],this)}$onAll(e){return this.$on(x,e)}$once(e,t){t.$__once=!0,this.$on(e,t)}$off(e,t){const n=this.eventObj[e];if(!e||!n||!n.length)return M(`${e} ${N}`),this;let r,o=n.length;for(;o--;)if(r=n[o],r===t){n.splice(o,1);break}return this}$offAll(e){return this.$off(x,e)}$emit(e,...t){return e=`${this.appName}/${e}`,this.$pureEmit(e,...t)}$pureEmit(e,...t){let n=0;return T.forEach(r=>{if(r[e])for(let i=r[e].length-1;i>=0;i--){n++;const s=r[e][i];s.$__once&&r[e].splice(i,1);try{s(...t)}catch(o){j(o)}}if(r[x])for(let e=r[x].length-1;e>=0;e--){n++;const i=r[x][e];i.$__once&&r[x].splice(e,1);try{i(...t)}catch(o){j(o)}}}),e&&0!==n||M(`${e} ${N}`),this}$clear(){const e=T.get(this.id)||{};return Object.keys(e).forEach(t=>delete e[t]),this}}var R=function(e,t){return(R=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n])})(e,t)};function W(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Class extends value "+String(t)+" is not a constructor or null");function n(){this.constructor=e}R(e,t),e.prototype=null===t?Object.create(t):(n.prototype=t.prototype,new n)}function k(e){var t="function"==typeof Symbol&&Symbol.iterator,n=t&&e[t],r=0;if(n)return n.call(e);if(e&&"number"==typeof e.length)return{next:function(){return e&&r>=e.length&&(e=void 0),{value:e&&e[r++],done:!e}}};throw new TypeError(t?"Object is not iterable.":"Symbol.iterator is not defined.")}function V(e,t){var n="function"==typeof Symbol&&e[Symbol.iterator];if(!n)return e;var r,o,i=n.call(e),s=[];try{for(;(void 0===t||t-- >0)&&!(r=i.next()).done;)s.push(r.value)}catch(l){o={error:l}}finally{try{r&&!r.done&&(n=i.return)&&n.call(i)}finally{if(o)throw o.error}}return s}function I(e,t,n){if(n||2===arguments.length)for(var r,o=0,i=t.length;o<i;o++)!r&&o in t||(r||(r=Array.prototype.slice.call(t,0,o)),r[o]=t[o]);return e.concat(r||Array.prototype.slice.call(t))}function q(e){return"function"==typeof e}"function"==typeof SuppressedError&&SuppressedError;var D,L=((D=function(e){return function(t){e(this),this.message=t?t.length+" errors occurred during unsubscription:\n"+t.map(function(e,t){return t+1+") "+e.toString()}).join("\n "):"",this.name="UnsubscriptionError",this.errors=t}}(function(e){Error.call(e),e.stack=(new Error).stack})).prototype=Object.create(Error.prototype),D.prototype.constructor=D,D);function z(e,t){if(e){var n=e.indexOf(t);0<=n&&e.splice(n,1)}}var J=function(){function e(e){this.initialTeardown=e,this.closed=!1,this._parentage=null,this._finalizers=null}var t;return e.prototype.unsubscribe=function(){var e,t,n,r,o;if(!this.closed){this.closed=!0;var i=this._parentage;if(i)if(this._parentage=null,Array.isArray(i))try{for(var s=k(i),l=s.next();!l.done;l=s.next()){l.value.remove(this)}}catch(h){e={error:h}}finally{try{l&&!l.done&&(t=s.return)&&t.call(s)}finally{if(e)throw e.error}}else i.remove(this);var a=this.initialTeardown;if(q(a))try{a()}catch(f){o=f instanceof L?f.errors:[f]}var c=this._finalizers;if(c){this._finalizers=null;try{for(var u=k(c),p=u.next();!p.done;p=u.next()){var d=p.value;try{H(d)}catch(g){o=null!=o?o:[],g instanceof L?o=I(I([],V(o)),V(g.errors)):o.push(g)}}}catch(y){n={error:y}}finally{try{p&&!p.done&&(r=u.return)&&r.call(u)}finally{if(n)throw n.error}}}if(o)throw new L(o)}},e.prototype.add=function(t){var n;if(t&&t!==this)if(this.closed)H(t);else{if(t instanceof e){if(t.closed||t._hasParent(this))return;t._addParent(this)}(this._finalizers=null!==(n=this._finalizers)&&void 0!==n?n:[]).push(t)}},e.prototype._hasParent=function(e){var t=this._parentage;return t===e||Array.isArray(t)&&t.includes(e)},e.prototype._addParent=function(e){var t=this._parentage;this._parentage=Array.isArray(t)?(t.push(e),t):t?[t,e]:e},e.prototype._removeParent=function(e){var t=this._parentage;t===e?this._parentage=null:Array.isArray(t)&&z(t,e)},e.prototype.remove=function(t){var n=this._finalizers;n&&z(n,t),t instanceof e&&t._removeParent(this)},e.EMPTY=((t=new e).closed=!0,t),e}();function Y(e){return e instanceof J||e&&"closed"in e&&q(e.remove)&&q(e.add)&&q(e.unsubscribe)}function H(e){q(e)?e():e.unsubscribe()}J.EMPTY;var U=void 0,B=function(e,t){for(var n=[],r=2;r<arguments.length;r++)n[r-2]=arguments[r];return setTimeout.apply(void 0,I([e,t],V(n)))};function K(){}var Q=function(e){function t(t){var n=e.call(this)||this;return n.isStopped=!1,t?(n.destination=t,Y(t)&&t.add(n)):n.destination=ee,n}return W(t,e),t.create=function(e,t,n){return new X(e,t,n)},t.prototype.next=function(e){this.isStopped||this._next(e)},t.prototype.error=function(e){this.isStopped||(this.isStopped=!0,this._error(e))},t.prototype.complete=function(){this.isStopped||(this.isStopped=!0,this._complete())},t.prototype.unsubscribe=function(){this.closed||(this.isStopped=!0,e.prototype.unsubscribe.call(this),this.destination=null)},t.prototype._next=function(e){this.destination.next(e)},t.prototype._error=function(e){try{this.destination.error(e)}finally{this.unsubscribe()}},t.prototype._complete=function(){try{this.destination.complete()}finally{this.unsubscribe()}},t}(J),G=function(){function e(e){this.partialObserver=e}return e.prototype.next=function(e){var t=this.partialObserver;if(t.next)try{t.next(e)}catch(n){Z(n)}},e.prototype.error=function(e){var t=this.partialObserver;if(t.error)try{t.error(e)}catch(n){Z(n)}else Z(e)},e.prototype.complete=function(){var e=this.partialObserver;if(e.complete)try{e.complete()}catch(t){Z(t)}},e}(),X=function(e){function t(t,n,r){var o,i=e.call(this)||this;return o=q(t)||!t?{next:null!=t?t:void 0,error:null!=n?n:void 0,complete:null!=r?r:void 0}:t,i.destination=new G(o),i}return W(t,e),t}(Q);function Z(e){var t;t=e,B(function(){throw t})}var ee={closed:!0,next:K,error:function(e){throw e},complete:K},te="function"==typeof Symbol&&Symbol.observable||"@@observable";function ne(e){return e}var re=function(){function e(e){e&&(this._subscribe=e)}return e.prototype.lift=function(t){var n=new e;return n.source=this,n.operator=t,n},e.prototype.subscribe=function(e,t,n){var r,o=this,i=(r=e)&&r instanceof Q||function(e){return e&&q(e.next)&&q(e.error)&&q(e.complete)}(r)&&Y(r)?e:new X(e,t,n);return function(){var e=o,t=e.operator,n=e.source;i.add(t?t.call(i,n):n?o._subscribe(i):o._trySubscribe(i))}(),i},e.prototype._trySubscribe=function(e){try{return this._subscribe(e)}catch(t){e.error(t)}},e.prototype.forEach=function(e,t){var n=this;return new(t=oe(t))(function(t,r){var o=new X({next:function(t){try{e(t)}catch(n){r(n),o.unsubscribe()}},error:r,complete:t});n.subscribe(o)})},e.prototype._subscribe=function(e){var t;return null===(t=this.source)||void 0===t?void 0:t.subscribe(e)},e.prototype[te]=function(){return this},e.prototype.pipe=function(){for(var e,t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];return(0===(e=t).length?ne:1===e.length?e[0]:function(t){return e.reduce(function(e,t){return t(e)},t)})(this)},e.prototype.toPromise=function(e){var t=this;return new(e=oe(e))(function(e,n){var r;t.subscribe(function(e){return r=e},function(e){return n(e)},function(){return e(r)})})},e.create=function(t){return new e(t)},e}();function oe(e){var t;return null!==(t=null!=e?e:U)&&void 0!==t?t:Promise}const ie=(e={})=>{const{parentOrigin:t="*",timeout:n,debug:r=!1,config:o={}}=e,i=(e=>(...t)=>{e&&console.log("[Penpal]",...t)})(r),s=((e,t)=>{const n=[];let r=!1;return{destroy(o){r||(r=!0,t(`${e}: Destroying connection`),n.forEach(e=>{e(o)}))},onDestroy(e){r?e():n.push(e)}}})("App",i),{destroy:l,onDestroy:u}=s,d=new Promise((e,r)=>{const d=((e,t)=>{let n;return void 0!==e&&(n=window.setTimeout(()=>{const n=new Error(`Connection timed out after ${e}ms`);n.code=v.ConnectionTimeout,t(n)},e)),()=>{clearTimeout(n)}})(n,l),h=n=>{if((()=>{try{clearTimeout(void 0)}catch(e){return!1}return!0})()&&n.source===parent&&n.data&&n.data.penpal===y.SynAck){const r=n.data,l=new F(r.id,r.appName),u=new F(r.id+"/api",r.appName),f=r.id,g={},y=e=>"api/"+e,m=e=>function(t){const n=y(e);u.$on(n,function e(r){t.next(r),r.reqid&&"succ"===r.result&&(t.complete(),u.$off(n,e))})},v=O({$emit:(e,...t)=>{l.getEventCallBackLength(e)>0?l.$pureEmit(e,...t):null==g||g.$off(e,()=>{},!0)},onQueryResult:e=>{u.$pureEmit(y(e.reqid),e)},onOsNotify:e=>{}}),b=C(t,v,c(a({},o),{postmateVersion:S}),s,i)(n),$={},A=b.bus;if(A){const e=(e,t,n)=>{n?(t.$__once=!0,l.$once(e,t)):l.$on(e,t),$[e]?$[e].push(t):($[e]=[t],A.$on(e,f))};Object.assign(g,A,{$on:e,$off:(e,t,n)=>{var r;if($[e]){const n=$[e].indexOf(t);n>-1&&($[e].splice(n,1),l.$off(e,t))}n&&delete $[e],(null==(r=$[e])?void 0:r.length)||(A.$off(e,f),delete $[e])},$once:(t,n)=>{e(t,n,!0)}})}const _=c(a({},b),{bus:g,query:(e,t)=>p(null,null,function*(){const n=(null==e?void 0:e.reqid)||(yield b.genReqId());return new Promise((r,o)=>{b.query(c(a({},e),{reqid:n}),t).then(e=>{"succ"===(null==e?void 0:e.result)?r(e):o(e)})})}),createObservableQuery:(e,...t)=>p(null,[e,...t],function*(e,t={}){const n=(null==e?void 0:e.reqid)||(yield b.genReqId()),r=new re(m(n));return b.query(c(a({},e),{reqid:n}),c(a({},t),{observable:!0})),r})});b&&(window.removeEventListener(w.Message,h),d(),e({methods:_,config:n.data.config}))}};window.addEventListener(w.Message,h),(()=>{i("Child: Handshake - Sending SYN");const e={penpal:y.Syn},n=t instanceof RegExp?"*":t;window.parent.postMessage(e,n)})(),u(e=>{window.removeEventListener(w.Message,h),e&&r(e)})});return{promise:d,destroy(){l()}}};let se=null;class le{constructor(){u(this,"options"),u(this,"flutterInAppWebView",null),u(this,"osConnector",null),u(this,"initPromise"),u(this,"isWeb",!0);let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};this.options=e,e.debug&&(d.setDebug(!0),d.log("Trim App initialized with debug mode")),d.log("Options:",e),this.initPromise=this.init()}init(){return p(this,null,function*(){return d.log("Starting initialization..."),new Promise(e=>{var t;h()?(this.isWeb=!1,d.log("Initializing mobile platform..."),new Promise(e=>{var t;f||(null==(t=window.flutter_inappwebview)?void 0:t._platformReady)?(d.log("flutter_inappwebview is ready"),e(window.flutter_inappwebview)):window.addEventListener("flutterInAppWebViewPlatformReady",()=>{d.log("flutter_inappwebview is ready"),e(window.flutter_inappwebview)})}).then(t=>{this.flutterInAppWebView=t,d.log("Mobile platform initialized"),e()})):(this.isWeb=!0,d.log("Initializing web platform..."),(se||(se=ie({debug:null==t?void 0:t.debug})),se.promise).then(t=>{this.osConnector=t,d.log("Web platform initialized"),e()}))})})}callAppMethod(e,t){return p(this,null,function*(){return this.flutterInAppWebView?((e,t,n)=>p(null,null,function*(){d.log(`callHandler: ${t}, params: ${n}`);const r=yield e.callHandler(t,n);if(r)try{const e=JSON.parse(r);d.log(`messageData: ${JSON.stringify(e)}`);const t=JSON.parse(e.result);return d.log(`appMessage: ${JSON.stringify(t)}`),t}catch(o){return d.error(`error: ${o}`),null}return null}))(this.flutterInAppWebView,e,t):null})}ready(){return p(this,null,function*(){return this.initPromise})}getOptions(){return this.options}setOptions(e){this.options=a(a({},this.options),e),void 0!==e.debug&&d.setDebug(e.debug),d.log("Options updated:",this.options)}getPlatformConfig(){return p(this,null,function*(){var e;if(yield this.initPromise,this.isWeb){const t=yield null==(e=this.osConnector)?void 0:e.methods.getPlatformConfig();if(!t)throw new Error("Failed to get platform config");return t}{const e=yield this.callAppMethod("getAppMessage");if(!e)throw new Error("Failed to get platform config");return{theme:e.nightMode,language:e.language,appVersion:e.appVersion,systemVersion:e.systemVersion,format:{date:e.dateFormat,time:e.timeFormat}}}})}setTitle(e){return p(this,null,function*(){var t;return yield this.initPromise,d.log("Setting title:",e),this.isWeb?null==(t=this.osConnector)?void 0:t.methods.setTitle(e):this.callAppMethod("updateTitle",e)})}setExitPageTips(e){return p(this,null,function*(){var t;return yield this.initPromise,this.isWeb?null==(t=this.osConnector)?void 0:t.methods.setExitPageTips(e):this.callAppMethod("setExitPageTips",e?JSON.stringify(e):"")})}openFile(e){return p(this,null,function*(){var t;return yield this.initPromise,d.log("Opening file:",e),this.isWeb?null==(t=this.osConnector)?void 0:t.methods.openFile(e):this.callAppMethod("openFile",e)})}openFileManager(e){return p(this,null,function*(){var t;return yield this.initPromise,d.log("Opening file manager:",e),this.isWeb?null==(t=this.osConnector)?void 0:t.methods.openFileManagerApp(e):this.callAppMethod("openFileManager",e)})}openApp(e){return p(this,null,function*(){var t;if(yield this.initPromise,d.log("Opening app:",e),this.isWeb)return null==(t=this.osConnector)?void 0:t.methods.openApp(e);throw new Error("openApp is not supported on mobile platform")})}openCustomApp(e,t){return p(this,null,function*(){var n;if(yield this.initPromise,d.log("Opening custom app:",e,t),this.isWeb)return null==(n=this.osConnector)?void 0:n.methods.openCustomApp(e,t);throw new Error("openCustomApp is not supported on mobile platform")})}openURL(e,t,n){return p(this,null,function*(){if(yield this.initPromise,d.log("Opening URL:",e,t,n),!this.isWeb)return this.callAppMethod("openSystemBrowser",e);if(!n)return void window.open(e,t,n);const r=window.open("",t,n);r&&(r.location.href=e)})}close(){return p(this,null,function*(){var e;return yield this.initPromise,this.isWeb?null==(e=this.osConnector)?void 0:e.methods.close():this.callAppMethod("exitPage")})}query(e,t){return p(this,null,function*(){var n;if(yield this.initPromise,this.isWeb){const r=yield null==(n=this.osConnector)?void 0:n.methods.query(e,t);if(!r)throw new Error("Failed to query");return r}throw new Error("query is not supported on mobile platform")})}$on(e,t){return p(this,null,function*(){var n;if(yield this.initPromise,!this.isWeb)throw new Error("$on is not supported on mobile platform");(null==(n=this.osConnector)?void 0:n.methods.bus).$on(e,t)})}$off(e,t){return p(this,null,function*(){var n;if(yield this.initPromise,!this.isWeb)throw new Error("$off is not supported on mobile platform");(null==(n=this.osConnector)?void 0:n.methods.bus).$off(e,t)})}$once(e,t){return p(this,null,function*(){var n;if(yield this.initPromise,!this.isWeb)throw new Error("$once is not supported on mobile platform");(null==(n=this.osConnector)?void 0:n.methods.bus).$once(e,t)})}pickFile(e){return p(this,null,function*(){var t;return yield this.initPromise,this.isWeb?null==(t=this.osConnector)?void 0:t.methods.pickFile(e):this.callAppMethod("openFolder",JSON.stringify(e))})}}return e.TrimApp=le,e.default=le,Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}}),e}({});
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var e=Object.defineProperty,t=Object.defineProperties,n=Object.getOwnPropertyDescriptors,r=Object.getOwnPropertySymbols,o=Object.prototype.hasOwnProperty,i=Object.prototype.propertyIsEnumerable,s=(t,n,r)=>n in t?e(t,n,{enumerable:!0,configurable:!0,writable:!0,value:r}):t[n]=r,l=(e,t)=>{for(var n in t||(t={}))o.call(t,n)&&s(e,n,t[n]);if(r)for(var n of r(t))i.call(t,n)&&s(e,n,t[n]);return e},a=(e,r)=>t(e,n(r)),c=(e,t,n)=>s(e,"symbol"!=typeof t?t+"":t,n),u=(e,t,n)=>new Promise((r,o)=>{var i=e=>{try{l(n.next(e))}catch(t){o(t)}},s=e=>{try{l(n.throw(e))}catch(t){o(t)}},l=e=>e.done?r(e.value):Promise.resolve(e.value).then(i,s);l((n=n.apply(e,t)).next())});const p=new class{constructor(){c(this,"debug",!1)}setDebug(e){this.debug=e}isDebug(){return this.debug}log(){if(this.debug){for(var e=arguments.length,t=new Array(e),n=0;n<e;n++)t[n]=arguments[n];console.log("[Trim App]",...t)}}warn(){if(this.debug){for(var e=arguments.length,t=new Array(e),n=0;n<e;n++)t[n]=arguments[n];console.warn("[Trim App]",...t)}}error(){if(this.debug){for(var e=arguments.length,t=new Array(e),n=0;n<e;n++)t[n]=arguments[n];console.error("[Trim App]",...t)}}info(){if(this.debug){for(var e=arguments.length,t=new Array(e),n=0;n<e;n++)t[n]=arguments[n];console.info("[Trim App]",...t)}}};const d=()=>function(e){const t=/FNOS\/([\d.]+)|FNAppType\/(\w+)|FNAppVer\/([\d.]+)/g,n={};let r;for(;null!==(r=t.exec(e));)r[1]&&(n.fnOSVersion=r[1]),r[2]&&(n.fnAppType=r[2]),r[3]&&(n.fnAppVersion=r[3]);return n}(navigator.userAgent).fnAppVersion;let f=!1;window.addEventListener("flutterInAppWebViewPlatformReady",()=>{f=!0});const h=/* @__PURE__ */new Map;d()&&(window.fnAppMessage=e=>{p.log("app-message",e);const t=JSON.parse(e),n=h.get(t.reqId);n&&(n(t),h.delete(t.reqId))});var g=/* @__PURE__ */(e=>(e.Call="call",e.Reply="reply",e.Syn="syn",e.SynAck="synAck",e.Ack="ack",e))(g||{}),y=/* @__PURE__ */(e=>(e.Fulfilled="fulfilled",e.Rejected="rejected",e))(y||{}),v=/* @__PURE__ */(e=>(e.ConnectionDestroyed="ConnectionDestroyed",e.ConnectionTimeout="ConnectionTimeout",e.NoIframeSrc="NoIframeSrc",e))(v||{}),m=/* @__PURE__ */(e=>(e.DataCloneError="DataCloneError",e))(m||{}),b=/* @__PURE__ */(e=>(e.Message="message",e))(b||{});const w=({name:e,message:t,stack:n})=>({name:e,message:t,stack:n});let $=0;const _=e=>e?e.split("."):[],A=(e,t,n)=>{const r=_(t);return r.reduce((e,t,o)=>(void 0===e[t]&&(e[t]={}),o===r.length-1&&(e[t]=n),e[t]),e),e},O=(e,t)=>{const n={};return Object.keys(e).forEach(r=>{const o=e[r],i=((e,t)=>{const n=_(t||"");return n.push(e),(e=>e.join("."))(n)})(r,t);"object"==typeof o&&Object.assign(n,O(o,i)),"function"==typeof o&&(n[i]=o)}),n},E=(e,t,n,r,o)=>{const{localName:i,local:s,remote:l,originForSending:a,originForReceiving:c}=t;let u=!1;o(`${i}: Connecting call sender`);const p=e=>(...t)=>{let n;o(`${i}: Sending ${e}() call`);try{l.closed&&(n=!0)}catch(p){n=!0}if(n&&r(),u){const t=new Error(`Unable to send ${e}() call due to destroyed connection`);throw t.code=v.ConnectionDestroyed,t}return new Promise((n,r)=>{const u=++$,p=t=>{if(t.source!==l||t.data.penpal!==g.Reply||t.data.id!==u)return;if("*"!==c&&t.origin!==c)return void o(`${i} received message from origin ${t.origin} which did not match expected origin ${c}`);const a=t.data;o(`${i}: Received ${e}() reply`),s.removeEventListener(b.Message,p);let d=a.returnValue;a.returnValueIsError&&(d=(e=>{const t=new Error;return Object.keys(e).forEach(n=>t[n]=e[n]),t})(d)),(a.resolution===y.Fulfilled?n:r)(d)};s.addEventListener(b.Message,p);const d={penpal:g.Call,id:u,methodName:e,args:t};l.postMessage(d,a)})},d=n.reduce((e,t)=>(e[t]=p(t),e),{});return Object.assign(e,(e=>{const t={};for(const n in e)A(t,n,e[n]);return t})(d)),()=>{u=!0}},P="0.1.2",S=(e,t,n,r,o)=>{const{destroy:i,onDestroy:s}=r;return r=>{if(!(e instanceof RegExp?e.test(r.origin):"*"===e||e===r.origin))return void o(`Child: Handshake - Received SYN-ACK from origin ${r.origin} which did not match expected origin ${e}`);o("Child: Handshake - Received SYN-ACK, responding with ACK");const l="null"===r.origin?"*":r.origin,a={penpal:g.Ack,methodNames:Object.keys(t),config:n};window.parent.postMessage(a,l);const c={localName:"Child",local:window,remote:window.parent,originForSending:l,originForReceiving:r.origin},u=((e,t,n)=>{const{localName:r,local:o,remote:i,originForSending:s,originForReceiving:l}=e;let a=!1;const c=e=>{if(e.source!==i||e.data.penpal!==g.Call)return;if("*"!==l&&e.origin!==l)return void n(`${r} received message from origin ${e.origin} which did not match expected origin ${l}`);const o=e.data,{methodName:c,args:u,id:p}=o;n(`${r}: Received ${c}() call`);const d=e=>t=>{if(n(`${r}: Sending ${c}() reply`),a)return void n(`${r}: Unable to send ${c}() reply due to destroyed connection`);const o={penpal:g.Reply,id:p,resolution:e,returnValue:t};e===y.Rejected&&t instanceof Error&&(o.returnValue=w(t),o.returnValueIsError=!0);try{i.postMessage(o,s)}catch(l){if(l.name===m.DataCloneError){const e={penpal:g.Reply,id:p,resolution:y.Rejected,returnValue:w(l),returnValueIsError:!0};i.postMessage(e,s)}throw l}};new Promise(e=>e(t[c].apply(t,u))).then(d(y.Fulfilled),d(y.Rejected))};return o.addEventListener(b.Message,c),()=>{a=!0,o.removeEventListener(b.Message,c)}})(c,t,o);s(u);const p={},d=E(p,c,r.data.methodNames,i,o);return s(d),p}};function C(e,t){null==console||console.warn(`[fnApp warn]: ${e}`,t)}function j(e,t){null==console||console.error(`[fnApp error]: ${e}`,t)}const x="_fn_all_event",M="事件订阅数量为空",N=window.__POWERED_BY_FNAPP__?window.__FNAPP.inject.appEventObjMap:/* @__PURE__ */new Map;class F{constructor(e,t){c(this,"id"),c(this,"appName"),c(this,"eventObj"),c(this,"getEventCallBackLength",e=>{var t;return(null==(t=this.eventObj[e])?void 0:t.length)||0}),this.id=e,this.appName=t,this.$clear(),N.get(this.id)||N.set(this.id,{}),this.eventObj=N.get(this.id)}$on(e,t){const n=this.eventObj[e];return n?(n.includes(t)||n.push(t),this):(this.eventObj[e]=[t],this)}$onAll(e){return this.$on(x,e)}$once(e,t){t.$__once=!0,this.$on(e,t)}$off(e,t){const n=this.eventObj[e];if(!e||!n||!n.length)return C(`${e} ${M}`),this;let r,o=n.length;for(;o--;)if(r=n[o],r===t){n.splice(o,1);break}return this}$offAll(e){return this.$off(x,e)}$emit(e,...t){return e=`${this.appName}/${e}`,this.$pureEmit(e,...t)}$pureEmit(e,...t){let n=0;return N.forEach(r=>{if(r[e])for(let i=r[e].length-1;i>=0;i--){n++;const s=r[e][i];s.$__once&&r[e].splice(i,1);try{s(...t)}catch(o){j(o)}}if(r[x])for(let e=r[x].length-1;e>=0;e--){n++;const i=r[x][e];i.$__once&&r[x].splice(e,1);try{i(...t)}catch(o){j(o)}}}),e&&0!==n||C(`${e} ${M}`),this}$clear(){const e=N.get(this.id)||{};return Object.keys(e).forEach(t=>delete e[t]),this}}var T=function(e,t){return(T=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n])})(e,t)};function R(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Class extends value "+String(t)+" is not a constructor or null");function n(){this.constructor=e}T(e,t),e.prototype=null===t?Object.create(t):(n.prototype=t.prototype,new n)}function k(e){var t="function"==typeof Symbol&&Symbol.iterator,n=t&&e[t],r=0;if(n)return n.call(e);if(e&&"number"==typeof e.length)return{next:function(){return e&&r>=e.length&&(e=void 0),{value:e&&e[r++],done:!e}}};throw new TypeError(t?"Object is not iterable.":"Symbol.iterator is not defined.")}function V(e,t){var n="function"==typeof Symbol&&e[Symbol.iterator];if(!n)return e;var r,o,i=n.call(e),s=[];try{for(;(void 0===t||t-- >0)&&!(r=i.next()).done;)s.push(r.value)}catch(l){o={error:l}}finally{try{r&&!r.done&&(n=i.return)&&n.call(i)}finally{if(o)throw o.error}}return s}function W(e,t,n){if(n||2===arguments.length)for(var r,o=0,i=t.length;o<i;o++)!r&&o in t||(r||(r=Array.prototype.slice.call(t,0,o)),r[o]=t[o]);return e.concat(r||Array.prototype.slice.call(t))}function I(e){return"function"==typeof e}"function"==typeof SuppressedError&&SuppressedError;var q,D=((q=function(e){return function(t){e(this),this.message=t?t.length+" errors occurred during unsubscription:\n"+t.map(function(e,t){return t+1+") "+e.toString()}).join("\n "):"",this.name="UnsubscriptionError",this.errors=t}}(function(e){Error.call(e),e.stack=(new Error).stack})).prototype=Object.create(Error.prototype),q.prototype.constructor=q,q);function z(e,t){if(e){var n=e.indexOf(t);0<=n&&e.splice(n,1)}}var L=function(){function e(e){this.initialTeardown=e,this.closed=!1,this._parentage=null,this._finalizers=null}var t;return e.prototype.unsubscribe=function(){var e,t,n,r,o;if(!this.closed){this.closed=!0;var i=this._parentage;if(i)if(this._parentage=null,Array.isArray(i))try{for(var s=k(i),l=s.next();!l.done;l=s.next()){l.value.remove(this)}}catch(f){e={error:f}}finally{try{l&&!l.done&&(t=s.return)&&t.call(s)}finally{if(e)throw e.error}}else i.remove(this);var a=this.initialTeardown;if(I(a))try{a()}catch(h){o=h instanceof D?h.errors:[h]}var c=this._finalizers;if(c){this._finalizers=null;try{for(var u=k(c),p=u.next();!p.done;p=u.next()){var d=p.value;try{Y(d)}catch(g){o=null!=o?o:[],g instanceof D?o=W(W([],V(o)),V(g.errors)):o.push(g)}}}catch(y){n={error:y}}finally{try{p&&!p.done&&(r=u.return)&&r.call(u)}finally{if(n)throw n.error}}}if(o)throw new D(o)}},e.prototype.add=function(t){var n;if(t&&t!==this)if(this.closed)Y(t);else{if(t instanceof e){if(t.closed||t._hasParent(this))return;t._addParent(this)}(this._finalizers=null!==(n=this._finalizers)&&void 0!==n?n:[]).push(t)}},e.prototype._hasParent=function(e){var t=this._parentage;return t===e||Array.isArray(t)&&t.includes(e)},e.prototype._addParent=function(e){var t=this._parentage;this._parentage=Array.isArray(t)?(t.push(e),t):t?[t,e]:e},e.prototype._removeParent=function(e){var t=this._parentage;t===e?this._parentage=null:Array.isArray(t)&&z(t,e)},e.prototype.remove=function(t){var n=this._finalizers;n&&z(n,t),t instanceof e&&t._removeParent(this)},e.EMPTY=((t=new e).closed=!0,t),e}();function J(e){return e instanceof L||e&&"closed"in e&&I(e.remove)&&I(e.add)&&I(e.unsubscribe)}function Y(e){I(e)?e():e.unsubscribe()}L.EMPTY;var H=void 0,B=function(e,t){for(var n=[],r=2;r<arguments.length;r++)n[r-2]=arguments[r];return setTimeout.apply(void 0,W([e,t],V(n)))};function K(){}var U=function(e){function t(t){var n=e.call(this)||this;return n.isStopped=!1,t?(n.destination=t,J(t)&&t.add(n)):n.destination=Z,n}return R(t,e),t.create=function(e,t,n){return new G(e,t,n)},t.prototype.next=function(e){this.isStopped||this._next(e)},t.prototype.error=function(e){this.isStopped||(this.isStopped=!0,this._error(e))},t.prototype.complete=function(){this.isStopped||(this.isStopped=!0,this._complete())},t.prototype.unsubscribe=function(){this.closed||(this.isStopped=!0,e.prototype.unsubscribe.call(this),this.destination=null)},t.prototype._next=function(e){this.destination.next(e)},t.prototype._error=function(e){try{this.destination.error(e)}finally{this.unsubscribe()}},t.prototype._complete=function(){try{this.destination.complete()}finally{this.unsubscribe()}},t}(L),Q=function(){function e(e){this.partialObserver=e}return e.prototype.next=function(e){var t=this.partialObserver;if(t.next)try{t.next(e)}catch(n){X(n)}},e.prototype.error=function(e){var t=this.partialObserver;if(t.error)try{t.error(e)}catch(n){X(n)}else X(e)},e.prototype.complete=function(){var e=this.partialObserver;if(e.complete)try{e.complete()}catch(t){X(t)}},e}(),G=function(e){function t(t,n,r){var o,i=e.call(this)||this;return o=I(t)||!t?{next:null!=t?t:void 0,error:null!=n?n:void 0,complete:null!=r?r:void 0}:t,i.destination=new Q(o),i}return R(t,e),t}(U);function X(e){var t;t=e,B(function(){throw t})}var Z={closed:!0,next:K,error:function(e){throw e},complete:K},ee="function"==typeof Symbol&&Symbol.observable||"@@observable";function te(e){return e}var ne=function(){function e(e){e&&(this._subscribe=e)}return e.prototype.lift=function(t){var n=new e;return n.source=this,n.operator=t,n},e.prototype.subscribe=function(e,t,n){var r,o=this,i=(r=e)&&r instanceof U||function(e){return e&&I(e.next)&&I(e.error)&&I(e.complete)}(r)&&J(r)?e:new G(e,t,n);return function(){var e=o,t=e.operator,n=e.source;i.add(t?t.call(i,n):n?o._subscribe(i):o._trySubscribe(i))}(),i},e.prototype._trySubscribe=function(e){try{return this._subscribe(e)}catch(t){e.error(t)}},e.prototype.forEach=function(e,t){var n=this;return new(t=re(t))(function(t,r){var o=new G({next:function(t){try{e(t)}catch(n){r(n),o.unsubscribe()}},error:r,complete:t});n.subscribe(o)})},e.prototype._subscribe=function(e){var t;return null===(t=this.source)||void 0===t?void 0:t.subscribe(e)},e.prototype[ee]=function(){return this},e.prototype.pipe=function(){for(var e,t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];return(0===(e=t).length?te:1===e.length?e[0]:function(t){return e.reduce(function(e,t){return t(e)},t)})(this)},e.prototype.toPromise=function(e){var t=this;return new(e=re(e))(function(e,n){var r;t.subscribe(function(e){return r=e},function(e){return n(e)},function(){return e(r)})})},e.create=function(t){return new e(t)},e}();function re(e){var t;return null!==(t=null!=e?e:H)&&void 0!==t?t:Promise}const oe=(e={})=>{const{parentOrigin:t="*",timeout:n,debug:r=!1,config:o={}}=e,i=(e=>(...t)=>{e&&console.log("[Penpal]",...t)})(r),s=((e,t)=>{const n=[];let r=!1;return{destroy(o){r||(r=!0,t(`${e}: Destroying connection`),n.forEach(e=>{e(o)}))},onDestroy(e){r?e():n.push(e)}}})("App",i),{destroy:c,onDestroy:p}=s,d=new Promise((e,r)=>{const d=((e,t)=>{let n;return void 0!==e&&(n=window.setTimeout(()=>{const n=new Error(`Connection timed out after ${e}ms`);n.code=v.ConnectionTimeout,t(n)},e)),()=>{clearTimeout(n)}})(n,c),f=n=>{if((()=>{try{clearTimeout(void 0)}catch(e){return!1}return!0})()&&n.source===parent&&n.data&&n.data.penpal===g.SynAck){const r=n.data,c=new F(r.id,r.appName),p=new F(r.id+"/api",r.appName),h=r.id,g={},y=e=>"api/"+e,v=e=>function(t){const n=y(e);p.$on(n,function e(r){t.next(r),r.reqid&&"succ"===r.result&&(t.complete(),p.$off(n,e))})},m=O({$emit:(e,...t)=>{c.getEventCallBackLength(e)>0?c.$pureEmit(e,...t):null==g||g.$off(e,()=>{},!0)},onQueryResult:e=>{p.$pureEmit(y(e.reqid),e)},onOsNotify:e=>{}}),w=S(t,m,a(l({},o),{postmateVersion:P}),s,i)(n),$={},_=w.bus;if(_){const e=(e,t,n)=>{n?(t.$__once=!0,c.$once(e,t)):c.$on(e,t),$[e]?$[e].push(t):($[e]=[t],_.$on(e,h))};Object.assign(g,_,{$on:e,$off:(e,t,n)=>{var r;if($[e]){const n=$[e].indexOf(t);n>-1&&($[e].splice(n,1),c.$off(e,t))}n&&delete $[e],(null==(r=$[e])?void 0:r.length)||(_.$off(e,h),delete $[e])},$once:(t,n)=>{e(t,n,!0)}})}const A=a(l({},w),{bus:g,query:(e,t)=>u(null,null,function*(){const n=(null==e?void 0:e.reqid)||(yield w.genReqId());return new Promise((r,o)=>{w.query(a(l({},e),{reqid:n}),t).then(e=>{"succ"===(null==e?void 0:e.result)?r(e):o(e)})})}),createObservableQuery:(e,...t)=>u(null,[e,...t],function*(e,t={}){const n=(null==e?void 0:e.reqid)||(yield w.genReqId()),r=new ne(v(n));return w.query(a(l({},e),{reqid:n}),a(l({},t),{observable:!0})),r})});w&&(window.removeEventListener(b.Message,f),d(),e({methods:A,config:n.data.config}))}};window.addEventListener(b.Message,f),(()=>{i("Child: Handshake - Sending SYN");const e={penpal:g.Syn},n=t instanceof RegExp?"*":t;window.parent.postMessage(e,n)})(),p(e=>{window.removeEventListener(b.Message,f),e&&r(e)})});return{promise:d,destroy(){c()}}};let ie=null;class se{constructor(){c(this,"options"),c(this,"flutterInAppWebView",null),c(this,"osConnector",null),c(this,"initPromise"),c(this,"isWeb",!0);let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};this.options=e,e.debug&&(p.setDebug(!0),p.log("Trim App initialized with debug mode")),p.log("Options:",e),this.initPromise=this.init()}init(){return u(this,null,function*(){return p.log("Starting initialization..."),new Promise(e=>{var t;d()?(this.isWeb=!1,p.log("Initializing mobile platform..."),new Promise(e=>{var t;f||(null==(t=window.flutter_inappwebview)?void 0:t._platformReady)?(p.log("flutter_inappwebview is ready"),e(window.flutter_inappwebview)):window.addEventListener("flutterInAppWebViewPlatformReady",()=>{p.log("flutter_inappwebview is ready"),e(window.flutter_inappwebview)})}).then(t=>{this.flutterInAppWebView=t,p.log("Mobile platform initialized"),e()})):(this.isWeb=!0,p.log("Initializing web platform..."),(ie||(ie=oe({debug:null==t?void 0:t.debug})),ie.promise).then(t=>{this.osConnector=t,p.log("Web platform initialized"),e()}))})})}callAppMethod(e,t){return u(this,null,function*(){return this.flutterInAppWebView?((e,t,n)=>u(null,null,function*(){p.log(`callHandler: ${t}, params: ${n}`);const r=yield e.callHandler(t,n);if(r)try{const e=JSON.parse(r);p.log(`messageData: ${JSON.stringify(e)}`);const t=JSON.parse(e.result);return p.log(`appMessage: ${JSON.stringify(t)}`),t}catch(o){return p.error(`error: ${o}`),null}return null}))(this.flutterInAppWebView,e,t):null})}ready(){return u(this,null,function*(){return this.initPromise})}getOptions(){return this.options}setOptions(e){this.options=l(l({},this.options),e),void 0!==e.debug&&p.setDebug(e.debug),p.log("Options updated:",this.options)}getPlatformConfig(){return u(this,null,function*(){var e;if(yield this.initPromise,this.isWeb){const t=yield null==(e=this.osConnector)?void 0:e.methods.getPlatformConfig();if(!t)throw new Error("Failed to get platform config");return t}{const e=yield this.callAppMethod("getAppMessage");if(!e)throw new Error("Failed to get platform config");return{theme:e.nightMode,language:e.language,appVersion:e.appVersion,systemVersion:e.systemVersion,format:{date:e.dateFormat,time:e.timeFormat}}}})}setTitle(e){return u(this,null,function*(){var t;return yield this.initPromise,p.log("Setting title:",e),this.isWeb?null==(t=this.osConnector)?void 0:t.methods.setTitle(e):this.callAppMethod("updateTitle",e)})}setExitPageTips(e){return u(this,null,function*(){var t;return yield this.initPromise,this.isWeb?null==(t=this.osConnector)?void 0:t.methods.setExitPageTips(e):this.callAppMethod("setExitPageTips",e?JSON.stringify(e):"")})}openFile(e){return u(this,null,function*(){var t;return yield this.initPromise,p.log("Opening file:",e),this.isWeb?null==(t=this.osConnector)?void 0:t.methods.openFile(e):this.callAppMethod("openFile",e)})}openFileManager(e){return u(this,null,function*(){var t;return yield this.initPromise,p.log("Opening file manager:",e),this.isWeb?null==(t=this.osConnector)?void 0:t.methods.openFileManagerApp(e):this.callAppMethod("openFileManager",e)})}openApp(e){return u(this,null,function*(){var t;if(yield this.initPromise,p.log("Opening app:",e),this.isWeb)return null==(t=this.osConnector)?void 0:t.methods.openApp(e);throw new Error("openApp is not supported on mobile platform")})}close(){return u(this,null,function*(){var e;return yield this.initPromise,this.isWeb?null==(e=this.osConnector)?void 0:e.methods.close():this.callAppMethod("exitPage")})}query(e,t){return u(this,null,function*(){var n;if(yield this.initPromise,this.isWeb){const r=yield null==(n=this.osConnector)?void 0:n.methods.query(e,t);if(!r)throw new Error("Failed to query");return r}throw new Error("query is not supported on mobile platform")})}pickFile(e){return u(this,null,function*(){var t;return yield this.initPromise,this.isWeb?null==(t=this.osConnector)?void 0:t.methods.pickFile(e):this.callAppMethod("openFolder",JSON.stringify(e))})}}export{se as TrimApp,se as default};
|
|
1
|
+
var e=Object.defineProperty,t=Object.defineProperties,n=Object.getOwnPropertyDescriptors,r=Object.getOwnPropertySymbols,o=Object.prototype.hasOwnProperty,i=Object.prototype.propertyIsEnumerable,s=(t,n,r)=>n in t?e(t,n,{enumerable:!0,configurable:!0,writable:!0,value:r}):t[n]=r,l=(e,t)=>{for(var n in t||(t={}))o.call(t,n)&&s(e,n,t[n]);if(r)for(var n of r(t))i.call(t,n)&&s(e,n,t[n]);return e},a=(e,r)=>t(e,n(r)),c=(e,t,n)=>s(e,"symbol"!=typeof t?t+"":t,n),u=(e,t,n)=>new Promise((r,o)=>{var i=e=>{try{l(n.next(e))}catch(t){o(t)}},s=e=>{try{l(n.throw(e))}catch(t){o(t)}},l=e=>e.done?r(e.value):Promise.resolve(e.value).then(i,s);l((n=n.apply(e,t)).next())});const p=new class{constructor(){c(this,"debug",!1)}setDebug(e){this.debug=e}isDebug(){return this.debug}log(){if(this.debug){for(var e=arguments.length,t=new Array(e),n=0;n<e;n++)t[n]=arguments[n];console.log("[Trim App]",...t)}}warn(){if(this.debug){for(var e=arguments.length,t=new Array(e),n=0;n<e;n++)t[n]=arguments[n];console.warn("[Trim App]",...t)}}error(){if(this.debug){for(var e=arguments.length,t=new Array(e),n=0;n<e;n++)t[n]=arguments[n];console.error("[Trim App]",...t)}}info(){if(this.debug){for(var e=arguments.length,t=new Array(e),n=0;n<e;n++)t[n]=arguments[n];console.info("[Trim App]",...t)}}};const d=()=>function(e){const t=/FNOS\/([\d.]+)|FNAppType\/(\w+)|FNAppVer\/([\d.]+)/g,n={};let r;for(;null!==(r=t.exec(e));)r[1]&&(n.fnOSVersion=r[1]),r[2]&&(n.fnAppType=r[2]),r[3]&&(n.fnAppVersion=r[3]);return n}(navigator.userAgent).fnAppVersion;let h=!1;window.addEventListener("flutterInAppWebViewPlatformReady",()=>{h=!0});const f=/* @__PURE__ */new Map;d()&&(window.fnAppMessage=e=>{p.log("app-message",e);const t=JSON.parse(e),n=f.get(t.reqId);n&&(n(t),f.delete(t.reqId))});var g=/* @__PURE__ */(e=>(e.Call="call",e.Reply="reply",e.Syn="syn",e.SynAck="synAck",e.Ack="ack",e))(g||{}),y=/* @__PURE__ */(e=>(e.Fulfilled="fulfilled",e.Rejected="rejected",e))(y||{}),m=/* @__PURE__ */(e=>(e.ConnectionDestroyed="ConnectionDestroyed",e.ConnectionTimeout="ConnectionTimeout",e.NoIframeSrc="NoIframeSrc",e))(m||{}),v=/* @__PURE__ */(e=>(e.DataCloneError="DataCloneError",e))(v||{}),b=/* @__PURE__ */(e=>(e.Message="message",e))(b||{});const w=({name:e,message:t,stack:n})=>({name:e,message:t,stack:n});let $=0;const A=e=>e?e.split("."):[],_=(e,t,n)=>{const r=A(t);return r.reduce((e,t,o)=>(void 0===e[t]&&(e[t]={}),o===r.length-1&&(e[t]=n),e[t]),e),e},E=(e,t)=>{const n={};return Object.keys(e).forEach(r=>{const o=e[r],i=((e,t)=>{const n=A(t||"");return n.push(e),(e=>e.join("."))(n)})(r,t);"object"==typeof o&&Object.assign(n,E(o,i)),"function"==typeof o&&(n[i]=o)}),n},O=(e,t,n,r,o)=>{const{localName:i,local:s,remote:l,originForSending:a,originForReceiving:c}=t;let u=!1;o(`${i}: Connecting call sender`);const p=e=>(...t)=>{let n;o(`${i}: Sending ${e}() call`);try{l.closed&&(n=!0)}catch(p){n=!0}if(n&&r(),u){const t=new Error(`Unable to send ${e}() call due to destroyed connection`);throw t.code=m.ConnectionDestroyed,t}return new Promise((n,r)=>{const u=++$,p=t=>{if(t.source!==l||t.data.penpal!==g.Reply||t.data.id!==u)return;if("*"!==c&&t.origin!==c)return void o(`${i} received message from origin ${t.origin} which did not match expected origin ${c}`);const a=t.data;o(`${i}: Received ${e}() reply`),s.removeEventListener(b.Message,p);let d=a.returnValue;a.returnValueIsError&&(d=(e=>{const t=new Error;return Object.keys(e).forEach(n=>t[n]=e[n]),t})(d)),(a.resolution===y.Fulfilled?n:r)(d)};s.addEventListener(b.Message,p);const d={penpal:g.Call,id:u,methodName:e,args:t};l.postMessage(d,a)})},d=n.reduce((e,t)=>(e[t]=p(t),e),{});return Object.assign(e,(e=>{const t={};for(const n in e)_(t,n,e[n]);return t})(d)),()=>{u=!0}},P="0.1.2",S=(e,t,n,r,o)=>{const{destroy:i,onDestroy:s}=r;return r=>{if(!(e instanceof RegExp?e.test(r.origin):"*"===e||e===r.origin))return void o(`Child: Handshake - Received SYN-ACK from origin ${r.origin} which did not match expected origin ${e}`);o("Child: Handshake - Received SYN-ACK, responding with ACK");const l="null"===r.origin?"*":r.origin,a={penpal:g.Ack,methodNames:Object.keys(t),config:n};window.parent.postMessage(a,l);const c={localName:"Child",local:window,remote:window.parent,originForSending:l,originForReceiving:r.origin},u=((e,t,n)=>{const{localName:r,local:o,remote:i,originForSending:s,originForReceiving:l}=e;let a=!1;const c=e=>{if(e.source!==i||e.data.penpal!==g.Call)return;if("*"!==l&&e.origin!==l)return void n(`${r} received message from origin ${e.origin} which did not match expected origin ${l}`);const o=e.data,{methodName:c,args:u,id:p}=o;n(`${r}: Received ${c}() call`);const d=e=>t=>{if(n(`${r}: Sending ${c}() reply`),a)return void n(`${r}: Unable to send ${c}() reply due to destroyed connection`);const o={penpal:g.Reply,id:p,resolution:e,returnValue:t};e===y.Rejected&&t instanceof Error&&(o.returnValue=w(t),o.returnValueIsError=!0);try{i.postMessage(o,s)}catch(l){if(l.name===v.DataCloneError){const e={penpal:g.Reply,id:p,resolution:y.Rejected,returnValue:w(l),returnValueIsError:!0};i.postMessage(e,s)}throw l}};new Promise(e=>e(t[c].apply(t,u))).then(d(y.Fulfilled),d(y.Rejected))};return o.addEventListener(b.Message,c),()=>{a=!0,o.removeEventListener(b.Message,c)}})(c,t,o);s(u);const p={},d=O(p,c,r.data.methodNames,i,o);return s(d),p}};function C(e,t){null==console||console.warn(`[fnApp warn]: ${e}`,t)}function M(e,t){null==console||console.error(`[fnApp error]: ${e}`,t)}const j="_fn_all_event",x="事件订阅数量为空",N=window.__POWERED_BY_FNAPP__?window.__FNAPP.inject.appEventObjMap:/* @__PURE__ */new Map;class F{constructor(e,t){c(this,"id"),c(this,"appName"),c(this,"eventObj"),c(this,"getEventCallBackLength",e=>{var t;return(null==(t=this.eventObj[e])?void 0:t.length)||0}),this.id=e,this.appName=t,this.$clear(),N.get(this.id)||N.set(this.id,{}),this.eventObj=N.get(this.id)}$on(e,t){const n=this.eventObj[e];return n?(n.includes(t)||n.push(t),this):(this.eventObj[e]=[t],this)}$onAll(e){return this.$on(j,e)}$once(e,t){t.$__once=!0,this.$on(e,t)}$off(e,t){const n=this.eventObj[e];if(!e||!n||!n.length)return C(`${e} ${x}`),this;let r,o=n.length;for(;o--;)if(r=n[o],r===t){n.splice(o,1);break}return this}$offAll(e){return this.$off(j,e)}$emit(e,...t){return e=`${this.appName}/${e}`,this.$pureEmit(e,...t)}$pureEmit(e,...t){let n=0;return N.forEach(r=>{if(r[e])for(let i=r[e].length-1;i>=0;i--){n++;const s=r[e][i];s.$__once&&r[e].splice(i,1);try{s(...t)}catch(o){M(o)}}if(r[j])for(let e=r[j].length-1;e>=0;e--){n++;const i=r[j][e];i.$__once&&r[j].splice(e,1);try{i(...t)}catch(o){M(o)}}}),e&&0!==n||C(`${e} ${x}`),this}$clear(){const e=N.get(this.id)||{};return Object.keys(e).forEach(t=>delete e[t]),this}}var R=function(e,t){return(R=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n])})(e,t)};function T(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Class extends value "+String(t)+" is not a constructor or null");function n(){this.constructor=e}R(e,t),e.prototype=null===t?Object.create(t):(n.prototype=t.prototype,new n)}function W(e){var t="function"==typeof Symbol&&Symbol.iterator,n=t&&e[t],r=0;if(n)return n.call(e);if(e&&"number"==typeof e.length)return{next:function(){return e&&r>=e.length&&(e=void 0),{value:e&&e[r++],done:!e}}};throw new TypeError(t?"Object is not iterable.":"Symbol.iterator is not defined.")}function k(e,t){var n="function"==typeof Symbol&&e[Symbol.iterator];if(!n)return e;var r,o,i=n.call(e),s=[];try{for(;(void 0===t||t-- >0)&&!(r=i.next()).done;)s.push(r.value)}catch(l){o={error:l}}finally{try{r&&!r.done&&(n=i.return)&&n.call(i)}finally{if(o)throw o.error}}return s}function V(e,t,n){if(n||2===arguments.length)for(var r,o=0,i=t.length;o<i;o++)!r&&o in t||(r||(r=Array.prototype.slice.call(t,0,o)),r[o]=t[o]);return e.concat(r||Array.prototype.slice.call(t))}function I(e){return"function"==typeof e}"function"==typeof SuppressedError&&SuppressedError;var q,D=((q=function(e){return function(t){e(this),this.message=t?t.length+" errors occurred during unsubscription:\n"+t.map(function(e,t){return t+1+") "+e.toString()}).join("\n "):"",this.name="UnsubscriptionError",this.errors=t}}(function(e){Error.call(e),e.stack=(new Error).stack})).prototype=Object.create(Error.prototype),q.prototype.constructor=q,q);function L(e,t){if(e){var n=e.indexOf(t);0<=n&&e.splice(n,1)}}var z=function(){function e(e){this.initialTeardown=e,this.closed=!1,this._parentage=null,this._finalizers=null}var t;return e.prototype.unsubscribe=function(){var e,t,n,r,o;if(!this.closed){this.closed=!0;var i=this._parentage;if(i)if(this._parentage=null,Array.isArray(i))try{for(var s=W(i),l=s.next();!l.done;l=s.next()){l.value.remove(this)}}catch(h){e={error:h}}finally{try{l&&!l.done&&(t=s.return)&&t.call(s)}finally{if(e)throw e.error}}else i.remove(this);var a=this.initialTeardown;if(I(a))try{a()}catch(f){o=f instanceof D?f.errors:[f]}var c=this._finalizers;if(c){this._finalizers=null;try{for(var u=W(c),p=u.next();!p.done;p=u.next()){var d=p.value;try{Y(d)}catch(g){o=null!=o?o:[],g instanceof D?o=V(V([],k(o)),k(g.errors)):o.push(g)}}}catch(y){n={error:y}}finally{try{p&&!p.done&&(r=u.return)&&r.call(u)}finally{if(n)throw n.error}}}if(o)throw new D(o)}},e.prototype.add=function(t){var n;if(t&&t!==this)if(this.closed)Y(t);else{if(t instanceof e){if(t.closed||t._hasParent(this))return;t._addParent(this)}(this._finalizers=null!==(n=this._finalizers)&&void 0!==n?n:[]).push(t)}},e.prototype._hasParent=function(e){var t=this._parentage;return t===e||Array.isArray(t)&&t.includes(e)},e.prototype._addParent=function(e){var t=this._parentage;this._parentage=Array.isArray(t)?(t.push(e),t):t?[t,e]:e},e.prototype._removeParent=function(e){var t=this._parentage;t===e?this._parentage=null:Array.isArray(t)&&L(t,e)},e.prototype.remove=function(t){var n=this._finalizers;n&&L(n,t),t instanceof e&&t._removeParent(this)},e.EMPTY=((t=new e).closed=!0,t),e}();function J(e){return e instanceof z||e&&"closed"in e&&I(e.remove)&&I(e.add)&&I(e.unsubscribe)}function Y(e){I(e)?e():e.unsubscribe()}z.EMPTY;var H=void 0,U=function(e,t){for(var n=[],r=2;r<arguments.length;r++)n[r-2]=arguments[r];return setTimeout.apply(void 0,V([e,t],k(n)))};function B(){}var K=function(e){function t(t){var n=e.call(this)||this;return n.isStopped=!1,t?(n.destination=t,J(t)&&t.add(n)):n.destination=Z,n}return T(t,e),t.create=function(e,t,n){return new G(e,t,n)},t.prototype.next=function(e){this.isStopped||this._next(e)},t.prototype.error=function(e){this.isStopped||(this.isStopped=!0,this._error(e))},t.prototype.complete=function(){this.isStopped||(this.isStopped=!0,this._complete())},t.prototype.unsubscribe=function(){this.closed||(this.isStopped=!0,e.prototype.unsubscribe.call(this),this.destination=null)},t.prototype._next=function(e){this.destination.next(e)},t.prototype._error=function(e){try{this.destination.error(e)}finally{this.unsubscribe()}},t.prototype._complete=function(){try{this.destination.complete()}finally{this.unsubscribe()}},t}(z),Q=function(){function e(e){this.partialObserver=e}return e.prototype.next=function(e){var t=this.partialObserver;if(t.next)try{t.next(e)}catch(n){X(n)}},e.prototype.error=function(e){var t=this.partialObserver;if(t.error)try{t.error(e)}catch(n){X(n)}else X(e)},e.prototype.complete=function(){var e=this.partialObserver;if(e.complete)try{e.complete()}catch(t){X(t)}},e}(),G=function(e){function t(t,n,r){var o,i=e.call(this)||this;return o=I(t)||!t?{next:null!=t?t:void 0,error:null!=n?n:void 0,complete:null!=r?r:void 0}:t,i.destination=new Q(o),i}return T(t,e),t}(K);function X(e){var t;t=e,U(function(){throw t})}var Z={closed:!0,next:B,error:function(e){throw e},complete:B},ee="function"==typeof Symbol&&Symbol.observable||"@@observable";function te(e){return e}var ne=function(){function e(e){e&&(this._subscribe=e)}return e.prototype.lift=function(t){var n=new e;return n.source=this,n.operator=t,n},e.prototype.subscribe=function(e,t,n){var r,o=this,i=(r=e)&&r instanceof K||function(e){return e&&I(e.next)&&I(e.error)&&I(e.complete)}(r)&&J(r)?e:new G(e,t,n);return function(){var e=o,t=e.operator,n=e.source;i.add(t?t.call(i,n):n?o._subscribe(i):o._trySubscribe(i))}(),i},e.prototype._trySubscribe=function(e){try{return this._subscribe(e)}catch(t){e.error(t)}},e.prototype.forEach=function(e,t){var n=this;return new(t=re(t))(function(t,r){var o=new G({next:function(t){try{e(t)}catch(n){r(n),o.unsubscribe()}},error:r,complete:t});n.subscribe(o)})},e.prototype._subscribe=function(e){var t;return null===(t=this.source)||void 0===t?void 0:t.subscribe(e)},e.prototype[ee]=function(){return this},e.prototype.pipe=function(){for(var e,t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];return(0===(e=t).length?te:1===e.length?e[0]:function(t){return e.reduce(function(e,t){return t(e)},t)})(this)},e.prototype.toPromise=function(e){var t=this;return new(e=re(e))(function(e,n){var r;t.subscribe(function(e){return r=e},function(e){return n(e)},function(){return e(r)})})},e.create=function(t){return new e(t)},e}();function re(e){var t;return null!==(t=null!=e?e:H)&&void 0!==t?t:Promise}const oe=(e={})=>{const{parentOrigin:t="*",timeout:n,debug:r=!1,config:o={}}=e,i=(e=>(...t)=>{e&&console.log("[Penpal]",...t)})(r),s=((e,t)=>{const n=[];let r=!1;return{destroy(o){r||(r=!0,t(`${e}: Destroying connection`),n.forEach(e=>{e(o)}))},onDestroy(e){r?e():n.push(e)}}})("App",i),{destroy:c,onDestroy:p}=s,d=new Promise((e,r)=>{const d=((e,t)=>{let n;return void 0!==e&&(n=window.setTimeout(()=>{const n=new Error(`Connection timed out after ${e}ms`);n.code=m.ConnectionTimeout,t(n)},e)),()=>{clearTimeout(n)}})(n,c),h=n=>{if((()=>{try{clearTimeout(void 0)}catch(e){return!1}return!0})()&&n.source===parent&&n.data&&n.data.penpal===g.SynAck){const r=n.data,c=new F(r.id,r.appName),p=new F(r.id+"/api",r.appName),f=r.id,g={},y=e=>"api/"+e,m=e=>function(t){const n=y(e);p.$on(n,function e(r){t.next(r),r.reqid&&"succ"===r.result&&(t.complete(),p.$off(n,e))})},v=E({$emit:(e,...t)=>{c.getEventCallBackLength(e)>0?c.$pureEmit(e,...t):null==g||g.$off(e,()=>{},!0)},onQueryResult:e=>{p.$pureEmit(y(e.reqid),e)},onOsNotify:e=>{}}),w=S(t,v,a(l({},o),{postmateVersion:P}),s,i)(n),$={},A=w.bus;if(A){const e=(e,t,n)=>{n?(t.$__once=!0,c.$once(e,t)):c.$on(e,t),$[e]?$[e].push(t):($[e]=[t],A.$on(e,f))};Object.assign(g,A,{$on:e,$off:(e,t,n)=>{var r;if($[e]){const n=$[e].indexOf(t);n>-1&&($[e].splice(n,1),c.$off(e,t))}n&&delete $[e],(null==(r=$[e])?void 0:r.length)||(A.$off(e,f),delete $[e])},$once:(t,n)=>{e(t,n,!0)}})}const _=a(l({},w),{bus:g,query:(e,t)=>u(null,null,function*(){const n=(null==e?void 0:e.reqid)||(yield w.genReqId());return new Promise((r,o)=>{w.query(a(l({},e),{reqid:n}),t).then(e=>{"succ"===(null==e?void 0:e.result)?r(e):o(e)})})}),createObservableQuery:(e,...t)=>u(null,[e,...t],function*(e,t={}){const n=(null==e?void 0:e.reqid)||(yield w.genReqId()),r=new ne(m(n));return w.query(a(l({},e),{reqid:n}),a(l({},t),{observable:!0})),r})});w&&(window.removeEventListener(b.Message,h),d(),e({methods:_,config:n.data.config}))}};window.addEventListener(b.Message,h),(()=>{i("Child: Handshake - Sending SYN");const e={penpal:g.Syn},n=t instanceof RegExp?"*":t;window.parent.postMessage(e,n)})(),p(e=>{window.removeEventListener(b.Message,h),e&&r(e)})});return{promise:d,destroy(){c()}}};let ie=null;class se{constructor(){c(this,"options"),c(this,"flutterInAppWebView",null),c(this,"osConnector",null),c(this,"initPromise"),c(this,"isWeb",!0);let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};this.options=e,e.debug&&(p.setDebug(!0),p.log("Trim App initialized with debug mode")),p.log("Options:",e),this.initPromise=this.init()}init(){return u(this,null,function*(){return p.log("Starting initialization..."),new Promise(e=>{var t;d()?(this.isWeb=!1,p.log("Initializing mobile platform..."),new Promise(e=>{var t;h||(null==(t=window.flutter_inappwebview)?void 0:t._platformReady)?(p.log("flutter_inappwebview is ready"),e(window.flutter_inappwebview)):window.addEventListener("flutterInAppWebViewPlatformReady",()=>{p.log("flutter_inappwebview is ready"),e(window.flutter_inappwebview)})}).then(t=>{this.flutterInAppWebView=t,p.log("Mobile platform initialized"),e()})):(this.isWeb=!0,p.log("Initializing web platform..."),(ie||(ie=oe({debug:null==t?void 0:t.debug})),ie.promise).then(t=>{this.osConnector=t,p.log("Web platform initialized"),e()}))})})}callAppMethod(e,t){return u(this,null,function*(){return this.flutterInAppWebView?((e,t,n)=>u(null,null,function*(){p.log(`callHandler: ${t}, params: ${n}`);const r=yield e.callHandler(t,n);if(r)try{const e=JSON.parse(r);p.log(`messageData: ${JSON.stringify(e)}`);const t=JSON.parse(e.result);return p.log(`appMessage: ${JSON.stringify(t)}`),t}catch(o){return p.error(`error: ${o}`),null}return null}))(this.flutterInAppWebView,e,t):null})}ready(){return u(this,null,function*(){return this.initPromise})}getOptions(){return this.options}setOptions(e){this.options=l(l({},this.options),e),void 0!==e.debug&&p.setDebug(e.debug),p.log("Options updated:",this.options)}getPlatformConfig(){return u(this,null,function*(){var e;if(yield this.initPromise,this.isWeb){const t=yield null==(e=this.osConnector)?void 0:e.methods.getPlatformConfig();if(!t)throw new Error("Failed to get platform config");return t}{const e=yield this.callAppMethod("getAppMessage");if(!e)throw new Error("Failed to get platform config");return{theme:e.nightMode,language:e.language,appVersion:e.appVersion,systemVersion:e.systemVersion,format:{date:e.dateFormat,time:e.timeFormat}}}})}setTitle(e){return u(this,null,function*(){var t;return yield this.initPromise,p.log("Setting title:",e),this.isWeb?null==(t=this.osConnector)?void 0:t.methods.setTitle(e):this.callAppMethod("updateTitle",e)})}setExitPageTips(e){return u(this,null,function*(){var t;return yield this.initPromise,this.isWeb?null==(t=this.osConnector)?void 0:t.methods.setExitPageTips(e):this.callAppMethod("setExitPageTips",e?JSON.stringify(e):"")})}openFile(e){return u(this,null,function*(){var t;return yield this.initPromise,p.log("Opening file:",e),this.isWeb?null==(t=this.osConnector)?void 0:t.methods.openFile(e):this.callAppMethod("openFile",e)})}openFileManager(e){return u(this,null,function*(){var t;return yield this.initPromise,p.log("Opening file manager:",e),this.isWeb?null==(t=this.osConnector)?void 0:t.methods.openFileManagerApp(e):this.callAppMethod("openFileManager",e)})}openApp(e){return u(this,null,function*(){var t;if(yield this.initPromise,p.log("Opening app:",e),this.isWeb)return null==(t=this.osConnector)?void 0:t.methods.openApp(e);throw new Error("openApp is not supported on mobile platform")})}openCustomApp(e,t){return u(this,null,function*(){var n;if(yield this.initPromise,p.log("Opening custom app:",e,t),this.isWeb)return null==(n=this.osConnector)?void 0:n.methods.openCustomApp(e,t);throw new Error("openCustomApp is not supported on mobile platform")})}openURL(e,t,n){return u(this,null,function*(){if(yield this.initPromise,p.log("Opening URL:",e,t,n),!this.isWeb)return this.callAppMethod("openSystemBrowser",e);if(!n)return void window.open(e,t,n);const r=window.open("",t,n);r&&(r.location.href=e)})}close(){return u(this,null,function*(){var e;return yield this.initPromise,this.isWeb?null==(e=this.osConnector)?void 0:e.methods.close():this.callAppMethod("exitPage")})}query(e,t){return u(this,null,function*(){var n;if(yield this.initPromise,this.isWeb){const r=yield null==(n=this.osConnector)?void 0:n.methods.query(e,t);if(!r)throw new Error("Failed to query");return r}throw new Error("query is not supported on mobile platform")})}$on(e,t){return u(this,null,function*(){var n;if(yield this.initPromise,!this.isWeb)throw new Error("$on is not supported on mobile platform");(null==(n=this.osConnector)?void 0:n.methods.bus).$on(e,t)})}$off(e,t){return u(this,null,function*(){var n;if(yield this.initPromise,!this.isWeb)throw new Error("$off is not supported on mobile platform");(null==(n=this.osConnector)?void 0:n.methods.bus).$off(e,t)})}$once(e,t){return u(this,null,function*(){var n;if(yield this.initPromise,!this.isWeb)throw new Error("$once is not supported on mobile platform");(null==(n=this.osConnector)?void 0:n.methods.bus).$once(e,t)})}pickFile(e){return u(this,null,function*(){var t;return yield this.initPromise,this.isWeb?null==(t=this.osConnector)?void 0:t.methods.pickFile(e):this.callAppMethod("openFolder",JSON.stringify(e))})}}export{se as TrimApp,se as default};
|
package/dist/index.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).TrimApp={})}(this,function(e){"use strict";var t=Object.defineProperty,n=Object.defineProperties,r=Object.getOwnPropertyDescriptors,o=Object.getOwnPropertySymbols,i=Object.prototype.hasOwnProperty,s=Object.prototype.propertyIsEnumerable,l=(e,n,r)=>n in e?t(e,n,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[n]=r,a=(e,t)=>{for(var n in t||(t={}))i.call(t,n)&&l(e,n,t[n]);if(o)for(var n of o(t))s.call(t,n)&&l(e,n,t[n]);return e},c=(e,t)=>n(e,r(t)),u=(e,t,n)=>l(e,"symbol"!=typeof t?t+"":t,n),p=(e,t,n)=>new Promise((r,o)=>{var i=e=>{try{l(n.next(e))}catch(t){o(t)}},s=e=>{try{l(n.throw(e))}catch(t){o(t)}},l=e=>e.done?r(e.value):Promise.resolve(e.value).then(i,s);l((n=n.apply(e,t)).next())});const d=new class{constructor(){u(this,"debug",!1)}setDebug(e){this.debug=e}isDebug(){return this.debug}log(){if(this.debug){for(var e=arguments.length,t=new Array(e),n=0;n<e;n++)t[n]=arguments[n];console.log("[Trim App]",...t)}}warn(){if(this.debug){for(var e=arguments.length,t=new Array(e),n=0;n<e;n++)t[n]=arguments[n];console.warn("[Trim App]",...t)}}error(){if(this.debug){for(var e=arguments.length,t=new Array(e),n=0;n<e;n++)t[n]=arguments[n];console.error("[Trim App]",...t)}}info(){if(this.debug){for(var e=arguments.length,t=new Array(e),n=0;n<e;n++)t[n]=arguments[n];console.info("[Trim App]",...t)}}};const f=()=>function(e){const t=/FNOS\/([\d.]+)|FNAppType\/(\w+)|FNAppVer\/([\d.]+)/g,n={};let r;for(;null!==(r=t.exec(e));)r[1]&&(n.fnOSVersion=r[1]),r[2]&&(n.fnAppType=r[2]),r[3]&&(n.fnAppVersion=r[3]);return n}(navigator.userAgent).fnAppVersion;let h=!1;window.addEventListener("flutterInAppWebViewPlatformReady",()=>{h=!0});const g=new Map;f()&&(window.fnAppMessage=e=>{d.log("app-message",e);const t=JSON.parse(e),n=g.get(t.reqId);n&&(n(t),g.delete(t.reqId))});var y=(e=>(e.Call="call",e.Reply="reply",e.Syn="syn",e.SynAck="synAck",e.Ack="ack",e))(y||{}),v=(e=>(e.Fulfilled="fulfilled",e.Rejected="rejected",e))(v||{}),m=(e=>(e.ConnectionDestroyed="ConnectionDestroyed",e.ConnectionTimeout="ConnectionTimeout",e.NoIframeSrc="NoIframeSrc",e))(m||{}),b=(e=>(e.DataCloneError="DataCloneError",e))(b||{}),w=(e=>(e.Message="message",e))(w||{});const _=({name:e,message:t,stack:n})=>({name:e,message:t,stack:n});let $=0;const A=e=>e?e.split("."):[],O=(e,t,n)=>{const r=A(t);return r.reduce((e,t,o)=>(void 0===e[t]&&(e[t]={}),o===r.length-1&&(e[t]=n),e[t]),e),e},E=(e,t)=>{const n={};return Object.keys(e).forEach(r=>{const o=e[r],i=((e,t)=>{const n=A(t||"");return n.push(e),(e=>e.join("."))(n)})(r,t);"object"==typeof o&&Object.assign(n,E(o,i)),"function"==typeof o&&(n[i]=o)}),n},P=(e,t,n,r,o)=>{const{localName:i,local:s,remote:l,originForSending:a,originForReceiving:c}=t;let u=!1;o(`${i}: Connecting call sender`);const p=e=>(...t)=>{let n;o(`${i}: Sending ${e}() call`);try{l.closed&&(n=!0)}catch(p){n=!0}if(n&&r(),u){const t=new Error(`Unable to send ${e}() call due to destroyed connection`);throw t.code=m.ConnectionDestroyed,t}return new Promise((n,r)=>{const u=++$,p=t=>{if(t.source!==l||t.data.penpal!==y.Reply||t.data.id!==u)return;if("*"!==c&&t.origin!==c)return void o(`${i} received message from origin ${t.origin} which did not match expected origin ${c}`);const a=t.data;o(`${i}: Received ${e}() reply`),s.removeEventListener(w.Message,p);let d=a.returnValue;a.returnValueIsError&&(d=(e=>{const t=new Error;return Object.keys(e).forEach(n=>t[n]=e[n]),t})(d)),(a.resolution===v.Fulfilled?n:r)(d)};s.addEventListener(w.Message,p);const d={penpal:y.Call,id:u,methodName:e,args:t};l.postMessage(d,a)})},d=n.reduce((e,t)=>(e[t]=p(t),e),{});return Object.assign(e,(e=>{const t={};for(const n in e)O(t,n,e[n]);return t})(d)),()=>{u=!0}},S="0.1.2",C=(e,t,n,r,o)=>{const{destroy:i,onDestroy:s}=r;return r=>{if(!(e instanceof RegExp?e.test(r.origin):"*"===e||e===r.origin))return void o(`Child: Handshake - Received SYN-ACK from origin ${r.origin} which did not match expected origin ${e}`);o("Child: Handshake - Received SYN-ACK, responding with ACK");const l="null"===r.origin?"*":r.origin,a={penpal:y.Ack,methodNames:Object.keys(t),config:n};window.parent.postMessage(a,l);const c={localName:"Child",local:window,remote:window.parent,originForSending:l,originForReceiving:r.origin},u=((e,t,n)=>{const{localName:r,local:o,remote:i,originForSending:s,originForReceiving:l}=e;let a=!1;const c=e=>{if(e.source!==i||e.data.penpal!==y.Call)return;if("*"!==l&&e.origin!==l)return void n(`${r} received message from origin ${e.origin} which did not match expected origin ${l}`);const o=e.data,{methodName:c,args:u,id:p}=o;n(`${r}: Received ${c}() call`);const d=e=>t=>{if(n(`${r}: Sending ${c}() reply`),a)return void n(`${r}: Unable to send ${c}() reply due to destroyed connection`);const o={penpal:y.Reply,id:p,resolution:e,returnValue:t};e===v.Rejected&&t instanceof Error&&(o.returnValue=_(t),o.returnValueIsError=!0);try{i.postMessage(o,s)}catch(l){if(l.name===b.DataCloneError){const e={penpal:y.Reply,id:p,resolution:v.Rejected,returnValue:_(l),returnValueIsError:!0};i.postMessage(e,s)}throw l}};new Promise(e=>e(t[c].apply(t,u))).then(d(v.Fulfilled),d(v.Rejected))};return o.addEventListener(w.Message,c),()=>{a=!0,o.removeEventListener(w.Message,c)}})(c,t,o);s(u);const p={},d=P(p,c,r.data.methodNames,i,o);return s(d),p}};function j(e,t){null==console||console.warn(`[fnApp warn]: ${e}`,t)}function x(e,t){null==console||console.error(`[fnApp error]: ${e}`,t)}const M="_fn_all_event",T="事件订阅数量为空",N=window.__POWERED_BY_FNAPP__?window.__FNAPP.inject.appEventObjMap:new Map;class F{constructor(e,t){u(this,"id"),u(this,"appName"),u(this,"eventObj"),u(this,"getEventCallBackLength",e=>{var t;return(null==(t=this.eventObj[e])?void 0:t.length)||0}),this.id=e,this.appName=t,this.$clear(),N.get(this.id)||N.set(this.id,{}),this.eventObj=N.get(this.id)}$on(e,t){const n=this.eventObj[e];return n?(n.includes(t)||n.push(t),this):(this.eventObj[e]=[t],this)}$onAll(e){return this.$on(M,e)}$once(e,t){t.$__once=!0,this.$on(e,t)}$off(e,t){const n=this.eventObj[e];if(!e||!n||!n.length)return j(`${e} ${T}`),this;let r,o=n.length;for(;o--;)if(r=n[o],r===t){n.splice(o,1);break}return this}$offAll(e){return this.$off(M,e)}$emit(e,...t){return e=`${this.appName}/${e}`,this.$pureEmit(e,...t)}$pureEmit(e,...t){let n=0;return N.forEach(r=>{if(r[e])for(let i=r[e].length-1;i>=0;i--){n++;const s=r[e][i];s.$__once&&r[e].splice(i,1);try{s(...t)}catch(o){x(o)}}if(r[M])for(let e=r[M].length-1;e>=0;e--){n++;const i=r[M][e];i.$__once&&r[M].splice(e,1);try{i(...t)}catch(o){x(o)}}}),e&&0!==n||j(`${e} ${T}`),this}$clear(){const e=N.get(this.id)||{};return Object.keys(e).forEach(t=>delete e[t]),this}}var R=function(e,t){return(R=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n])})(e,t)};function k(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Class extends value "+String(t)+" is not a constructor or null");function n(){this.constructor=e}R(e,t),e.prototype=null===t?Object.create(t):(n.prototype=t.prototype,new n)}function V(e){var t="function"==typeof Symbol&&Symbol.iterator,n=t&&e[t],r=0;if(n)return n.call(e);if(e&&"number"==typeof e.length)return{next:function(){return e&&r>=e.length&&(e=void 0),{value:e&&e[r++],done:!e}}};throw new TypeError(t?"Object is not iterable.":"Symbol.iterator is not defined.")}function W(e,t){var n="function"==typeof Symbol&&e[Symbol.iterator];if(!n)return e;var r,o,i=n.call(e),s=[];try{for(;(void 0===t||t-- >0)&&!(r=i.next()).done;)s.push(r.value)}catch(l){o={error:l}}finally{try{r&&!r.done&&(n=i.return)&&n.call(i)}finally{if(o)throw o.error}}return s}function I(e,t,n){if(n||2===arguments.length)for(var r,o=0,i=t.length;o<i;o++)!r&&o in t||(r||(r=Array.prototype.slice.call(t,0,o)),r[o]=t[o]);return e.concat(r||Array.prototype.slice.call(t))}function q(e){return"function"==typeof e}"function"==typeof SuppressedError&&SuppressedError;var D,z=((D=function(e){return function(t){e(this),this.message=t?t.length+" errors occurred during unsubscription:\n"+t.map(function(e,t){return t+1+") "+e.toString()}).join("\n "):"",this.name="UnsubscriptionError",this.errors=t}}(function(e){Error.call(e),e.stack=(new Error).stack})).prototype=Object.create(Error.prototype),D.prototype.constructor=D,D);function L(e,t){if(e){var n=e.indexOf(t);0<=n&&e.splice(n,1)}}var J=function(){function e(e){this.initialTeardown=e,this.closed=!1,this._parentage=null,this._finalizers=null}var t;return e.prototype.unsubscribe=function(){var e,t,n,r,o;if(!this.closed){this.closed=!0;var i=this._parentage;if(i)if(this._parentage=null,Array.isArray(i))try{for(var s=V(i),l=s.next();!l.done;l=s.next()){l.value.remove(this)}}catch(f){e={error:f}}finally{try{l&&!l.done&&(t=s.return)&&t.call(s)}finally{if(e)throw e.error}}else i.remove(this);var a=this.initialTeardown;if(q(a))try{a()}catch(h){o=h instanceof z?h.errors:[h]}var c=this._finalizers;if(c){this._finalizers=null;try{for(var u=V(c),p=u.next();!p.done;p=u.next()){var d=p.value;try{H(d)}catch(g){o=null!=o?o:[],g instanceof z?o=I(I([],W(o)),W(g.errors)):o.push(g)}}}catch(y){n={error:y}}finally{try{p&&!p.done&&(r=u.return)&&r.call(u)}finally{if(n)throw n.error}}}if(o)throw new z(o)}},e.prototype.add=function(t){var n;if(t&&t!==this)if(this.closed)H(t);else{if(t instanceof e){if(t.closed||t._hasParent(this))return;t._addParent(this)}(this._finalizers=null!==(n=this._finalizers)&&void 0!==n?n:[]).push(t)}},e.prototype._hasParent=function(e){var t=this._parentage;return t===e||Array.isArray(t)&&t.includes(e)},e.prototype._addParent=function(e){var t=this._parentage;this._parentage=Array.isArray(t)?(t.push(e),t):t?[t,e]:e},e.prototype._removeParent=function(e){var t=this._parentage;t===e?this._parentage=null:Array.isArray(t)&&L(t,e)},e.prototype.remove=function(t){var n=this._finalizers;n&&L(n,t),t instanceof e&&t._removeParent(this)},e.EMPTY=((t=new e).closed=!0,t),e}();function Y(e){return e instanceof J||e&&"closed"in e&&q(e.remove)&&q(e.add)&&q(e.unsubscribe)}function H(e){q(e)?e():e.unsubscribe()}J.EMPTY;var B=void 0,K=function(e,t){for(var n=[],r=2;r<arguments.length;r++)n[r-2]=arguments[r];return setTimeout.apply(void 0,I([e,t],W(n)))};function U(){}var Q=function(e){function t(t){var n=e.call(this)||this;return n.isStopped=!1,t?(n.destination=t,Y(t)&&t.add(n)):n.destination=ee,n}return k(t,e),t.create=function(e,t,n){return new X(e,t,n)},t.prototype.next=function(e){this.isStopped||this._next(e)},t.prototype.error=function(e){this.isStopped||(this.isStopped=!0,this._error(e))},t.prototype.complete=function(){this.isStopped||(this.isStopped=!0,this._complete())},t.prototype.unsubscribe=function(){this.closed||(this.isStopped=!0,e.prototype.unsubscribe.call(this),this.destination=null)},t.prototype._next=function(e){this.destination.next(e)},t.prototype._error=function(e){try{this.destination.error(e)}finally{this.unsubscribe()}},t.prototype._complete=function(){try{this.destination.complete()}finally{this.unsubscribe()}},t}(J),G=function(){function e(e){this.partialObserver=e}return e.prototype.next=function(e){var t=this.partialObserver;if(t.next)try{t.next(e)}catch(n){Z(n)}},e.prototype.error=function(e){var t=this.partialObserver;if(t.error)try{t.error(e)}catch(n){Z(n)}else Z(e)},e.prototype.complete=function(){var e=this.partialObserver;if(e.complete)try{e.complete()}catch(t){Z(t)}},e}(),X=function(e){function t(t,n,r){var o,i=e.call(this)||this;return o=q(t)||!t?{next:null!=t?t:void 0,error:null!=n?n:void 0,complete:null!=r?r:void 0}:t,i.destination=new G(o),i}return k(t,e),t}(Q);function Z(e){var t;t=e,K(function(){throw t})}var ee={closed:!0,next:U,error:function(e){throw e},complete:U},te="function"==typeof Symbol&&Symbol.observable||"@@observable";function ne(e){return e}var re=function(){function e(e){e&&(this._subscribe=e)}return e.prototype.lift=function(t){var n=new e;return n.source=this,n.operator=t,n},e.prototype.subscribe=function(e,t,n){var r,o=this,i=(r=e)&&r instanceof Q||function(e){return e&&q(e.next)&&q(e.error)&&q(e.complete)}(r)&&Y(r)?e:new X(e,t,n);return function(){var e=o,t=e.operator,n=e.source;i.add(t?t.call(i,n):n?o._subscribe(i):o._trySubscribe(i))}(),i},e.prototype._trySubscribe=function(e){try{return this._subscribe(e)}catch(t){e.error(t)}},e.prototype.forEach=function(e,t){var n=this;return new(t=oe(t))(function(t,r){var o=new X({next:function(t){try{e(t)}catch(n){r(n),o.unsubscribe()}},error:r,complete:t});n.subscribe(o)})},e.prototype._subscribe=function(e){var t;return null===(t=this.source)||void 0===t?void 0:t.subscribe(e)},e.prototype[te]=function(){return this},e.prototype.pipe=function(){for(var e,t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];return(0===(e=t).length?ne:1===e.length?e[0]:function(t){return e.reduce(function(e,t){return t(e)},t)})(this)},e.prototype.toPromise=function(e){var t=this;return new(e=oe(e))(function(e,n){var r;t.subscribe(function(e){return r=e},function(e){return n(e)},function(){return e(r)})})},e.create=function(t){return new e(t)},e}();function oe(e){var t;return null!==(t=null!=e?e:B)&&void 0!==t?t:Promise}const ie=(e={})=>{const{parentOrigin:t="*",timeout:n,debug:r=!1,config:o={}}=e,i=(e=>(...t)=>{e&&console.log("[Penpal]",...t)})(r),s=((e,t)=>{const n=[];let r=!1;return{destroy(o){r||(r=!0,t(`${e}: Destroying connection`),n.forEach(e=>{e(o)}))},onDestroy(e){r?e():n.push(e)}}})("App",i),{destroy:l,onDestroy:u}=s,d=new Promise((e,r)=>{const d=((e,t)=>{let n;return void 0!==e&&(n=window.setTimeout(()=>{const n=new Error(`Connection timed out after ${e}ms`);n.code=m.ConnectionTimeout,t(n)},e)),()=>{clearTimeout(n)}})(n,l),f=n=>{if((()=>{try{clearTimeout(void 0)}catch(e){return!1}return!0})()&&n.source===parent&&n.data&&n.data.penpal===y.SynAck){const r=n.data,l=new F(r.id,r.appName),u=new F(r.id+"/api",r.appName),h=r.id,g={},y=e=>"api/"+e,v=e=>function(t){const n=y(e);u.$on(n,function e(r){t.next(r),r.reqid&&"succ"===r.result&&(t.complete(),u.$off(n,e))})},m=E({$emit:(e,...t)=>{l.getEventCallBackLength(e)>0?l.$pureEmit(e,...t):null==g||g.$off(e,()=>{},!0)},onQueryResult:e=>{u.$pureEmit(y(e.reqid),e)},onOsNotify:e=>{}}),b=C(t,m,c(a({},o),{postmateVersion:S}),s,i)(n),_={},$=b.bus;if($){const e=(e,t,n)=>{n?(t.$__once=!0,l.$once(e,t)):l.$on(e,t),_[e]?_[e].push(t):(_[e]=[t],$.$on(e,h))};Object.assign(g,$,{$on:e,$off:(e,t,n)=>{var r;if(_[e]){const n=_[e].indexOf(t);n>-1&&(_[e].splice(n,1),l.$off(e,t))}n&&delete _[e],(null==(r=_[e])?void 0:r.length)||($.$off(e,h),delete _[e])},$once:(t,n)=>{e(t,n,!0)}})}const A=c(a({},b),{bus:g,query:(e,t)=>p(null,null,function*(){const n=(null==e?void 0:e.reqid)||(yield b.genReqId());return new Promise((r,o)=>{b.query(c(a({},e),{reqid:n}),t).then(e=>{"succ"===(null==e?void 0:e.result)?r(e):o(e)})})}),createObservableQuery:(e,...t)=>p(null,[e,...t],function*(e,t={}){const n=(null==e?void 0:e.reqid)||(yield b.genReqId()),r=new re(v(n));return b.query(c(a({},e),{reqid:n}),c(a({},t),{observable:!0})),r})});b&&(window.removeEventListener(w.Message,f),d(),e({methods:A,config:n.data.config}))}};window.addEventListener(w.Message,f),(()=>{i("Child: Handshake - Sending SYN");const e={penpal:y.Syn},n=t instanceof RegExp?"*":t;window.parent.postMessage(e,n)})(),u(e=>{window.removeEventListener(w.Message,f),e&&r(e)})});return{promise:d,destroy(){l()}}};let se=null;class le{constructor(){u(this,"options"),u(this,"flutterInAppWebView",null),u(this,"osConnector",null),u(this,"initPromise"),u(this,"isWeb",!0);let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};this.options=e,e.debug&&(d.setDebug(!0),d.log("Trim App initialized with debug mode")),d.log("Options:",e),this.initPromise=this.init()}init(){return p(this,null,function*(){return d.log("Starting initialization..."),new Promise(e=>{var t;f()?(this.isWeb=!1,d.log("Initializing mobile platform..."),new Promise(e=>{var t;h||(null==(t=window.flutter_inappwebview)?void 0:t._platformReady)?(d.log("flutter_inappwebview is ready"),e(window.flutter_inappwebview)):window.addEventListener("flutterInAppWebViewPlatformReady",()=>{d.log("flutter_inappwebview is ready"),e(window.flutter_inappwebview)})}).then(t=>{this.flutterInAppWebView=t,d.log("Mobile platform initialized"),e()})):(this.isWeb=!0,d.log("Initializing web platform..."),(se||(se=ie({debug:null==t?void 0:t.debug})),se.promise).then(t=>{this.osConnector=t,d.log("Web platform initialized"),e()}))})})}callAppMethod(e,t){return p(this,null,function*(){return this.flutterInAppWebView?((e,t,n)=>p(null,null,function*(){d.log(`callHandler: ${t}, params: ${n}`);const r=yield e.callHandler(t,n);if(r)try{const e=JSON.parse(r);d.log(`messageData: ${JSON.stringify(e)}`);const t=JSON.parse(e.result);return d.log(`appMessage: ${JSON.stringify(t)}`),t}catch(o){return d.error(`error: ${o}`),null}return null}))(this.flutterInAppWebView,e,t):null})}ready(){return p(this,null,function*(){return this.initPromise})}getOptions(){return this.options}setOptions(e){this.options=a(a({},this.options),e),void 0!==e.debug&&d.setDebug(e.debug),d.log("Options updated:",this.options)}getPlatformConfig(){return p(this,null,function*(){var e;if(yield this.initPromise,this.isWeb){const t=yield null==(e=this.osConnector)?void 0:e.methods.getPlatformConfig();if(!t)throw new Error("Failed to get platform config");return t}{const e=yield this.callAppMethod("getAppMessage");if(!e)throw new Error("Failed to get platform config");return{theme:e.nightMode,language:e.language,appVersion:e.appVersion,systemVersion:e.systemVersion,format:{date:e.dateFormat,time:e.timeFormat}}}})}setTitle(e){return p(this,null,function*(){var t;return yield this.initPromise,d.log("Setting title:",e),this.isWeb?null==(t=this.osConnector)?void 0:t.methods.setTitle(e):this.callAppMethod("updateTitle",e)})}setExitPageTips(e){return p(this,null,function*(){var t;return yield this.initPromise,this.isWeb?null==(t=this.osConnector)?void 0:t.methods.setExitPageTips(e):this.callAppMethod("setExitPageTips",e?JSON.stringify(e):"")})}openFile(e){return p(this,null,function*(){var t;return yield this.initPromise,d.log("Opening file:",e),this.isWeb?null==(t=this.osConnector)?void 0:t.methods.openFile(e):this.callAppMethod("openFile",e)})}openFileManager(e){return p(this,null,function*(){var t;return yield this.initPromise,d.log("Opening file manager:",e),this.isWeb?null==(t=this.osConnector)?void 0:t.methods.openFileManagerApp(e):this.callAppMethod("openFileManager",e)})}openApp(e){return p(this,null,function*(){var t;if(yield this.initPromise,d.log("Opening app:",e),this.isWeb)return null==(t=this.osConnector)?void 0:t.methods.openApp(e);throw new Error("openApp is not supported on mobile platform")})}close(){return p(this,null,function*(){var e;return yield this.initPromise,this.isWeb?null==(e=this.osConnector)?void 0:e.methods.close():this.callAppMethod("exitPage")})}query(e,t){return p(this,null,function*(){var n;if(yield this.initPromise,this.isWeb){const r=yield null==(n=this.osConnector)?void 0:n.methods.query(e,t);if(!r)throw new Error("Failed to query");return r}throw new Error("query is not supported on mobile platform")})}pickFile(e){return p(this,null,function*(){var t;return yield this.initPromise,this.isWeb?null==(t=this.osConnector)?void 0:t.methods.pickFile(e):this.callAppMethod("openFolder",JSON.stringify(e))})}}e.TrimApp=le,e.default=le,Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
|
|
1
|
+
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).TrimApp={})}(this,function(e){"use strict";var t=Object.defineProperty,n=Object.defineProperties,r=Object.getOwnPropertyDescriptors,o=Object.getOwnPropertySymbols,i=Object.prototype.hasOwnProperty,s=Object.prototype.propertyIsEnumerable,l=(e,n,r)=>n in e?t(e,n,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[n]=r,a=(e,t)=>{for(var n in t||(t={}))i.call(t,n)&&l(e,n,t[n]);if(o)for(var n of o(t))s.call(t,n)&&l(e,n,t[n]);return e},u=(e,t)=>n(e,r(t)),c=(e,t,n)=>l(e,"symbol"!=typeof t?t+"":t,n),p=(e,t,n)=>new Promise((r,o)=>{var i=e=>{try{l(n.next(e))}catch(t){o(t)}},s=e=>{try{l(n.throw(e))}catch(t){o(t)}},l=e=>e.done?r(e.value):Promise.resolve(e.value).then(i,s);l((n=n.apply(e,t)).next())});const d=new class{constructor(){c(this,"debug",!1)}setDebug(e){this.debug=e}isDebug(){return this.debug}log(){if(this.debug){for(var e=arguments.length,t=new Array(e),n=0;n<e;n++)t[n]=arguments[n];console.log("[Trim App]",...t)}}warn(){if(this.debug){for(var e=arguments.length,t=new Array(e),n=0;n<e;n++)t[n]=arguments[n];console.warn("[Trim App]",...t)}}error(){if(this.debug){for(var e=arguments.length,t=new Array(e),n=0;n<e;n++)t[n]=arguments[n];console.error("[Trim App]",...t)}}info(){if(this.debug){for(var e=arguments.length,t=new Array(e),n=0;n<e;n++)t[n]=arguments[n];console.info("[Trim App]",...t)}}};const f=()=>function(e){const t=/FNOS\/([\d.]+)|FNAppType\/(\w+)|FNAppVer\/([\d.]+)/g,n={};let r;for(;null!==(r=t.exec(e));)r[1]&&(n.fnOSVersion=r[1]),r[2]&&(n.fnAppType=r[2]),r[3]&&(n.fnAppVersion=r[3]);return n}(navigator.userAgent).fnAppVersion;let h=!1;window.addEventListener("flutterInAppWebViewPlatformReady",()=>{h=!0});const g=new Map;f()&&(window.fnAppMessage=e=>{d.log("app-message",e);const t=JSON.parse(e),n=g.get(t.reqId);n&&(n(t),g.delete(t.reqId))});var y=(e=>(e.Call="call",e.Reply="reply",e.Syn="syn",e.SynAck="synAck",e.Ack="ack",e))(y||{}),m=(e=>(e.Fulfilled="fulfilled",e.Rejected="rejected",e))(m||{}),v=(e=>(e.ConnectionDestroyed="ConnectionDestroyed",e.ConnectionTimeout="ConnectionTimeout",e.NoIframeSrc="NoIframeSrc",e))(v||{}),b=(e=>(e.DataCloneError="DataCloneError",e))(b||{}),w=(e=>(e.Message="message",e))(w||{});const $=({name:e,message:t,stack:n})=>({name:e,message:t,stack:n});let A=0;const _=e=>e?e.split("."):[],E=(e,t,n)=>{const r=_(t);return r.reduce((e,t,o)=>(void 0===e[t]&&(e[t]={}),o===r.length-1&&(e[t]=n),e[t]),e),e},O=(e,t)=>{const n={};return Object.keys(e).forEach(r=>{const o=e[r],i=((e,t)=>{const n=_(t||"");return n.push(e),(e=>e.join("."))(n)})(r,t);"object"==typeof o&&Object.assign(n,O(o,i)),"function"==typeof o&&(n[i]=o)}),n},P=(e,t,n,r,o)=>{const{localName:i,local:s,remote:l,originForSending:a,originForReceiving:u}=t;let c=!1;o(`${i}: Connecting call sender`);const p=e=>(...t)=>{let n;o(`${i}: Sending ${e}() call`);try{l.closed&&(n=!0)}catch(p){n=!0}if(n&&r(),c){const t=new Error(`Unable to send ${e}() call due to destroyed connection`);throw t.code=v.ConnectionDestroyed,t}return new Promise((n,r)=>{const c=++A,p=t=>{if(t.source!==l||t.data.penpal!==y.Reply||t.data.id!==c)return;if("*"!==u&&t.origin!==u)return void o(`${i} received message from origin ${t.origin} which did not match expected origin ${u}`);const a=t.data;o(`${i}: Received ${e}() reply`),s.removeEventListener(w.Message,p);let d=a.returnValue;a.returnValueIsError&&(d=(e=>{const t=new Error;return Object.keys(e).forEach(n=>t[n]=e[n]),t})(d)),(a.resolution===m.Fulfilled?n:r)(d)};s.addEventListener(w.Message,p);const d={penpal:y.Call,id:c,methodName:e,args:t};l.postMessage(d,a)})},d=n.reduce((e,t)=>(e[t]=p(t),e),{});return Object.assign(e,(e=>{const t={};for(const n in e)E(t,n,e[n]);return t})(d)),()=>{c=!0}},S="0.1.2",C=(e,t,n,r,o)=>{const{destroy:i,onDestroy:s}=r;return r=>{if(!(e instanceof RegExp?e.test(r.origin):"*"===e||e===r.origin))return void o(`Child: Handshake - Received SYN-ACK from origin ${r.origin} which did not match expected origin ${e}`);o("Child: Handshake - Received SYN-ACK, responding with ACK");const l="null"===r.origin?"*":r.origin,a={penpal:y.Ack,methodNames:Object.keys(t),config:n};window.parent.postMessage(a,l);const u={localName:"Child",local:window,remote:window.parent,originForSending:l,originForReceiving:r.origin},c=((e,t,n)=>{const{localName:r,local:o,remote:i,originForSending:s,originForReceiving:l}=e;let a=!1;const u=e=>{if(e.source!==i||e.data.penpal!==y.Call)return;if("*"!==l&&e.origin!==l)return void n(`${r} received message from origin ${e.origin} which did not match expected origin ${l}`);const o=e.data,{methodName:u,args:c,id:p}=o;n(`${r}: Received ${u}() call`);const d=e=>t=>{if(n(`${r}: Sending ${u}() reply`),a)return void n(`${r}: Unable to send ${u}() reply due to destroyed connection`);const o={penpal:y.Reply,id:p,resolution:e,returnValue:t};e===m.Rejected&&t instanceof Error&&(o.returnValue=$(t),o.returnValueIsError=!0);try{i.postMessage(o,s)}catch(l){if(l.name===b.DataCloneError){const e={penpal:y.Reply,id:p,resolution:m.Rejected,returnValue:$(l),returnValueIsError:!0};i.postMessage(e,s)}throw l}};new Promise(e=>e(t[u].apply(t,c))).then(d(m.Fulfilled),d(m.Rejected))};return o.addEventListener(w.Message,u),()=>{a=!0,o.removeEventListener(w.Message,u)}})(u,t,o);s(c);const p={},d=P(p,u,r.data.methodNames,i,o);return s(d),p}};function M(e,t){null==console||console.warn(`[fnApp warn]: ${e}`,t)}function j(e,t){null==console||console.error(`[fnApp error]: ${e}`,t)}const x="_fn_all_event",T="事件订阅数量为空",N=window.__POWERED_BY_FNAPP__?window.__FNAPP.inject.appEventObjMap:new Map;class F{constructor(e,t){c(this,"id"),c(this,"appName"),c(this,"eventObj"),c(this,"getEventCallBackLength",e=>{var t;return(null==(t=this.eventObj[e])?void 0:t.length)||0}),this.id=e,this.appName=t,this.$clear(),N.get(this.id)||N.set(this.id,{}),this.eventObj=N.get(this.id)}$on(e,t){const n=this.eventObj[e];return n?(n.includes(t)||n.push(t),this):(this.eventObj[e]=[t],this)}$onAll(e){return this.$on(x,e)}$once(e,t){t.$__once=!0,this.$on(e,t)}$off(e,t){const n=this.eventObj[e];if(!e||!n||!n.length)return M(`${e} ${T}`),this;let r,o=n.length;for(;o--;)if(r=n[o],r===t){n.splice(o,1);break}return this}$offAll(e){return this.$off(x,e)}$emit(e,...t){return e=`${this.appName}/${e}`,this.$pureEmit(e,...t)}$pureEmit(e,...t){let n=0;return N.forEach(r=>{if(r[e])for(let i=r[e].length-1;i>=0;i--){n++;const s=r[e][i];s.$__once&&r[e].splice(i,1);try{s(...t)}catch(o){j(o)}}if(r[x])for(let e=r[x].length-1;e>=0;e--){n++;const i=r[x][e];i.$__once&&r[x].splice(e,1);try{i(...t)}catch(o){j(o)}}}),e&&0!==n||M(`${e} ${T}`),this}$clear(){const e=N.get(this.id)||{};return Object.keys(e).forEach(t=>delete e[t]),this}}var R=function(e,t){return(R=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n])})(e,t)};function W(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Class extends value "+String(t)+" is not a constructor or null");function n(){this.constructor=e}R(e,t),e.prototype=null===t?Object.create(t):(n.prototype=t.prototype,new n)}function k(e){var t="function"==typeof Symbol&&Symbol.iterator,n=t&&e[t],r=0;if(n)return n.call(e);if(e&&"number"==typeof e.length)return{next:function(){return e&&r>=e.length&&(e=void 0),{value:e&&e[r++],done:!e}}};throw new TypeError(t?"Object is not iterable.":"Symbol.iterator is not defined.")}function V(e,t){var n="function"==typeof Symbol&&e[Symbol.iterator];if(!n)return e;var r,o,i=n.call(e),s=[];try{for(;(void 0===t||t-- >0)&&!(r=i.next()).done;)s.push(r.value)}catch(l){o={error:l}}finally{try{r&&!r.done&&(n=i.return)&&n.call(i)}finally{if(o)throw o.error}}return s}function I(e,t,n){if(n||2===arguments.length)for(var r,o=0,i=t.length;o<i;o++)!r&&o in t||(r||(r=Array.prototype.slice.call(t,0,o)),r[o]=t[o]);return e.concat(r||Array.prototype.slice.call(t))}function q(e){return"function"==typeof e}"function"==typeof SuppressedError&&SuppressedError;var D,L=((D=function(e){return function(t){e(this),this.message=t?t.length+" errors occurred during unsubscription:\n"+t.map(function(e,t){return t+1+") "+e.toString()}).join("\n "):"",this.name="UnsubscriptionError",this.errors=t}}(function(e){Error.call(e),e.stack=(new Error).stack})).prototype=Object.create(Error.prototype),D.prototype.constructor=D,D);function z(e,t){if(e){var n=e.indexOf(t);0<=n&&e.splice(n,1)}}var J=function(){function e(e){this.initialTeardown=e,this.closed=!1,this._parentage=null,this._finalizers=null}var t;return e.prototype.unsubscribe=function(){var e,t,n,r,o;if(!this.closed){this.closed=!0;var i=this._parentage;if(i)if(this._parentage=null,Array.isArray(i))try{for(var s=k(i),l=s.next();!l.done;l=s.next()){l.value.remove(this)}}catch(f){e={error:f}}finally{try{l&&!l.done&&(t=s.return)&&t.call(s)}finally{if(e)throw e.error}}else i.remove(this);var a=this.initialTeardown;if(q(a))try{a()}catch(h){o=h instanceof L?h.errors:[h]}var u=this._finalizers;if(u){this._finalizers=null;try{for(var c=k(u),p=c.next();!p.done;p=c.next()){var d=p.value;try{H(d)}catch(g){o=null!=o?o:[],g instanceof L?o=I(I([],V(o)),V(g.errors)):o.push(g)}}}catch(y){n={error:y}}finally{try{p&&!p.done&&(r=c.return)&&r.call(c)}finally{if(n)throw n.error}}}if(o)throw new L(o)}},e.prototype.add=function(t){var n;if(t&&t!==this)if(this.closed)H(t);else{if(t instanceof e){if(t.closed||t._hasParent(this))return;t._addParent(this)}(this._finalizers=null!==(n=this._finalizers)&&void 0!==n?n:[]).push(t)}},e.prototype._hasParent=function(e){var t=this._parentage;return t===e||Array.isArray(t)&&t.includes(e)},e.prototype._addParent=function(e){var t=this._parentage;this._parentage=Array.isArray(t)?(t.push(e),t):t?[t,e]:e},e.prototype._removeParent=function(e){var t=this._parentage;t===e?this._parentage=null:Array.isArray(t)&&z(t,e)},e.prototype.remove=function(t){var n=this._finalizers;n&&z(n,t),t instanceof e&&t._removeParent(this)},e.EMPTY=((t=new e).closed=!0,t),e}();function Y(e){return e instanceof J||e&&"closed"in e&&q(e.remove)&&q(e.add)&&q(e.unsubscribe)}function H(e){q(e)?e():e.unsubscribe()}J.EMPTY;var U=void 0,B=function(e,t){for(var n=[],r=2;r<arguments.length;r++)n[r-2]=arguments[r];return setTimeout.apply(void 0,I([e,t],V(n)))};function K(){}var Q=function(e){function t(t){var n=e.call(this)||this;return n.isStopped=!1,t?(n.destination=t,Y(t)&&t.add(n)):n.destination=ee,n}return W(t,e),t.create=function(e,t,n){return new X(e,t,n)},t.prototype.next=function(e){this.isStopped||this._next(e)},t.prototype.error=function(e){this.isStopped||(this.isStopped=!0,this._error(e))},t.prototype.complete=function(){this.isStopped||(this.isStopped=!0,this._complete())},t.prototype.unsubscribe=function(){this.closed||(this.isStopped=!0,e.prototype.unsubscribe.call(this),this.destination=null)},t.prototype._next=function(e){this.destination.next(e)},t.prototype._error=function(e){try{this.destination.error(e)}finally{this.unsubscribe()}},t.prototype._complete=function(){try{this.destination.complete()}finally{this.unsubscribe()}},t}(J),G=function(){function e(e){this.partialObserver=e}return e.prototype.next=function(e){var t=this.partialObserver;if(t.next)try{t.next(e)}catch(n){Z(n)}},e.prototype.error=function(e){var t=this.partialObserver;if(t.error)try{t.error(e)}catch(n){Z(n)}else Z(e)},e.prototype.complete=function(){var e=this.partialObserver;if(e.complete)try{e.complete()}catch(t){Z(t)}},e}(),X=function(e){function t(t,n,r){var o,i=e.call(this)||this;return o=q(t)||!t?{next:null!=t?t:void 0,error:null!=n?n:void 0,complete:null!=r?r:void 0}:t,i.destination=new G(o),i}return W(t,e),t}(Q);function Z(e){var t;t=e,B(function(){throw t})}var ee={closed:!0,next:K,error:function(e){throw e},complete:K},te="function"==typeof Symbol&&Symbol.observable||"@@observable";function ne(e){return e}var re=function(){function e(e){e&&(this._subscribe=e)}return e.prototype.lift=function(t){var n=new e;return n.source=this,n.operator=t,n},e.prototype.subscribe=function(e,t,n){var r,o=this,i=(r=e)&&r instanceof Q||function(e){return e&&q(e.next)&&q(e.error)&&q(e.complete)}(r)&&Y(r)?e:new X(e,t,n);return function(){var e=o,t=e.operator,n=e.source;i.add(t?t.call(i,n):n?o._subscribe(i):o._trySubscribe(i))}(),i},e.prototype._trySubscribe=function(e){try{return this._subscribe(e)}catch(t){e.error(t)}},e.prototype.forEach=function(e,t){var n=this;return new(t=oe(t))(function(t,r){var o=new X({next:function(t){try{e(t)}catch(n){r(n),o.unsubscribe()}},error:r,complete:t});n.subscribe(o)})},e.prototype._subscribe=function(e){var t;return null===(t=this.source)||void 0===t?void 0:t.subscribe(e)},e.prototype[te]=function(){return this},e.prototype.pipe=function(){for(var e,t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];return(0===(e=t).length?ne:1===e.length?e[0]:function(t){return e.reduce(function(e,t){return t(e)},t)})(this)},e.prototype.toPromise=function(e){var t=this;return new(e=oe(e))(function(e,n){var r;t.subscribe(function(e){return r=e},function(e){return n(e)},function(){return e(r)})})},e.create=function(t){return new e(t)},e}();function oe(e){var t;return null!==(t=null!=e?e:U)&&void 0!==t?t:Promise}const ie=(e={})=>{const{parentOrigin:t="*",timeout:n,debug:r=!1,config:o={}}=e,i=(e=>(...t)=>{e&&console.log("[Penpal]",...t)})(r),s=((e,t)=>{const n=[];let r=!1;return{destroy(o){r||(r=!0,t(`${e}: Destroying connection`),n.forEach(e=>{e(o)}))},onDestroy(e){r?e():n.push(e)}}})("App",i),{destroy:l,onDestroy:c}=s,d=new Promise((e,r)=>{const d=((e,t)=>{let n;return void 0!==e&&(n=window.setTimeout(()=>{const n=new Error(`Connection timed out after ${e}ms`);n.code=v.ConnectionTimeout,t(n)},e)),()=>{clearTimeout(n)}})(n,l),f=n=>{if((()=>{try{clearTimeout(void 0)}catch(e){return!1}return!0})()&&n.source===parent&&n.data&&n.data.penpal===y.SynAck){const r=n.data,l=new F(r.id,r.appName),c=new F(r.id+"/api",r.appName),h=r.id,g={},y=e=>"api/"+e,m=e=>function(t){const n=y(e);c.$on(n,function e(r){t.next(r),r.reqid&&"succ"===r.result&&(t.complete(),c.$off(n,e))})},v=O({$emit:(e,...t)=>{l.getEventCallBackLength(e)>0?l.$pureEmit(e,...t):null==g||g.$off(e,()=>{},!0)},onQueryResult:e=>{c.$pureEmit(y(e.reqid),e)},onOsNotify:e=>{}}),b=C(t,v,u(a({},o),{postmateVersion:S}),s,i)(n),$={},A=b.bus;if(A){const e=(e,t,n)=>{n?(t.$__once=!0,l.$once(e,t)):l.$on(e,t),$[e]?$[e].push(t):($[e]=[t],A.$on(e,h))};Object.assign(g,A,{$on:e,$off:(e,t,n)=>{var r;if($[e]){const n=$[e].indexOf(t);n>-1&&($[e].splice(n,1),l.$off(e,t))}n&&delete $[e],(null==(r=$[e])?void 0:r.length)||(A.$off(e,h),delete $[e])},$once:(t,n)=>{e(t,n,!0)}})}const _=u(a({},b),{bus:g,query:(e,t)=>p(null,null,function*(){const n=(null==e?void 0:e.reqid)||(yield b.genReqId());return new Promise((r,o)=>{b.query(u(a({},e),{reqid:n}),t).then(e=>{"succ"===(null==e?void 0:e.result)?r(e):o(e)})})}),createObservableQuery:(e,...t)=>p(null,[e,...t],function*(e,t={}){const n=(null==e?void 0:e.reqid)||(yield b.genReqId()),r=new re(m(n));return b.query(u(a({},e),{reqid:n}),u(a({},t),{observable:!0})),r})});b&&(window.removeEventListener(w.Message,f),d(),e({methods:_,config:n.data.config}))}};window.addEventListener(w.Message,f),(()=>{i("Child: Handshake - Sending SYN");const e={penpal:y.Syn},n=t instanceof RegExp?"*":t;window.parent.postMessage(e,n)})(),c(e=>{window.removeEventListener(w.Message,f),e&&r(e)})});return{promise:d,destroy(){l()}}};let se=null;class le{constructor(){c(this,"options"),c(this,"flutterInAppWebView",null),c(this,"osConnector",null),c(this,"initPromise"),c(this,"isWeb",!0);let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};this.options=e,e.debug&&(d.setDebug(!0),d.log("Trim App initialized with debug mode")),d.log("Options:",e),this.initPromise=this.init()}init(){return p(this,null,function*(){return d.log("Starting initialization..."),new Promise(e=>{var t;f()?(this.isWeb=!1,d.log("Initializing mobile platform..."),new Promise(e=>{var t;h||(null==(t=window.flutter_inappwebview)?void 0:t._platformReady)?(d.log("flutter_inappwebview is ready"),e(window.flutter_inappwebview)):window.addEventListener("flutterInAppWebViewPlatformReady",()=>{d.log("flutter_inappwebview is ready"),e(window.flutter_inappwebview)})}).then(t=>{this.flutterInAppWebView=t,d.log("Mobile platform initialized"),e()})):(this.isWeb=!0,d.log("Initializing web platform..."),(se||(se=ie({debug:null==t?void 0:t.debug})),se.promise).then(t=>{this.osConnector=t,d.log("Web platform initialized"),e()}))})})}callAppMethod(e,t){return p(this,null,function*(){return this.flutterInAppWebView?((e,t,n)=>p(null,null,function*(){d.log(`callHandler: ${t}, params: ${n}`);const r=yield e.callHandler(t,n);if(r)try{const e=JSON.parse(r);d.log(`messageData: ${JSON.stringify(e)}`);const t=JSON.parse(e.result);return d.log(`appMessage: ${JSON.stringify(t)}`),t}catch(o){return d.error(`error: ${o}`),null}return null}))(this.flutterInAppWebView,e,t):null})}ready(){return p(this,null,function*(){return this.initPromise})}getOptions(){return this.options}setOptions(e){this.options=a(a({},this.options),e),void 0!==e.debug&&d.setDebug(e.debug),d.log("Options updated:",this.options)}getPlatformConfig(){return p(this,null,function*(){var e;if(yield this.initPromise,this.isWeb){const t=yield null==(e=this.osConnector)?void 0:e.methods.getPlatformConfig();if(!t)throw new Error("Failed to get platform config");return t}{const e=yield this.callAppMethod("getAppMessage");if(!e)throw new Error("Failed to get platform config");return{theme:e.nightMode,language:e.language,appVersion:e.appVersion,systemVersion:e.systemVersion,format:{date:e.dateFormat,time:e.timeFormat}}}})}setTitle(e){return p(this,null,function*(){var t;return yield this.initPromise,d.log("Setting title:",e),this.isWeb?null==(t=this.osConnector)?void 0:t.methods.setTitle(e):this.callAppMethod("updateTitle",e)})}setExitPageTips(e){return p(this,null,function*(){var t;return yield this.initPromise,this.isWeb?null==(t=this.osConnector)?void 0:t.methods.setExitPageTips(e):this.callAppMethod("setExitPageTips",e?JSON.stringify(e):"")})}openFile(e){return p(this,null,function*(){var t;return yield this.initPromise,d.log("Opening file:",e),this.isWeb?null==(t=this.osConnector)?void 0:t.methods.openFile(e):this.callAppMethod("openFile",e)})}openFileManager(e){return p(this,null,function*(){var t;return yield this.initPromise,d.log("Opening file manager:",e),this.isWeb?null==(t=this.osConnector)?void 0:t.methods.openFileManagerApp(e):this.callAppMethod("openFileManager",e)})}openApp(e){return p(this,null,function*(){var t;if(yield this.initPromise,d.log("Opening app:",e),this.isWeb)return null==(t=this.osConnector)?void 0:t.methods.openApp(e);throw new Error("openApp is not supported on mobile platform")})}openCustomApp(e,t){return p(this,null,function*(){var n;if(yield this.initPromise,d.log("Opening custom app:",e,t),this.isWeb)return null==(n=this.osConnector)?void 0:n.methods.openCustomApp(e,t);throw new Error("openCustomApp is not supported on mobile platform")})}openURL(e,t,n){return p(this,null,function*(){if(yield this.initPromise,d.log("Opening URL:",e,t,n),!this.isWeb)return this.callAppMethod("openSystemBrowser",e);if(!n)return void window.open(e,t,n);const r=window.open("",t,n);r&&(r.location.href=e)})}close(){return p(this,null,function*(){var e;return yield this.initPromise,this.isWeb?null==(e=this.osConnector)?void 0:e.methods.close():this.callAppMethod("exitPage")})}query(e,t){return p(this,null,function*(){var n;if(yield this.initPromise,this.isWeb){const r=yield null==(n=this.osConnector)?void 0:n.methods.query(e,t);if(!r)throw new Error("Failed to query");return r}throw new Error("query is not supported on mobile platform")})}$on(e,t){return p(this,null,function*(){var n;if(yield this.initPromise,!this.isWeb)throw new Error("$on is not supported on mobile platform");(null==(n=this.osConnector)?void 0:n.methods.bus).$on(e,t)})}$off(e,t){return p(this,null,function*(){var n;if(yield this.initPromise,!this.isWeb)throw new Error("$off is not supported on mobile platform");(null==(n=this.osConnector)?void 0:n.methods.bus).$off(e,t)})}$once(e,t){return p(this,null,function*(){var n;if(yield this.initPromise,!this.isWeb)throw new Error("$once is not supported on mobile platform");(null==(n=this.osConnector)?void 0:n.methods.bus).$once(e,t)})}pickFile(e){return p(this,null,function*(){var t;return yield this.initPromise,this.isWeb?null==(t=this.osConnector)?void 0:t.methods.pickFile(e):this.callAppMethod("openFolder",JSON.stringify(e))})}}e.TrimApp=le,e.default=le,Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trimjs/web-app",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.6",
|
|
4
4
|
"description": "web app sdk for nas",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.cjs",
|
|
@@ -15,6 +15,9 @@
|
|
|
15
15
|
"files": [
|
|
16
16
|
"dist"
|
|
17
17
|
],
|
|
18
|
+
"publishConfig": {
|
|
19
|
+
"access": "public"
|
|
20
|
+
},
|
|
18
21
|
"devDependencies": {
|
|
19
22
|
"@babel/preset-env": "^7.21.4",
|
|
20
23
|
"@eslint/js": "^9.13.0",
|