# profile-builder/4.0.3/form-builder/blocks/src/lib/writeBackHandler.js

User Profile Builder – Beautiful User Registration Forms, User Profiles &amp; User Role Editor, version 4.0.3. 39 lines.

- Page: https://pluginprobe.com/plugins/profile-builder/4.0.3/code/form-builder/blocks/src/lib/writeBackHandler.js
- Raw: https://pluginprobe.com/plugins/profile-builder/4.0.3/raw/form-builder/blocks/src/lib/writeBackHandler.js
- Modified: 2026-08-17T08:51:38+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/profile-builder/4.0.3/code/form-builder/blocks/src/lib/writeBackHandler.js#L10-L20`.

```javascript
/**
 * Shared write-back: apply sanitized server values only if the user has not typed past them.
 */

/**
 * @param {Object}   opts
 * @param {Function} opts.getBlock
 * @param {Function} opts.updateBlockAttributes
 * @param {Function} [opts.markNonPersistent]
 * @returns {Function} `(clientId) => (sanitizedDiff, sentPatch) => void`
 */
export function makeWriteBackHandler( { getBlock, updateBlockAttributes, markNonPersistent } ) {
    return ( clientId ) => ( sanitizedDiff, sentPatch ) => {
        const current = getBlock( clientId );
        if ( ! current || ! current.attributes ) return;

        const safeUpdates = {};
        for ( const k of Object.keys( sanitizedDiff ) ) {
            // Apply only if the block's value still equals what we sent —
            // protects in-progress typing.
            // When sentPatch doesn't include the key (server volunteered it,
            // e.g. auto-gen), we require the block's current value to still
            // be empty so we never clobber explicit input.
            if ( sentPatch[ k ] !== undefined ) {
                if ( current.attributes[ k ] === sentPatch[ k ] ) {
                    safeUpdates[ k ] = sanitizedDiff[ k ];
                }
            } else if ( ! current.attributes[ k ] ) {
                safeUpdates[ k ] = sanitizedDiff[ k ];
            }
        }

        if ( Object.keys( safeUpdates ).length === 0 ) return;

        if ( typeof markNonPersistent === 'function' ) markNonPersistent();
        updateBlockAttributes( clientId, safeUpdates );
    };
}

```
