This repository has been archived by the owner on Oct 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(composables): interceptor functionality for events across applic…
…ation (#1103)
- Loading branch information
Showing
25 changed files
with
622 additions
and
4 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 |
---|---|---|
@@ -0,0 +1,150 @@ | ||
# Events interceptor <Badge text="since 0.4.0" type="info"/> | ||
|
||
Sometimes there is a need to react on some event in the application. You may want to show a notification when product is added to cart, or send that event to your Analytics system. | ||
|
||
To easily react to changes and events across the application we created `useIntercept` composable. | ||
It allows you to safely broadcast and intercept system events, and add your own events. | ||
|
||
:::tip Useful info | ||
|
||
All system events can be found in `INTERCEPTOR_KEYS` constant, you'll find it by importing it from composables package `import { INTERCEPTOR_KEYS } from "@shopware-pwa/composables"` | ||
::: | ||
|
||
## Usage examples | ||
|
||
### In any vue component | ||
|
||
In most cases, you'll use this mechanism indirectly. Let's take for example `useAddToCart` composable. | ||
It introduces a new type of methods `onXXX`, in our case it's `onAddToCart` | ||
|
||
so when you'd like to react on that event: | ||
|
||
```js | ||
setup({ product }, { root }) { | ||
const { | ||
onAddToCart, | ||
} = useAddToCart(root, product) | ||
|
||
onAddToCart(({product, quantity}) => { | ||
// here you can show notification, or send GTM event | ||
}) | ||
|
||
return { | ||
} | ||
}, | ||
``` | ||
|
||
:::warning Important | ||
Remember, that when you listen on the event in component, then you listen to it in every component instance. So if you have two active instances of this component and you want to show notification on adding to cart event, then you'd end up with two notifications. If you want to react on specific method do it in the component triggering the action or in Nuxt plugin (description below). | ||
::: | ||
|
||
```js | ||
setup({ product }, { root }) { | ||
const { | ||
addToCart | ||
} = useAddToCart(root, product) | ||
|
||
const yourAddToCartWrapper = async () => { | ||
await addToCart() | ||
// here you could show notification | ||
} | ||
|
||
return { | ||
yourAddToCartWrapper | ||
} | ||
}, | ||
``` | ||
|
||
### In Nuxt plugin | ||
|
||
If you don't want to change any component in order to react on some event, you can create new Nuxt plugin, for example, `src/plugins/my-interceptors.js`, add it to `nuxt.config.js` file: | ||
|
||
``` | ||
/* | ||
** Plugins to load before mounting the App | ||
** https://nuxtjs.org/guide/plugins | ||
*/ | ||
plugins: ['~/plugins/my-interceptor.js'], | ||
``` | ||
|
||
and then inside this file, you can use interceptors directly like this (it will be also useful for intercepting your own events) | ||
|
||
```js | ||
import { | ||
useIntercept, | ||
INTERCEPTOR_KEYS, | ||
useNotifications, | ||
} from "@shopware-pwa/composables"; | ||
|
||
export default async ({ app }) => { | ||
const { intercept } = useIntercept(app); | ||
const { pushSuccess } = useNotifications(app); | ||
intercept(INTERCEPTOR_KEYS.ADD_TO_CART, ({ product }) => { | ||
pushSuccess( | ||
`${product?.translated?.name || product?.name} has been added to cart.` | ||
); | ||
}); | ||
}; | ||
``` | ||
or like this | ||
```js | ||
import { | ||
useAddToCart | ||
useNotifications, | ||
} from "@shopware-pwa/composables"; | ||
export default async ({ app }) => { | ||
const { onAddToCart } = useAddToCart(app); | ||
const { pushSuccess } = useNotifications(app); | ||
onAddToCart(({ product }) => { | ||
pushSuccess( | ||
`${product?.translated?.name || product?.name} has been added to cart.` | ||
); | ||
}); | ||
}; | ||
``` | ||
### Broadcast custom event | ||
You can broadcast your custom events accross application. Your custom event will not be defined in `INTERCEPTOR_KEYS`, so you can broadcast your event like this: | ||
```js | ||
setup(props, { root }) { | ||
const { broadcast } = useIntercept(root) | ||
const yourMethod = async () => { | ||
// ...some action | ||
broadcast('my-custom-event', {param1: "xyz"}) | ||
} | ||
return { | ||
yourMethod | ||
} | ||
}, | ||
``` | ||
### Stop intercepting events | ||
If the interceptor is used in component, it ends life with the life of this component, so you don't need to worry about potential memory leaks and invoking methods in dead components. | ||
If you're invoking it in nuxt plugin it will listen for the whole time when the application is displayed. | ||
You might want to stop listening on the event at some point though, for example, another event causes that you don't want to listen to the event anymore. | ||
`disconnect` method from `useIntercept` comes with help. This is how you can do this: | ||
```js | ||
import { useIntercept, INTERCEPTOR_KEYS } from "@shopware-pwa/composables"; | ||
export default async ({ app }) => { | ||
const { intercept, disconnect } = useIntercept(app); | ||
const myOnAddToCartMethod = ({ product }) => { | ||
// do something with event | ||
}; | ||
intercept(INTERCEPTOR_KEYS.ADD_TO_CART, myOnAddToCartMethod); | ||
intercept("my-custom-event", () => { | ||
disconnect(INTERCEPTOR_KEYS.ADD_TO_CART, myOnAddToCartMethod); | ||
}); | ||
}; | ||
``` |
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
14 changes: 14 additions & 0 deletions
14
docs/landing/resources/api/composables.applicationvuecontext._interceptors.md
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [@shopware-pwa/composables](./composables.md) > [ApplicationVueContext](./composables.applicationvuecontext.md) > [$interceptors](./composables.applicationvuecontext._interceptors.md) | ||
|
||
## ApplicationVueContext.$interceptors property | ||
|
||
> This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment. | ||
> | ||
<b>Signature:</b> | ||
|
||
```typescript | ||
$interceptors?: any; | ||
``` |
14 changes: 14 additions & 0 deletions
14
docs/landing/resources/api/composables.applicationvuecontext.interceptors.md
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [@shopware-pwa/composables](./composables.md) > [ApplicationVueContext](./composables.applicationvuecontext.md) > [interceptors](./composables.applicationvuecontext.interceptors.md) | ||
|
||
## ApplicationVueContext.interceptors property | ||
|
||
> This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment. | ||
> | ||
<b>Signature:</b> | ||
|
||
```typescript | ||
interceptors?: any; | ||
``` |
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
19 changes: 19 additions & 0 deletions
19
docs/landing/resources/api/composables.interceptor_keys.md
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [@shopware-pwa/composables](./composables.md) > [INTERCEPTOR\_KEYS](./composables.interceptor_keys.md) | ||
|
||
## INTERCEPTOR\_KEYS variable | ||
|
||
> This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment. | ||
> | ||
Keys used accross composables with the description of incommint parameters. | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
INTERCEPTOR_KEYS: { | ||
ADD_TO_CART: string; | ||
ERROR: string; | ||
} | ||
``` |
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
19 changes: 19 additions & 0 deletions
19
docs/landing/resources/api/composables.iuseaddtocart.onaddtocart.md
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [@shopware-pwa/composables](./composables.md) > [IUseAddToCart](./composables.iuseaddtocart.md) > [onAddToCart](./composables.iuseaddtocart.onaddtocart.md) | ||
|
||
## IUseAddToCart.onAddToCart property | ||
|
||
> This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment. | ||
> | ||
React on product added to cart | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
onAddToCart: (fn: (params: { | ||
product: Product; | ||
quantity: Number; | ||
}) => void) => void; | ||
``` |
16 changes: 16 additions & 0 deletions
16
docs/landing/resources/api/composables.iuseintercept.broadcast.md
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [@shopware-pwa/composables](./composables.md) > [IUseIntercept](./composables.iuseintercept.md) > [broadcast](./composables.iuseintercept.broadcast.md) | ||
|
||
## IUseIntercept.broadcast property | ||
|
||
> This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment. | ||
> | ||
Broadcast new event | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
broadcast: (broadcastKey: string, value: any) => void; | ||
``` |
16 changes: 16 additions & 0 deletions
16
docs/landing/resources/api/composables.iuseintercept.disconnect.md
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [@shopware-pwa/composables](./composables.md) > [IUseIntercept](./composables.iuseintercept.md) > [disconnect](./composables.iuseintercept.disconnect.md) | ||
|
||
## IUseIntercept.disconnect property | ||
|
||
> This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment. | ||
> | ||
Stop listening on event | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
disconnect: (broadcastKey: string, method: Function) => void; | ||
``` |
Oops, something went wrong.
b315782
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: