@umijs/runtime
Advanced tools
Comparing version 3.0.0-beta.11 to 3.0.0-beta.12
@@ -19,2 +19,38 @@ export { Link, MemoryRouter, NavLink, Prompt, Redirect, Route, Router, Switch, matchPath, useHistory, useLocation, useParams, useRouteMatch, withRouter } from 'react-router-dom'; | ||
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { | ||
try { | ||
var info = gen[key](arg); | ||
var value = info.value; | ||
} catch (error) { | ||
reject(error); | ||
return; | ||
} | ||
if (info.done) { | ||
resolve(value); | ||
} else { | ||
Promise.resolve(value).then(_next, _throw); | ||
} | ||
} | ||
function _asyncToGenerator(fn) { | ||
return function () { | ||
var self = this, | ||
args = arguments; | ||
return new Promise(function (resolve, reject) { | ||
var gen = fn.apply(self, args); | ||
function _next(value) { | ||
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); | ||
} | ||
function _throw(err) { | ||
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); | ||
} | ||
_next(undefined); | ||
}); | ||
}; | ||
} | ||
function _classCallCheck(instance, Constructor) { | ||
@@ -135,2 +171,6 @@ if (!(instance instanceof Constructor)) { | ||
function isPromiseLike(obj) { | ||
return !!obj && _typeof(obj) === 'object' && typeof obj.then === 'function'; | ||
} | ||
var Plugin = | ||
@@ -142,3 +182,2 @@ /*#__PURE__*/ | ||
this.validKeys = void 0; | ||
this.hooks = {}; | ||
@@ -214,3 +253,4 @@ this.validKeys = (opts === null || opts === void 0 ? void 0 : opts.validKeys) || []; | ||
initialValue = _ref2.initialValue, | ||
args = _ref2.args; | ||
args = _ref2.args, | ||
async = _ref2.async; | ||
var hooks = this.getHooks(key) || []; | ||
@@ -224,13 +264,93 @@ | ||
case ApplyPluginsType.modify: | ||
return hooks.reduce(function (memo, hook) { | ||
assert(typeof hook === 'function' || _typeof(hook) === 'object', "applyPlugins failed, all hooks for key ".concat(key, " must be function or plain object.")); | ||
if (async) { | ||
return hooks.reduce( | ||
/*#__PURE__*/ | ||
function () { | ||
var _ref3 = _asyncToGenerator( | ||
/*#__PURE__*/ | ||
regeneratorRuntime.mark(function _callee(memo, hook) { | ||
var ret; | ||
return regeneratorRuntime.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
assert(typeof hook === 'function' || _typeof(hook) === 'object' || isPromiseLike(hook), "applyPlugins failed, all hooks for key ".concat(key, " must be function, plain object or Promise.")); | ||
if (typeof hook === 'function') { | ||
return hook(memo, args); | ||
} else { | ||
// TODO: deepmerge? | ||
return _objectSpread2({}, memo, {}, hook); | ||
} | ||
}, initialValue); | ||
if (!isPromiseLike(memo)) { | ||
_context.next = 5; | ||
break; | ||
} | ||
_context.next = 4; | ||
return memo; | ||
case 4: | ||
memo = _context.sent; | ||
case 5: | ||
if (!(typeof hook === 'function')) { | ||
_context.next = 16; | ||
break; | ||
} | ||
ret = hook(memo, args); | ||
if (!isPromiseLike(ret)) { | ||
_context.next = 13; | ||
break; | ||
} | ||
_context.next = 10; | ||
return ret; | ||
case 10: | ||
return _context.abrupt("return", _context.sent); | ||
case 13: | ||
return _context.abrupt("return", ret); | ||
case 14: | ||
_context.next = 21; | ||
break; | ||
case 16: | ||
if (!isPromiseLike(hook)) { | ||
_context.next = 20; | ||
break; | ||
} | ||
_context.next = 19; | ||
return hook; | ||
case 19: | ||
hook = _context.sent; | ||
case 20: | ||
return _context.abrupt("return", _objectSpread2({}, memo, {}, hook)); | ||
case 21: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee); | ||
})); | ||
return function (_x, _x2) { | ||
return _ref3.apply(this, arguments); | ||
}; | ||
}(), isPromiseLike(initialValue) ? initialValue : Promise.resolve(initialValue)); | ||
} else { | ||
return hooks.reduce(function (memo, hook) { | ||
assert(typeof hook === 'function' || _typeof(hook) === 'object', "applyPlugins failed, all hooks for key ".concat(key, " must be function or plain object.")); | ||
if (typeof hook === 'function') { | ||
return hook(memo, args); | ||
} else { | ||
// TODO: deepmerge? | ||
return _objectSpread2({}, memo, {}, hook); | ||
} | ||
}, initialValue); | ||
} | ||
case ApplyPluginsType.event: | ||
@@ -237,0 +357,0 @@ return hooks.forEach(function (hook) { |
@@ -23,2 +23,38 @@ 'use strict'; | ||
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { | ||
try { | ||
var info = gen[key](arg); | ||
var value = info.value; | ||
} catch (error) { | ||
reject(error); | ||
return; | ||
} | ||
if (info.done) { | ||
resolve(value); | ||
} else { | ||
Promise.resolve(value).then(_next, _throw); | ||
} | ||
} | ||
function _asyncToGenerator(fn) { | ||
return function () { | ||
var self = this, | ||
args = arguments; | ||
return new Promise(function (resolve, reject) { | ||
var gen = fn.apply(self, args); | ||
function _next(value) { | ||
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); | ||
} | ||
function _throw(err) { | ||
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); | ||
} | ||
_next(undefined); | ||
}); | ||
}; | ||
} | ||
function _classCallCheck(instance, Constructor) { | ||
@@ -137,2 +173,6 @@ if (!(instance instanceof Constructor)) { | ||
function isPromiseLike(obj) { | ||
return !!obj && _typeof(obj) === 'object' && typeof obj.then === 'function'; | ||
} | ||
var Plugin = | ||
@@ -144,3 +184,2 @@ /*#__PURE__*/ | ||
this.validKeys = void 0; | ||
this.hooks = {}; | ||
@@ -216,3 +255,4 @@ this.validKeys = (opts === null || opts === void 0 ? void 0 : opts.validKeys) || []; | ||
initialValue = _ref2.initialValue, | ||
args = _ref2.args; | ||
args = _ref2.args, | ||
async = _ref2.async; | ||
var hooks = this.getHooks(key) || []; | ||
@@ -226,13 +266,93 @@ | ||
case exports.ApplyPluginsType.modify: | ||
return hooks.reduce(function (memo, hook) { | ||
assert(typeof hook === 'function' || _typeof(hook) === 'object', "applyPlugins failed, all hooks for key ".concat(key, " must be function or plain object.")); | ||
if (async) { | ||
return hooks.reduce( | ||
/*#__PURE__*/ | ||
function () { | ||
var _ref3 = _asyncToGenerator( | ||
/*#__PURE__*/ | ||
regeneratorRuntime.mark(function _callee(memo, hook) { | ||
var ret; | ||
return regeneratorRuntime.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
assert(typeof hook === 'function' || _typeof(hook) === 'object' || isPromiseLike(hook), "applyPlugins failed, all hooks for key ".concat(key, " must be function, plain object or Promise.")); | ||
if (typeof hook === 'function') { | ||
return hook(memo, args); | ||
} else { | ||
// TODO: deepmerge? | ||
return _objectSpread2({}, memo, {}, hook); | ||
} | ||
}, initialValue); | ||
if (!isPromiseLike(memo)) { | ||
_context.next = 5; | ||
break; | ||
} | ||
_context.next = 4; | ||
return memo; | ||
case 4: | ||
memo = _context.sent; | ||
case 5: | ||
if (!(typeof hook === 'function')) { | ||
_context.next = 16; | ||
break; | ||
} | ||
ret = hook(memo, args); | ||
if (!isPromiseLike(ret)) { | ||
_context.next = 13; | ||
break; | ||
} | ||
_context.next = 10; | ||
return ret; | ||
case 10: | ||
return _context.abrupt("return", _context.sent); | ||
case 13: | ||
return _context.abrupt("return", ret); | ||
case 14: | ||
_context.next = 21; | ||
break; | ||
case 16: | ||
if (!isPromiseLike(hook)) { | ||
_context.next = 20; | ||
break; | ||
} | ||
_context.next = 19; | ||
return hook; | ||
case 19: | ||
hook = _context.sent; | ||
case 20: | ||
return _context.abrupt("return", _objectSpread2({}, memo, {}, hook)); | ||
case 21: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee); | ||
})); | ||
return function (_x, _x2) { | ||
return _ref3.apply(this, arguments); | ||
}; | ||
}(), isPromiseLike(initialValue) ? initialValue : Promise.resolve(initialValue)); | ||
} else { | ||
return hooks.reduce(function (memo, hook) { | ||
assert(typeof hook === 'function' || _typeof(hook) === 'object', "applyPlugins failed, all hooks for key ".concat(key, " must be function or plain object.")); | ||
if (typeof hook === 'function') { | ||
return hook(memo, args); | ||
} else { | ||
// TODO: deepmerge? | ||
return _objectSpread2({}, memo, {}, hook); | ||
} | ||
}, initialValue); | ||
} | ||
case exports.ApplyPluginsType.event: | ||
@@ -239,0 +359,0 @@ return hooks.forEach(function (hook) { |
@@ -21,3 +21,3 @@ export declare enum ApplyPluginsType { | ||
getHooks(keyWithDot: string): any; | ||
applyPlugins({ key, type, initialValue, args, }: { | ||
applyPlugins({ key, type, initialValue, args, async, }: { | ||
key: string; | ||
@@ -27,4 +27,5 @@ type: ApplyPluginsType; | ||
args?: object; | ||
async?: boolean; | ||
}): any; | ||
} | ||
export {}; |
{ | ||
"name": "@umijs/runtime", | ||
"version": "3.0.0-beta.11", | ||
"version": "3.0.0-beta.12", | ||
"description": "@umijs/runtime", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -143,2 +143,49 @@ import Plugin, { ApplyPluginsType } from './Plugin'; | ||
test('applyPlugins modify support Promise', async () => { | ||
const p = new Plugin({ | ||
validKeys: ['foo'], | ||
}); | ||
p.register({ | ||
apply: { | ||
async foo(memo: object, args: object) { | ||
return { ...memo, a: 1, ...args }; | ||
}, | ||
}, | ||
path: '/foo1.js', | ||
}); | ||
p.register({ | ||
apply: { | ||
foo: Promise.resolve({ | ||
b: 1, | ||
c: 1, | ||
}), | ||
}, | ||
path: '/foo3.js', | ||
}); | ||
p.register({ | ||
apply: { | ||
foo: { | ||
d: 1, | ||
}, | ||
}, | ||
path: '/foo3.js', | ||
}); | ||
expect( | ||
await p.applyPlugins({ | ||
key: 'foo', | ||
type: ApplyPluginsType.modify, | ||
async: true, | ||
args: { | ||
e: 1, | ||
}, | ||
}), | ||
).toEqual({ | ||
a: 1, | ||
b: 1, | ||
c: 1, | ||
d: 1, | ||
e: 1, | ||
}); | ||
}); | ||
test('applyPlugins event', () => { | ||
@@ -145,0 +192,0 @@ let count: number; |
@@ -26,2 +26,6 @@ import { assert } from '../utils'; | ||
function isPromiseLike(obj: any) { | ||
return !!obj && typeof obj === 'object' && typeof obj.then === 'function'; | ||
} | ||
export default class Plugin { | ||
@@ -76,2 +80,3 @@ validKeys: string[]; | ||
args, | ||
async, | ||
}: { | ||
@@ -82,2 +87,3 @@ key: string; | ||
args?: object; | ||
async?: boolean; | ||
}) { | ||
@@ -95,14 +101,46 @@ const hooks = this.getHooks(key) || []; | ||
case ApplyPluginsType.modify: | ||
return hooks.reduce((memo: any, hook: Function | object) => { | ||
assert( | ||
typeof hook === 'function' || typeof hook === 'object', | ||
`applyPlugins failed, all hooks for key ${key} must be function or plain object.`, | ||
if (async) { | ||
return hooks.reduce( | ||
async (memo: any, hook: Function | Promise<any> | object) => { | ||
assert( | ||
typeof hook === 'function' || | ||
typeof hook === 'object' || | ||
isPromiseLike(hook), | ||
`applyPlugins failed, all hooks for key ${key} must be function, plain object or Promise.`, | ||
); | ||
if (isPromiseLike(memo)) { | ||
memo = await memo; | ||
} | ||
if (typeof hook === 'function') { | ||
const ret = hook(memo, args); | ||
if (isPromiseLike(ret)) { | ||
return await ret; | ||
} else { | ||
return ret; | ||
} | ||
} else { | ||
if (isPromiseLike(hook)) { | ||
hook = await hook; | ||
} | ||
return { ...memo, ...hook }; | ||
} | ||
}, | ||
isPromiseLike(initialValue) | ||
? initialValue | ||
: Promise.resolve(initialValue), | ||
); | ||
if (typeof hook === 'function') { | ||
return hook(memo, args); | ||
} else { | ||
// TODO: deepmerge? | ||
return { ...memo, ...hook }; | ||
} | ||
}, initialValue); | ||
} else { | ||
return hooks.reduce((memo: any, hook: Function | object) => { | ||
assert( | ||
typeof hook === 'function' || typeof hook === 'object', | ||
`applyPlugins failed, all hooks for key ${key} must be function or plain object.`, | ||
); | ||
if (typeof hook === 'function') { | ||
return hook(memo, args); | ||
} else { | ||
// TODO: deepmerge? | ||
return { ...memo, ...hook }; | ||
} | ||
}, initialValue); | ||
} | ||
@@ -109,0 +147,0 @@ case ApplyPluginsType.event: |
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
36782
1224