import { PureComponent } from 'react'; import { Automat } from '../../lib/index.js'; /** * UnconnectedPartialSubscriber * * Demonstrates: * 1. UNCONNECTED OBJECT: Absolutely NO import of `cartAutomat.js`! * The component has no module-level dependency on the cart store. * Instead, it discovers and binds to it strictly via the named window registry: * `Automat.get('cart')` (or `window.__AUTOMATS__.get('cart')`). * * 2. PARTIAL SUBSCRIBER: * Subscribes ONLY to a slice: `couponCode` via array selector `['couponCode']`. * - Changes to items, prices, or quantities produce 0 re-renders. * - Changes to couponCode trigger immediate updates. * * 3. UNCONNECTED ACTION DISPATCH: * Calls actions directly on the resolved instance: * `Automat.get('cart')?.actions.setCouponCode(...)` without importing anything. */ export default class UnconnectedPartialSubscriber extends PureComponent { _renderCount = 0; constructor(props) { super(props); // 1. Resolve store solely via named window object (no imports of cartAutomat!) const store = typeof window !== 'undefined' ? (Automat.get('cart') || window.__AUTOMATS__?.get('cart')) : null; this.state = { // Extract only partial slice: couponCode couponCode: store?.state?.couponCode || '', connected: Boolean(store), }; } componentDidMount() { // 2. Discover store from named window object const store = Automat.get('cart') || (typeof window !== 'undefined' && window.__AUTOMATS__?.get('cart')); if (store) { // 3. Partial subscription using key array selector: ['couponCode'] // Automat performs shallow-equality on { couponCode } and skips setState // when items, quantities, or prices change! this.unsubscribe = store.subscribe(this, ['couponCode']); this.setState({ connected: true, couponCode: store.state?.couponCode || '' }); } } componentWillUnmount() { if (this.unsubscribe) { this.unsubscribe(); } } handleQuickCoupon = (code) => { // Dispatch action through unconnected named window reference const store = Automat.get('cart') || window.__AUTOMATS__?.get('cart'); store?.actions.setCouponCode(code); }; render() { this._renderCount++; const { couponCode, connected } = this.state; return (
🔌

Unconnected Partial Subscriber

Zero Store Imports
{/* Status & Efficiency stats */}
Component Renders: #{this._renderCount} {connected ? 'window.__AUTOMATS__.get("cart")' : 'Disconnected'}
{/* Connection Architecture Badge */}
🌐 Unconnected Architecture:
• No Store Import: Does not import cartAutomat. Resolves solely via Automat.get('cart').
• Partial Selector: Subscribed strictly via store.subscribe(this, ['couponCode']).
• Render Guard: Adding/deleting items or changing quantities triggers 0 re-renders here.
{/* Observed Partial Value */}
Observed Partial Slice (couponCode):
{couponCode ? `Active Coupon: "${couponCode}"` : '(No coupon applied)'} {couponCode && ( Applied )}
{/* Action Dispatch via Unconnected Reference */}
Dispatch Action via Named Window Reference:
); } }