Skip to content

Commit b43f56d

Browse files
author
Vlad Balin
committed
Added global hooks.
1 parent 08db80f commit b43f56d

File tree

4 files changed

+34
-0
lines changed

4 files changed

+34
-0
lines changed

backend/backend.js

+3
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,13 @@
2424
'use strict';
2525

2626
import type {Hook} from './types';
27+
import attachInnerStateInspectors from './innerStateInspectors';
2728

2829
var attachRenderer = require('./attachRenderer');
2930

3031
module.exports = function setupBackend(hook: Hook): boolean {
32+
attachInnerStateInspectors( hook );
33+
3134
var oldReact = window.React && window.React.__internals;
3235
if (oldReact && Object.keys(hook._renderers).length === 0) {
3336
hook.inject(oldReact);

backend/innerStateInspectors.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @flow
3+
*
4+
* Inner state inspectors for the built in JS data types.
5+
*/
6+
7+
'use strict';
8+
9+
import type {Hook} from './types';
10+
11+
export default
12+
function attachInnerStateInspectors({ addInnerStateInspector } : Hook ) {
13+
addInnerStateInspector( Date, ( x : Date ) => ({
14+
local : `${ x.toDateString() } ${ x.toTimeString() }`,
15+
iso : x.toISOString(),
16+
timestamp : x.getTime(),
17+
}), true );
18+
}

backend/installGlobalHook.js

+11
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,17 @@ function installGlobalHook(window: Object) {
198198
hook._listeners[evt].map(fn => fn(data));
199199
}
200200
},
201+
addInnerStateInspector: function(Ctor, getInnerState, skipIfDefined) {
202+
if ( !( skipIfDefined && Ctor.prototype.hasOwnProperty( '__inner_state__' ) ) ) {
203+
Object.defineProperty(Ctor.prototype, '__inner_state__', ({
204+
get: function() {
205+
return getInnerState(this);
206+
},
207+
enumerable: false,
208+
configurable: true,
209+
} : Object ));
210+
}
211+
},
201212
// Fiber-only:
202213
supportsFiber: true,
203214
_fiberRoots: {},

backend/types.js

+2
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ export type Helpers = {
9898
};
9999

100100
export type Handler = (data: any) => void;
101+
export type InnerStateInspector = (data: any) => any;
101102

102103
export type Hook = {
103104
_renderers: {[key: string]: ReactRenderer},
@@ -110,4 +111,5 @@ export type Hook = {
110111
off: (evt: string, handler: Handler) => void,
111112
reactDevtoolsAgent?: ?Object,
112113
getFiberRoots: (rendererID : string) => Set<Object>,
114+
addInnerStateInspector: ( Ctor : Function, handler : InnerStateInspector, skipIfDefined? : boolean ) => void,
113115
};

0 commit comments

Comments
 (0)