Repository: facebook/react
File: packages/react-client/src/ReactFlightReplyClient.js (JavaScript)

Code before:
      encodeFormAction === undefined
        ? defaultEncodeFormAction
        : function (
            this: any => Promise<any>,
            identifierPrefix: string,
          ): ReactCustomFormAction {
            return customEncodeFormAction(
              this,
              identifierPrefix,
              encodeFormAction,
            );
          };
    Object.defineProperties((reference: any), {
      $$FORM_ACTION: {value: $$FORM_ACTION},
      $$IS_SIGNATURE_EQUAL: {value: isSignatureEqual},
    });
    defineBind(reference);
  }
  knownServerReferences.set(reference, {id, bound});
}

// TODO: Ideally we'd use `isServerReference` from
// 'react-server/src/ReactFlightServerConfig', but that can only be imported
// in a react-server environment.
function isServerReference(reference: Object): boolean {
  return reference.$$typeof === Symbol.for('react.server.reference');
}

export function registerServerReference<T: Function>(
  reference: T,
  id: ServerReferenceId,
  encodeFormAction?: EncodeFormActionCallback,
): ServerReference<T> {
  const bound =
    isServerReference(reference) && reference.$$bound
      ? Promise.resolve(reference.$$bound)
      : null;

  registerBoundServerReference(reference, id, bound, encodeFormAction);
  return reference;
}

// $FlowFixMe[method-unbinding]
const FunctionBind = Function.prototype.bind;
// $FlowFixMe[method-unbinding]
const ArraySlice = Array.prototype.slice;

function defineBind<T: Function>(reference: T) {
  // TODO: Instead of checking if it's a server reference, we could also use
  // `reference.hasOwnProperty('bind')`.
  const originalBind = isServerReference(reference)

Review comment (suggestion):
This isn't good enough to support environments that have custom server reference first class types (like Meta does and all bundlers ideally should have).

But I doubt brand checking is actually the right strategy anyway.


Code after:
  }

  knownServerReferences.set(reference, {
    id,
    originalBind: reference.bind,
    bound,
  });

  // Expose encoder for use by SSR, as well as a special bind that can be used to
  // keep server capabilities.
  if (usedWithSSR) {
    // Only expose this in builds that would actually use it. Not needed in the browser.
    const $$FORM_ACTION =
      encodeFormAction === undefined
        ? defaultEncodeFormAction
        : function (
            this: any => Promise<any>,
            identifierPrefix: string,
          ): ReactCustomFormAction {
            return customEncodeFormAction(
              this,
              identifierPrefix,
              encodeFormAction,
            );
          };
    Object.defineProperties((reference: any), {
      $$FORM_ACTION: {value: $$FORM_ACTION},
      $$IS_SIGNATURE_EQUAL: {value: isSignatureEqual},
      bind: {value: bind},
    });
  }
}

export function registerServerReference<T: Function>(
  reference: T,
  id: ServerReferenceId,
  encodeFormAction?: EncodeFormActionCallback,
): ServerReference<T> {
  registerBoundServerReference(reference, id, null, encodeFormAction);
  return reference;
}

// $FlowFixMe[method-unbinding]
const FunctionBind = Function.prototype.bind;
// $FlowFixMe[method-unbinding]
const ArraySlice = Array.prototype.slice;
function bind(this: Function): Function {
  const referenceClosure = knownServerReferences.get(this);

  if (!referenceClosure) {
    // $FlowFixMe[prop-missing]