Browse Source

remove privateEvents, rename appLaunched, rename componentId to id

Daniel Zlotin 7 years ago
parent
commit
9fd8cf91ee

+ 3
- 6
lib/src/Navigation.ts View File

6
 import { Commands } from './commands/Commands';
6
 import { Commands } from './commands/Commands';
7
 import { LayoutTreeParser } from './commands/LayoutTreeParser';
7
 import { LayoutTreeParser } from './commands/LayoutTreeParser';
8
 import { LayoutTreeCrawler } from './commands/LayoutTreeCrawler';
8
 import { LayoutTreeCrawler } from './commands/LayoutTreeCrawler';
9
-import { PublicEventsRegistry } from './events/PublicEventsRegistry';
9
+import { EventsRegistry } from './events/EventsRegistry';
10
 import { ComponentProvider } from 'react-native';
10
 import { ComponentProvider } from 'react-native';
11
 import { Element } from './adapters/Element';
11
 import { Element } from './adapters/Element';
12
-import { PrivateEventsListener } from './events/PrivateEventsListener';
13
 
12
 
14
 export class Navigation {
13
 export class Navigation {
15
   public readonly Element;
14
   public readonly Element;
35
     this.layoutTreeCrawler = new LayoutTreeCrawler(this.uniqueIdProvider, this.store);
34
     this.layoutTreeCrawler = new LayoutTreeCrawler(this.uniqueIdProvider, this.store);
36
     this.nativeCommandsSender = new NativeCommandsSender();
35
     this.nativeCommandsSender = new NativeCommandsSender();
37
     this.commands = new Commands(this.nativeCommandsSender, this.layoutTreeParser, this.layoutTreeCrawler);
36
     this.commands = new Commands(this.nativeCommandsSender, this.layoutTreeParser, this.layoutTreeCrawler);
38
-    this.publicEventsRegistry = new PublicEventsRegistry(this.nativeEventsReceiver);
39
-
40
-    new PrivateEventsListener(this.nativeEventsReceiver, this.store).listenAndHandlePrivateEvents();
37
+    this.publicEventsRegistry = new EventsRegistry(this.nativeEventsReceiver);
41
   }
38
   }
42
 
39
 
43
   /**
40
   /**
135
   /**
132
   /**
136
    * Obtain the events registry instance
133
    * Obtain the events registry instance
137
    */
134
    */
138
-  public events(): PublicEventsRegistry {
135
+  public events(): EventsRegistry {
139
     return this.publicEventsRegistry;
136
     return this.publicEventsRegistry;
140
   }
137
   }
141
 }
138
 }

+ 6
- 9
lib/src/adapters/NativeCommandsSender.ts View File

2
 
2
 
3
 export class NativeCommandsSender {
3
 export class NativeCommandsSender {
4
   private nativeCommandsModule;
4
   private nativeCommandsModule;
5
-
6
   constructor() {
5
   constructor() {
7
     this.nativeCommandsModule = NativeModules.RNNBridgeModule;
6
     this.nativeCommandsModule = NativeModules.RNNBridgeModule;
8
   }
7
   }
12
   }
11
   }
13
 
12
 
14
   setDefaultOptions(options: object) {
13
   setDefaultOptions(options: object) {
15
-    this.nativeCommandsModule.setDefaultOptions(options);
14
+    return this.nativeCommandsModule.setDefaultOptions(options);
16
   }
15
   }
17
 
16
 
18
   setOptions(componentId: string, options: object) {
17
   setOptions(componentId: string, options: object) {
19
-    this.nativeCommandsModule.setOptions(componentId, options);
18
+    return this.nativeCommandsModule.setOptions(componentId, options);
20
   }
19
   }
21
 
20
 
22
-  async push(onComponentId: string, layout: object) {
23
-    const pushedComponentId = await this.nativeCommandsModule.push(onComponentId, layout);
24
-    return pushedComponentId;
21
+  push(onComponentId: string, layout: object) {
22
+    return this.nativeCommandsModule.push(onComponentId, layout);
25
   }
23
   }
26
 
24
 
27
   pop(componentId: string, options: object) {
25
   pop(componentId: string, options: object) {
36
     return this.nativeCommandsModule.popToRoot(componentId);
34
     return this.nativeCommandsModule.popToRoot(componentId);
37
   }
35
   }
38
 
36
 
39
-  async showModal(layout: object) {
40
-    const completed = await this.nativeCommandsModule.showModal(layout);
41
-    return completed;
37
+  showModal(layout: object) {
38
+    return this.nativeCommandsModule.showModal(layout);
42
   }
39
   }
43
 
40
 
44
   dismissModal(componentId: string) {
41
   dismissModal(componentId: string) {

+ 10
- 11
lib/src/adapters/NativeEventsReceiver.ts View File

1
-import { NativeModules, NativeEventEmitter } from 'react-native';
1
+import { NativeModules, NativeEventEmitter, EmitterSubscription } from 'react-native';
2
 
2
 
3
 export class NativeEventsReceiver {
3
 export class NativeEventsReceiver {
4
-  private emitter;
5
-
4
+  private emitter: NativeEventEmitter;
6
   constructor() {
5
   constructor() {
7
     this.emitter = new NativeEventEmitter(NativeModules.RNNEventEmitter);
6
     this.emitter = new NativeEventEmitter(NativeModules.RNNEventEmitter);
8
   }
7
   }
9
 
8
 
10
-  registerComponentDidAppear(callback) {
11
-    this.emitter.addListener('RNN.componentDidAppear', callback);
9
+  registerAppLaunched(callback): EmitterSubscription {
10
+    return this.emitter.addListener('RNN.appLaunched', callback);
12
   }
11
   }
13
 
12
 
14
-  registerComponentDidDisappear(callback) {
15
-    this.emitter.addListener('RNN.componentDidDisappear', callback);
13
+  registerComponentDidAppear(callback): EmitterSubscription {
14
+    return this.emitter.addListener('RNN.componentDidAppear', callback);
16
   }
15
   }
17
 
16
 
18
-  registerAppLaunched(callback) {
19
-    this.emitter.addListener('RNN.appLaunched', callback);
17
+  registerComponentDidDisappear(callback): EmitterSubscription {
18
+    return this.emitter.addListener('RNN.componentDidDisappear', callback);
20
   }
19
   }
21
 
20
 
22
-  registerNavigationButtonPressed(callback) {
23
-    this.emitter.addListener('RNN.navigationButtonPressed', callback);
21
+  registerInteraction(callback): EmitterSubscription {
22
+    return this.emitter.addListener('RNN.interaction', callback);
24
   }
23
   }
25
 }
24
 }

+ 1
- 1
lib/src/commands/OptionsProcessor.ts View File

23
   static processArrayOptions(key, array, store) {
23
   static processArrayOptions(key, array, store) {
24
     _.forEach(array, (value) => {
24
     _.forEach(array, (value) => {
25
       if (_.endsWith(key, 'Buttons') && value.passProps) {
25
       if (_.endsWith(key, 'Buttons') && value.passProps) {
26
-        store.setPropsForComponentId(value.id, value.passProps);
26
+        store.setPropsForId(value.id, value.passProps);
27
       }
27
       }
28
     });
28
     });
29
   }
29
   }

+ 3
- 3
lib/src/components/ComponentWrapper.tsx View File

19
         this._assertComponentId(props);
19
         this._assertComponentId(props);
20
         this.state = {
20
         this.state = {
21
           componentId: props.componentId,
21
           componentId: props.componentId,
22
-          allProps: _.merge({}, props, store.getPropsForComponentId(props.componentId))
22
+          allProps: _.merge({}, props, store.getPropsForId(props.componentId))
23
         };
23
         };
24
       }
24
       }
25
 
25
 
34
       }
34
       }
35
 
35
 
36
       componentWillMount() {
36
       componentWillMount() {
37
-        store.setRefForComponentId(this.state.componentId, this);
37
+        store.setRefForId(this.state.componentId, this);
38
       }
38
       }
39
 
39
 
