@wendellhu/redi
Advanced tools
Comparing version 0.16.2 to 0.17.0
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('react'), require('@wendellhu/redi'), require('rxjs')) : | ||
typeof define === 'function' && define.amd ? define(['exports', 'react', '@wendellhu/redi', 'rxjs'], factory) : | ||
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global["@wendellhu/redi/react-bindings"] = {}, global.React, global["@wendellhu/redi"], global.rxjs)); | ||
})(this, (function (exports, React, redi, rxjs) { 'use strict'; | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@wendellhu/redi'), require('react'), require('rxjs')) : | ||
typeof define === 'function' && define.amd ? define(['exports', '@wendellhu/redi', 'react', 'rxjs'], factory) : | ||
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global["@wendellhu/redi/react-bindings"] = {}, global["@wendellhu/redi"], global.React, global.rxjs)); | ||
})(this, (function (exports, redi, React, rxjs) { 'use strict'; | ||
@@ -31,6 +31,8 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } | ||
var __REDI_CONTEXT_LOCK__ = 'REDI_CONTEXT_LOCK'; | ||
// eslint-disable-next-line node/prefer-global/process | ||
var isNode = typeof process !== 'undefined' && process.versions != null && process.versions.node != null; | ||
var globalObject = (typeof globalThis !== 'undefined' && globalThis) || | ||
(typeof window !== 'undefined' && window) || | ||
(typeof global !== 'undefined' && global); | ||
var globalObject = (typeof globalThis !== 'undefined' && globalThis) | ||
|| (typeof window !== 'undefined' && window) | ||
// eslint-disable-next-line no-restricted-globals | ||
|| (typeof global !== 'undefined' && global); | ||
if (!globalObject[__REDI_CONTEXT_LOCK__]) { | ||
@@ -49,36 +51,2 @@ globalObject[__REDI_CONTEXT_LOCK__] = true; | ||
var __extends$1 = (undefined && undefined.__extends) || (function () { | ||
var extendStatics = function (d, b) { | ||
extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; | ||
return extendStatics(d, b); | ||
}; | ||
return function (d, b) { | ||
if (typeof b !== "function" && b !== null) | ||
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
var HooksNotInRediContextError = /** @class */ (function (_super) { | ||
__extends$1(HooksNotInRediContextError, _super); | ||
function HooksNotInRediContextError() { | ||
return _super.call(this, 'Using dependency injection outside of a RediContext.') || this; | ||
} | ||
return HooksNotInRediContextError; | ||
}(redi.RediError)); | ||
function useInjector() { | ||
var injectionContext = React__namespace.useContext(RediContext); | ||
if (!injectionContext.injector) { | ||
throw new HooksNotInRediContextError(); | ||
} | ||
return injectionContext.injector; | ||
} | ||
function useDependency(id, quantityOrLookUp, lookUp) { | ||
var injector = useInjector(); | ||
return React__namespace.useMemo(function () { return injector.get(id, quantityOrLookUp, lookUp); }, [id, quantityOrLookUp, lookUp]); | ||
} | ||
var __assign = (undefined && undefined.__assign) || function () { | ||
@@ -117,3 +85,3 @@ __assign = Object.assign || function(t) { | ||
* @param injector | ||
* @returns | ||
* @returns A component type that can be rendered. | ||
*/ | ||
@@ -133,3 +101,3 @@ function connectInjector(Comp, injector) { | ||
var __extends = (undefined && undefined.__extends) || (function () { | ||
var __extends$1 = (undefined && undefined.__extends) || (function () { | ||
var extendStatics = function (d, b) { | ||
@@ -150,7 +118,7 @@ extendStatics = Object.setPrototypeOf || | ||
var ClassComponentNotInRediContextError = /** @class */ (function (_super) { | ||
__extends(ClassComponentNotInRediContextError, _super); | ||
__extends$1(ClassComponentNotInRediContextError, _super); | ||
function ClassComponentNotInRediContextError(component) { | ||
return _super.call(this, "You should make \"RediContext\" as ".concat(component.constructor.name, "'s default context type. ") + | ||
'If you want to use multiple context, please check this on React doc site. ' + | ||
'https://reactjs.org/docs/context.html#classcontexttype') || this; | ||
return _super.call(this, "You should make \"RediContext\" as ".concat(component.constructor.name, "'s default context type. ") | ||
+ 'If you want to use multiple context, please check this on React doc site. ' | ||
+ 'https://reactjs.org/docs/context.html#classcontexttype') || this; | ||
} | ||
@@ -176,2 +144,36 @@ return ClassComponentNotInRediContextError; | ||
var __extends = (undefined && undefined.__extends) || (function () { | ||
var extendStatics = function (d, b) { | ||
extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; | ||
return extendStatics(d, b); | ||
}; | ||
return function (d, b) { | ||
if (typeof b !== "function" && b !== null) | ||
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
var HooksNotInRediContextError = /** @class */ (function (_super) { | ||
__extends(HooksNotInRediContextError, _super); | ||
function HooksNotInRediContextError() { | ||
return _super.call(this, 'Using dependency injection outside of a RediContext.') || this; | ||
} | ||
return HooksNotInRediContextError; | ||
}(redi.RediError)); | ||
function useInjector() { | ||
var injectionContext = React__namespace.useContext(RediContext); | ||
if (!injectionContext.injector) { | ||
throw new HooksNotInRediContextError(); | ||
} | ||
return injectionContext.injector; | ||
} | ||
function useDependency(id, quantityOrLookUp, lookUp) { | ||
var injector = useInjector(); | ||
return React__namespace.useMemo(function () { return injector.get(id, quantityOrLookUp, lookUp); }, [id, quantityOrLookUp, lookUp]); | ||
} | ||
var __spreadArray = (undefined && undefined.__spreadArray) || function (to, from, pack) { | ||
@@ -222,11 +224,10 @@ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { | ||
* @param deps A dependency array to decide if we should re-subscribe when the `observable` is a function. | ||
* @returns | ||
* @returns Value or null. | ||
*/ | ||
function useObservable(observable, defaultValue, shouldHaveSyncValue, deps) { | ||
if (typeof observable === 'function' && !deps) { | ||
throw new redi.RediError("Expected deps to be provided when observable is a function!"); | ||
throw new redi.RediError('Expected deps to be provided when observable is a function!'); | ||
} | ||
var observableRef = React.useRef(null); | ||
var initializedRef = React.useRef(false); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
var destObservable = React.useMemo(function () { return observable; }, __spreadArray([], (typeof deps !== 'undefined' ? deps : [observable]), true)); | ||
@@ -233,0 +234,0 @@ // This state is only for trigger React to re-render. We do not use `setValue` directly because it may cause |
324
dist/redi.js
@@ -47,3 +47,3 @@ (function (global, factory) { | ||
function isAsyncHook(thing) { | ||
if (thing && thing['__symbol'] === AsyncHookSymbol) { | ||
if (thing && thing.__symbol === AsyncHookSymbol) { | ||
// FIXME@wzhudev: should not be undefined but a symbol here | ||
@@ -63,14 +63,2 @@ return true; | ||
exports.Quantity = void 0; | ||
(function (Quantity) { | ||
Quantity["MANY"] = "many"; | ||
Quantity["OPTIONAL"] = "optional"; | ||
Quantity["REQUIRED"] = "required"; | ||
})(exports.Quantity || (exports.Quantity = {})); | ||
exports.LookUp = void 0; | ||
(function (LookUp) { | ||
LookUp["SELF"] = "self"; | ||
LookUp["SKIP_SELF"] = "skipSelf"; | ||
})(exports.LookUp || (exports.LookUp = {})); | ||
var __extends$4 = (undefined && undefined.__extends) || (function () { | ||
@@ -99,2 +87,14 @@ var extendStatics = function (d, b) { | ||
exports.Quantity = void 0; | ||
(function (Quantity) { | ||
Quantity["MANY"] = "many"; | ||
Quantity["OPTIONAL"] = "optional"; | ||
Quantity["REQUIRED"] = "required"; | ||
})(exports.Quantity || (exports.Quantity = {})); | ||
exports.LookUp = void 0; | ||
(function (LookUp) { | ||
LookUp["SELF"] = "self"; | ||
LookUp["SKIP_SELF"] = "skipSelf"; | ||
})(exports.LookUp || (exports.LookUp = {})); | ||
var __extends$3 = (undefined && undefined.__extends) || (function () { | ||
@@ -209,2 +209,25 @@ var extendStatics = function (d, b) { | ||
function changeLookup(target, index, lookUp) { | ||
var descriptor = getDependencyByIndex(target, index); | ||
descriptor.lookUp = lookUp; | ||
} | ||
function lookupDecoratorFactoryProducer(lookUp) { | ||
return function DecoratorFactory() { | ||
if (this instanceof DecoratorFactory) { | ||
return this; | ||
} | ||
return function (target, _key, index) { | ||
changeLookup(target, index, lookUp); | ||
}; | ||
}; | ||
} | ||
/** | ||
* when resolving this dependency, skip the current injector | ||
*/ | ||
var SkipSelf = lookupDecoratorFactoryProducer(exports.LookUp.SKIP_SELF); | ||
/** | ||
* when resolving this dependency, only search the current injector | ||
*/ | ||
var Self = lookupDecoratorFactoryProducer(exports.LookUp.SELF); | ||
var __extends$2 = (undefined && undefined.__extends) || (function () { | ||
@@ -230,3 +253,3 @@ var extendStatics = function (d, b) { | ||
var msg = "Expect \"".concat(quantity, "\" dependency items for id \"").concat(prettyPrintIdentifier(id), "\" but get ").concat(actual, "."); | ||
if (actual == 0) { | ||
if (actual === 0) { | ||
msg += ' Did you forget to register it?'; | ||
@@ -245,4 +268,4 @@ } | ||
function checkQuantity(id, quantity, length) { | ||
if ((quantity === exports.Quantity.OPTIONAL && length > 1) || | ||
(quantity === exports.Quantity.REQUIRED && length !== 1)) { | ||
if ((quantity === exports.Quantity.OPTIONAL && length > 1) | ||
|| (quantity === exports.Quantity.REQUIRED && length !== 1)) { | ||
throw new QuantityCheckError(id, quantity, length); | ||
@@ -285,2 +308,87 @@ } | ||
function changeToSelf(target, index, withNew) { | ||
var descriptor = getDependencyByIndex(target, index); | ||
descriptor.withNew = withNew; | ||
} | ||
function withNewDecoratorFactoryProducer(withNew) { | ||
return function DecoratorFactory() { | ||
if (this instanceof DecoratorFactory) { | ||
return this; | ||
} | ||
return function (target, _key, index) { | ||
changeToSelf(target, index, withNew); | ||
}; | ||
}; | ||
} | ||
/** | ||
* Always initialize a new instance of that dependency instead of getting the cached instance from the injector. | ||
*/ | ||
var WithNew = withNewDecoratorFactoryProducer(true); | ||
function normalizeFactoryDeps(deps, startIndex) { | ||
if (startIndex === void 0) { startIndex = 0; } | ||
if (!deps) { | ||
return []; | ||
} | ||
return deps.map(function (dep, index) { | ||
index += startIndex; | ||
if (!Array.isArray(dep)) { | ||
return { | ||
paramIndex: index, | ||
identifier: dep, | ||
quantity: exports.Quantity.REQUIRED, | ||
withNew: false, | ||
}; | ||
} | ||
var modifiers = dep.slice(0, dep.length - 1); | ||
var identifier = dep[dep.length - 1]; | ||
var lookUp; | ||
var quantity = exports.Quantity.REQUIRED; | ||
var withNew = false; | ||
modifiers.forEach(function (modifier) { | ||
if (modifier instanceof Self) { | ||
lookUp = exports.LookUp.SELF; | ||
} | ||
else if (modifier instanceof SkipSelf) { | ||
lookUp = exports.LookUp.SKIP_SELF; | ||
} | ||
else if (modifier instanceof Optional) { | ||
quantity = exports.Quantity.OPTIONAL; | ||
} | ||
else if (modifier instanceof Many) { | ||
quantity = exports.Quantity.MANY; | ||
} | ||
else if (modifier instanceof WithNew) { | ||
withNew = true; | ||
} | ||
else { | ||
throw new RediError("unknown dep modifier ".concat(modifier, ".")); | ||
} | ||
}); | ||
return { | ||
paramIndex: index, | ||
identifier: identifier, | ||
quantity: quantity, | ||
lookUp: lookUp, | ||
withNew: withNew, | ||
}; | ||
}); | ||
} | ||
/** | ||
* Register dependencies on a class. | ||
* | ||
* @param registerTarget The target constructor | ||
* @param deps Dependencies | ||
* @param startIndex The start index of the dependencies. Default is 0. When you want to set dependencies on a class | ||
* that has custom parameters, you should set `startIndex` to the count of these custom parameters. | ||
*/ | ||
function setDependencies(registerTarget, deps, startIndex) { | ||
if (startIndex === void 0) { startIndex = 0; } | ||
var normalizedDescriptors = normalizeFactoryDeps(deps, startIndex); | ||
normalizedDescriptors.forEach(function (descriptor) { | ||
setDependency(registerTarget, descriptor.identifier, descriptor.paramIndex, descriptor.quantity, descriptor.lookUp); | ||
}); | ||
} | ||
function forwardRef(wrapper) { | ||
@@ -470,102 +578,11 @@ return { | ||
function changeLookup(target, index, lookUp) { | ||
var descriptor = getDependencyByIndex(target, index); | ||
descriptor.lookUp = lookUp; | ||
} | ||
function lookupDecoratorFactoryProducer(lookUp) { | ||
return function DecoratorFactory() { | ||
if (this instanceof DecoratorFactory) { | ||
return this; | ||
} | ||
return function (target, _key, index) { | ||
changeLookup(target, index, lookUp); | ||
}; | ||
}; | ||
} | ||
/** | ||
* when resolving this dependency, skip the current injector | ||
*/ | ||
var SkipSelf = lookupDecoratorFactoryProducer(exports.LookUp.SKIP_SELF); | ||
/** | ||
* when resolving this dependency, only search the current injector | ||
*/ | ||
var Self = lookupDecoratorFactoryProducer(exports.LookUp.SELF); | ||
function changeToSelf(target, index, withNew) { | ||
var descriptor = getDependencyByIndex(target, index); | ||
descriptor.withNew = withNew; | ||
} | ||
function withNewDecoratorFactoryProducer(withNew) { | ||
return function DecoratorFactory() { | ||
if (this instanceof DecoratorFactory) { | ||
return this; | ||
} | ||
return function (target, _key, index) { | ||
changeToSelf(target, index, withNew); | ||
}; | ||
}; | ||
} | ||
/** | ||
* Always initialize a new instance of that dependency instead of getting the cached instance from the injector. | ||
*/ | ||
var WithNew = withNewDecoratorFactoryProducer(true); | ||
function normalizeFactoryDeps(deps, startIndex) { | ||
if (startIndex === void 0) { startIndex = 0; } | ||
if (!deps) { | ||
return []; | ||
} | ||
return deps.map(function (dep, index) { | ||
index += startIndex; | ||
if (!Array.isArray(dep)) { | ||
return { | ||
paramIndex: index, | ||
identifier: dep, | ||
quantity: exports.Quantity.REQUIRED, | ||
withNew: false, | ||
}; | ||
} | ||
var modifiers = dep.slice(0, dep.length - 1); | ||
var identifier = dep[dep.length - 1]; | ||
var lookUp = undefined; | ||
var quantity = exports.Quantity.REQUIRED; | ||
var withNew = false; | ||
modifiers.forEach(function (modifier) { | ||
if (modifier instanceof Self) { | ||
lookUp = exports.LookUp.SELF; | ||
} | ||
else if (modifier instanceof SkipSelf) { | ||
lookUp = exports.LookUp.SKIP_SELF; | ||
} | ||
else if (modifier instanceof Optional) { | ||
quantity = exports.Quantity.OPTIONAL; | ||
} | ||
else if (modifier instanceof Many) { | ||
quantity = exports.Quantity.MANY; | ||
} | ||
else if (modifier instanceof WithNew) { | ||
withNew = true; | ||
} | ||
else { | ||
throw new RediError("unknown dep modifier ".concat(modifier, ".")); | ||
} | ||
}); | ||
return { | ||
paramIndex: index, | ||
identifier: identifier, | ||
quantity: quantity, | ||
lookUp: lookUp, | ||
withNew: withNew, | ||
}; | ||
}); | ||
} | ||
/** | ||
* this run the callback when CPU is idle. Will fallback to setTimeout if | ||
* the browser doesn't support requestIdleCallback | ||
*/ | ||
// eslint-disable-next-line import/no-mutable-exports | ||
var runWhenIdle; | ||
(function () { | ||
if (typeof requestIdleCallback !== 'undefined' && | ||
typeof cancelIdleCallback !== 'undefined') { | ||
if (typeof requestIdleCallback !== 'undefined' | ||
&& typeof cancelIdleCallback !== 'undefined') { | ||
// use native requestIdleCallback | ||
@@ -725,5 +742,2 @@ runWhenIdle = function (runner, timeout) { | ||
}(RediError)); | ||
/** | ||
* | ||
*/ | ||
var Injector = /** @class */ (function () { | ||
@@ -740,2 +754,3 @@ /** | ||
this.resolutionOngoing = 0; | ||
this.disposingCallbacks = new Set(); | ||
this.disposed = false; | ||
@@ -749,2 +764,16 @@ this.dependencyCollection = new DependencyCollection(dependencies || []); | ||
/** | ||
* Add a callback function that will be triggered when the Injector is disposed. | ||
* Please note that when you callback is invoked, the injector is already disposed and | ||
* you will not be able to interact with this Injector any more. | ||
* | ||
* @param {() => void} callback The callback function that will be invoked when | ||
* the Injector is disposed. | ||
* @returns A disposable that will remove the callback. | ||
*/ | ||
Injector.prototype.onDispose = function (callback) { | ||
var _this = this; | ||
this.disposingCallbacks.add(callback); | ||
return { dispose: function () { return _this.disposingCallbacks.delete(callback); } }; | ||
}; | ||
/** | ||
* Create a child inject with a set of dependencies. | ||
@@ -768,4 +797,7 @@ * @param dependencies Dependencies that should be resolved by the newly created child injector. | ||
this.resolvedDependencyCollection.dispose(); | ||
// Detach itself from parent. | ||
this.deleteSelfFromParent(); | ||
this.disposed = true; | ||
this.disposingCallbacks.forEach(function (callback) { return callback(); }); | ||
this.disposingCallbacks.clear(); | ||
}; | ||
@@ -797,6 +829,6 @@ Injector.prototype.deleteSelfFromParent = function () { | ||
} | ||
else if (isAsyncDependencyItem(item) || | ||
isClassDependencyItem(item) || | ||
isValueDependencyItem(item) || | ||
isFactoryDependencyItem(item)) { | ||
else if (isAsyncDependencyItem(item) | ||
|| isClassDependencyItem(item) | ||
|| isValueDependencyItem(item) | ||
|| isFactoryDependencyItem(item)) { | ||
// Add dependency | ||
@@ -864,3 +896,3 @@ this.dependencyCollection.add(identifierOrCtor, item); | ||
return _this.has(id); | ||
} | ||
}, | ||
}; | ||
@@ -895,5 +927,5 @@ return cb.apply(void 0, __spreadArray([accessor], args, false)); | ||
var quantity = exports.Quantity.REQUIRED; | ||
if (quantityOrLookup === exports.Quantity.REQUIRED || | ||
quantityOrLookup === exports.Quantity.OPTIONAL || | ||
quantityOrLookup === exports.Quantity.MANY) { | ||
if (quantityOrLookup === exports.Quantity.REQUIRED | ||
|| quantityOrLookup === exports.Quantity.OPTIONAL | ||
|| quantityOrLookup === exports.Quantity.MANY) { | ||
quantity = quantityOrLookup; | ||
@@ -939,3 +971,3 @@ } | ||
return this._resolveClassImpl.apply(this, __spreadArray([{ | ||
useClass: ctor | ||
useClass: ctor, | ||
}], customArgs, false)); | ||
@@ -1029,5 +1061,5 @@ }; | ||
} | ||
var ctor = item.useClass; | ||
this.markNewResolution(ctor); | ||
var declaredDependencies = getDependencies(ctor) | ||
var Ctor = item.useClass; | ||
this.markNewResolution(Ctor); | ||
var declaredDependencies = getDependencies(Ctor) | ||
.sort(function (a, b) { return a.paramIndex - b.paramIndex; }) | ||
@@ -1045,3 +1077,3 @@ .map(function (descriptor) { return (__assign(__assign({}, descriptor), { identifier: normalizeForwardRef(descriptor.identifier) })); }); | ||
if (error instanceof DependencyNotFoundError || (error instanceof QuantityCheckError && error.actual === 0)) { | ||
throw new DependencyNotFoundForModuleError(ctor, dep.identifier, dep.paramIndex); | ||
throw new DependencyNotFoundForModuleError(Ctor, dep.identifier, dep.paramIndex); | ||
} | ||
@@ -1056,6 +1088,6 @@ throw error; | ||
if (args.length !== firstDependencyArgIndex) { | ||
console.warn("[redi]: Expect ".concat(firstDependencyArgIndex, " custom parameter(s) of ").concat(prettyPrintIdentifier(ctor), " but get ").concat(args.length, ".")); | ||
console.warn("[redi]: Expect ".concat(firstDependencyArgIndex, " custom parameter(s) of ").concat(prettyPrintIdentifier(Ctor), " but get ").concat(args.length, ".")); | ||
var delta = firstDependencyArgIndex - args.length; | ||
if (delta > 0) { | ||
args = __spreadArray(__spreadArray([], args, true), new Array(delta).fill(undefined), true); | ||
args = __spreadArray(__spreadArray([], args, true), (Array.from({ length: delta })).fill(undefined), true); | ||
} | ||
@@ -1066,3 +1098,3 @@ else { | ||
} | ||
var thing = new (ctor.bind.apply(ctor, __spreadArray(__spreadArray([void 0], args, false), resolvedArgs, false)))(); | ||
var thing = new (Ctor.bind.apply(Ctor, __spreadArray(__spreadArray([void 0], args, false), resolvedArgs, false)))(); | ||
(_a = item === null || item === void 0 ? void 0 : item.onInstantiation) === null || _a === void 0 ? void 0 : _a.call(item, thing); | ||
@@ -1128,3 +1160,3 @@ this.markResolutionCompleted(); | ||
useClass: thing, | ||
onInstantiation: item.onInstantiation | ||
onInstantiation: item.onInstantiation, | ||
}); | ||
@@ -1143,4 +1175,4 @@ } | ||
var onSelf = function () { | ||
if (_this.dependencyCollection.has(id) && | ||
!_this.resolvedDependencyCollection.has(id)) { | ||
if (_this.dependencyCollection.has(id) | ||
&& !_this.resolvedDependencyCollection.has(id)) { | ||
return NotInstantiatedSymbol; | ||
@@ -1174,4 +1206,4 @@ } | ||
} | ||
if (this.resolvedDependencyCollection.has(id) || | ||
this.dependencyCollection.has(id)) { | ||
if (this.resolvedDependencyCollection.has(id) | ||
|| this.dependencyCollection.has(id)) { | ||
return onSelf(); | ||
@@ -1233,22 +1265,8 @@ } | ||
/** | ||
* Register dependencies on a class. | ||
* | ||
* @param registerTarget The target constructor | ||
* @param deps Dependencies | ||
* @param startIndex The start index of the dependencies. Default is 0. When you want to set dependencies on a class | ||
* that has custom parameters, you should set `startIndex` to the count of these custom parameters. | ||
*/ | ||
function setDependencies(registerTarget, deps, startIndex) { | ||
if (startIndex === void 0) { startIndex = 0; } | ||
var normalizedDescriptors = normalizeFactoryDeps(deps, startIndex); | ||
normalizedDescriptors.forEach(function (descriptor) { | ||
setDependency(registerTarget, descriptor.identifier, descriptor.paramIndex, descriptor.quantity, descriptor.lookUp); | ||
}); | ||
} | ||
var globalObject = (typeof globalThis !== 'undefined' && globalThis) || | ||
(typeof window !== 'undefined' && window) || | ||
(typeof global !== 'undefined' && global); | ||
var globalObject = (typeof globalThis !== 'undefined' && globalThis) | ||
|| (typeof window !== 'undefined' && window) | ||
// eslint-disable-next-line no-restricted-globals | ||
|| (typeof global !== 'undefined' && global); | ||
var __REDI_GLOBAL_LOCK__ = 'REDI_GLOBAL_LOCK'; | ||
// eslint-disable-next-line node/prefer-global/process | ||
var isNode = typeof process !== 'undefined' && process.versions != null && process.versions.node != null; | ||
@@ -1255,0 +1273,0 @@ if (globalObject[__REDI_GLOBAL_LOCK__]) { |
@@ -1,6 +0,7 @@ | ||
import { DependencyDescriptor } from './dependencyDescriptor'; | ||
import { DependencyIdentifier, IdentifierDecorator } from './dependencyIdentifier'; | ||
import { Ctor } from './dependencyItem'; | ||
import { LookUp, Quantity } from './types'; | ||
import type { DependencyDescriptor } from './dependencyDescriptor'; | ||
import type { DependencyIdentifier, IdentifierDecorator } from './dependencyIdentifier'; | ||
import type { Ctor } from './dependencyItem'; | ||
import type { LookUp } from './types'; | ||
import { RediError } from './error'; | ||
import { Quantity } from './types'; | ||
export declare const TARGET: unique symbol; | ||
@@ -7,0 +8,0 @@ export declare const DEPENDENCIES: unique symbol; |
@@ -18,4 +18,4 @@ var __extends = (this && this.__extends) || (function () { | ||
import { prettyPrintIdentifier } from './dependencyItem'; | ||
import { RediError } from './error'; | ||
import { Quantity } from './types'; | ||
import { RediError } from './error'; | ||
export var TARGET = Symbol('$$TARGET'); | ||
@@ -22,0 +22,0 @@ export var DEPENDENCIES = Symbol('$$DEPENDENCIES'); |
@@ -1,6 +0,6 @@ | ||
import { DependencyIdentifier } from './dependencyIdentifier'; | ||
import { Ctor, DependencyItem } from './dependencyItem'; | ||
import { IDisposable } from './dispose'; | ||
import type { DependencyIdentifier } from './dependencyIdentifier'; | ||
import type { Ctor, DependencyItem } from './dependencyItem'; | ||
import type { IDisposable } from './dispose'; | ||
import { RediError } from './error'; | ||
import { Quantity } from './types'; | ||
import { RediError } from './error'; | ||
export type DependencyPair<T> = [DependencyIdentifier<T>, DependencyItem<T>]; | ||
@@ -7,0 +7,0 @@ export type DependencyClass<T> = [Ctor<T>]; |
@@ -20,4 +20,4 @@ var __extends = (this && this.__extends) || (function () { | ||
import { isDisposable } from './dispose'; | ||
import { RediError } from './error'; | ||
import { Quantity } from './types'; | ||
import { RediError } from './error'; | ||
export function isBareClassDependency(thing) { | ||
@@ -24,0 +24,0 @@ return thing.length === 1; |
@@ -1,2 +0,2 @@ | ||
import { Ctor, FactoryDep } from './dependencyItem'; | ||
import type { Ctor, FactoryDep } from './dependencyItem'; | ||
/** | ||
@@ -3,0 +3,0 @@ * Register dependencies on a class. |
@@ -1,3 +0,3 @@ | ||
import { DependencyIdentifier } from './dependencyIdentifier'; | ||
import { FactoryDep } from './dependencyItem'; | ||
import type { DependencyIdentifier } from './dependencyIdentifier'; | ||
import type { FactoryDep } from './dependencyItem'; | ||
import { LookUp, Quantity } from './types'; | ||
@@ -4,0 +4,0 @@ export interface DependencyDescriptor<T> { |
@@ -23,3 +23,3 @@ import { Self, SkipSelf } from './dependencyLookUp'; | ||
var identifier = dep[dep.length - 1]; | ||
var lookUp = undefined; | ||
var lookUp; | ||
var quantity = Quantity.REQUIRED; | ||
@@ -26,0 +26,0 @@ var withNew = false; |
@@ -1,5 +0,5 @@ | ||
import { DependencyIdentifier, NormalizedDependencyIdentifier } from './dependencyIdentifier'; | ||
import { Ctor } from './dependencyItem'; | ||
import type { DependencyIdentifier, NormalizedDependencyIdentifier } from './dependencyIdentifier'; | ||
import type { Ctor } from './dependencyItem'; | ||
export interface ForwardRef<T> { | ||
unwrap(): Ctor<T>; | ||
unwrap: () => Ctor<T>; | ||
} | ||
@@ -6,0 +6,0 @@ export declare function forwardRef<T>(wrapper: () => Ctor<T>): ForwardRef<T>; |
@@ -1,5 +0,5 @@ | ||
import { Ctor } from './dependencyItem'; | ||
import { ForwardRef } from './dependencyForwardRef'; | ||
import type { ForwardRef } from './dependencyForwardRef'; | ||
import type { Ctor } from './dependencyItem'; | ||
export declare const IdentifierDecoratorSymbol: unique symbol; | ||
export type IdentifierDecorator<T> = { | ||
export interface IdentifierDecorator<T> { | ||
[IdentifierDecoratorSymbol]: true; | ||
@@ -10,7 +10,7 @@ (...args: any[]): void; | ||
*/ | ||
toString(): string; | ||
toString: () => string; | ||
type: T; | ||
}; | ||
} | ||
export declare function isIdentifierDecorator<T>(thing: any): thing is IdentifierDecorator<T>; | ||
export type DependencyIdentifier<T> = string | Ctor<T> | ForwardRef<T> | IdentifierDecorator<T>; | ||
export type NormalizedDependencyIdentifier<T> = Exclude<DependencyIdentifier<T>, ForwardRef<T>>; |
@@ -1,5 +0,5 @@ | ||
import { DependencyIdentifier } from './dependencyIdentifier'; | ||
import { Self, SkipSelf } from './dependencyLookUp'; | ||
import { Many, Optional } from './dependencyQuantity'; | ||
import { WithNew } from './dependencyWithNew'; | ||
import type { DependencyIdentifier } from './dependencyIdentifier'; | ||
import type { Self, SkipSelf } from './dependencyLookUp'; | ||
import type { Many, Optional } from './dependencyQuantity'; | ||
import type { WithNew } from './dependencyWithNew'; | ||
export interface Ctor<T> { | ||
@@ -47,3 +47,3 @@ new (...args: any[]): T; | ||
__symbol: typeof AsyncHookSymbol; | ||
whenReady(): Promise<T>; | ||
whenReady: () => Promise<T>; | ||
} | ||
@@ -50,0 +50,0 @@ export declare function isAsyncHook<T>(thing: unknown): thing is AsyncHook<T>; |
@@ -37,3 +37,3 @@ import { IdentifierDecoratorSymbol, } from './dependencyIdentifier'; | ||
export function isAsyncHook(thing) { | ||
if (thing && thing['__symbol'] === AsyncHookSymbol) { | ||
if (thing && thing.__symbol === AsyncHookSymbol) { | ||
// FIXME@wzhudev: should not be undefined but a symbol here | ||
@@ -40,0 +40,0 @@ return true; |
@@ -1,2 +0,2 @@ | ||
import { DependencyIdentifier } from './dependencyIdentifier'; | ||
import type { DependencyIdentifier } from './dependencyIdentifier'; | ||
import { RediError } from './error'; | ||
@@ -3,0 +3,0 @@ import { Quantity } from './types'; |
@@ -25,3 +25,3 @@ var __extends = (this && this.__extends) || (function () { | ||
var msg = "Expect \"".concat(quantity, "\" dependency items for id \"").concat(prettyPrintIdentifier(id), "\" but get ").concat(actual, "."); | ||
if (actual == 0) { | ||
if (actual === 0) { | ||
msg += ' Did you forget to register it?'; | ||
@@ -41,4 +41,4 @@ } | ||
export function checkQuantity(id, quantity, length) { | ||
if ((quantity === Quantity.OPTIONAL && length > 1) || | ||
(quantity === Quantity.REQUIRED && length !== 1)) { | ||
if ((quantity === Quantity.OPTIONAL && length > 1) | ||
|| (quantity === Quantity.REQUIRED && length !== 1)) { | ||
throw new QuantityCheckError(id, quantity, length); | ||
@@ -45,0 +45,0 @@ } |
export interface IDisposable { | ||
dispose(): void; | ||
dispose: () => void; | ||
} | ||
export declare function isDisposable(thing: unknown): thing is IDisposable; |
@@ -1,5 +0,5 @@ | ||
import { IDisposable } from './dispose'; | ||
import type { IDisposable } from './dispose'; | ||
export interface IdleDeadline { | ||
readonly didTimeout: boolean; | ||
timeRemaining(): DOMHighResTimeStamp; | ||
timeRemaining: () => DOMHighResTimeStamp; | ||
} | ||
@@ -6,0 +6,0 @@ export type DisposableCallback = () => void; |
@@ -5,6 +5,7 @@ /** | ||
*/ | ||
// eslint-disable-next-line import/no-mutable-exports | ||
export var runWhenIdle; | ||
(function () { | ||
if (typeof requestIdleCallback !== 'undefined' && | ||
typeof cancelIdleCallback !== 'undefined') { | ||
if (typeof requestIdleCallback !== 'undefined' | ||
&& typeof cancelIdleCallback !== 'undefined') { | ||
// use native requestIdleCallback | ||
@@ -11,0 +12,0 @@ runWhenIdle = function (runner, timeout) { |
@@ -1,3 +0,4 @@ | ||
import { Dependency, DependencyOrInstance } from './dependencyCollection'; | ||
import { DependencyIdentifier } from './dependencyIdentifier'; | ||
import type { Dependency, DependencyOrInstance } from './dependencyCollection'; | ||
import type { DependencyIdentifier } from './dependencyIdentifier'; | ||
import type { IDisposable } from './dispose'; | ||
import { LookUp, Quantity } from './types'; | ||
@@ -8,5 +9,2 @@ export interface IAccessor { | ||
} | ||
/** | ||
* | ||
*/ | ||
export declare class Injector { | ||
@@ -18,2 +16,3 @@ private readonly parent; | ||
private resolutionOngoing; | ||
private disposingCallbacks; | ||
private disposed; | ||
@@ -27,2 +26,12 @@ /** | ||
/** | ||
* Add a callback function that will be triggered when the Injector is disposed. | ||
* Please note that when you callback is invoked, the injector is already disposed and | ||
* you will not be able to interact with this Injector any more. | ||
* | ||
* @param {() => void} callback The callback function that will be invoked when | ||
* the Injector is disposed. | ||
* @returns A disposable that will remove the callback. | ||
*/ | ||
onDispose(callback: () => void): IDisposable; | ||
/** | ||
* Create a child inject with a set of dependencies. | ||
@@ -29,0 +38,0 @@ * @param dependencies Dependencies that should be resolved by the newly created child injector. |
@@ -37,6 +37,6 @@ var __extends = (this && this.__extends) || (function () { | ||
import { getDependencies } from './decorators'; | ||
import { DependencyCollection, DependencyNotFoundError, DependencyNotFoundForModuleError, ResolvedDependencyCollection, popupResolvingStack, pushResolvingStack, } from './dependencyCollection'; | ||
import { DependencyCollection, DependencyNotFoundError, DependencyNotFoundForModuleError, popupResolvingStack, pushResolvingStack, ResolvedDependencyCollection, } from './dependencyCollection'; | ||
import { normalizeFactoryDeps } from './dependencyDescriptor'; | ||
import { normalizeForwardRef } from './dependencyForwardRef'; | ||
import { isAsyncDependencyItem, isAsyncHook, isClassDependencyItem, isCtor, isFactoryDependencyItem, isValueDependencyItem, prettyPrintIdentifier, AsyncHookSymbol, isExistingDependencyItem, } from './dependencyItem'; | ||
import { AsyncHookSymbol, isAsyncDependencyItem, isAsyncHook, isClassDependencyItem, isCtor, isExistingDependencyItem, isFactoryDependencyItem, isValueDependencyItem, prettyPrintIdentifier, } from './dependencyItem'; | ||
import { checkQuantity, QuantityCheckError } from './dependencyQuantity'; | ||
@@ -90,5 +90,2 @@ import { RediError } from './error'; | ||
}(RediError)); | ||
/** | ||
* | ||
*/ | ||
var Injector = /** @class */ (function () { | ||
@@ -105,2 +102,3 @@ /** | ||
this.resolutionOngoing = 0; | ||
this.disposingCallbacks = new Set(); | ||
this.disposed = false; | ||
@@ -114,2 +112,16 @@ this.dependencyCollection = new DependencyCollection(dependencies || []); | ||
/** | ||
* Add a callback function that will be triggered when the Injector is disposed. | ||
* Please note that when you callback is invoked, the injector is already disposed and | ||
* you will not be able to interact with this Injector any more. | ||
* | ||
* @param {() => void} callback The callback function that will be invoked when | ||
* the Injector is disposed. | ||
* @returns A disposable that will remove the callback. | ||
*/ | ||
Injector.prototype.onDispose = function (callback) { | ||
var _this = this; | ||
this.disposingCallbacks.add(callback); | ||
return { dispose: function () { return _this.disposingCallbacks.delete(callback); } }; | ||
}; | ||
/** | ||
* Create a child inject with a set of dependencies. | ||
@@ -133,4 +145,7 @@ * @param dependencies Dependencies that should be resolved by the newly created child injector. | ||
this.resolvedDependencyCollection.dispose(); | ||
// Detach itself from parent. | ||
this.deleteSelfFromParent(); | ||
this.disposed = true; | ||
this.disposingCallbacks.forEach(function (callback) { return callback(); }); | ||
this.disposingCallbacks.clear(); | ||
}; | ||
@@ -162,6 +177,6 @@ Injector.prototype.deleteSelfFromParent = function () { | ||
} | ||
else if (isAsyncDependencyItem(item) || | ||
isClassDependencyItem(item) || | ||
isValueDependencyItem(item) || | ||
isFactoryDependencyItem(item)) { | ||
else if (isAsyncDependencyItem(item) | ||
|| isClassDependencyItem(item) | ||
|| isValueDependencyItem(item) | ||
|| isFactoryDependencyItem(item)) { | ||
// Add dependency | ||
@@ -229,3 +244,3 @@ this.dependencyCollection.add(identifierOrCtor, item); | ||
return _this.has(id); | ||
} | ||
}, | ||
}; | ||
@@ -260,5 +275,5 @@ return cb.apply(void 0, __spreadArray([accessor], args, false)); | ||
var quantity = Quantity.REQUIRED; | ||
if (quantityOrLookup === Quantity.REQUIRED || | ||
quantityOrLookup === Quantity.OPTIONAL || | ||
quantityOrLookup === Quantity.MANY) { | ||
if (quantityOrLookup === Quantity.REQUIRED | ||
|| quantityOrLookup === Quantity.OPTIONAL | ||
|| quantityOrLookup === Quantity.MANY) { | ||
quantity = quantityOrLookup; | ||
@@ -304,3 +319,3 @@ } | ||
return this._resolveClassImpl.apply(this, __spreadArray([{ | ||
useClass: ctor | ||
useClass: ctor, | ||
}], customArgs, false)); | ||
@@ -394,5 +409,5 @@ }; | ||
} | ||
var ctor = item.useClass; | ||
this.markNewResolution(ctor); | ||
var declaredDependencies = getDependencies(ctor) | ||
var Ctor = item.useClass; | ||
this.markNewResolution(Ctor); | ||
var declaredDependencies = getDependencies(Ctor) | ||
.sort(function (a, b) { return a.paramIndex - b.paramIndex; }) | ||
@@ -410,3 +425,3 @@ .map(function (descriptor) { return (__assign(__assign({}, descriptor), { identifier: normalizeForwardRef(descriptor.identifier) })); }); | ||
if (error instanceof DependencyNotFoundError || (error instanceof QuantityCheckError && error.actual === 0)) { | ||
throw new DependencyNotFoundForModuleError(ctor, dep.identifier, dep.paramIndex); | ||
throw new DependencyNotFoundForModuleError(Ctor, dep.identifier, dep.paramIndex); | ||
} | ||
@@ -421,6 +436,6 @@ throw error; | ||
if (args.length !== firstDependencyArgIndex) { | ||
console.warn("[redi]: Expect ".concat(firstDependencyArgIndex, " custom parameter(s) of ").concat(prettyPrintIdentifier(ctor), " but get ").concat(args.length, ".")); | ||
console.warn("[redi]: Expect ".concat(firstDependencyArgIndex, " custom parameter(s) of ").concat(prettyPrintIdentifier(Ctor), " but get ").concat(args.length, ".")); | ||
var delta = firstDependencyArgIndex - args.length; | ||
if (delta > 0) { | ||
args = __spreadArray(__spreadArray([], args, true), new Array(delta).fill(undefined), true); | ||
args = __spreadArray(__spreadArray([], args, true), (Array.from({ length: delta })).fill(undefined), true); | ||
} | ||
@@ -431,3 +446,3 @@ else { | ||
} | ||
var thing = new (ctor.bind.apply(ctor, __spreadArray(__spreadArray([void 0], args, false), resolvedArgs, false)))(); | ||
var thing = new (Ctor.bind.apply(Ctor, __spreadArray(__spreadArray([void 0], args, false), resolvedArgs, false)))(); | ||
(_a = item === null || item === void 0 ? void 0 : item.onInstantiation) === null || _a === void 0 ? void 0 : _a.call(item, thing); | ||
@@ -493,3 +508,3 @@ this.markResolutionCompleted(); | ||
useClass: thing, | ||
onInstantiation: item.onInstantiation | ||
onInstantiation: item.onInstantiation, | ||
}); | ||
@@ -508,4 +523,4 @@ } | ||
var onSelf = function () { | ||
if (_this.dependencyCollection.has(id) && | ||
!_this.resolvedDependencyCollection.has(id)) { | ||
if (_this.dependencyCollection.has(id) | ||
&& !_this.resolvedDependencyCollection.has(id)) { | ||
return NotInstantiatedSymbol; | ||
@@ -539,4 +554,4 @@ } | ||
} | ||
if (this.resolvedDependencyCollection.has(id) || | ||
this.dependencyCollection.has(id)) { | ||
if (this.resolvedDependencyCollection.has(id) | ||
|| this.dependencyCollection.has(id)) { | ||
return onSelf(); | ||
@@ -543,0 +558,0 @@ } |
export { createIdentifier } from './decorators'; | ||
export { Quantity, LookUp } from './types'; | ||
export { Many, Optional, Inject } from './dependencyQuantity'; | ||
export { Dependency, DependencyPair } from './dependencyCollection'; | ||
export { setDependencies } from './dependencyDeclare'; | ||
export { forwardRef } from './dependencyForwardRef'; | ||
export { Injector, IAccessor } from './injector'; | ||
export { SkipSelf, Self } from './dependencyLookUp'; | ||
export { DependencyPair, Dependency } from './dependencyCollection'; | ||
export { DependencyIdentifier, IdentifierDecorator, } from './dependencyIdentifier'; | ||
export { AsyncDependencyItem, AsyncHook, ClassDependencyItem, Ctor, DependencyItem, FactoryDependencyItem, isAsyncDependencyItem, isAsyncHook, isClassDependencyItem, isCtor, isFactoryDependencyItem, isValueDependencyItem, SyncDependencyItem, ValueDependencyItem, } from './dependencyItem'; | ||
export { Self, SkipSelf } from './dependencyLookUp'; | ||
export { Inject, Many, Optional } from './dependencyQuantity'; | ||
export { WithNew } from './dependencyWithNew'; | ||
export { IDisposable, isDisposable } from './dispose'; | ||
export { setDependencies } from './dependencyDeclare'; | ||
export { WithNew } from './dependencyWithNew'; | ||
export { AsyncDependencyItem, AsyncHook, ClassDependencyItem, Ctor, DependencyItem, FactoryDependencyItem, isAsyncDependencyItem, isAsyncHook, isClassDependencyItem, isCtor, isFactoryDependencyItem, isValueDependencyItem, SyncDependencyItem, ValueDependencyItem, } from './dependencyItem'; | ||
export { RediError } from './error'; | ||
export { IAccessor, Injector } from './injector'; | ||
export { LookUp, Quantity } from './types'; |
export { createIdentifier } from './decorators'; | ||
export { Quantity, LookUp } from './types'; | ||
export { Many, Optional, Inject } from './dependencyQuantity'; | ||
export { setDependencies } from './dependencyDeclare'; | ||
export { forwardRef } from './dependencyForwardRef'; | ||
export { Injector } from './injector'; | ||
export { SkipSelf, Self } from './dependencyLookUp'; | ||
export { isAsyncDependencyItem, isAsyncHook, isClassDependencyItem, isCtor, isFactoryDependencyItem, isValueDependencyItem, } from './dependencyItem'; | ||
export { Self, SkipSelf } from './dependencyLookUp'; | ||
export { Inject, Many, Optional } from './dependencyQuantity'; | ||
export { WithNew } from './dependencyWithNew'; | ||
export { isDisposable } from './dispose'; | ||
export { setDependencies } from './dependencyDeclare'; | ||
export { WithNew } from './dependencyWithNew'; | ||
export { isAsyncDependencyItem, isAsyncHook, isClassDependencyItem, isCtor, isFactoryDependencyItem, isValueDependencyItem, } from './dependencyItem'; | ||
export { RediError } from './error'; | ||
var globalObject = (typeof globalThis !== 'undefined' && globalThis) || | ||
(typeof window !== 'undefined' && window) || | ||
(typeof global !== 'undefined' && global); | ||
export { Injector } from './injector'; | ||
export { LookUp, Quantity } from './types'; | ||
var globalObject = (typeof globalThis !== 'undefined' && globalThis) | ||
|| (typeof window !== 'undefined' && window) | ||
// eslint-disable-next-line no-restricted-globals | ||
|| (typeof global !== 'undefined' && global); | ||
var __REDI_GLOBAL_LOCK__ = 'REDI_GLOBAL_LOCK'; | ||
// eslint-disable-next-line node/prefer-global/process | ||
var isNode = typeof process !== 'undefined' && process.versions != null && process.versions.node != null; | ||
@@ -17,0 +19,0 @@ if (globalObject[__REDI_GLOBAL_LOCK__]) { |
@@ -1,5 +0,5 @@ | ||
export { useInjector, useDependency } from './reactHooks'; | ||
export { connectInjector, connectDependencies } from './reactComponent'; | ||
export { RediContext, RediProvider, RediConsumer } from './reactContext'; | ||
export { connectDependencies, connectInjector } from './reactComponent'; | ||
export { RediConsumer, RediContext, RediProvider } from './reactContext'; | ||
export { WithDependency } from './reactDecorators'; | ||
export { useDependency, useInjector } from './reactHooks'; | ||
export * from './reactRx'; |
@@ -1,6 +0,6 @@ | ||
export { useInjector, useDependency } from './reactHooks'; | ||
export { connectInjector, connectDependencies } from './reactComponent'; | ||
export { RediContext, RediProvider, RediConsumer } from './reactContext'; | ||
export { connectDependencies, connectInjector } from './reactComponent'; | ||
export { RediConsumer, RediContext, RediProvider } from './reactContext'; | ||
export { WithDependency } from './reactDecorators'; | ||
export { useDependency, useInjector } from './reactHooks'; | ||
export * from './reactRx'; | ||
//# sourceMappingURL=publicApi.js.map |
@@ -0,9 +1,10 @@ | ||
import type { Dependency } from '@wendellhu/redi'; | ||
import { Injector } from '@wendellhu/redi'; | ||
import * as React from 'react'; | ||
import { Injector, Dependency } from '@wendellhu/redi'; | ||
/** | ||
* @param Comp | ||
* @param injector | ||
* @returns | ||
* @returns A component type that can be rendered. | ||
*/ | ||
export declare function connectInjector<P>(Comp: React.ComponentType<P>, injector: Injector): React.ComponentType<P>; | ||
export declare function connectDependencies<P>(Comp: React.ComponentType<P>, dependencies: Dependency[]): React.ComponentType<P>; |
@@ -12,5 +12,5 @@ var __assign = (this && this.__assign) || function () { | ||
}; | ||
import { Injector } from '@wendellhu/redi'; | ||
import * as React from 'react'; | ||
import { Injector } from '@wendellhu/redi'; | ||
import { RediProvider, RediConsumer } from './reactContext'; | ||
import { RediConsumer, RediProvider } from './reactContext'; | ||
function RediInjector(props) { | ||
@@ -38,3 +38,3 @@ var children = props.children, dependencies = props.dependencies; | ||
* @param injector | ||
* @returns | ||
* @returns A component type that can be rendered. | ||
*/ | ||
@@ -41,0 +41,0 @@ export function connectInjector(Comp, injector) { |
@@ -0,3 +1,3 @@ | ||
import type { Injector } from '@wendellhu/redi'; | ||
import * as React from 'react'; | ||
import { Injector } from '@wendellhu/redi'; | ||
export interface IRediContext { | ||
@@ -4,0 +4,0 @@ injector: Injector | null; |
import * as React from 'react'; | ||
var __REDI_CONTEXT_LOCK__ = 'REDI_CONTEXT_LOCK'; | ||
// eslint-disable-next-line node/prefer-global/process | ||
var isNode = typeof process !== 'undefined' && process.versions != null && process.versions.node != null; | ||
var globalObject = (typeof globalThis !== 'undefined' && globalThis) || | ||
(typeof window !== 'undefined' && window) || | ||
(typeof global !== 'undefined' && global); | ||
var globalObject = (typeof globalThis !== 'undefined' && globalThis) | ||
|| (typeof window !== 'undefined' && window) | ||
// eslint-disable-next-line no-restricted-globals | ||
|| (typeof global !== 'undefined' && global); | ||
if (!globalObject[__REDI_CONTEXT_LOCK__]) { | ||
@@ -8,0 +10,0 @@ globalObject[__REDI_CONTEXT_LOCK__] = true; |
@@ -1,2 +0,3 @@ | ||
import { DependencyIdentifier, Quantity, LookUp } from '@wendellhu/redi'; | ||
import type { DependencyIdentifier, LookUp } from '@wendellhu/redi'; | ||
import { Quantity } from '@wendellhu/redi'; | ||
export declare function WithDependency<T>(id: DependencyIdentifier<T>, quantity?: Quantity, lookUp?: LookUp): any; |
@@ -20,5 +20,5 @@ var __extends = (this && this.__extends) || (function () { | ||
function ClassComponentNotInRediContextError(component) { | ||
return _super.call(this, "You should make \"RediContext\" as ".concat(component.constructor.name, "'s default context type. ") + | ||
'If you want to use multiple context, please check this on React doc site. ' + | ||
'https://reactjs.org/docs/context.html#classcontexttype') || this; | ||
return _super.call(this, "You should make \"RediContext\" as ".concat(component.constructor.name, "'s default context type. ") | ||
+ 'If you want to use multiple context, please check this on React doc site. ' | ||
+ 'https://reactjs.org/docs/context.html#classcontexttype') || this; | ||
} | ||
@@ -25,0 +25,0 @@ return ClassComponentNotInRediContextError; |
@@ -1,2 +0,2 @@ | ||
import { DependencyIdentifier, Injector, LookUp, Quantity } from '@wendellhu/redi'; | ||
import type { DependencyIdentifier, Injector, LookUp, Quantity } from '@wendellhu/redi'; | ||
export declare function useInjector(): Injector; | ||
@@ -3,0 +3,0 @@ export declare function useDependency<T>(id: DependencyIdentifier<T>, lookUp?: LookUp): T; |
@@ -16,4 +16,4 @@ var __extends = (this && this.__extends) || (function () { | ||
})(); | ||
import { RediError, } from '@wendellhu/redi'; | ||
import * as React from 'react'; | ||
import { RediError, } from '@wendellhu/redi'; | ||
import { RediContext } from './reactContext'; | ||
@@ -20,0 +20,0 @@ var HooksNotInRediContextError = /** @class */ (function (_super) { |
@@ -1,3 +0,3 @@ | ||
import { ReactNode } from 'react'; | ||
import { Observable } from 'rxjs'; | ||
import type { ReactNode } from 'react'; | ||
import type { Observable } from 'rxjs'; | ||
/** | ||
@@ -4,0 +4,0 @@ * unwrap an observable value, return it to the component for rendering, and |
@@ -10,5 +10,5 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { | ||
}; | ||
import React, { useEffect, useState, createContext, useMemo, useContext, useCallback, useRef, } from 'react'; | ||
import { RediError } from '@wendellhu/redi'; | ||
import React, { createContext, useCallback, useContext, useEffect, useMemo, useRef, useState, } from 'react'; | ||
import { BehaviorSubject } from 'rxjs'; | ||
import { RediError } from '@wendellhu/redi'; | ||
/** | ||
@@ -50,11 +50,10 @@ * unwrap an observable value, return it to the component for rendering, and | ||
* @param deps A dependency array to decide if we should re-subscribe when the `observable` is a function. | ||
* @returns | ||
* @returns Value or null. | ||
*/ | ||
export function useObservable(observable, defaultValue, shouldHaveSyncValue, deps) { | ||
if (typeof observable === 'function' && !deps) { | ||
throw new RediError("Expected deps to be provided when observable is a function!"); | ||
throw new RediError('Expected deps to be provided when observable is a function!'); | ||
} | ||
var observableRef = useRef(null); | ||
var initializedRef = useRef(false); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
var destObservable = useMemo(function () { return observable; }, __spreadArray([], (typeof deps !== 'undefined' ? deps : [observable]), true)); | ||
@@ -61,0 +60,0 @@ // This state is only for trigger React to re-render. We do not use `setValue` directly because it may cause |
{ | ||
"schema": "https://raw.githubusercontent.com/wzhudev/squirrel/master/src/schema/package.schema.json", | ||
"name": "@wendellhu/redi", | ||
"version": "0.16.2", | ||
"version": "0.17.0", | ||
"description": "A dependency library for TypeScript and JavaScript, along with a binding for React.", | ||
"author": "Wenzhao Hu<wzhudev@gmail.com>", | ||
"license": "MIT", | ||
"lint-staged": { | ||
"*": "eslint --fix" | ||
}, | ||
"main": "./dist/redi.js", | ||
@@ -9,0 +12,0 @@ "module": "./fesm/redi.mjs", |
@@ -16,11 +16,11 @@ # redi | ||
class AuthService { | ||
public getCurrentUserInfo(): UserInfo {} | ||
public getCurrentUserInfo(): UserInfo {} | ||
} | ||
class FileListService { | ||
constructor(@Inject(AuthService) private readonly authService: AuthService) {} | ||
constructor(@Inject(AuthService) private readonly authService: AuthService) {} | ||
public getUserFiles(): Promise<Files> { | ||
const currentUser = this.authService.getCurrentUserInfo() | ||
} | ||
public getUserFiles(): Promise<Files> { | ||
const currentUser = this.authService.getCurrentUserInfo() | ||
} | ||
} | ||
@@ -27,0 +27,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
513141
5151