@trimjs/web-app 0.1.0 → 0.1.2
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/dist/index.cjs +3 -3
- package/dist/index.d.ts +72 -0
- package/dist/index.iife.js +2 -2
- package/dist/index.js +489 -445
- package/dist/index.umd.cjs +2 -2
- package/dist/logger.d.ts +38 -0
- package/dist/mobile/index.d.ts +23 -0
- package/dist/mobile/types.d.ts +9 -0
- package/dist/types.d.ts +17 -0
- package/dist/utils/platform.d.ts +2 -0
- package/dist/web/index.d.ts +14 -0
- package/package.json +7 -4
package/dist/index.umd.cjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
(function(
|
|
1
|
+
(function(h,u){typeof exports=="object"&&typeof module!="undefined"?u(exports):typeof define=="function"&&define.amd?define(["exports"],u):(h=typeof globalThis!="undefined"?globalThis:h||self,u(h.TrimApp={}))})(this,function(h){"use strict";var at=Object.defineProperty,lt=Object.defineProperties;var ut=Object.getOwnPropertyDescriptors;var Ce=Object.getOwnPropertySymbols;var ft=Object.prototype.hasOwnProperty,pt=Object.prototype.propertyIsEnumerable;var ae=(h,u,a)=>u in h?at(h,u,{enumerable:!0,configurable:!0,writable:!0,value:a}):h[u]=a,V=(h,u)=>{for(var a in u||(u={}))ft.call(u,a)&&ae(h,a,u[a]);if(Ce)for(var a of Ce(u))pt.call(u,a)&&ae(h,a,u[a]);return h},N=(h,u)=>lt(h,ut(u));var M=(h,u,a)=>ae(h,typeof u!="symbol"?u+"":u,a);var v=(h,u,a)=>new Promise((X,H)=>{var K=_=>{try{U(a.next(_))}catch(B){H(B)}},Z=_=>{try{U(a.throw(_))}catch(B){H(B)}},U=_=>_.done?X(_.value):Promise.resolve(_.value).then(K,Z);U((a=a.apply(h,u)).next())});class u{constructor(){M(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 a=new u;function X(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 H=()=>X(navigator.userAgent).fnAppVersion;let K=!1;window.addEventListener("flutterInAppWebViewPlatformReady",()=>{K=!0});const Z=()=>new Promise(n=>{var e;K||(e=window.flutter_inappwebview)!=null&&e._platformReady?(a.log("flutter_inappwebview is ready"),n(window.flutter_inappwebview)):window.addEventListener("flutterInAppWebViewPlatformReady",()=>{a.log("flutter_inappwebview is ready"),n(window.flutter_inappwebview)})}),U=(n,e,t)=>v(this,null,function*(){a.log(`callHandler: ${e}, params: ${t}`);const r=yield n.callHandler(e,t);if(r)try{const o=JSON.parse(r);a.log(`messageData: ${JSON.stringify(o)}`);const i=JSON.parse(o.result);return a.log(`appMessage: ${JSON.stringify(i)}`),i}catch(o){return a.error(`error: ${o}`),null}return null}),_=new Map;function B(){window.fnAppMessage=n=>{a.log("app-message",n);const e=JSON.parse(n),t=_.get(e.reqId);t&&(t(e),_.delete(e.reqId))}}H()&&B();var T=(n=>(n.Call="call",n.Reply="reply",n.Syn="syn",n.SynAck="synAck",n.Ack="ack",n))(T||{}),q=(n=>(n.Fulfilled="fulfilled",n.Rejected="rejected",n))(q||{}),ee=(n=>(n.ConnectionDestroyed="ConnectionDestroyed",n.ConnectionTimeout="ConnectionTimeout",n.NoIframeSrc="NoIframeSrc",n))(ee||{}),le=(n=>(n.DataCloneError="DataCloneError",n))(le||{}),j=(n=>(n.Message="message",n))(j||{});const Te=(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)}}},Fe=n=>(...e)=>{n&&console.log("[Penpal]",...e)},ue=({name:n,message:e,stack:t})=>({name:n,message:e,stack:t}),Oe=n=>{const e=new Error;return Object.keys(n).forEach(t=>e[t]=n[t]),e},Re=(n,e,t)=>{const{localName:r,local:o,remote:i,originForSending:s,originForReceiving:c}=n;let d=!1;const A=E=>{if(E.source!==i||E.data.penpal!==T.Call)return;if(c!=="*"&&E.origin!==c){t(`${r} received message from origin ${E.origin} which did not match expected origin ${c}`);return}const $=E.data,{methodName:y,args:g,id:b}=$;t(`${r}: Received ${y}() call`);const l=m=>P=>{if(t(`${r}: Sending ${y}() reply`),d){t(`${r}: Unable to send ${y}() reply due to destroyed connection`);return}const C={penpal:T.Reply,id:b,resolution:m,returnValue:P};m===q.Rejected&&P instanceof Error&&(C.returnValue=ue(P),C.returnValueIsError=!0);try{i.postMessage(C,s)}catch(O){if(O.name===le.DataCloneError){const R={penpal:T.Reply,id:b,resolution:q.Rejected,returnValue:ue(O),returnValueIsError:!0};i.postMessage(R,s)}throw O}};new Promise(m=>m(e[y].apply(e,g))).then(l(q.Fulfilled),l(q.Rejected))};return o.addEventListener(j.Message,A),()=>{d=!0,o.removeEventListener(j.Message,A)}};let xe=0;const De=()=>++xe,fe=".",pe=n=>n?n.split(fe):[],Ie=n=>n.join(fe),je=(n,e)=>{const t=pe(e||"");return t.push(n),Ie(t)},ke=(n,e,t)=>{const r=pe(e);return r.reduce((o,i,s)=>(typeof o[i]=="undefined"&&(o[i]={}),s===r.length-1&&(o[i]=t),o[i]),n),n},de=(n,e)=>{const t={};return Object.keys(n).forEach(r=>{const o=n[r],i=je(r,e);typeof o=="object"&&Object.assign(t,de(o,i)),typeof o=="function"&&(t[i]=o)}),t},Ve=n=>{const e={};for(const t in n)ke(e,t,n[t]);return e},We=(n,e,t,r,o)=>{const{localName:i,local:s,remote:c,originForSending:d,originForReceiving:A}=e;let E=!1;o(`${i}: Connecting call sender`);const $=g=>(...b)=>{o(`${i}: Sending ${g}() call`);let l;try{c.closed&&(l=!0)}catch(m){l=!0}if(l&&r(),E){const m=new Error(`Unable to send ${g}() call due to destroyed connection`);throw m.code=ee.ConnectionDestroyed,m}return new Promise((m,P)=>{const C=De(),O=x=>{if(x.source!==c||x.data.penpal!==T.Reply||x.data.id!==C)return;if(A!=="*"&&x.origin!==A){o(`${i} received message from origin ${x.origin} which did not match expected origin ${A}`);return}const Y=x.data;o(`${i}: Received ${g}() reply`),s.removeEventListener(j.Message,O);let z=Y.returnValue;Y.returnValueIsError&&(z=Oe(z)),(Y.resolution===q.Fulfilled?m:P)(z)};s.addEventListener(j.Message,O);const R={penpal:T.Call,id:C,methodName:g,args:b};c.postMessage(R,d)})},y=t.reduce((g,b)=>(g[b]=$(b),g),{});return Object.assign(n,Ve(y)),()=>{E=!0}},qe=(n,e)=>{let t;return n!==void 0&&(t=window.setTimeout(()=>{const r=new Error(`Connection timed out after ${n}ms`);r.code=ee.ConnectionTimeout,e(r)},n)),()=>{clearTimeout(t)}},Le={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"}},Ne=(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 A=c.origin==="null"?"*":c.origin,E={penpal:T.Ack,methodNames:Object.keys(e),config:t};window.parent.postMessage(E,A);const $={localName:"Child",local:window,remote:window.parent,originForSending:A,originForReceiving:c.origin},y=Re($,e,o);s(y);const g={},b=We(g,$,c.data.methodNames,i,o);return s(b),g}};function he(n,e){console==null||console.warn(`[fnApp warn]: ${n}`,e)}function ge(n,e){console==null||console.error(`[fnApp error]: ${n}`,e)}const L="_fn_all_event",ye="事件订阅数量为空",J=window.__POWERED_BY_FNAPP__?window.__FNAPP.inject.appEventObjMap:new Map;class me{constructor(e,t){M(this,"id");M(this,"appName");M(this,"eventObj");M(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(),J.get(this.id)||J.set(this.id,{}),this.eventObj=J.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(L,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 he(`${e} ${ye}`),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(L,e)}$emit(e,...t){return e=`${this.appName}/${e}`,this.$pureEmit(e,...t)}$pureEmit(e,...t){let r=0,o=0;return J.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(d){ge(d)}}if(i[L])for(let s=i[L].length-1;s>=0;s--){r++;const c=i[L][s];c.$__once&&i[L].splice(s,1);try{c(...t)}catch(d){ge(d)}}}),(!e||r===0&&o===0)&&he(`${e} ${ye}`),this}$clear(){var r;const e=(r=J.get(this.id))!=null?r:{};return Object.keys(e).forEach(o=>delete e[o]),this}}var te=function(n,e){return te=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])},te(n,e)};function be(n,e){if(typeof e!="function"&&e!==null)throw new TypeError("Class extends value "+String(e)+" is not a constructor or null");te(n,e);function t(){this.constructor=n}n.prototype=e===null?Object.create(e):(t.prototype=e.prototype,new t)}function we(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 ne(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 re(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 F(n){return typeof n=="function"}function He(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 ie=He(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 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"}})});
|
|
3
|
+
`):"",this.name="UnsubscriptionError",this.errors=t}});function ve(n,e){if(n){var t=n.indexOf(e);0<=t&&n.splice(t,1)}}var oe=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=we(s),d=c.next();!d.done;d=c.next()){var A=d.value;A.remove(this)}}catch(l){e={error:l}}finally{try{d&&!d.done&&(t=c.return)&&t.call(c)}finally{if(e)throw e.error}}else s.remove(this);var E=this.initialTeardown;if(F(E))try{E()}catch(l){i=l instanceof ie?l.errors:[l]}var $=this._finalizers;if($){this._finalizers=null;try{for(var y=we($),g=y.next();!g.done;g=y.next()){var b=g.value;try{Se(b)}catch(l){i=i!=null?i:[],l instanceof ie?i=re(re([],ne(i)),ne(l.errors)):i.push(l)}}}catch(l){r={error:l}}finally{try{g&&!g.done&&(o=y.return)&&o.call(y)}finally{if(r)throw r.error}}}if(i)throw new ie(i)}},n.prototype.add=function(e){var t;if(e&&e!==this)if(this.closed)Se(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)&&ve(t,e)},n.prototype.remove=function(e){var t=this._finalizers;t&&ve(t,e),e instanceof n&&e._removeParent(this)},n.EMPTY=function(){var e=new n;return e.closed=!0,e}(),n}();oe.EMPTY;function Ee(n){return n instanceof oe||n&&"closed"in n&&F(n.remove)&&F(n.add)&&F(n.unsubscribe)}function Se(n){F(n)?n():n.unsubscribe()}var Ae={onUnhandledError:null,onStoppedNotification:null,Promise:void 0,useDeprecatedSynchronousErrorHandling:!1,useDeprecatedNextContext:!1},Ue={setTimeout:function(n,e){for(var t=[],r=2;r<arguments.length;r++)t[r-2]=arguments[r];return setTimeout.apply(void 0,re([n,e],ne(t)))},clearTimeout:function(n){return clearTimeout(n)},delegate:void 0};function Be(n){Ue.setTimeout(function(){throw n})}function $e(){}function Je(n){n()}var Pe=function(n){be(e,n);function e(t){var r=n.call(this)||this;return r.isStopped=!1,t?(r.destination=t,Ee(t)&&t.add(r)):r.destination=Qe,r}return e.create=function(t,r,o){return new ce(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}(oe),Ye=Function.prototype.bind;function se(n,e){return Ye.call(n,e)}var ze=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){Q(r)}},n.prototype.error=function(e){var t=this.partialObserver;if(t.error)try{t.error(e)}catch(r){Q(r)}else Q(e)},n.prototype.complete=function(){var e=this.partialObserver;if(e.complete)try{e.complete()}catch(t){Q(t)}},n}(),ce=function(n){be(e,n);function e(t,r,o){var i=n.call(this)||this,s;if(F(t)||!t)s={next:t!=null?t:void 0,error:r!=null?r:void 0,complete:o!=null?o:void 0};else{var c;i&&Ae.useDeprecatedNextContext?(c=Object.create(t),c.unsubscribe=function(){return i.unsubscribe()},s={next:t.next&&se(t.next,c),error:t.error&&se(t.error,c),complete:t.complete&&se(t.complete,c)}):s=t}return i.destination=new ze(s),i}return e}(Pe);function Q(n){Be(n)}function Ke(n){throw n}var Qe={closed:!0,next:$e,error:Ke,complete:$e},Ge=function(){return typeof Symbol=="function"&&Symbol.observable||"@@observable"}();function Xe(n){return n}function Ze(n){return n.length===0?Xe:n.length===1?n[0]:function(t){return n.reduce(function(r,o){return o(r)},t)}}var et=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=nt(e)?e:new ce(e,t,r);return Je(function(){var s=o,c=s.operator,d=s.source;i.add(c?c.call(i,d):d?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 ce({next:function(c){try{e(c)}catch(d){i(d),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[Ge]=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=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!=null?n:Ae.Promise)!==null&&e!==void 0?e:Promise}function tt(n){return n&&F(n.next)&&F(n.error)&&F(n.complete)}function nt(n){return n&&n instanceof Pe||tt(n)&&Ee(n)}const rt=()=>{try{clearTimeout(void 0)}catch(n){return!1}return!0},it=(n={})=>{const{parentOrigin:e="*",timeout:t,debug:r=!1,config:o={}}=n,i=Fe(r),s=Te("App",i),{destroy:c,onDestroy:d}=s,A=()=>{i("Child: Handshake - Sending SYN");const $={penpal:T.Syn},y=e instanceof RegExp?"*":e;window.parent.postMessage($,y)};return{promise:new Promise(($,y)=>{const g=qe(t,c),b=l=>{if(rt()&&!(l.source!==parent||!l.data)&&l.data.penpal===T.SynAck){const m=l.data,P=new me(m.id,m.appName),C=new me(m.id+"/api",m.appName),O=m.id,R={},x=f=>"api/"+f,Y={$emit:(f,...p)=>{P.getEventCallBackLength(f)>0?P.$pureEmit(f,...p):R==null||R.$off(f,()=>{},!0)},onQueryResult:f=>{C.$pureEmit(x(f.reqid),f)},onOsNotify:f=>{}},z=f=>function(w){const k=x(f);C.$on(k,function I(S){w.next(S),S.reqid&&S.result==="succ"&&(w.complete(),C.$off(k,I))})},st=de(Y),W=Ne(e,st,N(V({},o),{postmateVersion:Le.version}),s,i)(l),D={},G=W.bus;if(G){const f=(p,w,k)=>{k?(w.$__once=!0,P.$once(p,w)):P.$on(p,w),D[p]?D[p].push(w):(D[p]=[w],G.$on(p,O))};Object.assign(R,G,{$on:f,$off:(p,w,k)=>{var I;if(D[p]){const S=D[p].indexOf(w);S>-1&&(D[p].splice(S,1),P.$off(p,w))}k&&delete D[p],(I=D[p])!=null&&I.length||(G.$off(p,O),delete D[p])},$once:(p,w)=>{f(p,w,!0)}})}const ct=N(V({},W),{bus:R,query:(f,p)=>v(this,null,function*(){const w=(f==null?void 0:f.reqid)||(yield W.genReqId());return new Promise((k,I)=>{W.query(N(V({},f),{reqid:w}),p).then(S=>{(S==null?void 0:S.result)==="succ"?k(S):I(S)})})}),createObservableQuery:(w,...k)=>v(this,[w,...k],function*(f,p={}){const I=(f==null?void 0:f.reqid)||(yield W.genReqId()),S=new et(z(I));return W.query(N(V({},f),{reqid:I}),N(V({},p),{observable:!0})),S})});W&&(window.removeEventListener(j.Message,b),g(),$({methods:ct,config:l.data.config}))}};window.addEventListener(j.Message,b),A(),d(l=>{window.removeEventListener(j.Message,b),l&&y(l)})}),destroy(){c()}}},ot=n=>it({debug:n==null?void 0:n.debug}).promise;class _e{constructor(e={}){M(this,"options");M(this,"flutterInAppWebView",null);M(this,"osConnector",null);M(this,"initPromise");M(this,"isWeb",!0);this.options=e,e.debug&&(a.setDebug(!0),a.log("Trim App initialized with debug mode")),a.log("Options:",e),this.initPromise=this.init()}init(){return v(this,null,function*(){return a.log("Starting initialization..."),new Promise(e=>{H()?(this.isWeb=!1,a.log("Initializing mobile platform..."),Z().then(t=>{this.flutterInAppWebView=t,a.log("Mobile platform initialized"),e()})):(this.isWeb=!0,a.log("Initializing web platform..."),ot().then(t=>{this.osConnector=t,a.log("Web platform initialized"),e()}))})})}callAppMethod(e,t){return v(this,null,function*(){return this.flutterInAppWebView?U(this.flutterInAppWebView,e,t):null})}ready(){return v(this,null,function*(){return this.initPromise})}getOptions(){return this.options}setOptions(e){this.options=V(V({},this.options),e),e.debug!==void 0&&a.setDebug(e.debug),a.log("Options updated:",this.options)}getPlatformConfig(){return v(this,null,function*(){var e;if(yield this.initPromise,this.isWeb){const t=yield(e=this.osConnector)==null?void 0:e.methods.getPlatformConfig();if(!t)throw new Error("Failed to get platform config");return t}else{const t=yield 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}}}})}setTitle(e){return v(this,null,function*(){var t;return yield this.initPromise,a.log("Setting title:",e),this.isWeb?(t=this.osConnector)==null?void 0:t.methods.setTitle(e):this.callAppMethod("updateTitle",e)})}setExitPageTips(e){return v(this,null,function*(){var t;return yield this.initPromise,this.isWeb?(t=this.osConnector)==null?void 0:t.methods.setExitPageTips(e):this.callAppMethod("setExitPageTips",e?JSON.stringify(e):"")})}openFile(e){return v(this,null,function*(){var t;return yield this.initPromise,a.log("Opening file:",e),this.isWeb?(t=this.osConnector)==null?void 0:t.methods.openFile(e):this.callAppMethod("openFile",e)})}openFileManager(e){return v(this,null,function*(){var t;return yield this.initPromise,a.log("Opening file manager:",e),this.isWeb?(t=this.osConnector)==null?void 0:t.methods.openFileManagerApp(e):this.callAppMethod("openFileManager",e)})}close(){return v(this,null,function*(){var e;return yield this.initPromise,this.isWeb?(e=this.osConnector)==null?void 0:e.methods.close():this.callAppMethod("exitPage")})}query(e,t){return v(this,null,function*(){var r;if(yield this.initPromise,this.isWeb){const o=yield(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")})}pickFile(e){return v(this,null,function*(){var t;return yield this.initPromise,this.isWeb?(t=this.osConnector)==null?void 0:t.methods.pickFile(e):this.callAppMethod("openFolder",JSON.stringify(e))})}}h.TrimApp=_e,h.default=_e,Object.defineProperties(h,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
|
package/dist/logger.d.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Logger 工具类
|
|
3
|
+
* 只有在 debug 模式下才会输出日志
|
|
4
|
+
*/
|
|
5
|
+
declare class Logger {
|
|
6
|
+
private debug;
|
|
7
|
+
/**
|
|
8
|
+
* 设置 debug 模式
|
|
9
|
+
* @param enabled 是否启用 debug
|
|
10
|
+
*/
|
|
11
|
+
setDebug(enabled: boolean): void;
|
|
12
|
+
/**
|
|
13
|
+
* 获取当前 debug 状态
|
|
14
|
+
*/
|
|
15
|
+
isDebug(): boolean;
|
|
16
|
+
/**
|
|
17
|
+
* 输出日志(只在 debug 模式下)
|
|
18
|
+
* @param args 日志参数
|
|
19
|
+
*/
|
|
20
|
+
log(...args: any[]): void;
|
|
21
|
+
/**
|
|
22
|
+
* 输出警告日志(只在 debug 模式下)
|
|
23
|
+
* @param args 日志参数
|
|
24
|
+
*/
|
|
25
|
+
warn(...args: any[]): void;
|
|
26
|
+
/**
|
|
27
|
+
* 输出错误日志(只在 debug 模式下)
|
|
28
|
+
* @param args 日志参数
|
|
29
|
+
*/
|
|
30
|
+
error(...args: any[]): void;
|
|
31
|
+
/**
|
|
32
|
+
* 输出信息日志(只在 debug 模式下)
|
|
33
|
+
* @param args 日志参数
|
|
34
|
+
*/
|
|
35
|
+
info(...args: any[]): void;
|
|
36
|
+
}
|
|
37
|
+
export declare const logger: Logger;
|
|
38
|
+
export {};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export interface FlutterInAppWebView {
|
|
2
|
+
callHandler: (fn: string, params?: string) => Promise<string>;
|
|
3
|
+
_platformReady?: boolean;
|
|
4
|
+
}
|
|
5
|
+
declare global {
|
|
6
|
+
interface Window {
|
|
7
|
+
flutter_inappwebview: FlutterInAppWebView;
|
|
8
|
+
fnAppMessage?: (message: string) => void;
|
|
9
|
+
onAppHeaderGoBack?: () => boolean | number | string;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
export declare const initMobile: () => Promise<FlutterInAppWebView>;
|
|
13
|
+
export interface FnAppMessage<T> {
|
|
14
|
+
reqId: string;
|
|
15
|
+
method: string;
|
|
16
|
+
result: T;
|
|
17
|
+
}
|
|
18
|
+
export declare const callHandler: <T>(flutterInAppWebView: FlutterInAppWebView, fn: string, params?: string) => Promise<T | null>;
|
|
19
|
+
export declare function pickFilePromise(flutterInAppWebView: FlutterInAppWebView, params: {
|
|
20
|
+
title: string;
|
|
21
|
+
multiple: boolean;
|
|
22
|
+
directory: boolean;
|
|
23
|
+
}): Promise<string[] | undefined>;
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SDK 配置选项
|
|
3
|
+
*/
|
|
4
|
+
export interface TrimAppOptions {
|
|
5
|
+
/** 是否开启 debug 模式,开启后会输出日志 */
|
|
6
|
+
debug?: boolean;
|
|
7
|
+
}
|
|
8
|
+
export interface PlatformConfig {
|
|
9
|
+
theme: "dark" | "light";
|
|
10
|
+
language: string;
|
|
11
|
+
appVersion?: string;
|
|
12
|
+
systemVersion: string;
|
|
13
|
+
format: {
|
|
14
|
+
date?: string;
|
|
15
|
+
time?: string;
|
|
16
|
+
};
|
|
17
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type OSConfig } from "@fn/micro-app-postmate/src/parent/type";
|
|
2
|
+
import { type OSMethods, type ConnectionResult, type AsyncMethodReturns } from "@fn/micro-app-postmate/src/types";
|
|
3
|
+
import type { PlatformConfig, TrimAppOptions } from "../types";
|
|
4
|
+
export type OSConnection = AsyncMethodReturns<ConnectionResult<OSMethods & {
|
|
5
|
+
setExitPageTips: (params?: {
|
|
6
|
+
title?: string;
|
|
7
|
+
content?: string;
|
|
8
|
+
}) => void;
|
|
9
|
+
openFileManagerApp: (path: string) => void;
|
|
10
|
+
openFile: (path: string) => void;
|
|
11
|
+
getPlatformConfig: () => Promise<PlatformConfig>;
|
|
12
|
+
close: () => void;
|
|
13
|
+
}, OSConfig>>;
|
|
14
|
+
export declare const initWeb: (options?: TrimAppOptions) => Promise<AsyncMethodReturns<ConnectionResult<OSMethods>>>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trimjs/web-app",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "web app sdk for nas",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.cjs",
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
"types": "dist/index.d.ts",
|
|
9
9
|
"scripts": {
|
|
10
10
|
"dev": "vite",
|
|
11
|
-
"build": "tsc -b
|
|
11
|
+
"build": "vite build & tsc -b",
|
|
12
12
|
"lint": "eslint .",
|
|
13
13
|
"typecheck": "tsc --noEmit"
|
|
14
14
|
},
|
|
@@ -16,10 +16,13 @@
|
|
|
16
16
|
"src",
|
|
17
17
|
"dist"
|
|
18
18
|
],
|
|
19
|
+
"publishConfig": {
|
|
20
|
+
"access": "public"
|
|
21
|
+
},
|
|
19
22
|
"devDependencies": {
|
|
20
|
-
"@fn/micro-app-postmate": "workspace:*",
|
|
21
|
-
"@fn/micro-app-event": "workspace:*",
|
|
22
23
|
"@eslint/js": "^9.13.0",
|
|
24
|
+
"@fn/micro-app-event": "workspace:*",
|
|
25
|
+
"@fn/micro-app-postmate": "workspace:*",
|
|
23
26
|
"eslint": "^9.13.0",
|
|
24
27
|
"globals": "^15.11.0",
|
|
25
28
|
"typescript": "~5.6.2",
|