40
       componentWillUnmount() {
40
       componentWillUnmount() {
61
 
61
 
62
       componentWillReceiveProps(nextProps) {
62
       componentWillReceiveProps(nextProps) {
63
         this.setState({
63
         this.setState({
64
-          allProps: _.merge({}, nextProps, store.getPropsForComponentId(this.state.componentId))
64
+          allProps: _.merge({}, nextProps, store.getPropsForId(this.state.componentId))
65
         });
65
         });
66
       }
66
       }
67
 
67
 

+ 0
- 80
lib/src/components/Lifecycle.test.ts View File

1
-import { Lifecycle } from './Lifecycle';
2
-import { Store } from '../components/Store';
3
-
4
-describe('Lifecycle', () => {
5
-  let store;
6
-  let uut;
7
-  let mockRef;
8
-
9
-  beforeEach(() => {
10
-    mockRef = {
11
-      didAppear: jest.fn(),
12
-      didDisappear: jest.fn(),
13
-      onNavigationButtonPressed: jest.fn()
14
-    };
15
-    store = new Store();
16
-    store.setRefForComponentId('myUniqueId', mockRef);
17
-
18
-    uut = new Lifecycle(store);
19
-  });
20
-
21
-  describe('componentDidAppear', () => {
22
-    it('calls didAppear on component ref from store', () => {
23
-      uut.componentDidAppear('myUniqueId');
24
-      expect(mockRef.didAppear).toHaveBeenCalledTimes(1);
25
-    });
26
-
27
-    it('skips undefined refs', () => {
28
-      uut.componentDidAppear('myUniqueId2');
29
-      expect(mockRef.didAppear).not.toHaveBeenCalled();
30
-    });
31
-
32
-    it('skips unimplemented didAppear', () => {
33
-      mockRef = {};
34
-      expect(mockRef.didAppear).toBeUndefined();
35
-      store.setRefForComponentId('myUniqueId', mockRef);
36
-      uut.componentDidAppear('myUniqueId');
37
-    });
38
-  });
39
-
40
-  describe('componentDidDisappear', () => {
41
-    it('calls didDisappear on component ref from store', () => {
42
-      uut.componentDidDisappear('myUniqueId');
43
-      expect(mockRef.didDisappear).toHaveBeenCalledTimes(1);
44
-    });
45
-
46
-    it('skips undefined refs', () => {
47
-      uut.componentDidDisappear('myUniqueId2');
48
-      expect(mockRef.didDisappear).not.toHaveBeenCalled();
49
-    });
50
-
51
-    it('skips unimplemented didDisappear', () => {
52
-      mockRef = {};
53
-      expect(mockRef.didDisappear).toBeUndefined();
54
-      store.setRefForComponentId('myUniqueId', mockRef);
55
-      uut.componentDidDisappear('myUniqueId');
56
-    });
57
-  });
58
-
59
-  describe('onNavigationButtonPressed', () => {
60
-    it('calls onNavigationButtonPressed on component ref from store', () => {
61
-      uut.onNavigationButtonPressed({
62
-        componentId: 'myUniqueId',
63
-        buttonId: 'myButtonId'
64
-      });
65
-      expect(mockRef.onNavigationButtonPressed).toHaveBeenCalledTimes(1);
66
-    });
67
-
68
-    it('skips undefined refs', () => {
69
-      uut.onNavigationButtonPressed('myButtonId');
70
-      expect(mockRef.didDisappear).not.toHaveBeenCalled();
71
-    });
72
-
73
-    it('skips unimplemented onNavigationButtonPressed', () => {
74
-      mockRef = {};
75
-      expect(mockRef.onNavigationButtonPressed).toBeUndefined();
76
-      store.setRefForComponentId('myUniqueId', mockRef);
77
-      uut.componentDidAppear('myUniqueId');
78
-    });
79
-  });
80
-});

+ 15
- 15
lib/src/components/Store.test.ts View File

8
   });
8
   });
9
 
9
 
10
   it('initial state', () => {
10
   it('initial state', () => {
11
-    expect(uut.getPropsForComponentId('component1')).toEqual({});
11
+    expect(uut.getPropsForId('component1')).toEqual({});
12
   });
12
   });
13
 
13
 
14
-  it('holds props by componentId', () => {
15
-    uut.setPropsForComponentId('component1', { a: 1, b: 2 });
16
-    expect(uut.getPropsForComponentId('component1')).toEqual({ a: 1, b: 2 });
14
+  it('holds props by id', () => {
15
+    uut.setPropsForId('component1', { a: 1, b: 2 });
16
+    expect(uut.getPropsForId('component1')).toEqual({ a: 1, b: 2 });
17
   });
17
   });
