-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Passing change handlers to host
- Loading branch information
Showing
5 changed files
with
69 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,11 +2,7 @@ import type { Channel } from '@omnicajs/vue-remote/host' | |
import type { ExtensionApi } from '~types/endpoint' | ||
import type { None } from '~types/scaffolding' | ||
|
||
import type { | ||
ContextAccessor, | ||
EventSchema, | ||
TypeOf, | ||
} from '~types/context/schema' | ||
import type { ContextAccessor, TypeOf } from '~types/context/schema' | ||
|
||
import type { Schema } from '~types/context/order/card' | ||
import type { SchemaMap } from '~types/context' | ||
|
@@ -36,14 +32,15 @@ import { createWidgetEndpoint } from '@/index' | |
import { | ||
createEndpoint, | ||
fromMessagePort, | ||
retain, | ||
} from '@remote-ui/rpc' | ||
|
||
import { injectAccessor } from '@/context/store' | ||
|
||
import { useContext } from '@/context/order/card' | ||
import { useField } from '@/context/store' | ||
|
||
const createOrderContext = () => { | ||
const createHostContext = () => { | ||
const order = reactive({ | ||
customer: { | ||
email: '[email protected]', | ||
|
@@ -97,6 +94,28 @@ const createOrderContext = () => { | |
throw new Error(`Field ${field} is not supported`) | ||
} | ||
}, | ||
|
||
on (context: string, event: string, handler: (payload: unknown) => void) { | ||
if (context !== 'order/card') { | ||
throw new Error(`[crm:embed:host] Context ${context} is not supported`) | ||
} | ||
|
||
retain(handler) | ||
|
||
switch (event) { | ||
case 'change:customer.email': | ||
watch(() => order.customer.email, handler) | ||
break | ||
case 'change:customer.phone': | ||
watch(() => order.customer.phone, handler) | ||
break | ||
case 'change:delivery.address': | ||
watch(() => order.delivery.address, handler) | ||
break | ||
default: | ||
throw new Error(`Event ${event} is not supported`) | ||
} | ||
}, | ||
} as ContextAccessor<SchemaMap> | ||
|
||
return { | ||
|
@@ -112,36 +131,26 @@ describe('order/card', () => { | |
port1.start() | ||
port2.start() | ||
|
||
const { order, accessor } = createOrderContext() | ||
const { order, accessor } = createHostContext() | ||
|
||
const host = createEndpoint<ExtensionApi<None, EventSchema<Schema>>>(fromMessagePort(port1)) | ||
const host = createEndpoint<ExtensionApi<None>>(fromMessagePort(port1)) | ||
|
||
host.expose(accessor) | ||
|
||
const app = createApp({ template: '<div />' }) | ||
const pinia = createPinia() | ||
|
||
pinia.use(injectAccessor<SchemaMap>(createWidgetEndpoint<None, EventSchema<Schema>>({ | ||
async run () {}, | ||
pinia.use(injectAccessor<SchemaMap>(createWidgetEndpoint<None>({ | ||
async run () { | ||
const app = createApp({ template: '<div />' }) | ||
|
||
on: (contextId, event, payload) => { | ||
const context = useContext() | ||
if (contextId === 'order/card') { | ||
context.on(event, payload) | ||
} | ||
app.use(pinia) | ||
}, | ||
|
||
release () {}, | ||
}, fromMessagePort(port2)))) | ||
|
||
app.use(pinia) | ||
|
||
await host.call.run(null as unknown as Channel, 'order/card:customer', {}) | ||
|
||
watch(() => order.customer.email, (email) => host.call.on('order/card', 'change:customer.email', email)) | ||
watch(() => order.customer.phone, (phone) => host.call.on('order/card', 'change:customer.phone', phone)) | ||
watch(() => order.delivery.address, (address) => host.call.on('order/card', 'change:delivery.address', address)) | ||
|
||
const wrapper = mount({ | ||
setup () { | ||
const orderContext = useContext() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters