recoil#ReadWriteSelectorFamilyOptions TypeScript Examples

The following examples show how to use recoil#ReadWriteSelectorFamilyOptions. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: api.ts    From Chromogen with MIT License 6 votes vote down vote up
// Overload function signature
export function selectorFamily<T>(
  config:
    | ReadWriteSelectorFamilyOptions<T, SerializableParam>
    | ReadOnlySelectorFamilyOptions<T, SerializableParam>,
) {
  const { key, get } = config;
  const { transactions, selectorFamilies } = ledger;

  // Testing whether returned function from configGet is async
  if (
    !get
    || transactions.length > 0
    || get(dummyParam).constructor.name === 'AsyncFunction'
    || get(dummyParam)
      .toString()
      .match(/^\s*return\s*_.*\.apply\(this, arguments\);$/m)
  ) {
    return recoilSelectorFamily(config);
  }

  const getter = wrapFamilyGetter(key, get);

  const newConfig: SelectorFamilyConfig<any, SerializableParam> = { key, get: getter };

  let isSettable = false;

  if ('set' in config) {
    isSettable = true;
    const setter = wrapFamilySetter(key, config.set);
    newConfig.set = setter;
  }

  // Create selector generator & add to selectorFamily for test setup
  const trackedSelectorFamily = recoilSelectorFamily(newConfig);
  selectorFamilies[key] = { trackedSelectorFamily, prevParams: new Set(), isSettable };
  return trackedSelectorFamily;
}