@netless/app-embedded-page-sdk
Advanced tools
Comparing version 0.0.1 to 0.0.2
@@ -56,3 +56,3 @@ import type { ReceiveMessages, SendMessages, DiffOne, InitData } from "@netless/app-embedded-page"; | ||
} | ||
export interface EmbeddedApp<State = any, Message = any> { | ||
export interface EmbeddedApp<State = Record<string, any>, Message = any> { | ||
readonly state: Readonly<State>; | ||
@@ -62,2 +62,3 @@ readonly page?: string; | ||
readonly meta: Readonly<InitData["meta"]>; | ||
ensureState(partialState: Partial<State>): void; | ||
setState(partialState: Partial<State>): void; | ||
@@ -67,3 +68,2 @@ setPage(page: string): void; | ||
destroy(): void; | ||
onInit: Emitter<InitData>; | ||
onStateChanged: Emitter<Diff<State>>; | ||
@@ -78,5 +78,7 @@ onPageChanged: Emitter<DiffOne<string>>; | ||
* type Message = { type: "click"; payload: { id: string } }; | ||
* const app = createEmbeddedApp<State, Message>({ count: 0 }); | ||
* const app = await createEmbeddedApp<State, Message>((app) => { | ||
* app.ensureState({ count: 0 }) | ||
* }); | ||
*/ | ||
export declare function createEmbeddedApp<State = any, Message = any>(state: State): EmbeddedApp<State, Message>; | ||
export declare function createEmbeddedApp<State = Record<string, any>, Message = any>(callback?: (app: EmbeddedApp<State, Message>) => void): Promise<EmbeddedApp<State, Message>>; | ||
export {}; |
@@ -1,2 +0,2 @@ | ||
"use strict";var I=Object.defineProperty;var b=Object.getOwnPropertySymbols;var D=Object.prototype.hasOwnProperty,S=Object.prototype.propertyIsEnumerable;var w=(s,e,t)=>e in s?I(s,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):s[e]=t,u=(s,e)=>{for(var t in e||(e={}))D.call(e,t)&&w(s,t,e[t]);if(b)for(var t of b(e))S.call(e,t)&&w(s,t,e[t]);return s};Object.defineProperty(exports,"__esModule",{value:!0});exports[Symbol.toStringTag]="Module";class C{constructor(){this.disposers=new Map,this.disposerIDGenCount=-1}add(e,t=this.genDisposerID()){return this.flush(t),this.disposers.set(t,e()),t}addEventListener(e,t,o,n,d=this.genDisposerID()){return this.add(()=>(e.addEventListener(t,o,n),()=>e.removeEventListener(t,o,n)),d),d}setTimeout(e,t,o=this.genDisposerID()){return this.add(()=>{const n=window.setTimeout(()=>{this.remove(o),e()},t);return()=>window.clearTimeout(n)},o)}setInterval(e,t,o=this.genDisposerID()){return this.add(()=>{const n=window.setInterval(e,t);return()=>window.clearInterval(n)},o)}remove(e){const t=this.disposers.get(e);return this.disposers.delete(e),t}flush(e){const t=this.remove(e);if(t)try{t()}catch(o){console.error(o)}}flushAll(){this.disposers.forEach(e=>{try{e()}catch(t){console.error(t)}}),this.disposers.clear()}genDisposerID(){const{MAX_SAFE_INTEGER:e=9007199254740991}=Number;return this.disposerIDGenCount=(this.disposerIDGenCount+1)%e,`disposer-${this.disposerIDGenCount}`}}function E(s){return typeof s=="object"&&s!==null}function p(){const s=new Set;return{dispatch:n=>s.forEach(d=>d(n)),addListener:n=>s.add(n),removeListener:n=>s.delete(n)}}function M(s){s=u({},s);let e,t=!1,o;const n=p(),d=p(),y=p(),f=p(),v=p(),m=new C;let c,h,g;function l(r){parent.postMessage(r,"*")}return m.addEventListener(window,"message",r=>{if(!E(r.data)){console.warn("window message data should be object, instead got",r.data);return}const a=r.data;if(a.type==="Init"){const{payload:i}=a;i.state=s=u(u({},s),i.state),e=i.page,t=i.writable,o=i.meta,n.dispatch(i)}else a.type==="ReceiveMessage"?d.dispatch(a.payload):(a.type==="StateChanged"?(c=a.payload,l({type:"GetState"})):a.type==="GetState"&&(s=a.payload,c&&(y.dispatch(c),c=void 0)),a.type==="PageChanged"?(h=a.payload,l({type:"GetPage"})):a.type==="GetPage"&&(e=a.payload,h&&(f.dispatch(h),h=void 0)),a.type==="WritableChanged"?(g=a.payload,l({type:"GetWritable"})):a.type==="GetWritable"&&(t=a.payload,g&&(v.dispatch(g),g=void 0)))}),{get state(){return s},get page(){return e},get isWritable(){return t},get meta(){return o},setState:r=>{for(const[a,i]of Object.entries(r))i===void 0?delete s[a]:s[a]=i;l({type:"SetState",payload:r})},setPage:r=>{l({type:"SetPage",payload:r})},sendMessage:r=>{l({type:"SendMessage",payload:r})},destroy:()=>m.flushAll(),onInit:n,onStateChanged:y,onPageChanged:f,onWritableChanged:v,onMessage:d}}exports.createEmbeddedApp=M; | ||
"use strict";var D=Object.defineProperty;var I=Object.getOwnPropertySymbols;var C=Object.prototype.hasOwnProperty,E=Object.prototype.propertyIsEnumerable;var b=(n,e,t)=>e in n?D(n,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):n[e]=t,g=(n,e)=>{for(var t in e||(e={}))C.call(e,t)&&b(n,t,e[t]);if(I)for(var t of I(e))E.call(e,t)&&b(n,t,e[t]);return n};Object.defineProperty(exports,"__esModule",{value:!0});exports[Symbol.toStringTag]="Module";class M{constructor(){this.disposers=new Map,this.disposerIDGenCount=-1}add(e,t=this.genDisposerID()){return this.flush(t),this.disposers.set(t,e()),t}addEventListener(e,t,r,o,i=this.genDisposerID()){return this.add(()=>(e.addEventListener(t,r,o),()=>e.removeEventListener(t,r,o)),i),i}setTimeout(e,t,r=this.genDisposerID()){return this.add(()=>{const o=window.setTimeout(()=>{this.remove(r),e()},t);return()=>window.clearTimeout(o)},r)}setInterval(e,t,r=this.genDisposerID()){return this.add(()=>{const o=window.setInterval(e,t);return()=>window.clearInterval(o)},r)}remove(e){const t=this.disposers.get(e);return this.disposers.delete(e),t}flush(e){const t=this.remove(e);if(t)try{t()}catch(r){console.error(r)}}flushAll(){this.disposers.forEach(e=>{try{e()}catch(t){console.error(t)}}),this.disposers.clear()}genDisposerID(){const{MAX_SAFE_INTEGER:e=9007199254740991}=Number;return this.disposerIDGenCount=(this.disposerIDGenCount+1)%e,`disposer-${this.disposerIDGenCount}`}}function P(n){return typeof n=="object"&&n!==null}function l(){const n=new Set;return{dispatch:o=>n.forEach(i=>i(o)),addListener:o=>n.add(o),removeListener:o=>n.delete(o)}}function G(n){let e,t,r=!1,o;const i=l(),y=l(),f=l(),v=l(),m=l(),w=new M;let c,h,u;function p(a){parent.postMessage(a,"*")}w.addEventListener(window,"message",a=>{if(!P(a.data)){console.warn("window message data should be object, instead got",a.data);return}const s=a.data;if(s.type==="Init"){const{payload:d}=s;e=d.state,t=d.page,r=d.writable,o=d.meta,i.dispatch(d)}else s.type==="ReceiveMessage"?y.dispatch(s.payload):(s.type==="StateChanged"?(c=s.payload,p({type:"GetState"})):s.type==="GetState"&&(e=s.payload,c&&(f.dispatch(c),c=void 0)),s.type==="PageChanged"?(h=s.payload,p({type:"GetPage"})):s.type==="GetPage"&&(t=s.payload,h&&(v.dispatch(h),h=void 0)),s.type==="WritableChanged"?(u=s.payload,p({type:"GetWritable"})):s.type==="GetWritable"&&(r=s.payload,u&&(m.dispatch(u),u=void 0)))});const S={get state(){return e},get page(){return t},get isWritable(){return r},get meta(){return o},ensureState:a=>{e=g(g({},a),e)},setState:a=>{for(const[s,d]of Object.entries(a))d===void 0?delete e[s]:e[s]=d;p({type:"SetState",payload:a})},setPage:a=>{t=a,p({type:"SetPage",payload:a})},sendMessage:a=>{p({type:"SendMessage",payload:a})},destroy:()=>w.flushAll(),onStateChanged:f,onPageChanged:v,onWritableChanged:m,onMessage:y};return new Promise(a=>{const s=()=>{i.removeListener(s),n==null||n(S),a(S)};i.addListener(s)})}exports.createEmbeddedApp=G; | ||
//# sourceMappingURL=main.cjs.js.map |
@@ -78,4 +78,4 @@ var __defProp = Object.defineProperty; | ||
} | ||
function createEmbeddedApp(state) { | ||
state = __spreadValues({}, state); | ||
function createEmbeddedApp(callback) { | ||
let state; | ||
let page; | ||
@@ -104,3 +104,3 @@ let writable = false; | ||
const { payload } = event; | ||
payload.state = state = __spreadValues(__spreadValues({}, state), payload.state); | ||
state = payload.state; | ||
page = payload.page; | ||
@@ -145,2 +145,5 @@ writable = payload.writable; | ||
}); | ||
const ensureState = (initialState) => { | ||
state = __spreadValues(__spreadValues({}, initialState), state); | ||
}; | ||
const setState = (newState) => { | ||
@@ -156,4 +159,5 @@ for (const [key, value] of Object.entries(newState)) { | ||
}; | ||
const setPage = (page2) => { | ||
postMessage({ type: "SetPage", payload: page2 }); | ||
const setPage = (newPage) => { | ||
page = newPage; | ||
postMessage({ type: "SetPage", payload: newPage }); | ||
}; | ||
@@ -164,3 +168,3 @@ const sendMessage = (payload) => { | ||
const destroy = () => sideEffectManager.flushAll(); | ||
return { | ||
const app = { | ||
get state() { | ||
@@ -178,2 +182,3 @@ return state; | ||
}, | ||
ensureState, | ||
setState, | ||
@@ -183,3 +188,2 @@ setPage, | ||
destroy, | ||
onInit, | ||
onStateChanged, | ||
@@ -190,4 +194,12 @@ onPageChanged, | ||
}; | ||
return new Promise((resolve) => { | ||
const handler = () => { | ||
onInit.removeListener(handler); | ||
callback == null ? void 0 : callback(app); | ||
resolve(app); | ||
}; | ||
onInit.addListener(handler); | ||
}); | ||
} | ||
export { createEmbeddedApp }; | ||
//# sourceMappingURL=main.es.js.map |
@@ -1,2 +0,2 @@ | ||
(()=>{var C=Object.defineProperty;var S=Object.getOwnPropertySymbols;var P=Object.prototype.hasOwnProperty,M=Object.prototype.propertyIsEnumerable;var D=(r,i,d)=>i in r?C(r,i,{enumerable:!0,configurable:!0,writable:!0,value:d}):r[i]=d,v=(r,i)=>{for(var d in i||(i={}))P.call(i,d)&&D(r,d,i[d]);if(S)for(var d of S(i))M.call(i,d)&&D(r,d,i[d]);return r};var T=function(r){"use strict";class i{constructor(){this.disposers=new Map,this.disposerIDGenCount=-1}add(e,t=this.genDisposerID()){return this.flush(t),this.disposers.set(t,e()),t}addEventListener(e,t,o,a,p=this.genDisposerID()){return this.add(()=>(e.addEventListener(t,o,a),()=>e.removeEventListener(t,o,a)),p),p}setTimeout(e,t,o=this.genDisposerID()){return this.add(()=>{const a=window.setTimeout(()=>{this.remove(o),e()},t);return()=>window.clearTimeout(a)},o)}setInterval(e,t,o=this.genDisposerID()){return this.add(()=>{const a=window.setInterval(e,t);return()=>window.clearInterval(a)},o)}remove(e){const t=this.disposers.get(e);return this.disposers.delete(e),t}flush(e){const t=this.remove(e);if(t)try{t()}catch(o){console.error(o)}}flushAll(){this.disposers.forEach(e=>{try{e()}catch(t){console.error(t)}}),this.disposers.clear()}genDisposerID(){const{MAX_SAFE_INTEGER:e=9007199254740991}=Number;return this.disposerIDGenCount=(this.disposerIDGenCount+1)%e,`disposer-${this.disposerIDGenCount}`}}function d(n){return typeof n=="object"&&n!==null}function g(){const n=new Set;return{dispatch:a=>n.forEach(p=>p(a)),addListener:a=>n.add(a),removeListener:a=>n.delete(a)}}function E(n){n=v({},n);let e,t=!1,o;const a=g(),p=g(),m=g(),b=g(),w=g(),I=new i;let u,y,f;function h(l){parent.postMessage(l,"*")}return I.addEventListener(window,"message",l=>{if(!d(l.data)){console.warn("window message data should be object, instead got",l.data);return}const s=l.data;if(s.type==="Init"){const{payload:c}=s;c.state=n=v(v({},n),c.state),e=c.page,t=c.writable,o=c.meta,a.dispatch(c)}else s.type==="ReceiveMessage"?p.dispatch(s.payload):(s.type==="StateChanged"?(u=s.payload,h({type:"GetState"})):s.type==="GetState"&&(n=s.payload,u&&(m.dispatch(u),u=void 0)),s.type==="PageChanged"?(y=s.payload,h({type:"GetPage"})):s.type==="GetPage"&&(e=s.payload,y&&(b.dispatch(y),y=void 0)),s.type==="WritableChanged"?(f=s.payload,h({type:"GetWritable"})):s.type==="GetWritable"&&(t=s.payload,f&&(w.dispatch(f),f=void 0)))}),{get state(){return n},get page(){return e},get isWritable(){return t},get meta(){return o},setState:l=>{for(const[s,c]of Object.entries(l))c===void 0?delete n[s]:n[s]=c;h({type:"SetState",payload:l})},setPage:l=>{h({type:"SetPage",payload:l})},sendMessage:l=>{h({type:"SendMessage",payload:l})},destroy:()=>I.flushAll(),onInit:a,onStateChanged:m,onPageChanged:b,onWritableChanged:w,onMessage:p}}return r.createEmbeddedApp=E,Object.defineProperty(r,"__esModule",{value:!0}),r[Symbol.toStringTag]="Module",r}({});})(); | ||
(()=>{var M=Object.defineProperty;var E=Object.getOwnPropertySymbols;var G=Object.prototype.hasOwnProperty,L=Object.prototype.propertyIsEnumerable;var C=(r,i,d)=>i in r?M(r,i,{enumerable:!0,configurable:!0,writable:!0,value:d}):r[i]=d,v=(r,i)=>{for(var d in i||(i={}))G.call(i,d)&&C(r,d,i[d]);if(E)for(var d of E(i))L.call(i,d)&&C(r,d,i[d]);return r};var O=function(r){"use strict";class i{constructor(){this.disposers=new Map,this.disposerIDGenCount=-1}add(e,t=this.genDisposerID()){return this.flush(t),this.disposers.set(t,e()),t}addEventListener(e,t,o,a,l=this.genDisposerID()){return this.add(()=>(e.addEventListener(t,o,a),()=>e.removeEventListener(t,o,a)),l),l}setTimeout(e,t,o=this.genDisposerID()){return this.add(()=>{const a=window.setTimeout(()=>{this.remove(o),e()},t);return()=>window.clearTimeout(a)},o)}setInterval(e,t,o=this.genDisposerID()){return this.add(()=>{const a=window.setInterval(e,t);return()=>window.clearInterval(a)},o)}remove(e){const t=this.disposers.get(e);return this.disposers.delete(e),t}flush(e){const t=this.remove(e);if(t)try{t()}catch(o){console.error(o)}}flushAll(){this.disposers.forEach(e=>{try{e()}catch(t){console.error(t)}}),this.disposers.clear()}genDisposerID(){const{MAX_SAFE_INTEGER:e=9007199254740991}=Number;return this.disposerIDGenCount=(this.disposerIDGenCount+1)%e,`disposer-${this.disposerIDGenCount}`}}function d(p){return typeof p=="object"&&p!==null}function u(){const p=new Set;return{dispatch:a=>p.forEach(l=>l(a)),addListener:a=>p.add(a),removeListener:a=>p.delete(a)}}function P(p){let e,t,o=!1,a;const l=u(),m=u(),w=u(),S=u(),b=u(),I=new i;let g,y,f;function h(n){parent.postMessage(n,"*")}I.addEventListener(window,"message",n=>{if(!d(n.data)){console.warn("window message data should be object, instead got",n.data);return}const s=n.data;if(s.type==="Init"){const{payload:c}=s;e=c.state,t=c.page,o=c.writable,a=c.meta,l.dispatch(c)}else s.type==="ReceiveMessage"?m.dispatch(s.payload):(s.type==="StateChanged"?(g=s.payload,h({type:"GetState"})):s.type==="GetState"&&(e=s.payload,g&&(w.dispatch(g),g=void 0)),s.type==="PageChanged"?(y=s.payload,h({type:"GetPage"})):s.type==="GetPage"&&(t=s.payload,y&&(S.dispatch(y),y=void 0)),s.type==="WritableChanged"?(f=s.payload,h({type:"GetWritable"})):s.type==="GetWritable"&&(o=s.payload,f&&(b.dispatch(f),f=void 0)))});const D={get state(){return e},get page(){return t},get isWritable(){return o},get meta(){return a},ensureState:n=>{e=v(v({},n),e)},setState:n=>{for(const[s,c]of Object.entries(n))c===void 0?delete e[s]:e[s]=c;h({type:"SetState",payload:n})},setPage:n=>{t=n,h({type:"SetPage",payload:n})},sendMessage:n=>{h({type:"SendMessage",payload:n})},destroy:()=>I.flushAll(),onStateChanged:w,onPageChanged:S,onWritableChanged:b,onMessage:m};return new Promise(n=>{const s=()=>{l.removeListener(s),p==null||p(D),n(D)};l.addListener(s)})}return r.createEmbeddedApp=P,Object.defineProperty(r,"__esModule",{value:!0}),r[Symbol.toStringTag]="Module",r}({});})(); | ||
//# sourceMappingURL=main.iife.js.map |
{ | ||
"name": "@netless/app-embedded-page-sdk", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"main": "dist/main.cjs.js", | ||
@@ -11,9 +11,9 @@ "module": "dist/main.es.js", | ||
"build:dev": "vite build --mode development && npm run types", | ||
"cleanup": "rimraf ./build" | ||
"cleanup": "rimraf ./dist" | ||
}, | ||
"devDependencies": { | ||
"@netless/app-embedded-page": "workspace:*", | ||
"@netless/app-shared": "workspace:*", | ||
"@netless/app-embedded-page": "workspace:*", | ||
"side-effect-manager": "^0.1.2" | ||
"side-effect-manager": "^0.1.3" | ||
} | ||
} |
@@ -13,16 +13,10 @@ ## @netless/app-embedded-page-sdk | ||
- **createEmbeddedApp(initialState?)** | ||
- **createEmbeddedApp()** | ||
创建一个 Embedded App 实例。 | ||
返回: `EmbeddedApp<State, Message>` | ||
返回: `Promise<EmbeddedApp<State, Message>>` | ||
**initialState** | ||
类型: `State = Record<string, unknown>` | ||
在 `onInit` 回调执行之前,访问 `app.state` 会得到这个值。 | ||
```js | ||
const app = createEmbeddedApp({ count: 0 }); | ||
const app = await createEmbeddedApp(); | ||
``` | ||
@@ -58,2 +52,23 @@ | ||
- **app.ensureState(partialState)** | ||
确保 `app.state` 包含某些初始值,类似于执行了: | ||
```js | ||
// 这段代码不能直接运行,因为 app.state 是只读的 | ||
app.state = { ...partialState, ...app.state }; | ||
``` | ||
和 `app.setState()` 不一样,它不会同步这些初始值给所有客户端。 | ||
**partialState** | ||
类型: `Partial<State>` | ||
```js | ||
app.state; // { a: 1 } | ||
app.ensureState({ a: 0, b: 0 }); | ||
app.state; // { a: 1, b: 0 } | ||
``` | ||
- **app.setState(partialState)** | ||
@@ -63,2 +78,4 @@ | ||
当设置某个字段为 `undefined` 时,它会被从 `app.state` 里删除。 | ||
> **注意:** 不要依赖状态变化的顺序关系 | ||
@@ -105,16 +122,2 @@ > | ||
- **app.onInit** | ||
当应用初始化时发送一次。 | ||
类型: `Emitter<{ state, page?, writable, meta: { roomUUID?, userPayload? } }>` | ||
```js | ||
app.onInit.addListener(data => { | ||
console.log("已初始化", data); | ||
// it is safe to access state, page, writable and meta now | ||
console.log(app.state, app.page, app.writable, app.meta); | ||
}); | ||
``` | ||
- **app.onStateChanged** | ||
@@ -121,0 +124,0 @@ |
@@ -15,16 +15,10 @@ ## @netless/app-embedded-page-sdk | ||
- **createEmbeddedApp(initialState?)** | ||
- **createEmbeddedApp()** | ||
Creates an embedded app instance. | ||
Returns: `EmbeddedApp<State, Message>` | ||
Returns: `Promise<EmbeddedApp<State, Message>>` | ||
**initialState** | ||
Type: `State = Record<string, unknown>` | ||
It will be the value of `app.state` if you access it before `onInit` happens. | ||
```js | ||
const app = createEmbeddedApp({ count: 0 }); | ||
const app = await createEmbeddedApp(); | ||
``` | ||
@@ -61,2 +55,23 @@ | ||
- **app.ensureState(partialState)** | ||
Make sure `app.state` has some initial values, work as if: | ||
```js | ||
// this code won't work because app.state is readonly | ||
app.state = { ...partialState, ...app.state }; | ||
``` | ||
This method modifies state locally. | ||
**partialState** | ||
Type: `Partial<State>` | ||
```js | ||
app.state; // { a: 1 } | ||
app.ensureState({ a: 0, b: 0 }); | ||
app.state; // { a: 1, b: 0 } | ||
``` | ||
- **app.setState(partialState)** | ||
@@ -66,2 +81,4 @@ | ||
When some field's value is `undefined`, it will be removed from `app.state`. | ||
> **Important:** Do not rely on the order of state changes: | ||
@@ -78,4 +95,4 @@ > | ||
app.state; //=> { count: 0, a: 1 } | ||
app.setState({ count: app.state.count + 1, b: 2 }); | ||
app.state; //=> { count: 1, a: 1, b: 2 } | ||
app.setState({ count: app.state.count + 1, a: undefined, b: 2 }); | ||
app.state; //=> { count: 1, b: 2 } | ||
``` | ||
@@ -109,16 +126,2 @@ | ||
- **app.onInit** | ||
Fires once when the app is initialized. | ||
Type: `Emitter<{ state, page?, writable, meta: { roomUUID?, userPayload? } }>` | ||
```js | ||
app.onInit.addListener(data => { | ||
console.log("connected", data); | ||
// it is safe to access state, page, writable and meta now | ||
console.log(app.state, app.page, app.writable, app.meta); | ||
}); | ||
``` | ||
- **app.onStateChanged** | ||
@@ -125,0 +128,0 @@ |
@@ -52,3 +52,3 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ | ||
export interface EmbeddedApp<State = any, Message = any> { | ||
export interface EmbeddedApp<State = Record<string, any>, Message = any> { | ||
readonly state: Readonly<State>; | ||
@@ -58,2 +58,3 @@ readonly page?: string; | ||
readonly meta: Readonly<InitData["meta"]>; | ||
ensureState(partialState: Partial<State>): void; | ||
setState(partialState: Partial<State>): void; | ||
@@ -63,3 +64,2 @@ setPage(page: string): void; | ||
destroy(): void; | ||
onInit: Emitter<InitData>; | ||
onStateChanged: Emitter<Diff<State>>; | ||
@@ -75,8 +75,10 @@ onPageChanged: Emitter<DiffOne<string>>; | ||
* type Message = { type: "click"; payload: { id: string } }; | ||
* const app = createEmbeddedApp<State, Message>({ count: 0 }); | ||
* const app = await createEmbeddedApp<State, Message>((app) => { | ||
* app.ensureState({ count: 0 }) | ||
* }); | ||
*/ | ||
export function createEmbeddedApp<State = any, Message = any>( | ||
state: State | ||
): EmbeddedApp<State, Message> { | ||
state = { ...state }; | ||
export function createEmbeddedApp<State = Record<string, any>, Message = any>( | ||
callback?: (app: EmbeddedApp<State, Message>) => void | ||
): Promise<EmbeddedApp<State, Message>> { | ||
let state: State; | ||
let page: string | undefined; | ||
@@ -112,3 +114,3 @@ let writable = false; | ||
const { payload } = event; | ||
payload.state = state = { ...state, ...payload.state }; | ||
state = payload.state as unknown as State; | ||
page = payload.page; | ||
@@ -156,2 +158,6 @@ writable = payload.writable; | ||
const ensureState = (initialState: Partial<State>) => { | ||
state = { ...initialState, ...state }; | ||
}; | ||
const setState = (newState: Partial<State>) => { | ||
@@ -168,4 +174,5 @@ for (const [key, value] of Object.entries(newState)) { | ||
const setPage = (page: string) => { | ||
postMessage({ type: "SetPage", payload: page }); | ||
const setPage = (newPage: string) => { | ||
page = newPage; | ||
postMessage({ type: "SetPage", payload: newPage }); | ||
}; | ||
@@ -179,3 +186,3 @@ | ||
return { | ||
const app: EmbeddedApp<State, Message> = { | ||
get state() { | ||
@@ -193,2 +200,3 @@ return state; | ||
}, | ||
ensureState, | ||
setState, | ||
@@ -198,3 +206,2 @@ setPage, | ||
destroy, | ||
onInit, | ||
onStateChanged, | ||
@@ -205,2 +212,11 @@ onPageChanged, | ||
}; | ||
return new Promise(resolve => { | ||
const handler = () => { | ||
onInit.removeListener(handler); | ||
callback?.(app); | ||
resolve(app); | ||
}; | ||
onInit.addListener(handler); | ||
}); | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
63139
500
209