| 1 |
/** |
| 2 |
* Live snapshot of global `wppb_manage_fields` for Existing Fields + Conditional Logic. |
| 3 |
* Seeded from the bridge; refreshed by post-save GET and per-edit PUT sync. |
| 4 |
*/ |
| 5 |
import { useSyncExternalStore } from '@wordpress/element'; |
| 6 |
|
| 7 |
import { createPubsubStore } from './createPubsubStore'; |
| 8 |
|
| 9 |
const store = createPubsubStore( |
| 10 |
( window.wppbFb && window.wppbFb.existingFields && window.wppbFb.existingFields.fields ) || [], |
| 11 |
); |
| 12 |
|
| 13 |
/** Replace the snapshot (post-save refetch, per-edit PUT reconcile, delete). */ |
| 14 |
export const setExistingFieldsSnapshot = ( next ) => store.set( next ); |
| 15 |
|
| 16 |
/** Read imperatively (store-mutating callbacks outside React). */ |
| 17 |
export const getExistingFieldsSnapshot = () => store.get(); |
| 18 |
|
| 19 |
/** Read reactively (React components). */ |
| 20 |
export const useExistingFieldsSnapshot = () => |
| 21 |
useSyncExternalStore( store.subscribe, store.get ); |
| 22 |
|