angularjs 源码解析之injector(2)

function decorator(serviceName, decorFn) { var origProvider = providerInjector.get(serviceName + providerSuffix), orig$get = origProvider.$get; origProvider.$get = function() { // 通过上面获取的provider生成需要的service实例,再以$delegate注入到参数列表 var origInstance = instanceInjector.invoke(orig$get, origProvider); return instanceInjector.invoke(decorFn, null, {$delegate: origInstance}); }; }

3. createInternalInjector

3.1 整体结构

// 从cache中获取,没有的话调用factory进行创建,具体看getService解析

function createInternalInjector(cache, factory) { function getService(serviceName) { } function invoke(fn, self, locals, serviceName){ } function instantiate(Type, locals, serviceName) { } return { // 执行fn,具有参数注入功能 invoke: invoke, // 实例化fn, 可以参数注入 instantiate: instantiate, // 获取provider或者service get: getService, // 获取方法的参数列表,供注入使用 annotate: annotate, // 确认是否含有provider或service has: function(name) { return providerCache.hasOwnProperty(name + providerSuffix) || cache.hasOwnProperty(name); } }; }

3.2 annotate

获取fn的参数列表

// type1 function fn (a, b, c) -> ['a', 'b', 'c'] // type2 ['a', 'b', fn] -> ['a', 'b'] // type3 function fn () {} fn.$inject = ['a', 'c'] -> ['a', 'c']

源码:

function annotate(fn, strictDi, name) { var $inject, fnText, argDecl, last; if (typeof fn === 'function') { if (!($inject = fn.$inject)) { $inject = []; if (fn.length) { // 严格模式下或抛错 if (strictDi) { if (!isString(name) || !name) { name = fn.name || anonFn(fn); } throw $injectorMinErr('strictdi', '{0} is not using explicit annotation and cannot be invoked in strict mode', name); } // 将注释去掉 fnText = fn.toString().replace(STRIP_COMMENTS, ''); // 将参数全部选出fn(a,b,c,d) -> 'a,b,c,d' argDecl = fnText.match(FN_ARGS); // 分割成array forEach(argDecl[1].split(FN_ARG_SPLIT), function(arg){ arg.replace(FN_ARG, function(all, underscore, name){ $inject.push(name); }); }); } fn.$inject = $inject; } } else if (isArray(fn)) { last = fn.length - 1; assertArgFn(fn[last], 'fn'); $inject = fn.slice(0, last); } else { assertArgFn(fn, 'fn', true); } return $inject; }

3.3 getService

// 当cache中没有该service时,进入else, 先cache[serviceName] = INSTANTIATING 做一个标记 // 因为接下来调用factory(serviceName),其实是一个递归调用 // function(servicename) { // var provider = providerInjector.get(servicename + providerSuffix); // return instanceInjector.invoke(provider.$get, provider, undefined, servicename); // } // instanceInjector.invoke(provider.$get 时会将需要注入的参数get出来然后注入 // 因此做上标记后就可以判断是否有循环依赖 function getService(serviceName) { if (cache.hasOwnProperty(serviceName)) { if (cache[serviceName] === INSTANTIATING) { throw $injectorMinErr('cdep', 'Circular dependency found: {0}', serviceName + ' <- ' + path.join(' <- ')); } return cache[serviceName]; } else { try { path.unshift(serviceName); cache[serviceName] = INSTANTIATING; return cache[serviceName] = factory(serviceName); } catch (err) { if (cache[serviceName] === INSTANTIATING) { delete cache[serviceName]; } throw err; } finally { path.shift(); } } }

3.4 invoke

function invoke(fn, self, locals, serviceName){ if (typeof locals === 'string') { serviceName = locals; locals = null; } var args = [], // 获取参数列表 $inject = annotate(fn, strictDi, serviceName), length, i, key; for(i = 0, length = $inject.length; i < length; i++) { key = $inject[i]; if (typeof key !== 'string') { throw $injectorMinErr('itkn', 'Incorrect injection token! Expected service name as string, got {0}', key); } // locals优先 args.push( locals && locals.hasOwnProperty(key) ? locals[key] : getService(key) ); } if (isArray(fn)) { fn = fn[length]; } return fn.apply(self, args); }

3.5 instantiate

function instantiate(Type, locals, serviceName) { var Constructor = function() {}, instance, returnedValue; // 当type为array时,获取最后的参数如:['$window', function($win){}] Constructor.prototype = (isArray(Type) ? Type[Type.length - 1] : Type).prototype; instance = new Constructor(); // 调用invoke执行Type方法 returnedValue = invoke(Type, instance, locals, serviceName); return isObject(returnedValue) || isFunction(returnedValue) ? returnedValue : instance; }

instantiate 的作用是用来实例化Type的,在实例化的过程中可以自动传入参数到构造函数。

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wzfsdz.html