@trimjs/web-app 0.2.2 → 0.2.4

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 CHANGED
@@ -200,6 +200,36 @@ await app.openFileManager("/path/to/folder");
200
200
 
201
201
  ---
202
202
 
203
+ #### openApp
204
+
205
+ ```typescript
206
+ async openApp(anchor: string): Promise<void | null>
207
+ ```
208
+
209
+ 通过应用锚点打开指定应用(仅支持 Web 平台)。
210
+
211
+ **参数:**
212
+
213
+ - `anchor: string` - 应用锚点(唯一标识)
214
+
215
+ **返回值:**
216
+
217
+ - 成功时返回 `Promise<void>`
218
+ - 失败时返回 `Promise<null>`
219
+
220
+ **注意:**
221
+
222
+ - 此方法仅在 Web 平台(iframe)中可用
223
+ - 在移动端调用会抛出错误:`openApp is not supported on mobile platform`
224
+
225
+ **示例:**
226
+
227
+ ```typescript
228
+ await app.openApp("trim.notes");
229
+ ```
230
+
231
+ ---
232
+
203
233
  #### setExitPageTips
204
234
 
205
235
  ```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 x(e,t){null==console||console.warn(`[fnApp warn]: ${e}`,t)}function C(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 x(`${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){C(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){C(o)}}}),e&&0!==n||x(`${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)})}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 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;
package/dist/index.d.ts CHANGED
@@ -178,6 +178,11 @@ declare class TrimApp {
178
178
  * 打开文件管理器
179
179
  */
180
180
  openFileManager(path: string): Promise<unknown>;
181
+ /**
182
+ * 打开应用
183
+ * @param anchor 锚点
184
+ */
185
+ openApp(anchor: string): Promise<void | undefined>;
181
186
  /**
182
187
  * 关闭应用
183
188
  */
@@ -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)})}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 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}({});
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)})}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 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 +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",j=(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 x(e,t){null==console||console.warn(`[fnApp warn]: ${e}`,t)}function C(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 x(`${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){C(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){C(o)}}}),e&&0!==n||x(`${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=j(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)})}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},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"}})});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trimjs/web-app",
3
- "version": "0.2.2",
3
+ "version": "0.2.4",
4
4
  "description": "web app sdk for nas",
5
5
  "type": "module",
6
6
  "main": "dist/index.cjs",