Skip to content

Commit

Permalink
Add spec for BlobModule (facebook#24909)
Browse files Browse the repository at this point in the history
Summary:
Part of facebook#24875. Not sure that the id’s types are necessarily correct here…

## Changelog

[General] [Added] - Add TM spec for BlobModule
Pull Request resolved: facebook#24909

Reviewed By: fkgozali

Differential Revision: D15433753

Pulled By: RSNara

fbshipit-source-id: 68193d1a82fc7c66d6cc7ba4f22a0d3786987599
  • Loading branch information
ericlewis authored and M-i-k-e-l committed Mar 10, 2020
1 parent e82b489 commit c6e77da
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 15 deletions.
29 changes: 21 additions & 8 deletions Libraries/Blob/BlobManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

const Blob = require('./Blob');
const BlobRegistry = require('./BlobRegistry');
const {BlobModule} = require('../BatchedBridge/NativeModules');
import NativeBlobModule from './NativeBlobModule';
import invariant from 'invariant';

import type {BlobData, BlobOptions, BlobCollector} from './BlobTypes';

Expand Down Expand Up @@ -53,7 +54,7 @@ class BlobManager {
/**
* If the native blob module is available.
*/
static isAvailable = !!BlobModule;
static isAvailable = !!NativeBlobModule;

/**
* Create blob from existing array of blobs.
Expand All @@ -62,6 +63,8 @@ class BlobManager {
parts: Array<Blob | string>,
options?: BlobOptions,
): Blob {
invariant(NativeBlobModule, 'NativeBlobModule is available.');

const blobId = uuidv4();
const items = parts.map(part => {
if (
Expand Down Expand Up @@ -92,7 +95,7 @@ class BlobManager {
}
}, 0);

BlobModule.createFromParts(items, blobId);
NativeBlobModule.createFromParts(items, blobId);

return BlobManager.createFromOptions({
blobId,
Expand Down Expand Up @@ -127,42 +130,52 @@ class BlobManager {
* Deallocate resources for a blob.
*/
static release(blobId: string): void {
invariant(NativeBlobModule, 'NativeBlobModule is available.');

BlobRegistry.unregister(blobId);
if (BlobRegistry.has(blobId)) {
return;
}
BlobModule.release(blobId);
NativeBlobModule.release(blobId);
}

/**
* Inject the blob content handler in the networking module to support blob
* requests and responses.
*/
static addNetworkingHandler(): void {
BlobModule.addNetworkingHandler();
invariant(NativeBlobModule, 'NativeBlobModule is available.');

NativeBlobModule.addNetworkingHandler();
}

/**
* Indicate the websocket should return a blob for incoming binary
* messages.
*/
static addWebSocketHandler(socketId: number): void {
BlobModule.addWebSocketHandler(socketId);
invariant(NativeBlobModule, 'NativeBlobModule is available.');

NativeBlobModule.addWebSocketHandler(socketId);
}

/**
* Indicate the websocket should no longer return a blob for incoming
* binary messages.
*/
static removeWebSocketHandler(socketId: number): void {
BlobModule.removeWebSocketHandler(socketId);
invariant(NativeBlobModule, 'NativeBlobModule is available.');

NativeBlobModule.removeWebSocketHandler(socketId);
}

/**
* Send a blob message to a websocket.
*/
static sendOverSocket(blob: Blob, socketId: number): void {
BlobModule.sendOverSocket(blob.data, socketId);
invariant(NativeBlobModule, 'NativeBlobModule is available.');

NativeBlobModule.sendOverSocket(blob.data, socketId);
}
}

Expand Down
26 changes: 26 additions & 0 deletions Libraries/Blob/NativeBlobModule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
* @format
*/

'use strict';

import type {TurboModule} from 'RCTExport';
import * as TurboModuleRegistry from 'TurboModuleRegistry';

export interface Spec extends TurboModule {
+getConstants: () => {|BLOB_URI_SCHEME: string, BLOB_URI_HOST: ?string|};
+addNetworkingHandler: () => void;
+addWebSocketHandler: (id: number) => void;
+removeWebSocketHandler: (id: number) => void;
+sendOverSocket: (blob: Object, id: number) => void;
+createFromParts: (parts: Array<Object>, blobId: string) => void;
+release: (blobId: string) => void;
}

export default TurboModuleRegistry.get<Spec>('BlobModule');
14 changes: 9 additions & 5 deletions Libraries/Blob/URL.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@

const Blob = require('./Blob');

const {BlobModule} = require('../BatchedBridge/NativeModules');
import NativeBlobModule from './NativeBlobModule';

let BLOB_URL_PREFIX = null;

if (BlobModule && typeof BlobModule.BLOB_URI_SCHEME === 'string') {
BLOB_URL_PREFIX = BlobModule.BLOB_URI_SCHEME + ':';
if (typeof BlobModule.BLOB_URI_HOST === 'string') {
BLOB_URL_PREFIX += `//${BlobModule.BLOB_URI_HOST}/`;
if (
NativeBlobModule &&
typeof NativeBlobModule.getConstants().BLOB_URI_SCHEME === 'string'
) {
const constants = NativeBlobModule.getConstants();
BLOB_URL_PREFIX = constants.BLOB_URI_SCHEME + ':';
if (typeof constants.BLOB_URI_HOST === 'string') {
BLOB_URL_PREFIX += `//${constants.BLOB_URI_HOST}/`;
}
}

Expand Down
3 changes: 1 addition & 2 deletions jest/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,7 @@ const mockNativeModules = {
},
},
BlobModule: {
BLOB_URI_SCHEME: 'content',
BLOB_URI_HOST: null,
getConstants: () => ({BLOB_URI_SCHEME: 'content', BLOB_URI_HOST: null}),
addNetworkingHandler: jest.fn(),
enableBlobSupport: jest.fn(),
disableBlobSupport: jest.fn(),
Expand Down

1 comment on commit c6e77da

@renanmav
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How should we use Blob in react-native now @ericlewis? I’m getting this error:
Captura de Tela 2020-09-22 às 11 30 33

Please sign in to comment.