Skip to content

Commit e02def9

Browse files
committed
refactor(pos): move addEventListener to window via WindowEventMap augmentation
Remove addEventListener/removeEventListener from DataTargetApi. POS events are received via the standard window.addEventListener() Web API. PosEventMap augments WindowEventMap for type-safe event listening.
1 parent dd4cfa1 commit e02def9

3 files changed

Lines changed: 19 additions & 36 deletions

File tree

packages/ui-extensions/src/surfaces/point-of-sale/api.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ export type {ActionTargetApi} from './api/action-target-api/action-target-api';
1616
export type {
1717
DataTargetApi,
1818
PosEventMap,
19-
AddEventListener,
20-
RemoveEventListener,
2119
} from './api/data-target-api/data-target-api';
2220

2321
export type {

packages/ui-extensions/src/surfaces/point-of-sale/api/data-target-api/data-target-api.ts

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,22 @@ import type {ReturnTransactionData} from '../../event/data/ReturnTransactionData
1111
import type {ExchangeTransactionData} from '../../event/data/ExchangeTransactionData';
1212

1313
/**
14-
* Maps POS event names to their callback payload types.
14+
* Maps POS event names to their event payload types.
1515
*
16-
* Used with `addEventListener` / `removeEventListener` to provide type-safe
17-
* event observation. These are discrete events — for state changes like cart
18-
* contents, use `shopify.cart.current.subscribe()` instead.
16+
* These events are dispatched on `window` and received via the standard
17+
* `window.addEventListener(type, callback)` Web API. For continuous state
18+
* changes like cart contents, use `shopify.cart.current.subscribe()` instead.
19+
*
20+
* @example
21+
* ```ts
22+
* shopify.extend('pos.app.ready.data', () => {
23+
* window.addEventListener('transaction_complete', (event) => {
24+
* console.log(event.transaction.orderId);
25+
* });
26+
* });
27+
* ```
1928
*/
20-
export type PosEventMap = {
29+
export interface PosEventMap {
2130
/**
2231
* Fires after a sale, return, or exchange transaction completes.
2332
*/
@@ -51,41 +60,14 @@ export type PosEventMap = {
5160
closingTime: string;
5261
};
5362
};
54-
};
55-
56-
/**
57-
* Registers a listener for a POS host event. The listener is called each time
58-
* the event fires. This is passive and fire-and-forget — the host does not wait
59-
* for the listener and ignores return values.
60-
*/
61-
export type AddEventListener<EventMap extends {[key: string]: unknown}> = <
62-
K extends keyof EventMap,
63-
>(
64-
type: K,
65-
listener: (event: EventMap[K]) => void,
66-
) => void;
67-
68-
/**
69-
* Removes a previously registered event listener. The `type` and `listener`
70-
* must match those passed to `addEventListener` (same function reference).
71-
*/
72-
export type RemoveEventListener<EventMap extends {[key: string]: unknown}> = <
73-
K extends keyof EventMap,
74-
>(
75-
type: K,
76-
listener: (event: EventMap[K]) => void,
77-
) => void;
63+
}
7864

7965
/**
80-
* API surface for non-rendering data extension targets.
66+
* API surface for non-rendering data extension targets. Provides access to POS APIs (cart, storage, session, etc.) without rendering UI. POS host events are received via the standard `window.addEventListener()` Web API.
8167
*/
8268
export type DataTargetApi<T> = {
8369
extensionPoint: T;
8470
i18n: I18n;
85-
/** Register a listener for discrete POS events. */
86-
addEventListener: AddEventListener<PosEventMap>;
87-
/** Remove a previously registered event listener. */
88-
removeEventListener: RemoveEventListener<PosEventMap>;
8971
} & SessionApi &
9072
StorageApi &
9173
LocaleApi &
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import {Navigation} from './api/navigation-api/navigation-api';
2+
import type {PosEventMap} from './api/data-target-api/data-target-api';
23

34
declare global {
45
// @ts-expect-error - Intentionally overriding navigation type for POS context
56
const navigation: Navigation;
7+
8+
interface WindowEventMap extends PosEventMap {}
69
}

0 commit comments

Comments
 (0)