Skip to content

Commit 14999ed

Browse files
committed
refactor(pos): rename storage.keys to storage.latest per TAG feedback
Aligns with ui-api-design#1416 rename. Updates JSDoc with cross-target reactivity example and .value/.subscribe() documentation. Updates tester mock with Proxy-based latest implementation.
1 parent 9104128 commit 14999ed

3 files changed

Lines changed: 35 additions & 14 deletions

File tree

packages/ui-extensions-tester/src/point-of-sale/index.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type {
22
LineItem,
33
Storage,
4+
StorageKeys,
45
CartApiContent,
56
} from '@shopify/ui-extensions/point-of-sale';
67

@@ -38,7 +39,23 @@ export function createStorage<
3839
const store = new Map<string, unknown>(
3940
initialValues ? Object.entries(initialValues) : [],
4041
);
42+
const createSubscribable = (key: string) => ({
43+
get value() {
44+
return store.get(key) as never;
45+
},
46+
subscribe(_fn: (value: unknown) => void) {
47+
return () => {};
48+
},
49+
});
50+
51+
const latestProxy = new Proxy({} as StorageKeys<T>, {
52+
get(_target, prop) {
53+
return createSubscribable(prop as string);
54+
},
55+
});
56+
4157
const storage: Storage<T> = {
58+
latest: latestProxy,
4259
set: async (key, value) => {
4360
store.set(key as string, value);
4461
},

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ export type {
121121
export type {CountryCode} from './types/country-code';
122122

123123
export type {Session} from './types/session';
124-
export type {Storage} from './types/storage';
124+
export type {Storage, StorageKeys} from './types/storage';
125125
export {StorageError} from './types/storage';
126126

127127
export type {PinPadApiContent, PinPadApi} from './api/pin-pad-api';

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

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,27 @@ export interface Storage<
2222
/**
2323
* Reactive access to storage values as Subscribables.
2424
*
25-
* Each key is exposed as a `ReadonlySignalLike<T | undefined>`, enabling
26-
* reactive updates across targets of the same extension. One target can
27-
* subscribe to a key and react when another target updates it via `set()`
28-
* or `delete()`.
25+
* Each key is exposed as a `ReadonlySignalLike<T | undefined>`, enabling cross-target
26+
* reactivity. One extension target can subscribe to a key and react when
27+
* another target updates it via `set()` or `update()`.
2928
*
30-
* Only available on API version `2026-04` and later.
29+
* The `value` property provides synchronous access to the current stored value.
30+
* The `subscribe()` method registers a callback that fires whenever the value
31+
* changes, including changes made by other extension targets within the same app.
3132
*
32-
* @example
33+
* @example Cross-target reactivity
3334
* ```typescript
34-
* // Subscribe to changes from another target:
35-
* const unsubscribe = api.storage.keys.syncStatus.subscribe((value) => {
36-
* console.log('syncStatus changed:', value);
35+
* // In one extension target:
36+
* await shopify.storage.set('syncStatus', 'complete');
37+
*
38+
* // In another target:
39+
* const status = shopify.storage.latest.syncStatus.value; // 'complete'
40+
* const unsubscribe = shopify.storage.latest.syncStatus.subscribe((value) => {
41+
* // Reacts when another target updates this key
3742
* });
3843
* ```
3944
*/
40-
keys?: StorageKeys<BaseStorageTypes>;
45+
latest: StorageKeys<BaseStorageTypes>;
4146

4247
/**
4348
* Stores a value under the specified key, overwriting any existing value. Values must be JSON-serializable and return `StorageError` when storage limits are exceeded. Commonly used for storing user preferences, caching API responses, or passing contextual data from tiles to modals.
@@ -103,9 +108,8 @@ export interface Storage<
103108
/**
104109
* Provides reactive, subscribable access to individual storage keys.
105110
*
106-
* Each property is a `ReadonlySignalLike` that reflects the current value of
107-
* the corresponding storage key. Values are `undefined` when the key does not
108-
* exist.
111+
* Each property is a `ReadonlySignalLike` that reflects the current value of the
112+
* corresponding storage key. Values are `undefined` when the key does not exist.
109113
*
110114
* Mutations are performed through the existing `Storage` methods (`set`,
111115
* `delete`, `clear`, etc.) — `StorageKeys` is read-only and reactive.

0 commit comments

Comments
 (0)