18
 
18
 
19
-  it('defensive for invalid componentId and props', () => {
20
-    uut.setPropsForComponentId('component1', undefined);
21
-    uut.setPropsForComponentId(undefined, undefined);
22
-    expect(uut.getPropsForComponentId('component1')).toEqual({});
19
+  it('defensive for invalid Id and props', () => {
20
+    uut.setPropsForId('component1', undefined);
21
+    uut.setPropsForId(undefined, undefined);
22
+    expect(uut.getPropsForId('component1')).toEqual({});
23
   });
23
   });
24
 
24
 
25
   it('holds original components classes by componentName', () => {
25
   it('holds original components classes by componentName', () => {
32
 
32
 
33
   it('holds component refs by id', () => {
33
   it('holds component refs by id', () => {
34
     const ref = {};
34
     const ref = {};
35
-    uut.setRefForComponentId('refUniqueId', ref);
36
-    expect(uut.getRefForComponentId('other')).toBeUndefined();
37
-    expect(uut.getRefForComponentId('refUniqueId')).toBe(ref);
35
+    uut.setRefForId('refUniqueId', ref);
36
+    expect(uut.getRefForId('other')).toBeUndefined();
37
+    expect(uut.getRefForId('refUniqueId')).toBe(ref);
38
   });
38
   });
39
 
39
 
40
   it('clean by id', () => {
40
   it('clean by id', () => {
41
-    uut.setRefForComponentId('refUniqueId', {});
42
-    uut.setPropsForComponentId('refUniqueId', { foo: 'bar' });
41
+    uut.setRefForId('refUniqueId', {});
42
+    uut.setPropsForId('refUniqueId', { foo: 'bar' });
43
 
43
 
44
     uut.cleanId('refUniqueId');
44
     uut.cleanId('refUniqueId');
45
 
45
 
46
-    expect(uut.getRefForComponentId('refUniqueId')).toBeUndefined();
47
-    expect(uut.getPropsForComponentId('refUniqueId')).toEqual({});
46
+    expect(uut.getRefForId('refUniqueId')).toBeUndefined();
47
+    expect(uut.getPropsForId('refUniqueId')).toEqual({});
48
   });
48
   });
49
 });
49
 });

+ 13
- 19
lib/src/components/Store.ts View File

1
 import * as _ from 'lodash';
1
 import * as _ from 'lodash';
2
 
2
 
3
 export class Store {
3
 export class Store {
4
-  private propsByComponentId: {};
5
-  private componentsByName: {};
6
-  private refsById: {};
7
-
8
-  constructor() {
9
-    this.propsByComponentId = {};
10
-    this.componentsByName = {};
11
-    this.refsById = {};
12
-  }
4
+  private componentsByName = {};
5
+  private propsById = {};
6
+  private refsById = {};
13
 
7
 
14
-  setPropsForComponentId(componentId, props) {
15
-    _.set(this.propsByComponentId, componentId, props);
8
+  setPropsForId(componentId: string, props) {
9
+    _.set(this.propsById, componentId, props);
16
   }
10
   }
17
 
11
 
18
-  getPropsForComponentId(componentId) {
19
-    return _.get(this.propsByComponentId, componentId, {});
12
+  getPropsForId(componentId: string) {
13
+    return _.get(this.propsById, componentId, {});
20
   }
14
   }
21
 
15
 
22
-  setOriginalComponentClassForName(componentName, ComponentClass) {
16
+  setOriginalComponentClassForName(componentName: string, ComponentClass) {
23
     _.set(this.componentsByName, componentName, ComponentClass);
17
     _.set(this.componentsByName, componentName, ComponentClass);
24
   }
18
   }
25
 
19
 
26
-  getOriginalComponentClassForName(componentName) {
20
+  getOriginalComponentClassForName(componentName: string) {
27
     return _.get(this.componentsByName, componentName);
21
     return _.get(this.componentsByName, componentName);
28
   }
22
   }
29
 
23
 
30
-  setRefForComponentId(id, ref) {
24
+  setRefForId(id: string, ref) {
31
     _.set(this.refsById, id, ref);
25
     _.set(this.refsById, id, ref);
32
   }
26
   }
33
 
27
 
34
-  getRefForComponentId(id) {
28
+  getRefForId(id: string) {
35
     return _.get(this.refsById, id);
29
     return _.get(this.refsById, id);
36
   }
30
   }
37
 
31
 
38
-  cleanId(id) {
32
+  cleanId(id: string) {
39
     _.unset(this.refsById, id);
33
     _.unset(this.refsById, id);
40
-    _.unset(this.propsByComponentId, id);
34
+    _.unset(this.propsById, id);
41
   }
35
   }
42
 }
36
 }

