about summary refs log tree commit diff
path: root/src/state/env.ts
blob: f47a7037fab73d3972c153e2053948bc8d7fe3e7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
 * The environment is a place where services and shared dependencies between
 * models live. They are made available to every model via dependency injection.
 */

import {getEnv, IStateTreeNode} from 'mobx-state-tree'
import * as auth from '@adxp/auth'
import {API} from '../api'

export class Environment {
  api = new API()
  authStore?: auth.BrowserStore

  constructor() {}

  async setup() {
    this.authStore = await auth.BrowserStore.load()
  }
}

/**
 * Extension to the MST models that adds the environment property.
 * Usage:
 *
 *   .extend(withEnvironment)
 *
 */
export const withEnvironment = (self: IStateTreeNode) => ({
  views: {
    get environment() {
      return getEnv<Environment>(self)
    },
  },
})