serviceprovider
A simple and lightweight Service Provider for node.js project.
Initially intended to mimic behaviour of Angular's service provider on React Native.
Install
npm i -S serviceprovider
Usage
// Import;// or; // Initialize the ProviderProvider.initproviders: array, store?: ReduxStore; // Resolve the serviceProvider.resolveservice: Class; // Notify services that the application is mounted// Should be called from 'app.componentDidMount'// Service should implement 'appDidMount' method too support this feature.Provider.appDidMount; // Notify services that the application will be unmount// Should be called from 'app.componentWillUnmount'// Service should implement 'appWillUnmount' method too support this feature.Provider.appWillUnmount;
Dependency Injection and Redux Store
The dependency will be automatically injected via constructor
for each service if the service define static deps
array. If redux store is passed from calling Provider.init
, it will also pass along the constructor of every service.
example
import ServiceB from './service.b';
import ServiceC from './service.c';
class ServiceA {
static deps = [ServiceB, ServiceC];
constructor(store, deps) {
this.serviceB = deps[0];
this.serviceC = deps[1];
}
}
Example
HelloComponent (hello.component.js)
import React Component from 'react';import Text from 'react-native'; import Provider from 'serviceprovider';import GreetService from './greet.service'; _unSubAll = false; { ; thisgreetService = Provider; thisstate = ; } { thisgreetServicestateEvent ; } { this_unSubAll = true; } { return <Text>Hello thisstatename</Text>; }
NameAlternator (name.alternator.js)
import Provider from 'serviceprovider';import GreetService from './greet.service'; { const greetService = Provider; let i = 0; greetService; ; }
GreetService (greet.service.js)
import BehaviorSubject from 'rxjs/BehaviorSubject'; _stateSubject = null; stateEvent = this_stateSubject; { this_stateSubject; }
Main (App.js)
import React Component from 'react';import View Text from 'react-native';import Provider from 'serviceprovider'; import 'rxjs/add/operator/takeWhile'; // Componentsimport HelloComponent from './hello.component';import NameAlternator from './name.alternator'; // Servicesimport GreetService from './greet.service'; // The list of the service providers = GreetService ; // Initialize the Provider in top module class { ; Provider; ; } // UI Render { return <View> <Text /><Text /><Text /><Text /> <HelloComponent /> </View> ; }
Result
License
MIT License
Copyright 2017 Attawit Kittikrairit
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.