+ 22
- 0
lib/src/events/EventsRegistry.test.ts View File

1
+import { EventsRegistry } from './EventsRegistry';
2
+import { NativeEventsReceiver } from '../adapters/NativeEventsReceiver.mock';
3
+
4
+describe('EventsRegistry', () => {
5
+  let uut: EventsRegistry;
6
+  let mockNativeEventsReceiver;
7
+
8
+  beforeEach(() => {
9
+    mockNativeEventsReceiver = new NativeEventsReceiver();
10
+    uut = new EventsRegistry(mockNativeEventsReceiver);
11
+  });
12
+
13
+  it('exposes appLaunch event', () => {
14
+    const subscription = {};
15
+    mockNativeEventsReceiver.registerAppLaunched.mockReturnValueOnce(subscription);
16
+    const cb = jest.fn();
17
+    const result = uut.appLaunched(cb);
18
+    expect(mockNativeEventsReceiver.registerAppLaunched).toHaveBeenCalledTimes(1);
19
+    expect(mockNativeEventsReceiver.registerAppLaunched).toHaveBeenCalledWith(cb);
20
+    expect(subscription).toBe(result);
21
+  });
22
+});

+ 13
- 0
lib/src/events/EventsRegistry.ts View File

1
+import { EventSubscription } from 'react-native';
2
+
3
+export class EventsRegistry {
4
+  private nativeEventsReceiver;
5
+
6
+  constructor(nativeEventsReceiver) {
7
+    this.nativeEventsReceiver = nativeEventsReceiver;
8
+  }
9
+
10
+  public appLaunched(callback): EventSubscription {
11
+    return this.nativeEventsReceiver.registerAppLaunched(callback);
12
+  }
13
+}

+ 0
- 52
lib/src/events/PrivateEventsListener.test.ts View File

1
-import { PrivateEventsListener } from './PrivateEventsListener';
2
-import { NativeEventsReceiver } from '../adapters/NativeEventsReceiver.mock';
3
-import { Store } from '../components/Store';
4
-
5
-describe('PrivateEventsListener', () => {
6
-  let uut: PrivateEventsListener;
7
-  let nativeEventsReceiver;
8
-  let store;
9
-
10
-  beforeEach(() => {
11
-    nativeEventsReceiver = new NativeEventsReceiver();
12
-    store = new Store();
13
-    uut = new PrivateEventsListener(nativeEventsReceiver, store);
14
-  });
15
-
16
-  it('register and handle componentDidAppear', () => {
17
-    const mockRef = {
18
-      didAppear: jest.fn()
19
-    };
20
-    store.setRefForComponentId('myComponentId', mockRef);
21
-    uut.listenAndHandlePrivateEvents();
22
-    expect(nativeEventsReceiver.registerComponentDidAppear).toHaveBeenCalledTimes(1);
23
-    const callbackFunction = nativeEventsReceiver.registerComponentDidAppear.mock.calls[0][0];
24
-    expect(callbackFunction).toBeInstanceOf(Function);
25
-
26
-    expect(mockRef.didAppear).not.toHaveBeenCalled();
27
-    callbackFunction('myComponentId');
28
-
29
-    expect(mockRef.didAppear).toHaveBeenCalledTimes(1);
30
-  });
31
-
32
-  it('register and listen componentDidDisappear', () => {
33
-    uut.listenAndHandlePrivateEvents();
34
-    expect(nativeEventsReceiver.registerComponentDidDisappear).toHaveBeenCalledTimes(1);
35
-  });
36
-
37
-  it('register and handle onNavigationButtonPressed', () => {
38
-    const mockRef = {
39
-      onNavigationButtonPressed: jest.fn()
40
-    };
41
-    store.setRefForComponentId('myComponentId', mockRef);
42
-    uut.listenAndHandlePrivateEvents();
43
-    expect(nativeEventsReceiver.registerNavigationButtonPressed).toHaveBeenCalledTimes(1);
44
-    const callbackFunction = nativeEventsReceiver.registerNavigationButtonPressed.mock.calls[0][0];
45
-    expect(callbackFunction).toBeInstanceOf(Function);
46
-
47
-    expect(mockRef.onNavigationButtonPressed).not.toHaveBeenCalled();
48
-    callbackFunction({ componentId: 'myComponentId', buttonId: 'myButtonId' });
49
-
50
-    expect(mockRef.onNavigationButtonPressed).toHaveBeenCalledTimes(1);
51
-  });
52
-});

