Convert CallbackQueue to a class (#7647)
It turns out that flow cannot type `this` with a function constructor + prototype overrides. Turning it to a class makes flow happy and has minimal impact on the output. In open source, we already use the loose version of the class transform and internally we have one that's outputting even less code if you have `@preventMunge` in the header. See discussion in https://www.facebook.com/groups/2003630259862046/permalink/2098480820376989/
Christopher Chedeau committed
Sep 2, 2016 at 15:27 UTC
a70acb37d9dc392b395bd921d90d5488542c2402
2 files changed
+24
-23
src/renderers/shared/stack/event/ReactSyntheticEvent.js
+1
-1
@@ -18,4 +18,4 @@ export type DispatchConfig = any;
18
19
export class ReactSyntheticEvent extends SyntheticEvent {
20
dispatchConfig: DispatchConfig;
21
-};
21
+}
src/renderers/shared/utils/CallbackQueue.js
+23
-22
@@ -27,12 +27,14 @@ var invariant = require('invariant');
27
* @implements PooledClass
28
* @internal
29
*/
30
-function CallbackQueue() {
31
- this._callbacks = null;
32
- this._contexts = null;
33
-}
30
+class CallbackQueue<T> {
31
+ _callbacks: ?Array<() => void>;
32
+ _contexts: ?Array<T>;
33
35
-Object.assign(CallbackQueue.prototype, {
34
+ constructor() {
35
+ this._callbacks = null;
36
+ this._contexts = null;
37
+ }
38
39
/**
40
* Enqueues a callback to be invoked when `notifyAll` is invoked.
@@ -41,12 +43,12 @@ Object.assign(CallbackQueue.prototype, {
43
* @param {?object} context Context to call `callback` with.
44
* @internal
45
*/
44
- enqueue: function(callback, context) {
46
+ enqueue(callback: () => void, context: T) {
47
this._callbacks = this._callbacks || [];
46
- this._contexts = this._contexts || [];
48
this._callbacks.push(callback);
49
+ this._contexts = this._contexts || [];
50
this._contexts.push(context);
49
- },
51
+ }
52
53
/**
54
* Invokes all enqueued callbacks and clears the queue. This is invoked after
@@ -54,10 +56,10 @@ Object.assign(CallbackQueue.prototype, {
56
*
57
* @internal
58
*/
57
- notifyAll: function() {
59
+ notifyAll() {
60
var callbacks = this._callbacks;
61
var contexts = this._contexts;
60
- if (callbacks) {
62
+ if (callbacks && contexts) {
63
invariant(
64
callbacks.length === contexts.length,
65
'Mismatched list of contexts in callback queue'
@@ -70,36 +72,35 @@ Object.assign(CallbackQueue.prototype, {
72
callbacks.length = 0;
73
contexts.length = 0;
74
}
73
- },
75
+ }
76
75
- checkpoint: function() {
77
+ checkpoint() {
78
return this._callbacks ? this._callbacks.length : 0;
77
- },
79
+ }
80
79
- rollback: function(len) {
80
- if (this._callbacks) {
81
+ rollback(len: number) {
82
+ if (this._callbacks && this._contexts) {
83
this._callbacks.length = len;
84
this._contexts.length = len;
85
}
84
- },
86
+ }
87
88
/**
89
* Resets the internal queue.
90
*
91
* @internal
92
*/
91
- reset: function() {
93
+ reset() {
94
this._callbacks = null;
95
this._contexts = null;
94
- },
96
+ }
97
98
/**
99
* `PooledClass` looks for this.
100
*/
99
- destructor: function() {
101
+ destructor() {
102
this.reset();
101
- },
102
-
103
-});
103
+ }
104
+}
105
106
module.exports = PooledClass.addPoolingTo(CallbackQueue);