mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
Refactored bridge to support transferrables (e.g. typed array buffers) and added transferable param to postMessage for op codes
This commit is contained in:
@@ -44,7 +44,7 @@ export default class Agent extends EventEmitter {
|
||||
|
||||
onHookOperations = (operations: Uint32Array) => {
|
||||
debug('onHookOperations', operations);
|
||||
this._bridge.send('operations', operations);
|
||||
this._bridge.send('operations', operations, [operations.buffer]);
|
||||
};
|
||||
|
||||
onHookRootCommitted = (rootID: string) => {
|
||||
|
||||
+11
-28
@@ -12,7 +12,7 @@ type Message = {|
|
||||
|};
|
||||
|
||||
export default class Bridge extends EventEmitter {
|
||||
_messageQueue: Array<Message> = [];
|
||||
_messageQueue: Array<any> = [];
|
||||
_time: number | null = null;
|
||||
_timeoutID: TimeoutID | null = null;
|
||||
|
||||
@@ -22,33 +22,20 @@ export default class Bridge extends EventEmitter {
|
||||
super();
|
||||
|
||||
this.wall = wall;
|
||||
wall.listen(messages => {
|
||||
if (Array.isArray(messages)) {
|
||||
messages.forEach(message => this._emit(message));
|
||||
} else {
|
||||
this._emit(messages);
|
||||
}
|
||||
|
||||
wall.listen((message: Message) => {
|
||||
this._emit(message);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Send an event.
|
||||
*
|
||||
* @param {String} event
|
||||
* @param {*} payload
|
||||
*/
|
||||
|
||||
send(event: string, payload: any) {
|
||||
send(event: string, payload: any, transferable?: Array<any>) {
|
||||
const time = this._time;
|
||||
|
||||
if (time === null) {
|
||||
this.wall.send([{ event, payload }]);
|
||||
this.wall.send(event, payload, transferable);
|
||||
this._time = Date.now();
|
||||
} else {
|
||||
this._messageQueue.push({
|
||||
event,
|
||||
payload,
|
||||
});
|
||||
this._messageQueue.push(event, payload, transferable);
|
||||
|
||||
const now = Date.now();
|
||||
if (now - time > BATCH_DURATION) {
|
||||
@@ -59,24 +46,20 @@ export default class Bridge extends EventEmitter {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Log a message to the devtools background page.
|
||||
*
|
||||
* @param {String} message
|
||||
*/
|
||||
|
||||
log(message: string): void {
|
||||
this.send('log', message);
|
||||
}
|
||||
|
||||
_flush() {
|
||||
if (this._messageQueue.length) {
|
||||
this.wall.send(this._messageQueue);
|
||||
while (this._messageQueue.length) {
|
||||
this.wall.send.apply(this.wall, this._messageQueue.splice(0, 3));
|
||||
}
|
||||
|
||||
if (this._timeoutID !== null) {
|
||||
clearTimeout(this._timeoutID);
|
||||
this._timeoutID = null;
|
||||
}
|
||||
|
||||
this._messageQueue = [];
|
||||
this._time = null;
|
||||
}
|
||||
|
||||
@@ -102,6 +102,11 @@ export default class Store extends EventEmitter {
|
||||
}
|
||||
|
||||
onBridgeOperations = (operations: Uint32Array) => {
|
||||
if (!(operations instanceof Uint32Array)) {
|
||||
// $FlowFixMe TODO HACK Temporary workaround for the fact that Chrome is not transferring the typed array.
|
||||
operations = Uint32Array.from(Object.values(operations));
|
||||
}
|
||||
|
||||
debug('onBridgeOperations', operations);
|
||||
|
||||
let haveRootsChanged = false;
|
||||
|
||||
+1
-1
@@ -8,5 +8,5 @@ export type Hook = any;
|
||||
|
||||
export type Wall = {|
|
||||
listen: (fn: Function) => void,
|
||||
send: (data: any) => void,
|
||||
send: (event: string, payload: any, transferable?: Array<any>) => void,
|
||||
|};
|
||||
|
||||
Reference in New Issue
Block a user