Skip to content

Commit

Permalink
Allow UnpackingJSBundleLoader's client to queue action to perform aft…
Browse files Browse the repository at this point in the history
…er unpacking

Reviewed By: tadeuzagallo

Differential Revision: D3735948

fbshipit-source-id: 5971a5367eb4b5a90e0f23b9279759e9f4060222
  • Loading branch information
Michał Gregorczyk authored and Facebook Github Bot 8 committed Sep 7, 2016
1 parent d323856 commit 2618ba2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public class UnpackingJSBundleLoader extends JSBundleLoader {
private final String mSourceURL;
private final Context mContext;
private final int mLoadFlags;
private final @Nullable Runnable mOnUnpackedCallback;

/**
* Description of what needs to be unpacked.
Expand All @@ -78,25 +79,32 @@ public class UnpackingJSBundleLoader extends JSBundleLoader {
mSourceURL = Assertions.assertNotNull(builder.sourceURL);
mUnpackers = builder.unpackers.toArray(new Unpacker[builder.unpackers.size()]);
mLoadFlags = builder.loadFlags;
mOnUnpackedCallback = builder.callback;
}

/**
* Checks if any file needs to be extracted again, and if so, clears the destination
* directory and unpacks everything again.
*/
/* package */ void prepare() {
boolean unpacked = false;

final File lockFilePath = new File(mContext.getFilesDir(), LOCK_FILE);
Systrace.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "UnpackingJSBundleLoader.prepare");
try (FileLocker lock = FileLocker.lock(lockFilePath)) {
prepareLocked();
unpacked = prepareLocked();
} catch (IOException ioe) {
throw new RuntimeException(ioe);
} finally {
Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE);
}

if (unpacked && mOnUnpackedCallback != null) {
mOnUnpackedCallback.run();
}
}

private void prepareLocked() throws IOException {
private boolean prepareLocked() throws IOException {
final File dotFinishedFilePath = new File(mDirectoryPath, DOT_UNPACKED_FILE);
boolean shouldReconstruct = !mDirectoryPath.exists() || !dotFinishedFilePath.exists();

Expand All @@ -106,7 +114,7 @@ private void prepareLocked() throws IOException {
}

if (!shouldReconstruct) {
return;
return false;
}

boolean succeeded = false;
Expand Down Expand Up @@ -138,6 +146,8 @@ private void prepareLocked() throws IOException {
SysUtil.dumbDeleteRecursive(mDirectoryPath);
}
}

return true;
}

@Override
Expand Down Expand Up @@ -201,13 +211,15 @@ public static class Builder {
private @Nullable String sourceURL;
private final ArrayList<Unpacker> unpackers;
private int loadFlags;
private @Nullable Runnable callback;

public Builder() {
this.unpackers = new ArrayList<Unpacker>();
context = null;
destinationPath = null;
sourceURL = null;
loadFlags = 0;
callback = null;
}

public Builder setContext(Context context) {
Expand Down Expand Up @@ -257,6 +269,11 @@ Builder addUnpacker(Unpacker u) {
return this;
}

public Builder setOnUnpackedCallback(Runnable callback) {
this.callback = callback;
return this;
}

public UnpackingJSBundleLoader build() {
Assertions.assertNotNull(destinationPath);
for (int i = 0; i < unpackers.size(); ++i) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public class UnpackingJSBundleLoaderTest {
private CatalystInstanceImpl mCatalystInstanceImpl;
private UnpackingJSBundleLoader.Unpacker[] mMockUnpackers;

private Runnable mCallback;

@Before
public void setUp() throws IOException {
mDestinationPath = folder.newFolder("destination");
Expand All @@ -75,6 +77,8 @@ public void setUp() throws IOException {
for (int i = 0; i < mMockUnpackers.length; ++i) {
mMockUnpackers[i] = mock(UnpackingJSBundleLoader.Unpacker.class);
}

mCallback = mock(Runnable.class);
}

private void addUnpackers() {
Expand Down Expand Up @@ -193,4 +197,17 @@ public void testDropsDirectoryOnException() throws IOException {
assertFalse(mDestinationPath.exists());
}
}

@Test
public void testCallbackIsCalledAfterUnpack() {
mBuilder.setOnUnpackedCallback(mCallback).build().prepare();
verify(mCallback).run();
}

@Test
public void testCallbackIsNotCalledIfNothingIsUnpacked() {
mBuilder.build().prepare();
mBuilder.setOnUnpackedCallback(mCallback).build().prepare();
verifyNoMoreInteractions(mCallback);
}
}

0 comments on commit 2618ba2

Please sign in to comment.