How to use @dbeining/react-atom - 10 common examples

To help you get started, we’ve selected a few @dbeining/react-atom examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github njordhov / react-blockstack / src / index.js View on Github external
function getAppManifestAtom (appUri) {
  // Out: Atom promise containing a either an app manifest or a null value.
  // Avoid passing outside this module to avoid conflicts if there are multiple react-atom packages in the project
  const atom = Atom.of(null)
  const setValue = (value) => swap(atom, state => value)
  try {
      const manifestUri = appUri + "/manifest.json"
      const controller = new AbortController()
      const cleanup = () => controller.abort()
      console.info("FETCHING:", manifestUri)
      fetch(manifestUri, {signal: controller.signal})
      .then ( response => {response.json().then( setValue )})
      .catch ( err => {console.warn("Failed to get manifest for:", appUri, err)})
      // .finally (() => setValue({}))
    } catch (err) {
      console.warn("Failed fetching when mounting:", err)
      setValue({error: err})
    }
  return (atom)
}
github njordhov / react-blockstack / dist / index.js View on Github external
function getAppManifestAtom(appUri) {
  // Out: Atom promise containing a either an app manifest or a null value.
  // Avoid passing outside this module to avoid conflicts if there are multiple react-atom packages in the project
  var atom = _reactAtom.Atom.of(null);

  var setValue = function setValue(value) {
    return (0, _reactAtom.swap)(atom, function (state) {
      return value;
    });
  };

  try {
    var manifestUri = appUri + "/manifest.json";
    var controller = new AbortController();

    var cleanup = function cleanup() {
      return controller.abort();
    };

    console.info("FETCHING:", manifestUri);
github smapiot / piral / src / packages / piral-core / src / state / createGlobalState.ts View on Github external
LoadingIndicator: DefaultLoadingIndicator,
      Router: BrowserRouter,
      Layout: DefaultLayout,
    },
    errorComponents: {},
    registry: {
      extensions: {},
      pages: {},
    },
    routes: {},
    data: {},
    portals: {},
    modules: [],
  };

  const globalState = Atom.of(extend(defaultState, customState));

  if (process.env.NODE_ENV === 'development' || process.env.DEBUG_PILET !== undefined) {
    addChangeHandler(globalState, 'debugging', ({ current, previous }) => {
      const action = new Error().stack.split('\n')[6].replace(/^\s+at\s+Atom\./, '');
      console.group(
        `%c Piral State Change %c ${new Date().toLocaleTimeString()}`,
        'color: gray; font-weight: lighter;',
        'color: black; font-weight: bold;',
      );
      console.log('%c Previous', `color: #9E9E9E; font-weight: bold`, previous);
      console.log('%c Action', `color: #03A9F4; font-weight: bold`, action);
      console.log('%c Next', `color: #4CAF50; font-weight: bold`, current);
      console.groupEnd();
    });
  }
github smapiot / piral / src / packages / piral-core / src / state / createGlobalState.ts View on Github external
},
    errorComponents: {},
    registry: {
      extensions: {},
      pages: {},
    },
    routes: {},
    data: {},
    portals: {},
    modules: [],
  };

  const globalState = Atom.of(extend(defaultState, customState));

  if (process.env.NODE_ENV === 'development' || process.env.DEBUG_PILET !== undefined) {
    addChangeHandler(globalState, 'debugging', ({ current, previous }) => {
      const action = new Error().stack.split('\n')[6].replace(/^\s+at\s+Atom\./, '');
      console.group(
        `%c Piral State Change %c ${new Date().toLocaleTimeString()}`,
        'color: gray; font-weight: lighter;',
        'color: black; font-weight: bold;',
      );
      console.log('%c Previous', `color: #9E9E9E; font-weight: bold`, previous);
      console.log('%c Action', `color: #03A9F4; font-weight: bold`, action);
      console.log('%c Next', `color: #4CAF50; font-weight: bold`, current);
      console.groupEnd();
    });
  }

  return globalState;
}
github smapiot / piral / packages / piral-core / src / state / createGlobalState.ts View on Github external
feeds: {},
    forms: {},
    user: {
      current: undefined,
      features: {},
      permissions: {},
    },
    search: {
      input: '',
      loading: false,
      results: [],
    },
  });

  if (process.env.NODE_ENV !== 'production') {
    addChangeHandler(globalState, 'debugging', ({ current, previous }) => {
      const action = new Error().stack.split('\n')[6].replace(/^\s+at\s+Atom\./, '');
      console.group(
        `%c Piral State Change %c ${new Date().toLocaleTimeString()}`,
        'color: gray; font-weight: lighter;',
        'color: black; font-weight: bold;',
      );
      console.log('%c Previous', `color: #9E9E9E; font-weight: bold`, previous);
      console.log('%c Action', `color: #03A9F4; font-weight: bold`, action);
      console.log('%c Next', `color: #4CAF50; font-weight: bold`, current);
      console.groupEnd();
    });
  }

  return globalState;
}
github smapiot / piral / src / packages / piral-core / src / state / actions / data.ts View on Github external
key: string,
  value: any,
  owner: string,
  target: DataStoreTarget,
  expires: number,
) {
  const isNull = !value && typeof value === 'object';
  const data = isNull
    ? value
    : {
        value,
        owner,
        target,
        expires,
      };
  swap(ctx, state => ({
    ...state,
    data: updateKey(state.data, key, data),
  }));

  this.emit('store-data', {
    name,
    target,
    value,
    owner,
    expires,
  });
}
github njordhov / react-blockstack / dist / index.js View on Github external
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }

function _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

var defaultValue = {
  userData: null,
  signIn: null,
  signOut: null
};

var contextAtom = _reactAtom.Atom.of(defaultValue);
/**
 * React hook for the Blockstack SDK
 *
 * @return {{userSession: UserSession, userData: ?UserData, signIn: ?function, signOut: ?function, person: ?Person}} Blockstack SDK context
 *
 * @example
 *
 *     useBlockstack()
 */


function useBlockstack() {
  return (0, _reactAtom.useAtom)(contextAtom);
}

function setContext(update) {
github smapiot / piral / src / packages / piral-modals / src / actions.ts View on Github external
export function closeModal(ctx: Atom, dialog: OpenModalDialog) {
  swap(ctx, state => ({
    ...state,
    modals: excludeItem(state.modals, dialog),
  }));
}
github smapiot / piral / src / packages / piral-translate / src / create.ts View on Github external
return context => {
    context.defineActions(createActions(localizer));

    swap(context.state, state => ({
      ...state,
      components: {
        ...state.components,
        LanguagesPicker: DefaultPicker,
      },
      language: {
        loading: false,
        available: localizer.languages,
        selected: localizer.language,
      },
    }));

    return () => {
      let localTranslations: LocalizationMessages = {};

      return {
github smapiot / piral / src / packages / piral-core / src / actions / app.ts View on Github external
export function injectPilet(ctx: Atom, pilet: Pilet) {
  swap(ctx, state => ({
    ...state,
    modules: replaceOrAddItem(state.modules, pilet, m => m.name === pilet.name),
    registry: removeNested(state.registry, m => m.pilet === pilet.name),
  }));
}

@dbeining/react-atom

State management made simple for React. Built on React Hooks. Inspired by `atom`s in `reagent.cljs`.

MIT
Latest version published 3 years ago

Package Health Score

48 / 100
Full package analysis