@trimjs/web-app 0.0.4 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -12
- package/dist/index.cjs +2 -2
- package/dist/index.iife.js +2 -2
- package/dist/index.js +307 -304
- package/dist/index.umd.cjs +2 -2
- package/package.json +3 -5
- package/src/index.ts +1 -1
- package/src/mobile/index.ts +6 -1
package/dist/index.umd.cjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
(function(w,v){typeof exports=="object"&&typeof module<"u"?v(exports):typeof define=="function"&&define.amd?define(["exports"],v):(w=typeof globalThis<"u"?globalThis:w||self,v(w.TrimApp={}))})(this,function(w){"use strict";var
|
|
1
|
+
(function(w,v){typeof exports=="object"&&typeof module<"u"?v(exports):typeof define=="function"&&define.amd?define(["exports"],v):(w=typeof globalThis<"u"?globalThis:w||self,v(w.TrimApp={}))})(this,function(w){"use strict";var rt=Object.defineProperty;var it=(w,v,l)=>v in w?rt(w,v,{enumerable:!0,configurable:!0,writable:!0,value:l}):w[v]=l;var P=(w,v,l)=>it(w,typeof v!="symbol"?v+"":v,l);class v{constructor(){P(this,"debug",!1)}setDebug(e){this.debug=e}isDebug(){return this.debug}log(...e){this.debug&&console.log("[Trim App]",...e)}warn(...e){this.debug&&console.warn("[Trim App]",...e)}error(...e){this.debug&&console.error("[Trim App]",...e)}info(...e){this.debug&&console.info("[Trim App]",...e)}}const l=new v;function ve(n){const e=/FNOS\/([\d.]+)|FNAppType\/(\w+)|FNAppVer\/([\d.]+)/g,t={};let r;for(;(r=e.exec(n))!==null;)r[1]&&(t.fnOSVersion=r[1]),r[2]&&(t.fnAppType=r[2]),r[3]&&(t.fnAppVersion=r[3]);return t}const X=()=>ve(navigator.userAgent).fnAppVersion;let Z=!1;window.addEventListener("flutterInAppWebViewPlatformReady",()=>{Z=!0});const Ee=()=>new Promise(n=>{var e;Z||(e=window.flutter_inappwebview)!=null&&e._platformReady?(l.log("flutter_inappwebview is ready"),n(window.flutter_inappwebview)):window.addEventListener("flutterInAppWebViewPlatformReady",()=>{l.log("flutter_inappwebview is ready"),n(window.flutter_inappwebview)})}),Se=async(n,e,t)=>{l.log(`callHandler: ${e}, params: ${t}`);const r=await n.callHandler(e,t);if(r)try{const o=JSON.parse(r);l.log(`messageData: ${JSON.stringify(o)}`);const i=JSON.parse(o.result);return l.log(`appMessage: ${JSON.stringify(i)}`),i}catch(o){return l.error(`error: ${o}`),null}return null},ee=new Map;function Ae(){window.fnAppMessage=n=>{l.log("app-message",n);const e=JSON.parse(n),t=ee.get(e.reqId);t&&(t(e),ee.delete(e.reqId))}}X()&&Ae();var C=(n=>(n.Call="call",n.Reply="reply",n.Syn="syn",n.SynAck="synAck",n.Ack="ack",n))(C||{}),j=(n=>(n.Fulfilled="fulfilled",n.Rejected="rejected",n))(j||{}),U=(n=>(n.ConnectionDestroyed="ConnectionDestroyed",n.ConnectionTimeout="ConnectionTimeout",n.NoIframeSrc="NoIframeSrc",n))(U||{}),te=(n=>(n.DataCloneError="DataCloneError",n))(te||{}),D=(n=>(n.Message="message",n))(D||{});const $e=(n,e)=>{const t=[];let r=!1;return{destroy(o){r||(r=!0,e(`${n}: Destroying connection`),t.forEach(i=>{i(o)}))},onDestroy(o){r?o():t.push(o)}}},Pe=n=>(...e)=>{n&&console.log("[Penpal]",...e)},ne=({name:n,message:e,stack:t})=>({name:n,message:e,stack:t}),Me=n=>{const e=new Error;return Object.keys(n).forEach(t=>e[t]=n[t]),e},_e=(n,e,t)=>{const{localName:r,local:o,remote:i,originForSending:s,originForReceiving:c}=n;let p=!1;const E=b=>{if(b.source!==i||b.data.penpal!==C.Call)return;if(c!=="*"&&b.origin!==c){t(`${r} received message from origin ${b.origin} which did not match expected origin ${c}`);return}const S=b.data,{methodName:g,args:d,id:m}=S;t(`${r}: Received ${g}() call`);const a=y=>$=>{if(t(`${r}: Sending ${g}() reply`),p){t(`${r}: Unable to send ${g}() reply due to destroyed connection`);return}const M={penpal:C.Reply,id:m,resolution:y,returnValue:$};y===j.Rejected&&$ instanceof Error&&(M.returnValue=ne($),M.returnValueIsError=!0);try{i.postMessage(M,s)}catch(F){if(F.name===te.DataCloneError){const O={penpal:C.Reply,id:m,resolution:j.Rejected,returnValue:ne(F),returnValueIsError:!0};i.postMessage(O,s)}throw F}};new Promise(y=>y(e[g].apply(e,d))).then(a(j.Fulfilled),a(j.Rejected))};return o.addEventListener(D.Message,E),()=>{p=!0,o.removeEventListener(D.Message,E)}};let Ce=0;const Te=()=>++Ce,re=".",ie=n=>n?n.split(re):[],Fe=n=>n.join(re),Oe=(n,e)=>{const t=ie(e||"");return t.push(n),Fe(t)},Re=(n,e,t)=>{const r=ie(e);return r.reduce((o,i,s)=>(typeof o[i]>"u"&&(o[i]={}),s===r.length-1&&(o[i]=t),o[i]),n),n},oe=(n,e)=>{const t={};return Object.keys(n).forEach(r=>{const o=n[r],i=Oe(r,e);typeof o=="object"&&Object.assign(t,oe(o,i)),typeof o=="function"&&(t[i]=o)}),t},xe=n=>{const e={};for(const t in n)Re(e,t,n[t]);return e},De=(n,e,t,r,o)=>{const{localName:i,local:s,remote:c,originForSending:p,originForReceiving:E}=e;let b=!1;o(`${i}: Connecting call sender`);const S=d=>(...m)=>{o(`${i}: Sending ${d}() call`);let a;try{c.closed&&(a=!0)}catch{a=!0}if(a&&r(),b){const y=new Error(`Unable to send ${d}() call due to destroyed connection`);throw y.code=U.ConnectionDestroyed,y}return new Promise((y,$)=>{const M=Te(),F=R=>{if(R.source!==c||R.data.penpal!==C.Reply||R.data.id!==M)return;if(E!=="*"&&R.origin!==E){o(`${i} received message from origin ${R.origin} which did not match expected origin ${E}`);return}const q=R.data;o(`${i}: Received ${d}() reply`),s.removeEventListener(D.Message,F);let L=q.returnValue;q.returnValueIsError&&(L=Me(L)),(q.resolution===j.Fulfilled?y:$)(L)};s.addEventListener(D.Message,F);const O={penpal:C.Call,id:M,methodName:d,args:m};c.postMessage(O,p)})},g=t.reduce((d,m)=>(d[m]=S(m),d),{});return Object.assign(n,xe(g)),()=>{b=!0}},Ie=(n,e)=>{let t;return n!==void 0&&(t=window.setTimeout(()=>{const r=new Error(`Connection timed out after ${n}ms`);r.code=U.ConnectionTimeout,e(r)},n)),()=>{clearTimeout(t)}},je={name:"@fn/micro-app-postmate",version:"0.1.2",type:"module",main:"src/index.ts",scripts:{dev:"vite",build:"tsc -b && vite build",lint:"eslint ."},files:["src","dist"],peerDependencies:{"@fn/micro-app-event":"^0.0.1",rxjs:"^7.8.0"},devDependencies:{"@eslint/js":"^9.13.0","@fn/micro-app-event":"workspace:*",eslint:"^9.13.0",globals:"^15.11.0",typescript:"~5.6.2","typescript-eslint":"^8.11.0",rxjs:"^7.8.0",vite:"^5.4.10"},publishConfig:{registry:"http://verdaccio.teiron-inc.cn:4873"}},ke=(n,e,t,r,o)=>{const{destroy:i,onDestroy:s}=r;return c=>{if(!(n instanceof RegExp?n.test(c.origin):n==="*"||n===c.origin)){o(`Child: Handshake - Received SYN-ACK from origin ${c.origin} which did not match expected origin ${n}`);return}o("Child: Handshake - Received SYN-ACK, responding with ACK");const E=c.origin==="null"?"*":c.origin,b={penpal:C.Ack,methodNames:Object.keys(e),config:t};window.parent.postMessage(b,E);const S={localName:"Child",local:window,remote:window.parent,originForSending:E,originForReceiving:c.origin},g=_e(S,e,o);s(g);const d={},m=De(d,S,c.data.methodNames,i,o);return s(m),d}};function se(n,e){console==null||console.warn(`[fnApp warn]: ${n}`,e)}function ce(n,e){console==null||console.error(`[fnApp error]: ${n}`,e)}const k="_fn_all_event",ae="事件订阅数量为空",W=window.__POWERED_BY_FNAPP__?window.__FNAPP.inject.appEventObjMap:new Map;class le{constructor(e,t){P(this,"id");P(this,"appName");P(this,"eventObj");P(this,"getEventCallBackLength",e=>{var t;return((t=this.eventObj[e])==null?void 0:t.length)||0});console.log("id",e,t),this.id=e,this.appName=t,this.$clear(),W.get(this.id)||W.set(this.id,{}),this.eventObj=W.get(this.id)}$on(e,t){const r=this.eventObj[e];return r?(r.includes(t)||r.push(t),this):(this.eventObj[e]=[t],this)}$onAll(e){return this.$on(k,e)}$once(e,t){t.$__once=!0,this.$on(e,t)}$off(e,t){const r=this.eventObj[e];if(!e||!r||!r.length)return se(`${e} ${ae}`),this;let o,i=r.length;for(;i--;)if(o=r[i],o===t){r.splice(i,1);break}return this}$offAll(e){return this.$off(k,e)}$emit(e,...t){return e=`${this.appName}/${e}`,this.$pureEmit(e,...t)}$pureEmit(e,...t){let r=0,o=0;return W.forEach(i=>{if(i[e])for(let s=i[e].length-1;s>=0;s--){r++;const c=i[e][s];c.$__once&&i[e].splice(s,1);try{c(...t)}catch(p){ce(p)}}if(i[k])for(let s=i[k].length-1;s>=0;s--){r++;const c=i[k][s];c.$__once&&i[k].splice(s,1);try{c(...t)}catch(p){ce(p)}}}),(!e||r===0&&o===0)&&se(`${e} ${ae}`),this}$clear(){const e=W.get(this.id)??{};return Object.keys(e).forEach(r=>delete e[r]),this}}var B=function(n,e){return B=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,r){t.__proto__=r}||function(t,r){for(var o in r)Object.prototype.hasOwnProperty.call(r,o)&&(t[o]=r[o])},B(n,e)};function ue(n,e){if(typeof e!="function"&&e!==null)throw new TypeError("Class extends value "+String(e)+" is not a constructor or null");B(n,e);function t(){this.constructor=n}n.prototype=e===null?Object.create(e):(t.prototype=e.prototype,new t)}function fe(n){var e=typeof Symbol=="function"&&Symbol.iterator,t=e&&n[e],r=0;if(t)return t.call(n);if(n&&typeof n.length=="number")return{next:function(){return n&&r>=n.length&&(n=void 0),{value:n&&n[r++],done:!n}}};throw new TypeError(e?"Object is not iterable.":"Symbol.iterator is not defined.")}function J(n,e){var t=typeof Symbol=="function"&&n[Symbol.iterator];if(!t)return n;var r=t.call(n),o,i=[],s;try{for(;(e===void 0||e-- >0)&&!(o=r.next()).done;)i.push(o.value)}catch(c){s={error:c}}finally{try{o&&!o.done&&(t=r.return)&&t.call(r)}finally{if(s)throw s.error}}return i}function Y(n,e,t){if(t||arguments.length===2)for(var r=0,o=e.length,i;r<o;r++)(i||!(r in e))&&(i||(i=Array.prototype.slice.call(e,0,r)),i[r]=e[r]);return n.concat(i||Array.prototype.slice.call(e))}typeof SuppressedError=="function"&&SuppressedError;function T(n){return typeof n=="function"}function Ve(n){var e=function(r){Error.call(r),r.stack=new Error().stack},t=n(e);return t.prototype=Object.create(Error.prototype),t.prototype.constructor=t,t}var z=Ve(function(n){return function(t){n(this),this.message=t?t.length+` errors occurred during unsubscription:
|
|
2
2
|
`+t.map(function(r,o){return o+1+") "+r.toString()}).join(`
|
|
3
|
-
`):"",this.name="UnsubscriptionError",this.errors=t}});function fe(n,e){if(n){var t=n.indexOf(e);0<=t&&n.splice(t,1)}}var K=function(){function n(e){this.initialTeardown=e,this.closed=!1,this._parentage=null,this._finalizers=null}return n.prototype.unsubscribe=function(){var e,t,r,o,i;if(!this.closed){this.closed=!0;var s=this._parentage;if(s)if(this._parentage=null,Array.isArray(s))try{for(var c=ue(s),p=c.next();!p.done;p=c.next()){var E=p.value;E.remove(this)}}catch(a){e={error:a}}finally{try{p&&!p.done&&(t=c.return)&&t.call(c)}finally{if(e)throw e.error}}else s.remove(this);var b=this.initialTeardown;if(T(b))try{b()}catch(a){i=a instanceof z?a.errors:[a]}var S=this._finalizers;if(S){this._finalizers=null;try{for(var g=ue(S),d=g.next();!d.done;d=g.next()){var m=d.value;try{de(m)}catch(a){i=i??[],a instanceof z?i=Y(Y([],J(i)),J(a.errors)):i.push(a)}}}catch(a){r={error:a}}finally{try{d&&!d.done&&(o=g.return)&&o.call(g)}finally{if(r)throw r.error}}}if(i)throw new z(i)}},n.prototype.add=function(e){var t;if(e&&e!==this)if(this.closed)de(e);else{if(e instanceof n){if(e.closed||e._hasParent(this))return;e._addParent(this)}(this._finalizers=(t=this._finalizers)!==null&&t!==void 0?t:[]).push(e)}},n.prototype._hasParent=function(e){var t=this._parentage;return t===e||Array.isArray(t)&&t.includes(e)},n.prototype._addParent=function(e){var t=this._parentage;this._parentage=Array.isArray(t)?(t.push(e),t):t?[t,e]:e},n.prototype._removeParent=function(e){var t=this._parentage;t===e?this._parentage=null:Array.isArray(t)&&fe(t,e)},n.prototype.remove=function(e){var t=this._finalizers;t&&fe(t,e),e instanceof n&&e._removeParent(this)},n.EMPTY=function(){var e=new n;return e.closed=!0,e}(),n}();K.EMPTY;function pe(n){return n instanceof K||n&&"closed"in n&&T(n.remove)&&T(n.add)&&T(n.unsubscribe)}function de(n){T(n)?n():n.unsubscribe()}var he={onUnhandledError:null,onStoppedNotification:null,Promise:void 0,useDeprecatedSynchronousErrorHandling:!1,useDeprecatedNextContext:!1},Ve={setTimeout:function(n,e){for(var t=[],r=2;r<arguments.length;r++)t[r-2]=arguments[r];return setTimeout.apply(void 0,Y([n,e],J(t)))},clearTimeout:function(n){return clearTimeout(n)},delegate:void 0};function qe(n){Ve.setTimeout(function(){throw n})}function ge(){}function We(n){n()}var ye=function(n){le(e,n);function e(t){var r=n.call(this)||this;return r.isStopped=!1,t?(r.destination=t,pe(t)&&t.add(r)):r.destination=Ue,r}return e.create=function(t,r,o){return new G(t,r,o)},e.prototype.next=function(t){this.isStopped||this._next(t)},e.prototype.error=function(t){this.isStopped||(this.isStopped=!0,this._error(t))},e.prototype.complete=function(){this.isStopped||(this.isStopped=!0,this._complete())},e.prototype.unsubscribe=function(){this.closed||(this.isStopped=!0,n.prototype.unsubscribe.call(this),this.destination=null)},e.prototype._next=function(t){this.destination.next(t)},e.prototype._error=function(t){try{this.destination.error(t)}finally{this.unsubscribe()}},e.prototype._complete=function(){try{this.destination.complete()}finally{this.unsubscribe()}},e}(K),Ne=Function.prototype.bind;function Q(n,e){return Ne.call(n,e)}var Le=function(){function n(e){this.partialObserver=e}return n.prototype.next=function(e){var t=this.partialObserver;if(t.next)try{t.next(e)}catch(r){L(r)}},n.prototype.error=function(e){var t=this.partialObserver;if(t.error)try{t.error(e)}catch(r){L(r)}else L(e)},n.prototype.complete=function(){var e=this.partialObserver;if(e.complete)try{e.complete()}catch(t){L(t)}},n}(),G=function(n){le(e,n);function e(t,r,o){var i=n.call(this)||this,s;if(T(t)||!t)s={next:t??void 0,error:r??void 0,complete:o??void 0};else{var c;i&&he.useDeprecatedNextContext?(c=Object.create(t),c.unsubscribe=function(){return i.unsubscribe()},s={next:t.next&&Q(t.next,c),error:t.error&&Q(t.error,c),complete:t.complete&&Q(t.complete,c)}):s=t}return i.destination=new Le(s),i}return e}(ye);function L(n){qe(n)}function He(n){throw n}var Ue={closed:!0,next:ge,error:He,complete:ge},Be=function(){return typeof Symbol=="function"&&Symbol.observable||"@@observable"}();function Je(n){return n}function Ye(n){return n.length===0?Je:n.length===1?n[0]:function(t){return n.reduce(function(r,o){return o(r)},t)}}var ze=function(){function n(e){e&&(this._subscribe=e)}return n.prototype.lift=function(e){var t=new n;return t.source=this,t.operator=e,t},n.prototype.subscribe=function(e,t,r){var o=this,i=Qe(e)?e:new G(e,t,r);return We(function(){var s=o,c=s.operator,p=s.source;i.add(c?c.call(i,p):p?o._subscribe(i):o._trySubscribe(i))}),i},n.prototype._trySubscribe=function(e){try{return this._subscribe(e)}catch(t){e.error(t)}},n.prototype.forEach=function(e,t){var r=this;return t=me(t),new t(function(o,i){var s=new G({next:function(c){try{e(c)}catch(p){i(p),s.unsubscribe()}},error:i,complete:o});r.subscribe(s)})},n.prototype._subscribe=function(e){var t;return(t=this.source)===null||t===void 0?void 0:t.subscribe(e)},n.prototype[Be]=function(){return this},n.prototype.pipe=function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];return Ye(e)(this)},n.prototype.toPromise=function(e){var t=this;return e=me(e),new e(function(r,o){var i;t.subscribe(function(s){return i=s},function(s){return o(s)},function(){return r(i)})})},n.create=function(e){return new n(e)},n}();function me(n){var e;return(e=n??he.Promise)!==null&&e!==void 0?e:Promise}function Ke(n){return n&&T(n.next)&&T(n.error)&&T(n.complete)}function Qe(n){return n&&n instanceof ye||Ke(n)&&pe(n)}const Ge=()=>{try{clearTimeout(void 0)}catch{return!1}return!0},Xe=(n={})=>{const{parentOrigin:e="*",timeout:t,debug:r=!1,config:o={}}=n,i=$e(r),s=Ae("App",i),{destroy:c,onDestroy:p}=s,E=()=>{i("Child: Handshake - Sending SYN");const S={penpal:C.Syn},g=e instanceof RegExp?"*":e;window.parent.postMessage(S,g)};return{promise:new Promise((S,g)=>{const d=De(t,c),m=a=>{if(Ge()&&!(a.source!==parent||!a.data)&&a.data.penpal===C.SynAck){const y=a.data,$=new ae(y.id,y.appName),M=new ae(y.id+"/api",y.appName),F=y.id,O={},R=u=>"api/"+u,W={$emit:(u,...f)=>{$.getEventCallBackLength(u)>0?$.$pureEmit(u,...f):O==null||O.$off(u,()=>{},!0)},onQueryResult:u=>{M.$pureEmit(R(u.reqid),u)},onOsNotify:u=>{}},N=u=>function(h){const _=R(u);M.$on(_,function V(A){h.next(A),A.reqid&&A.result==="succ"&&(h.complete(),M.$off(_,V))})},et=ie(W),j=Ie(e,et,{...o,postmateVersion:je.version},s,i)(a),x={},H=j.bus;if(H){const u=(f,h,_)=>{_?(h.$__once=!0,$.$once(f,h)):$.$on(f,h),x[f]?x[f].push(h):(x[f]=[h],H.$on(f,F))};Object.assign(O,H,{$on:u,$off:(f,h,_)=>{var V;if(x[f]){const A=x[f].indexOf(h);A>-1&&(x[f].splice(A,1),$.$off(f,h))}_&&delete x[f],(V=x[f])!=null&&V.length||(H.$off(f,F),delete x[f])},$once:(f,h)=>{u(f,h,!0)}})}const tt={...j,bus:O,query:async(u,f)=>{const h=(u==null?void 0:u.reqid)||await j.genReqId();return new Promise((_,V)=>{j.query({...u,reqid:h},f).then(A=>{(A==null?void 0:A.result)==="succ"?_(A):V(A)})})},createObservableQuery:async(u,f={})=>{const h=(u==null?void 0:u.reqid)||await j.genReqId(),_=new ze(N(h));return j.query({...u,reqid:h},{...f,observable:!0}),_}};j&&(window.removeEventListener(D.Message,m),d(),S({methods:tt,config:a.data.config}))}};window.addEventListener(D.Message,m),E(),p(a=>{window.removeEventListener(D.Message,m),a&&g(a)})}),destroy(){c()}}},Ze=n=>Xe({debug:n==null?void 0:n.debug}).promise;class be{constructor(e={}){P(this,"options");P(this,"flutterInAppWebView",null);P(this,"osConnector",null);P(this,"initPromise");P(this,"isWeb",!0);this.options=e,e.debug&&(l.setDebug(!0),l.log("Trim App initialized with debug mode")),l.log("Options:",e),this.initPromise=this.init()}async init(){return l.log("Starting initialization..."),new Promise(e=>{X()?(this.isWeb=!1,l.log("Initializing mobile platform..."),ve().then(t=>{this.flutterInAppWebView=t,l.log("Mobile platform initialized"),e()})):(this.isWeb=!0,l.log("Initializing web platform..."),Ze().then(t=>{this.osConnector=t,l.log("Web platform initialized"),e()}))})}async callAppMethod(e,t){return this.flutterInAppWebView?Ee(this.flutterInAppWebView,e,t):null}async ready(){return this.initPromise}getOptions(){return this.options}setOptions(e){this.options={...this.options,...e},e.debug!==void 0&&l.setDebug(e.debug),l.log("Options updated:",this.options)}async getPlatformConfig(){var e;if(await this.initPromise,this.isWeb){const t=await((e=this.osConnector)==null?void 0:e.methods.getPlatformConfig());if(!t)throw new Error("Failed to get platform config");return t}else{const t=await this.callAppMethod("getAppMessage");if(!t)throw new Error("Failed to get platform config");return{theme:t.nightMode,language:t.language,appVersion:t.appVersion,systemVersion:t.systemVersion,format:{date:t.dateFormat,time:t.timeFormat}}}}async setTitle(e){var t;return await this.initPromise,l.log("Setting title:",e),this.isWeb?(t=this.osConnector)==null?void 0:t.methods.setTitle(e):this.callAppMethod("updateTitle",e)}async setExitPageTips(e){var t;return await this.initPromise,this.isWeb?(t=this.osConnector)==null?void 0:t.methods.setExitPageTips(e):this.callAppMethod("setExitPageTips",e?JSON.stringify(e):"")}async openFile(e){var t;return await this.initPromise,l.log("Opening file:",e),this.isWeb?(t=this.osConnector)==null?void 0:t.methods.openFile(e):this.callAppMethod("openFile",e)}async openFileManager(e){var t;return await this.initPromise,l.log("Opening file manager:",e),this.isWeb?(t=this.osConnector)==null?void 0:t.methods.openFileManagerApp(e):this.callAppMethod("openFileManager",e)}async close(){var e;if(await this.initPromise,this.isWeb)return(e=this.osConnector)==null?void 0:e.methods.close();throw new Error("close is not supported on mobile platform")}async query(e,t){var r;if(await this.initPromise,this.isWeb){const o=await((r=this.osConnector)==null?void 0:r.methods.query(e,t));if(!o)throw new Error("Failed to query");return o}else throw new Error("query is not supported on mobile platform")}async pickFile(e){var t;return await this.initPromise,this.isWeb?(t=this.osConnector)==null?void 0:t.methods.pickFile(e):this.callAppMethod("openFolder",JSON.stringify(e))}}w.TrimApp=be,w.default=be,Object.defineProperties(w,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
|
|
3
|
+
`):"",this.name="UnsubscriptionError",this.errors=t}});function pe(n,e){if(n){var t=n.indexOf(e);0<=t&&n.splice(t,1)}}var K=function(){function n(e){this.initialTeardown=e,this.closed=!1,this._parentage=null,this._finalizers=null}return n.prototype.unsubscribe=function(){var e,t,r,o,i;if(!this.closed){this.closed=!0;var s=this._parentage;if(s)if(this._parentage=null,Array.isArray(s))try{for(var c=fe(s),p=c.next();!p.done;p=c.next()){var E=p.value;E.remove(this)}}catch(a){e={error:a}}finally{try{p&&!p.done&&(t=c.return)&&t.call(c)}finally{if(e)throw e.error}}else s.remove(this);var b=this.initialTeardown;if(T(b))try{b()}catch(a){i=a instanceof z?a.errors:[a]}var S=this._finalizers;if(S){this._finalizers=null;try{for(var g=fe(S),d=g.next();!d.done;d=g.next()){var m=d.value;try{he(m)}catch(a){i=i??[],a instanceof z?i=Y(Y([],J(i)),J(a.errors)):i.push(a)}}}catch(a){r={error:a}}finally{try{d&&!d.done&&(o=g.return)&&o.call(g)}finally{if(r)throw r.error}}}if(i)throw new z(i)}},n.prototype.add=function(e){var t;if(e&&e!==this)if(this.closed)he(e);else{if(e instanceof n){if(e.closed||e._hasParent(this))return;e._addParent(this)}(this._finalizers=(t=this._finalizers)!==null&&t!==void 0?t:[]).push(e)}},n.prototype._hasParent=function(e){var t=this._parentage;return t===e||Array.isArray(t)&&t.includes(e)},n.prototype._addParent=function(e){var t=this._parentage;this._parentage=Array.isArray(t)?(t.push(e),t):t?[t,e]:e},n.prototype._removeParent=function(e){var t=this._parentage;t===e?this._parentage=null:Array.isArray(t)&&pe(t,e)},n.prototype.remove=function(e){var t=this._finalizers;t&&pe(t,e),e instanceof n&&e._removeParent(this)},n.EMPTY=function(){var e=new n;return e.closed=!0,e}(),n}();K.EMPTY;function de(n){return n instanceof K||n&&"closed"in n&&T(n.remove)&&T(n.add)&&T(n.unsubscribe)}function he(n){T(n)?n():n.unsubscribe()}var ge={onUnhandledError:null,onStoppedNotification:null,Promise:void 0,useDeprecatedSynchronousErrorHandling:!1,useDeprecatedNextContext:!1},We={setTimeout:function(n,e){for(var t=[],r=2;r<arguments.length;r++)t[r-2]=arguments[r];return setTimeout.apply(void 0,Y([n,e],J(t)))},clearTimeout:function(n){return clearTimeout(n)},delegate:void 0};function qe(n){We.setTimeout(function(){throw n})}function ye(){}function Le(n){n()}var me=function(n){ue(e,n);function e(t){var r=n.call(this)||this;return r.isStopped=!1,t?(r.destination=t,de(t)&&t.add(r)):r.destination=Be,r}return e.create=function(t,r,o){return new G(t,r,o)},e.prototype.next=function(t){this.isStopped||this._next(t)},e.prototype.error=function(t){this.isStopped||(this.isStopped=!0,this._error(t))},e.prototype.complete=function(){this.isStopped||(this.isStopped=!0,this._complete())},e.prototype.unsubscribe=function(){this.closed||(this.isStopped=!0,n.prototype.unsubscribe.call(this),this.destination=null)},e.prototype._next=function(t){this.destination.next(t)},e.prototype._error=function(t){try{this.destination.error(t)}finally{this.unsubscribe()}},e.prototype._complete=function(){try{this.destination.complete()}finally{this.unsubscribe()}},e}(K),Ne=Function.prototype.bind;function Q(n,e){return Ne.call(n,e)}var He=function(){function n(e){this.partialObserver=e}return n.prototype.next=function(e){var t=this.partialObserver;if(t.next)try{t.next(e)}catch(r){N(r)}},n.prototype.error=function(e){var t=this.partialObserver;if(t.error)try{t.error(e)}catch(r){N(r)}else N(e)},n.prototype.complete=function(){var e=this.partialObserver;if(e.complete)try{e.complete()}catch(t){N(t)}},n}(),G=function(n){ue(e,n);function e(t,r,o){var i=n.call(this)||this,s;if(T(t)||!t)s={next:t??void 0,error:r??void 0,complete:o??void 0};else{var c;i&&ge.useDeprecatedNextContext?(c=Object.create(t),c.unsubscribe=function(){return i.unsubscribe()},s={next:t.next&&Q(t.next,c),error:t.error&&Q(t.error,c),complete:t.complete&&Q(t.complete,c)}):s=t}return i.destination=new He(s),i}return e}(me);function N(n){qe(n)}function Ue(n){throw n}var Be={closed:!0,next:ye,error:Ue,complete:ye},Je=function(){return typeof Symbol=="function"&&Symbol.observable||"@@observable"}();function Ye(n){return n}function ze(n){return n.length===0?Ye:n.length===1?n[0]:function(t){return n.reduce(function(r,o){return o(r)},t)}}var Ke=function(){function n(e){e&&(this._subscribe=e)}return n.prototype.lift=function(e){var t=new n;return t.source=this,t.operator=e,t},n.prototype.subscribe=function(e,t,r){var o=this,i=Ge(e)?e:new G(e,t,r);return Le(function(){var s=o,c=s.operator,p=s.source;i.add(c?c.call(i,p):p?o._subscribe(i):o._trySubscribe(i))}),i},n.prototype._trySubscribe=function(e){try{return this._subscribe(e)}catch(t){e.error(t)}},n.prototype.forEach=function(e,t){var r=this;return t=be(t),new t(function(o,i){var s=new G({next:function(c){try{e(c)}catch(p){i(p),s.unsubscribe()}},error:i,complete:o});r.subscribe(s)})},n.prototype._subscribe=function(e){var t;return(t=this.source)===null||t===void 0?void 0:t.subscribe(e)},n.prototype[Je]=function(){return this},n.prototype.pipe=function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];return ze(e)(this)},n.prototype.toPromise=function(e){var t=this;return e=be(e),new e(function(r,o){var i;t.subscribe(function(s){return i=s},function(s){return o(s)},function(){return r(i)})})},n.create=function(e){return new n(e)},n}();function be(n){var e;return(e=n??ge.Promise)!==null&&e!==void 0?e:Promise}function Qe(n){return n&&T(n.next)&&T(n.error)&&T(n.complete)}function Ge(n){return n&&n instanceof me||Qe(n)&&de(n)}const Xe=()=>{try{clearTimeout(void 0)}catch{return!1}return!0},Ze=(n={})=>{const{parentOrigin:e="*",timeout:t,debug:r=!1,config:o={}}=n,i=Pe(r),s=$e("App",i),{destroy:c,onDestroy:p}=s,E=()=>{i("Child: Handshake - Sending SYN");const S={penpal:C.Syn},g=e instanceof RegExp?"*":e;window.parent.postMessage(S,g)};return{promise:new Promise((S,g)=>{const d=Ie(t,c),m=a=>{if(Xe()&&!(a.source!==parent||!a.data)&&a.data.penpal===C.SynAck){const y=a.data,$=new le(y.id,y.appName),M=new le(y.id+"/api",y.appName),F=y.id,O={},R=u=>"api/"+u,q={$emit:(u,...f)=>{$.getEventCallBackLength(u)>0?$.$pureEmit(u,...f):O==null||O.$off(u,()=>{},!0)},onQueryResult:u=>{M.$pureEmit(R(u.reqid),u)},onOsNotify:u=>{}},L=u=>function(h){const _=R(u);M.$on(_,function V(A){h.next(A),A.reqid&&A.result==="succ"&&(h.complete(),M.$off(_,V))})},tt=oe(q),I=ke(e,tt,{...o,postmateVersion:je.version},s,i)(a),x={},H=I.bus;if(H){const u=(f,h,_)=>{_?(h.$__once=!0,$.$once(f,h)):$.$on(f,h),x[f]?x[f].push(h):(x[f]=[h],H.$on(f,F))};Object.assign(O,H,{$on:u,$off:(f,h,_)=>{var V;if(x[f]){const A=x[f].indexOf(h);A>-1&&(x[f].splice(A,1),$.$off(f,h))}_&&delete x[f],(V=x[f])!=null&&V.length||(H.$off(f,F),delete x[f])},$once:(f,h)=>{u(f,h,!0)}})}const nt={...I,bus:O,query:async(u,f)=>{const h=(u==null?void 0:u.reqid)||await I.genReqId();return new Promise((_,V)=>{I.query({...u,reqid:h},f).then(A=>{(A==null?void 0:A.result)==="succ"?_(A):V(A)})})},createObservableQuery:async(u,f={})=>{const h=(u==null?void 0:u.reqid)||await I.genReqId(),_=new Ke(L(h));return I.query({...u,reqid:h},{...f,observable:!0}),_}};I&&(window.removeEventListener(D.Message,m),d(),S({methods:nt,config:a.data.config}))}};window.addEventListener(D.Message,m),E(),p(a=>{window.removeEventListener(D.Message,m),a&&g(a)})}),destroy(){c()}}},et=n=>Ze({debug:n==null?void 0:n.debug}).promise;class we{constructor(e={}){P(this,"options");P(this,"flutterInAppWebView",null);P(this,"osConnector",null);P(this,"initPromise");P(this,"isWeb",!0);this.options=e,e.debug&&(l.setDebug(!0),l.log("Trim App initialized with debug mode")),l.log("Options:",e),this.initPromise=this.init()}async init(){return l.log("Starting initialization..."),new Promise(e=>{X()?(this.isWeb=!1,l.log("Initializing mobile platform..."),Ee().then(t=>{this.flutterInAppWebView=t,l.log("Mobile platform initialized"),e()})):(this.isWeb=!0,l.log("Initializing web platform..."),et().then(t=>{this.osConnector=t,l.log("Web platform initialized"),e()}))})}async callAppMethod(e,t){return this.flutterInAppWebView?Se(this.flutterInAppWebView,e,t):null}async ready(){return this.initPromise}getOptions(){return this.options}setOptions(e){this.options={...this.options,...e},e.debug!==void 0&&l.setDebug(e.debug),l.log("Options updated:",this.options)}async getPlatformConfig(){var e;if(await this.initPromise,this.isWeb){const t=await((e=this.osConnector)==null?void 0:e.methods.getPlatformConfig());if(!t)throw new Error("Failed to get platform config");return t}else{const t=await this.callAppMethod("getAppMessage");if(!t)throw new Error("Failed to get platform config");return{theme:t.nightMode,language:t.language,appVersion:t.appVersion,systemVersion:t.systemVersion,format:{date:t.dateFormat,time:t.timeFormat}}}}async setTitle(e){var t;return await this.initPromise,l.log("Setting title:",e),this.isWeb?(t=this.osConnector)==null?void 0:t.methods.setTitle(e):this.callAppMethod("updateTitle",e)}async setExitPageTips(e){var t;return await this.initPromise,this.isWeb?(t=this.osConnector)==null?void 0:t.methods.setExitPageTips(e):this.callAppMethod("setExitPageTips",e?JSON.stringify(e):"")}async openFile(e){var t;return await this.initPromise,l.log("Opening file:",e),this.isWeb?(t=this.osConnector)==null?void 0:t.methods.openFile(e):this.callAppMethod("openFile",e)}async openFileManager(e){var t;return await this.initPromise,l.log("Opening file manager:",e),this.isWeb?(t=this.osConnector)==null?void 0:t.methods.openFileManagerApp(e):this.callAppMethod("openFileManager",e)}async close(){var e;return await this.initPromise,this.isWeb?(e=this.osConnector)==null?void 0:e.methods.close():this.callAppMethod("exitPage")}async query(e,t){var r;if(await this.initPromise,this.isWeb){const o=await((r=this.osConnector)==null?void 0:r.methods.query(e,t));if(!o)throw new Error("Failed to query");return o}else throw new Error("query is not supported on mobile platform")}async pickFile(e){var t;return await this.initPromise,this.isWeb?(t=this.osConnector)==null?void 0:t.methods.pickFile(e):this.callAppMethod("openFolder",JSON.stringify(e))}}w.TrimApp=we,w.default=we,Object.defineProperties(w,{__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.0.
|
|
3
|
+
"version": "0.0.6",
|
|
4
4
|
"description": "web app sdk for nas",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "src/index.ts",
|
|
@@ -15,11 +15,9 @@
|
|
|
15
15
|
"src",
|
|
16
16
|
"dist"
|
|
17
17
|
],
|
|
18
|
-
"dependencies": {
|
|
19
|
-
"@fn/micro-app-postmate": "^0.1.2",
|
|
20
|
-
"@fn/micro-app-event": "^0.0.1"
|
|
21
|
-
},
|
|
22
18
|
"devDependencies": {
|
|
19
|
+
"@fn/micro-app-postmate": "workspace:*",
|
|
20
|
+
"@fn/micro-app-event": "workspace:*",
|
|
23
21
|
"@eslint/js": "^9.13.0",
|
|
24
22
|
"eslint": "^9.13.0",
|
|
25
23
|
"globals": "^15.11.0",
|
package/src/index.ts
CHANGED
package/src/mobile/index.ts
CHANGED
|
@@ -14,12 +14,17 @@ declare global {
|
|
|
14
14
|
}
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
+
let isReady = false;
|
|
18
|
+
window.addEventListener("flutterInAppWebViewPlatformReady", () => {
|
|
19
|
+
isReady = true;
|
|
20
|
+
});
|
|
21
|
+
|
|
17
22
|
export const initMobile = () => {
|
|
18
23
|
/**
|
|
19
24
|
* 处理app有回掉的方法消息
|
|
20
25
|
*/
|
|
21
26
|
return new Promise<FlutterInAppWebView>((res) => {
|
|
22
|
-
if (window.flutter_inappwebview?._platformReady) {
|
|
27
|
+
if (isReady || window.flutter_inappwebview?._platformReady) {
|
|
23
28
|
logger.log("flutter_inappwebview is ready");
|
|
24
29
|
res(window.flutter_inappwebview);
|
|
25
30
|
} else {
|