SHARE
TWEET

Untitled

a guest Sep 15th, 2019 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Available variables:
  2.   // - Machine
  3.   // - interpret
  4.   // - assign
  5.   // - send
  6.   // - sendParent
  7.   // - spawn
  8.   // - raise
  9.   // - actions
  10.   // - XState (all XState exports)
  11.  
  12.  const probeConfig = {
  13.   id: 'probe',
  14.   src: 'probeService',
  15.   onDone: [
  16.     { target: '#acs.authorized', cond: 'isAuthorized' },
  17.     { target: '#acs.unauthorized' },
  18.   ],
  19.   onError: { target: '#acs.error', actions: ['setError'] },
  20. };
  21.  
  22. const tokenConfig = Object.assign({}, probeConfig, { src: 'tokenService' });
  23.  
  24. const config = {
  25.   id: 'acs',
  26.   initial: 'unknown',
  27.   meta: {
  28.     version: '0.0.0',
  29.   },
  30.   context: {
  31.     error: null,
  32.   },
  33.   on: {
  34.     login: [
  35.       { target: 'exit', in: 'unknown' },
  36.       { target: 'exit', in: 'unauthorized' },
  37.       { target: 'exit', in: 'error' },
  38.     ],
  39.     logout: [{ target: 'exit', in: 'authorized' }],
  40.   },
  41.   states: {
  42.     unknown: {
  43.       initial: 'pending',
  44.       states: {
  45.         pending: {
  46.           invoke: probeConfig,
  47.         },
  48.       },
  49.     },
  50.     authorized: {
  51.       initial: 'idle',
  52.       states: {
  53.         idle: {
  54.           on: { probe: 'pending' },
  55.         },
  56.         pending: {
  57.           invoke: tokenConfig,
  58.         },
  59.       },
  60.     },
  61.     unauthorized: {
  62.       initial: 'idle',
  63.       states: {
  64.         idle: {
  65.           on: { probe: 'pending' },
  66.         },
  67.         pending: {
  68.           invoke: tokenConfig,
  69.         },
  70.       },
  71.     },
  72.     exit: {
  73.       type: 'final',
  74.       entry: ['redirect'],
  75.     },
  76.     error: {
  77.       entry: ['setError'],
  78.       on: {
  79.         probe: 'unknown.pending',
  80.       },
  81.     },
  82.   },
  83. };
  84.  
  85. const auth = Machine(config);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top