recoil#ReadOnlySelectorOptions TypeScript Examples

The following examples show how to use recoil#ReadOnlySelectorOptions. 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 selector(config: ReadWriteSelectorOptions<any> | ReadOnlySelectorOptions<any>) {
  const { key, get } = config;
  const { transactions, selectors, setters } = ledger;
  if (
    transactions.length > 0
    || !get
    || get.constructor.name === 'AsyncFunction'
    || get.toString().match(/^\s*return\s*_.*\.apply\(this, arguments\);$/m)
  ) {
    return recoilSelector(config);
  }

  // Wrap get method with tracking logic & update config
  const getter = wrapGetter(key, get);
  const newConfig: SelectorConfig<any> = { key, get: getter };

  // Add setter to newConfig only if set method is defined
  if ('set' in config) {
    const setter = wrapSetter(key, config.set);
    newConfig.set = setter;
    setters.push(key);
  }

  // Create selector & add to ledger
  const trackedSelector = recoilSelector(newConfig);
  selectors.push(trackedSelector.key);
  return trackedSelector;
}