Add LazyReactPackage

Summary:
LazyReactPackage is an extension of ReactPackage that allows us to lazily construct native modules.
It's a separate class to avoid breaking existing packages both internally and in open source.

Reviewed By: astreet

Differential Revision: D3334258

fbshipit-source-id: e090e146adc4e8e156cae217689e2258ab9837aa
This commit is contained in:
Aaron Chiu
2016-08-10 17:12:35 -07:00
committed by Facebook Github Bot 3
parent dba1ce46bf
commit 1feb462f44
6 changed files with 323 additions and 0 deletions

View File

@@ -0,0 +1,40 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
package com.facebook.react;
import java.util.ArrayList;
import java.util.List;
import com.facebook.react.bridge.ModuleSpec;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
/**
* React package supporting lazy creation of native modules.
*
* TODO(t11394819): Make this default and deprecate ReactPackage
*/
public abstract class LazyReactPackage implements ReactPackage {
/**
* @param reactContext react application context that can be used to create modules
* @return list of module specs that can create the native modules
*/
public abstract List<ModuleSpec> getNativeModules(
ReactApplicationContext reactContext);
@Override
public final List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
List<NativeModule> modules = new ArrayList<>();
for (ModuleSpec holder : getNativeModules(reactContext)) {
modules.add(holder.getProvider().get());
}
return modules;
}
}