@shopify/react-effect
Advanced tools
Comparing version 2.0.1 to 2.1.0
@@ -10,2 +10,9 @@ # Changelog | ||
## [2.1.0] | ||
### Added | ||
- Added a `maxPasses` option to `extract()` in order to limit the potential for infinite loops. This option defaults to 5 max render/ resolve cycles [#574](https://github.com/Shopify/quilt/pull/574) | ||
- All `afterEachPass`/ `betweenEachPass` callbacks now receive an argument detailing the current pass index, whether the extraction process is complete, and the duration of the render/ resolve phases [#574](https://github.com/Shopify/quilt/pull/574) | ||
## [2.0.0] | ||
@@ -12,0 +19,0 @@ |
import * as React from 'react'; | ||
import { EffectKind } from './types'; | ||
import { EffectKind, Pass } from './types'; | ||
interface Options { | ||
@@ -14,4 +14,4 @@ include?: symbol[] | boolean; | ||
resolve(): Promise<void>; | ||
betweenEachPass(): void; | ||
afterEachPass(): void; | ||
betweenEachPass(pass: Pass): void; | ||
afterEachPass(pass: Pass): void; | ||
shouldPerform(kind: EffectKind): boolean; | ||
@@ -18,0 +18,0 @@ } |
@@ -40,3 +40,3 @@ "use strict"; | ||
}; | ||
EffectManager.prototype.betweenEachPass = function () { | ||
EffectManager.prototype.betweenEachPass = function (pass) { | ||
var e_1, _a; | ||
@@ -47,3 +47,3 @@ try { | ||
if (typeof kind.betweenEachPass === 'function') { | ||
kind.betweenEachPass(); | ||
kind.betweenEachPass(pass); | ||
} | ||
@@ -60,3 +60,3 @@ } | ||
}; | ||
EffectManager.prototype.afterEachPass = function () { | ||
EffectManager.prototype.afterEachPass = function (pass) { | ||
var e_2, _a; | ||
@@ -67,3 +67,3 @@ try { | ||
if (typeof kind.afterEachPass === 'function') { | ||
kind.afterEachPass(); | ||
kind.afterEachPass(pass); | ||
} | ||
@@ -70,0 +70,0 @@ } |
import * as React from 'react'; | ||
import { Pass } from './types'; | ||
interface Options { | ||
include?: symbol[] | boolean; | ||
maxPasses?: number; | ||
decorate?(element: React.ReactElement<any>): React.ReactElement<any>; | ||
renderFunction?(element: React.ReactElement<{}>): string; | ||
betweenEachPass?(): any; | ||
afterEachPass?(): any; | ||
betweenEachPass?(pass: Pass): any; | ||
afterEachPass?(pass: Pass): any; | ||
} | ||
export declare function extract(app: React.ReactElement<any>, { include, decorate, renderFunction, betweenEachPass, afterEachPass, }?: Options): Promise<string>; | ||
export declare function extract(app: React.ReactElement<any>, { include, maxPasses, decorate, renderFunction, betweenEachPass, afterEachPass, }?: Options): Promise<string>; | ||
export {}; |
@@ -7,19 +7,33 @@ "use strict"; | ||
var context_1 = require("./context"); | ||
var DEFAULT_MAX_PASSES = 5; | ||
function extract(app, _a) { | ||
var _b = _a === void 0 ? {} : _a, include = _b.include, _c = _b.decorate, decorate = _c === void 0 ? identity : _c, _d = _b.renderFunction, renderFunction = _d === void 0 ? server_1.renderToStaticMarkup : _d, betweenEachPass = _b.betweenEachPass, afterEachPass = _b.afterEachPass; | ||
var _b = _a === void 0 ? {} : _a, include = _b.include, _c = _b.maxPasses, maxPasses = _c === void 0 ? DEFAULT_MAX_PASSES : _c, _d = _b.decorate, decorate = _d === void 0 ? identity : _d, _e = _b.renderFunction, renderFunction = _e === void 0 ? server_1.renderToStaticMarkup : _e, betweenEachPass = _b.betweenEachPass, afterEachPass = _b.afterEachPass; | ||
var manager = new context_1.EffectManager({ include: include }); | ||
var element = (React.createElement(context_1.EffectContext.Provider, { value: manager }, decorate(app))); | ||
return (function perform() { | ||
return (function perform(index) { | ||
if (index === void 0) { index = 0; } | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
var result; | ||
var start, result, duration, resolveStart, renderDuration, resolveDuration; | ||
return tslib_1.__generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
start = Date.now(); | ||
result = renderFunction(element); | ||
if (!manager.finished) return [3 /*break*/, 4]; | ||
return [4 /*yield*/, manager.afterEachPass()]; | ||
duration = Date.now() - start; | ||
return [4 /*yield*/, manager.afterEachPass({ | ||
index: index, | ||
finished: manager.finished, | ||
renderDuration: duration, | ||
resolveDuration: 0, | ||
})]; | ||
case 1: | ||
_a.sent(); | ||
if (!afterEachPass) return [3 /*break*/, 3]; | ||
return [4 /*yield*/, afterEachPass()]; | ||
return [4 /*yield*/, afterEachPass({ | ||
index: index, | ||
finished: manager.finished, | ||
renderDuration: duration, | ||
resolveDuration: 0, | ||
})]; | ||
case 2: | ||
@@ -29,22 +43,50 @@ _a.sent(); | ||
case 3: return [2 /*return*/, result]; | ||
case 4: return [4 /*yield*/, manager.resolve()]; | ||
case 4: | ||
resolveStart = Date.now(); | ||
renderDuration = resolveStart - start; | ||
return [4 /*yield*/, manager.resolve()]; | ||
case 5: | ||
_a.sent(); | ||
return [4 /*yield*/, manager.betweenEachPass()]; | ||
resolveDuration = Date.now() - resolveStart; | ||
return [4 /*yield*/, manager.betweenEachPass({ | ||
index: index, | ||
finished: false, | ||
renderDuration: renderDuration, | ||
resolveDuration: resolveDuration, | ||
})]; | ||
case 6: | ||
_a.sent(); | ||
if (!betweenEachPass) return [3 /*break*/, 8]; | ||
return [4 /*yield*/, betweenEachPass()]; | ||
return [4 /*yield*/, betweenEachPass({ | ||
index: index, | ||
finished: false, | ||
renderDuration: renderDuration, | ||
resolveDuration: resolveDuration, | ||
})]; | ||
case 7: | ||
_a.sent(); | ||
_a.label = 8; | ||
case 8: return [4 /*yield*/, manager.afterEachPass()]; | ||
case 8: return [4 /*yield*/, manager.afterEachPass({ | ||
index: index, | ||
finished: false, | ||
renderDuration: renderDuration, | ||
resolveDuration: resolveDuration, | ||
})]; | ||
case 9: | ||
_a.sent(); | ||
if (!afterEachPass) return [3 /*break*/, 11]; | ||
return [4 /*yield*/, afterEachPass()]; | ||
return [4 /*yield*/, afterEachPass({ | ||
index: index, | ||
finished: false, | ||
renderDuration: renderDuration, | ||
resolveDuration: resolveDuration, | ||
})]; | ||
case 10: | ||
_a.sent(); | ||
_a.label = 11; | ||
case 11: return [2 /*return*/, perform()]; | ||
case 11: | ||
if (index + 1 >= maxPasses) { | ||
return [2 /*return*/, result]; | ||
} | ||
return [2 /*return*/, perform(index + 1)]; | ||
} | ||
@@ -51,0 +93,0 @@ }); |
@@ -0,5 +1,11 @@ | ||
export interface Pass { | ||
index: number; | ||
finished: boolean; | ||
renderDuration: number; | ||
resolveDuration: number; | ||
} | ||
export interface EffectKind { | ||
readonly id: symbol; | ||
betweenEachPass?(): any; | ||
afterEachPass?(): any; | ||
betweenEachPass?(pass: Pass): any; | ||
afterEachPass?(pass: Pass): any; | ||
} |
{ | ||
"name": "@shopify/react-effect", | ||
"version": "2.0.1", | ||
"version": "2.1.0", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "description": "A component and set of utilities for performing effects within a universal React app", |
@@ -72,6 +72,8 @@ # `@shopify/react-effect` | ||
- `betweenEachPass`: a function that is called after a pass of your tree that did not "finish" (that is, there were still promises that got collected). This function can return a promise, and it will be waited on before continuing. | ||
- `maxPasses`: a number that limits the number of render/ resolve cycles `extract` is allowed to perform. This option defaults to `5`. | ||
- `afterEachPass`: a function that is called after each pass of your tree, regardless of whether traversal is "finished". This function can return a promise, and it will be waited on before continuing. | ||
- `betweenEachPass`: a function that is called after a pass of your tree that did not "finish" (that is, there were still promises that got collected). This function can return a promise, and it will be waited on before continuing. It is called with a single argument: a `Pass` object, which contains the `index`, `finished`, `renderDuration` and `resolveDuration` of the just-completed pass. | ||
- `afterEachPass`: a function that is called after each pass of your tree, regardless of whether traversal is "finished". This function can return a promise, and it will be waited on before continuing. This function is called with the same argument as the `betweenEachPass` option. | ||
- `decorate`: a function that takes the root React element in your tree and returns a new tree to use. You can use this to wrap your application in context providers that only your server render requires. | ||
@@ -78,0 +80,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
19912
283
144