+ 0
- 17
lib/src/events/PrivateEventsListener.ts View File

1
-import { Lifecycle } from '../components/Lifecycle';
2
-
3
-export class PrivateEventsListener {
4
-  private lifecycle;
5
-
6
-  constructor(
7
-    private readonly nativeEventsReceiver,
8
-    private readonly store) {
9
-    this.lifecycle = new Lifecycle(this.store);
10
-  }
11
-
12
-  public listenAndHandlePrivateEvents() {
13
-    this.nativeEventsReceiver.registerComponentDidAppear(this.lifecycle.componentDidAppear);
14
-    this.nativeEventsReceiver.registerComponentDidDisappear(this.lifecycle.componentDidDisappear);
15
-    this.nativeEventsReceiver.registerNavigationButtonPressed(this.lifecycle.onNavigationButtonPressed);
16
-  }
17
-}

+ 0
- 33
lib/src/events/PublicEventsRegistry.test.ts View File

1
-import { PublicEventsRegistry } from './PublicEventsRegistry';
2
-import { NativeEventsReceiver } from '../adapters/NativeEventsReceiver.mock';
3
-
4
-describe('PublicEventsRegistry', () => {
5
-  let uut;
6
-  let nativeEventsReceiver;
7
-
8
-  beforeEach(() => {
9
-    nativeEventsReceiver = new NativeEventsReceiver();
10
-    uut = new PublicEventsRegistry(nativeEventsReceiver);
11
-  });
12
-
13
-  it('exposes onAppLaunch event', () => {
14
-    const cb = jest.fn();
15
-    uut.onAppLaunched(cb);
16
-    expect(nativeEventsReceiver.registerAppLaunched).toHaveBeenCalledTimes(1);
17
-    expect(nativeEventsReceiver.registerAppLaunched).toHaveBeenCalledWith(cb);
18
-  });
19
-
20
-  it('exposes navigationCommands events', () => {
21
-    const cb = jest.fn();
22
-    uut.navigationCommands(cb);
23
-    expect(nativeEventsReceiver.registerNavigationCommands).toHaveBeenCalledTimes(1);
24
-    expect(nativeEventsReceiver.registerNavigationCommands).toHaveBeenCalledWith(cb);
25
-  });
26
-
27
-  it('exposes componentLifecycle events', () => {
28
-    const cb = jest.fn();
29
-    uut.componentLifecycle(cb);
30
-    expect(nativeEventsReceiver.registerComponentLifecycle).toHaveBeenCalledTimes(1);
31
-    expect(nativeEventsReceiver.registerComponentLifecycle).toHaveBeenCalledWith(cb);
32
-  });
33
-});

+ 0
- 19
lib/src/events/PublicEventsRegistry.ts View File

1
-export class PublicEventsRegistry {
2
-  private nativeEventsReceiver;
3
-
4
-  constructor(nativeEventsReceiver) {
5
-    this.nativeEventsReceiver = nativeEventsReceiver;
6
-  }
7
-
8
-  public onAppLaunched(callback) {
9
-    this.nativeEventsReceiver.registerAppLaunched(callback);
10
-  }
11
-
12
-  public navigationCommands(callback) {
13
-    this.nativeEventsReceiver.registerNavigationCommands(callback);
14
-  }
15
-
16
-  public componentLifecycle(callback) {
17
-    this.nativeEventsReceiver.registerComponentLifecycle(callback);
18
-  }
19
-}