mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Add spec for BlobModule (#24909)
Summary: Part of #24875. Not sure that the id’s types are necessarily correct here… ## Changelog [General] [Added] - Add TM spec for BlobModule Pull Request resolved: https://github.com/facebook/react-native/pull/24909 Reviewed By: fkgozali Differential Revision: D15433753 Pulled By: RSNara fbshipit-source-id: 68193d1a82fc7c66d6cc7ba4f22a0d3786987599
This commit is contained in:
committed by
Facebook Github Bot
parent
8ea749ad3e
commit
342c81d754
@@ -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';
|
||||
|
||||
@@ -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.
|
||||
@@ -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 (
|
||||
@@ -92,7 +95,7 @@ class BlobManager {
|
||||
}
|
||||
}, 0);
|
||||
|
||||
BlobModule.createFromParts(items, blobId);
|
||||
NativeBlobModule.createFromParts(items, blobId);
|
||||
|
||||
return BlobManager.createFromOptions({
|
||||
blobId,
|
||||
@@ -127,11 +130,13 @@ 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);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -139,7 +144,9 @@ class BlobManager {
|
||||
* requests and responses.
|
||||
*/
|
||||
static addNetworkingHandler(): void {
|
||||
BlobModule.addNetworkingHandler();
|
||||
invariant(NativeBlobModule, 'NativeBlobModule is available.');
|
||||
|
||||
NativeBlobModule.addNetworkingHandler();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -147,7 +154,9 @@ class BlobManager {
|
||||
* messages.
|
||||
*/
|
||||
static addWebSocketHandler(socketId: number): void {
|
||||
BlobModule.addWebSocketHandler(socketId);
|
||||
invariant(NativeBlobModule, 'NativeBlobModule is available.');
|
||||
|
||||
NativeBlobModule.addWebSocketHandler(socketId);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -155,14 +164,18 @@ class BlobManager {
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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');
|
||||
@@ -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}/`;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+1
-2
@@ -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(),
|
||||
|
||||
Reference in New Issue
Block a user