function readAtomItems()

in packages/recoil-sync/RecoilSync.js [197:238]


function readAtomItems<T>(
  effectRegistration: EffectRegistration<T>,
  readFromStorage?: ReadItem,
  diff?: ItemDiff,
): ?Loadable<T | DefaultValue> {
  const {options} = effectRegistration;
  const readFromStorageRequired =
    readFromStorage ??
    (itemKey =>
      RecoilLoadable.error(
        `Read functionality not provided for ${
          options.storeKey != null ? `"${options.storeKey}" ` : ''
        }store in useRecoilSync() hook while updating item "${itemKey}".`,
      ));

  effectRegistration.subscribedItemKeys = new Set();
  const read: ReadItem = itemKey => {
    effectRegistration.subscribedItemKeys.add(itemKey);
    const value = diff?.has(itemKey)
      ? diff?.get(itemKey)
      : readFromStorageRequired(itemKey);

    if (RecoilLoadable.isLoadable(value)) {
      // $FlowIssue[incompatible-type]
      const loadable: Loadable<mixed> = value;
      if (loadable.state === 'hasError') {
        throw loadable.contents;
      }
    }
    return value;
  };

  let value;
  try {
    value = options.read({read});
  } catch (error) {
    return RecoilLoadable.error(error);
  }
  return value instanceof DefaultValue
    ? null
    : validateLoadable(value, options);
}