File tree 5 files changed +97
-0
lines changed
5 files changed +97
-0
lines changed Original file line number Diff line number Diff line change
1
+ import PropTypes from 'prop-types' ;
2
+ import { createActionsFromDefinitions } from '../../src/actions/lib/actions' ;
3
+
4
+ export const exampleFeatureActions = createActionsFromDefinitions ( {
5
+ greet : {
6
+ name : PropTypes . string . isRequired ,
7
+ } ,
8
+ } , PropTypes . checkPropTypes ) ;
9
+
10
+ export default exampleFeatureActions ;
Original file line number Diff line number Diff line change
1
+ export default {
2
+ async getName ( ) {
3
+ return Promise . resolve ( 'Franz' ) ;
4
+ } ,
5
+ } ;
Original file line number Diff line number Diff line change
1
+ import { reaction , runInAction } from 'mobx' ;
2
+ import { ExampleFeatureStore } from './store' ;
3
+ import state , { resetState } from './state' ;
4
+ import api from './api' ;
5
+
6
+ const debug = require ( 'debug' ) ( 'Franz:feature:EXAMPLE_FEATURE' ) ;
7
+
8
+ let store = null ;
9
+
10
+ export default function initAnnouncements ( stores , actions ) {
11
+ const { features } = stores ;
12
+
13
+ // Toggle workspace feature
14
+ reaction (
15
+ ( ) => (
16
+ features . features . isExampleFeatureEnabled
17
+ ) ,
18
+ ( isEnabled ) => {
19
+ if ( isEnabled ) {
20
+ debug ( 'Initializing `EXAMPLE_FEATURE` feature' ) ;
21
+ store = new ExampleFeatureStore ( stores , api , actions , state ) ;
22
+ store . initialize ( ) ;
23
+ runInAction ( ( ) => { state . isFeatureActive = true ; } ) ;
24
+ } else if ( store ) {
25
+ debug ( 'Disabling `EXAMPLE_FEATURE` feature' ) ;
26
+ runInAction ( ( ) => { state . isFeatureActive = false ; } ) ;
27
+ store . teardown ( ) ;
28
+ store = null ;
29
+ resetState ( ) ; // Reset state to default
30
+ }
31
+ } ,
32
+ {
33
+ fireImmediately : true ,
34
+ } ,
35
+ ) ;
36
+ }
Original file line number Diff line number Diff line change
1
+ import { observable } from 'mobx' ;
2
+
3
+ const defaultState = {
4
+ name : null ,
5
+ isFeatureActive : false ,
6
+ } ;
7
+
8
+ export const exampleFeatureState = observable ( defaultState ) ;
9
+
10
+ export function resetState ( ) {
11
+ Object . assign ( exampleFeatureState , defaultState ) ;
12
+ }
13
+
14
+ export default exampleFeatureState ;
Original file line number Diff line number Diff line change
1
+ import { action , observable , reaction } from 'mobx' ;
2
+ import Store from '../../src/stores/lib/Store' ;
3
+ import Request from '../../src/stores/lib/Request' ;
4
+
5
+ const debug = require ( 'debug' ) ( 'Franz:feature:EXAMPLE_FEATURE:store' ) ;
6
+
7
+ export class ExampleFeatureStore extends Store {
8
+ @observable getNameRequest = new Request ( this . api , 'getName' ) ;
9
+
10
+ constructor ( stores , api , actions , state ) {
11
+ super ( stores , api , actions ) ;
12
+ this . state = state ;
13
+ }
14
+
15
+ setup ( ) {
16
+ debug ( 'fetching name from api' ) ;
17
+ this . getNameRequest . execute ( ) ;
18
+
19
+ // Update the name on the state when the request resolved
20
+ reaction (
21
+ ( ) => this . getNameRequest . result ,
22
+ name => this . _setName ( name ) ,
23
+ ) ;
24
+ }
25
+
26
+ @action _setName = ( name ) => {
27
+ debug ( 'setting name' , name ) ;
28
+ this . state . name = name ;
29
+ } ;
30
+ }
31
+
32
+ export default ExampleFeatureStore ;
You can’t perform that action at this time.
0 commit comments