-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuse-suspender.js
165 lines (144 loc) · 3.42 KB
/
use-suspender.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
const eq = require("fast-deep-equal/react")
/**
* @typedef {"initial" | "pending" | "resolved" | "rejected"} States
*/
const STATE_INITIAL = "initial"
const STATE_PENDING = "pending"
const STATE_RESOLVED = "resolved"
const STATE_REJECTED = "rejected"
/**
* @typedef {Object} InitialOperationState
* @prop {States} state
* @prop {Error} error
* @prop {any} result
* @prop {Promise<void>} suspender
* @prop {any[]} args
*/
/**
* @type {InitialOperationState}
*
* @api private
*/
const initialOperationState = {
state: STATE_INITIAL,
error: null,
result: null,
suspender: null,
args: []
}
/**
* Calls a function and returns a Promise that resolves a result
*
* @param {(...args: any[]) => any} fn
* @param {any[]} args
* @param {any} ctx
*
* @return {Promise<any>}
*
* @api private
*/
function getPromise(fn, args, ctx) {
try {
const res = fn.apply(ctx, args)
return res instanceof Promise ? res : Promise.resolve(res)
} catch (err) {
return Promise.reject(err)
}
}
/**
* Creates a new useSuspender hook for given function.
*
* @template T
*
* @param {(...args: any[]) => T} fn A function to create a useSuspender hook with
* @param {any} [ctx = undefined] thisArg value
*
* @return {(...args: any[]) => T} useSuspender
*
* @api public
*/
function createSuspender(fn, ctx) {
if (typeof fn !== "function") {
throw new TypeError("First argument expected to be a function.")
}
let operation = {...initialOperationState}
/**
* Resets operation the operation
*
* @return {void}
*
* @api private
*/
function reset() {
operation = {...initialOperationState}
}
/**
* Calls a suspender function and sets its Promise on the operation
* Takes the same arguments as getPromise function.
*
* @param {any[]} args
*
* @return {Promise<void>}
*
* @api private
*/
function call(args) {
operation.suspender = getPromise(fn, args, ctx)
.then(result => {
operation.result = result
operation.state = STATE_RESOLVED
})
.catch(error => {
operation.error = error
operation.state = STATE_REJECTED
})
operation.state = STATE_PENDING
operation.args = args
return operation.suspender
}
/**
* Executes a suspender with given arguments.
* Will throw a Promise to notify React.Suspense
*
* @param {any[]} ...args A list of arguments to execute suspender with
*
* @return {T}
*
* @throws {Promise<void>} If the Promise haven't been fulfilled yet
*
* @throws {Error} If suspender's Promise has been rejected with an error
*
* @api public
*/
function useSuspender(...args) {
if (operation.state === STATE_PENDING && eq(args, operation.args)) {
throw operation.suspender
}
if (operation.state === STATE_REJECTED) {
throw operation.error
}
if (operation.state === STATE_RESOLVED) {
const {result} = operation
reset()
return result
}
throw call(args)
}
/**
* Calls useSuspense early
*
* @param {any[]} [args = []] A list of arguments to execute suspender with
*
* @return {void}
*
* @api public
*/
useSuspender.callEarly = function callEarly(...args) {
call(args)
}
// For those who want to use object destructing on createSuspender result.
useSuspender.useSuspender = useSuspender
return useSuspender
}
module.exports = createSuspender
module.exports.default = createSuspender