forked from cujojs/wire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwire.js
1089 lines (892 loc) · 33.3 KB
/
wire.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/** @license MIT License (c) copyright B Cavalier & J Hann */
/**
* wire
* Javascript IOC Container
*
* wire is part of the cujo.js family of libraries (http://cujojs.com/)
*
* Licensed under the MIT License at:
* http://www.opensource.org/licenses/mit-license.php
*
* @version 0.7.5
*/
(function(global, define){
define(['require', 'when', 'wire/base'], function(require, when, basePlugin) {
"use strict";
var VERSION, tos, arrayProto, apIndexOf, apSlice, rootSpec, rootContext, delegate, emptyObject,
defer, chain, whenAll, isArray, indexOf, lifecycleSteps, undef;
wire.version = VERSION = "0.7.5";
rootSpec = global['wire'] || {};
lifecycleSteps = ['create', 'configure', 'initialize', 'ready'];
emptyObject = {};
tos = Object.prototype.toString;
arrayProto = Array.prototype;
apSlice = arrayProto.slice;
apIndexOf = arrayProto.indexOf;
// Polyfills
/**
* Object.create
*/
delegate = Object.create || createObject;
/**
* Array.isArray
*/
isArray = Array.isArray || function (it) {
return tos.call(it) == '[object Array]';
};
/**
* Array.prototype.indexOf
*/
indexOf = apIndexOf
? function (array, item) {
return apIndexOf.call(array, item);
}
: function (array, item) {
for (var i = 0, len = array.length; i < len; i++) {
if (array[i] === item) return i;
}
return -1;
};
emptyObject = {};
// Local refs to when.js
defer = when.defer;
chain = when.chain;
whenAll = when.all;
/**
* Helper to always return a promise
* @param it anything
*/
function promise(it) {
return when.isPromise(it) ? it : when(it);
}
/**
* Helper to reject a deferred when another is rejected
* @param resolver {Object} resolver to reject
*/
function chainReject(resolver) {
return function (err) {
resolver.reject(err);
};
}
/**
* Creates an already-rejected Deferred using err as the rejection reason
* @param err anything - the rejection reason
*/
function rejected(err) {
var d = defer();
d.reject(err);
return d.promise;
}
/**
* Abstract the platform's loader
* @param moduleId {String} moduleId to load
* @returns {Promise} a promise that resolves to the loaded module
*/
function loadModule(moduleId) {
// TODO: Choose loadModule implementation based on platform
var deferred = defer();
require([moduleId], deferred.resolve);
return deferred.promise;
}
//
// AMD Module API
//
/**
* The top-level wire function that wires contexts as direct children
* of the (possibly implicit) root context. It ensures that the root
* context has been wired before wiring children.
*
* @public
*
* @param spec {String|Array|*}
*/
function wire(spec) {
// If the root context is not yet wired, wire it first
if (!rootContext) {
rootContext = wireContext(rootSpec);
}
// Use the rootContext to wire all new contexts.
return when(rootContext,
function (root) {
return root.wire(spec);
}
);
}
//
// AMD loader plugin API
//
//noinspection JSUnusedLocalSymbols
/**
* AMD Loader plugin API
* @param name {String} spec module id, or comma-separated list of module ids
* @param require unused
* @param callback {Function|Promise} callback to call or promise to resolve when wiring is completed
* @param config unused
*/
function amdLoad(name, require, callback, config) {
var resolver = callback.resolve
? callback
: {
resolve: callback,
reject: function (err) { throw err; }
};
// If it's a string, try to split on ',' since it could be a comma-separated
// list of spec module ids
chain(wire(name.split(',')), resolver);
}
wire.load = amdLoad;
//
// AMD Analyze/Build plugin API
//
// Separate builder plugin as per CommonJS LoaderPlugin spec:
// http://wiki.commonjs.org/wiki/Modules/LoaderPlugin
// plugin-builder: wire/cram/builder
// cram v 0.2+ supports plugin-builder property
wire['plugin-builder'] = 'wire/cram/builder';
//
// Private functions
//
/**
* Creates a new context from the supplied specs, with the supplied parent context.
* If specs is an {Array}, it may be a mixed array of string module ids, and object
* literal specs. All spec module ids will be loaded, and then all specs will be
* merged from left-to-right (rightmost wins), and the resulting, merged spec will
* be wired.
*
* @private
*
* @param specs {String|Array|*}
* @param parent {Object} parent content
*
* @return {Promise} a promise for the new context
*/
function wireContext(specs, parent) {
// Do the actual wiring after all specs have been loaded
function doWireContexts(spec) {
return when(createScope(spec, parent),
function (scope) {
return scope.objects;
}
);
}
return when(ensureAllSpecsLoaded(isArray(specs) ? specs : [specs]), doWireContexts);
}
/**
* Given a mixed array of strings and non-strings, returns a promise that will resolve
* to an array containing resolved modules by loading all the strings found in the
* specs array as module ids
* @private
*
* @param specs {Array} mixed array of strings and non-strings
*
* @returns {Promise} a promise that resolves to an array of resolved modules
*/
function ensureAllSpecsLoaded(specs) {
return when.reduce(specs, function(merged, module) {
return isString(module)
? when(loadModule(module), function(spec) { return mixinSpec(merged, spec); })
: mixinSpec(merged, module)
}, {});
}
/**
* Do the work of creating a new scope and fully wiring its contents
* @private
*
* @param scopeDef {Object} The spec (or portion of a spec) to be wired into a new scope
* @param parent {scope} scope to use as the parent, and thus from which to inherit
* plugins, components, etc.
* @param [scopeName] {String} optional name for the new scope
*
* @return {Promise} a promise for the new scope
*/
function createScope(scopeDef, parent, scopeName) {
var scope, scopeParent, local, proxied, objects,
pluginApi, resolvers, factories, facets, listeners, proxies,
modulesToLoad, moduleLoadPromises,
wireApi, modulesReady, scopeReady, scopeDestroyed,
contextPromise, doDestroy;
// Empty parent scope if none provided
parent = parent || {};
initFromParent(parent);
initPluginApi();
// TODO: Find a better way to load and scan the base plugin
scanPlugin(basePlugin);
contextPromise = initContextPromise(scopeDef, scopeReady);
initWireApi(objects);
createComponents(local, scopeDef);
// Once all modules are loaded, all the components can finish
ensureAllModulesLoaded();
// Setup overwritable doDestroy so that this context
// can only be destroyed once
doDestroy = function () {
// Retain a do-nothing doDestroy() func, in case
// it is called again for some reason.
doDestroy = function () {
};
return destroyContext();
};
// Return promise
// Context will be ready when this promise resolves
return scopeReady.promise;
//
// Initialization
//
function initFromParent(parent) {
local = {};
// Descend scope and plugins from parent so that this scope can
// use them directly via the prototype chain
objects = delegate(parent.objects || {});
resolvers = delegate(parent.resolvers || {});
factories = delegate(parent.factories || {});
facets = delegate(parent.facets || {});
// Set/override integral resolvers and factories
resolvers.wire = wireResolver;
factories.module = moduleFactory;
factories.create = instanceFactory;
factories.wire = wireFactory;
listeners = delegateArray(parent.listeners);// ? [].concat(parent.listeners) : [];
// Proxies is an array, have to concat
proxies = delegateArray(parent.proxies);// ? [].concat(parent.proxies) : [];
proxied = [];
modulesToLoad = [];
moduleLoadPromises = {};
modulesReady = defer();
scopeReady = defer();
scopeDestroyed = defer();
// A proxy of this scope that can be used as a parent to
// any child scopes that may be created.
scopeParent = {
name: scopeName,
objects: objects,
destroyed: scopeDestroyed
};
// Full scope definition. This will be given to sub-scopes,
// but should never be given to child contexts
scope = delegate(scopeParent);
scope.local = local;
scope.resolvers = resolvers;
scope.factories = factories;
scope.facets = facets;
scope.listeners = listeners;
scope.proxies = proxies;
scope.resolveRef = doResolveRef;
scope.destroy = destroy;
scope.path = createPath(scopeName, parent.path);
// When the parent begins its destroy phase, this child must
// begin its destroy phase and complete it before the parent.
// The context hierarchy will be destroyed from child to parent.
if (parent.destroyed) {
when(parent.destroyed, destroy);
}
}
function initWireApi(objects) {
wireApi = objects.wire = wireChild;
wireApi.destroy = objects.destroy = apiDestroy;
// Consider deprecating resolve
// Any reference you could resolve using this should simply be
// injected instead.
wireApi.resolve = objects.resolve = apiResolveRef;
}
function initPluginApi() {
// Plugin API
// wire() API that is passed to plugins.
pluginApi = function (spec, name, path) {
// FIXME: Why does returning when(item) here cause
// the resulting, returned promise never to resolve
// in wire-factory1.html?
return promise(createItem(spec, createPath(name, path)));
};
pluginApi.resolveRef = apiResolveRef;
}
function initContextPromise(scopeDef, scopeReady) {
var promises = [];
// Setup a promise for each item in this scope
for (var name in scopeDef) {
if (scopeDef.hasOwnProperty(name)) {
promises.push(local[name] = objects[name] = defer());
}
}
// When all scope item promises are resolved, the scope
// is resolved.
// When this scope is ready, resolve the contextPromise
// with the objects that were created
return whenAll(promises,
function () {
scopeReady.resolve(scope);
return objects;
},
chainReject(scopeReady)
);
}
//
// Context Startup
//
function createComponents(names, scopeDef) {
// Process/create each item in scope and resolve its
// promise when completed.
for (var name in names) {
// No need to check hasOwnProperty since we know names
// only contains scopeDef's own prop names.
createScopeItem(name, scopeDef[name], objects[name]);
}
}
function ensureAllModulesLoaded() {
// Once all modules have been loaded, resolve modulesReady
whenAll(modulesToLoad, function (modules) {
modulesReady.resolve(modules);
moduleLoadPromises = modulesToLoad = null;
});
}
//
// Context Destroy
//
function destroyContext() {
var p, promises, pDeferred, i;
scopeDestroyed.resolve();
// TODO: Clear out the context prototypes?
promises = [];
for (i = 0; (p = proxied[i++]);) {
pDeferred = defer();
promises.push(pDeferred);
processListeners(pDeferred, 'destroy', p);
}
// *After* listeners are processed,
whenAll(promises, function () {
function deleteAll(container) {
for(var p in container) delete container[p];
}
deleteAll(local);
deleteAll(objects);
deleteAll(scope);
var p, i;
for (i = 0; (p = proxied[i++]);) {
p.destroy();
}
// Free Objects
local = objects = scope = proxied = proxies = parent
= resolvers = factories = facets = wireApi = undef;
// Free Arrays
listeners = undef;
});
return scopeDestroyed;
}
//
// Context API
//
// API of a wired context that is returned, via promise, to
// the caller. It will also have properties for all the
// objects that were created in this scope.
/**
* Resolves a reference in the current context, using any reference resolvers
* available in the current context
*
* @param ref {String} reference name (may contain resolver prefix, e.g. "resolver!refname"
*/
function apiResolveRef(ref) {
return when(doResolveRef(ref));
}
/**
* Destroys the current context
*/
function apiDestroy() {
return destroy();
}
/**
* Wires a child spec with this context as its parent
* @param spec
*/
function wireChild(spec) {
return wireContext(spec, scopeParent);
}
//
// Scope functions
//
function createPath(name, basePath) {
var path = basePath || scope.path;
return (path && name) ? (path + '.' + name) : name;
}
function createScopeItem(name, val, itemPromise) {
// NOTE: Order is important here.
// The object & local property assignment MUST happen before
// the chain resolves so that the concrete item is in place.
// Otherwise, the whole scope can be marked as resolved before
// the final item has been resolved.
var p = createItem(val, name);
return when(p, function (resolved) {
objects[name] = local[name] = resolved;
itemPromise.resolve(resolved);
}, chainReject(itemPromise));
}
function createItem(val, name) {
var created;
if (isRef(val)) {
// Reference
created = resolveRef(val, name);
} else if (isArray(val)) {
// Array
created = createArray(val, name);
} else if (isStrictlyObject(val)) {
// Module or nested scope
created = createModule(val, name);
} else {
// Plain value
created = val;
}
return created;
}
function getModule(moduleId, spec) {
var module, loadPromise;
if (isString(moduleId)) {
var m = moduleLoadPromises[moduleId];
if (!m) {
modulesToLoad.push(moduleId);
m = moduleLoadPromises[moduleId] = {
id: moduleId,
deferred: defer()
};
moduleLoadPromises[moduleId] = m;
loadPromise = when(loadModule(moduleId), function (module) {
scanPlugin(module, spec);
chain(modulesReady, m.deferred, module);
});
modulesToLoad.push(loadPromise);
}
module = m.deferred;
} else {
module = moduleId;
scanPlugin(module);
}
return module;
}
function scanPlugin(module, spec) {
if (module && isFunction(module.wire$plugin)) {
var plugin = module.wire$plugin(contextPromise, scopeDestroyed.promise, spec);
if (plugin) {
addPlugin(plugin.resolvers, resolvers);
addPlugin(plugin.factories, factories);
addPlugin(plugin.facets, facets);
listeners.push(plugin);
addProxies(plugin.proxies);
}
}
}
function addProxies(proxiesToAdd) {
if (!proxiesToAdd) return;
var newProxies, p, i = 0;
newProxies = [];
while (p = proxiesToAdd[i++]) {
if (indexOf(proxies, p) < 0) {
newProxies.push(p)
}
}
scope.proxies = proxies = newProxies.concat(proxies);
}
function addPlugin(src, registry) {
for (var name in src) {
if (registry.hasOwnProperty(name)) {
throw new Error("Two plugins for same type in scope: " + name);
}
registry[name] = src[name];
}
}
function createArray(arrayDef, name) {
// Minor optimization, if it's an empty array spec, just return
// an empty array.
return arrayDef.length
? when.map(arrayDef, function(item) {
return createItem(item, name + '[]');
})
: [];
}
function createModule(spec, name) {
// Look for a factory, then use it to create the object
return when(findFactory(spec),
function (factory) {
var factoryPromise = defer();
if (!spec.id) spec.id = name;
factory(factoryPromise.resolver, spec, pluginApi);
return processObject(factoryPromise, spec);
},
function () {
// No factory found, treat object spec as a nested scope
return when(createScope(spec, scope, name),
function (created) {
// Return *only* the objects, and none of the
// other scope stuff (like plugins, promises etc)
return created.local;
},
rejected
);
}
);
}
function findFactory(spec) {
// FIXME: Should not have to wait for all modules to load,
// but rather only the module containing the particular
// factory we need. But how to know which factory before
// they are all loaded?
// Maybe need a special syntax for factories, something like:
// create: "factory!whatever-arg-the-factory-takes"
// args: [factory args here]
function getFactory() {
var f, factory;
for (f in factories) {
if (spec.hasOwnProperty(f)) {
factory = factories[f];
break;
}
}
// Intentionally returns undefined if no factory found
return factory;
}
return getFactory() || when(modulesReady, function () {
return getFactory() || rejected(spec);
});
}
/**
* When the target component has been created, create its proxy,
* then push it through all its lifecycle stages.
*
* @private
*
* @param target the component being created, may be a promise
* @param spec the component's spec (the portion of the overall spec used to
* create the target component)
*
* @returns {Promise} a promise for the fully wired component
*/
function processObject(target, spec) {
return when(target,
function (object) {
var proxy = createProxy(object, spec);
proxied.push(proxy);
// Push the object through the lifecycle steps, processing
// facets at each step.
return when.reduce(lifecycleSteps,
function (object, step) {
return processFacets(step, proxy);
}, proxy);
}, rejected);
}
function createProxy(object, spec) {
var proxier, proxy, id, i;
i = 0;
id = spec.id;
while ((proxier = proxies[i++]) && !(proxy = proxier(object, spec))) {}
proxy.target = object;
proxy.spec = spec;
proxy.id = id;
proxy.path = createPath(id);
return proxy;
}
function processFacets(step, proxy) {
var promises, options, name, spec;
promises = [];
spec = proxy.spec;
for (name in facets) {
options = spec[name];
if (options) {
processStep(promises, facets[name], step, proxy, options);
}
}
var d = defer();
whenAll(promises,
function () { processListeners(d, step, proxy); },
chainReject(d)
);
return d;
}
function processListeners(promise, step, proxy) {
var listenerPromises = [];
for (var i = 0; i < listeners.length; i++) {
processStep(listenerPromises, listeners[i], step, proxy);
}
// FIXME: Use only proxy here, caller should resolve target
return chain(whenAll(listenerPromises), promise, proxy.target);
}
function processStep(promises, processor, step, proxy, options) {
var facet, facetPromise;
if (processor && processor[step]) {
facetPromise = defer();
promises.push(facetPromise);
facet = delegate(proxy);
facet.options = options;
processor[step](facetPromise.resolver, facet, pluginApi);
}
}
//
// Built-in Factories
//
/**
* Factory that loads an AMD module
*
* @param resolver {Resolver} resolver to resolve with the created component
* @param spec {Object} portion of the spec for the component to be created
*/
function moduleFactory(resolver, spec /*, wire */) {
chain(getModule(spec.module, spec), resolver);
}
/**
* Factory that uses an AMD module either directly, or as a
* constructor or plain function to create the resulting item.
*
* @param resolver {Resolver} resolver to resolve with the created component
* @param spec {Object} portion of the spec for the component to be created
*/
function instanceFactory(resolver, spec /*, wire */) {
var fail, create, module, args, isConstructor, name;
fail = chainReject(resolver);
name = spec.id;
create = spec.create;
if (isStrictlyObject(create)) {
module = create.module;
args = create.args;
isConstructor = create.isConstructor;
} else {
module = create;
}
// Load the module, and use it to create the object
function handleModule(module) {
function resolve(resolvedArgs) {
try {
var instantiated = instantiate(module, resolvedArgs, isConstructor);
resolver.resolve(instantiated);
} catch (e) {
resolver.reject(e);
}
}
try {
// We'll either use the module directly, or we need
// to instantiate/invoke it.
if (isFunction(module)) {
// Instantiate or invoke it and use the result
if (args) {
args = isArray(args) ? args : [args];
when(createArray(args, name), resolve, fail);
} else {
// No args, don't need to process them, so can directly
// insantiate the module and resolve
resolve([]);
}
} else {
// Simply use the module as is
resolver.resolve(module);
}
} catch (e) {
fail(e);
}
}
when(getModule(module, spec), handleModule, fail);
}
/**
* Factory that creates either a child context, or a *function* that will create
* that child context. In the case that a child is created, this factory returns
* a promise that will resolve when the child has completed wiring.
*
* @param resolver {Resolver} resolver to resolve with the created component
* @param spec {Object} portion of the spec for the component to be created
*/
function wireFactory(resolver, spec/*, wire, name*/) {
var options, module, defer;
options = spec.wire;
// Get child spec and options
if (isString(options)) {
module = options;
} else {
module = options.spec;
defer = options.defer;
}
function createChild(/** {Object|String}? */ mixin) {
var toWire = mixin ? [].concat(module, mixin) : module;
return wireChild(toWire);
}
if (defer) {
// Resolve with the createChild *function* itself
// which can be used later to wire the spec
resolver.resolve(createChild);
} else {
// Start wiring the child
var context = createChild();
// Resolve immediately with the child promise
resolver.resolve(context);
}
}
//
// Reference resolution
//
function resolveRef(ref, name) {
var refName = ref.$ref;
return doResolveRef(refName, ref, name == refName);
}
function doResolveRef(refName, refObj, excludeSelf) {
var promise, registry;
registry = excludeSelf ? parent.objects : objects;
if (refName in registry) {
promise = registry[refName];
} else {
var split;
promise = defer();
split = refName.indexOf('!');
if (split > 0) {
var resolverName = refName.substring(0, split);
refName = refName.substring(split + 1);
// Wait for modules, since the reference may need to be
// resolved by a resolver plugin
when(modulesReady, function () {
var resolver = resolvers[resolverName];
if (resolver) {
resolver(promise, refName, refObj, pluginApi);
} else {
promise.reject("No resolver found for ref: " + refName);
}
});
} else {
promise.reject("Cannot resolve ref: " + refName);
}
}
return promise;
}
/**
* Builtin reference resolver that resolves to the context-specific
* wire function.
*
* @param resolver {Resolver} resolver to resolve
*/
function wireResolver(resolver /*, name, refObj, wire*/) {
resolver.resolve(wireApi);
}
//
// Destroy
//
/**
* Destroy the current context. Waits for the context to finish
* wiring and then immediately destroys it.
*
* @return {Promise} a promise that will resolve once the context
* has been destroyed
*/
function destroy() {
return when(scopeReady, doDestroy, doDestroy);
}
} // createScope
/**
* Add components in from to those in to. If duplicates are found, it
* is an error.
* @param to {Object} target object
* @param from {Object} source object
*/
function mixinSpec(to, from) {
for (var name in from) {
if (from.hasOwnProperty(name) && !(name in emptyObject)) {
if (to.hasOwnProperty(name)) {
throw new Error("Duplicate component name in sibling specs: " + name);
} else {
to[name] = from[name];
}
}
}
return to;
}
function isRef(it) {
return it && it.$ref;
}
function isString(it) {
return typeof it == 'string';
}
function isStrictlyObject(it) {
// In IE7 tos.call(null) is '[object Object]'
// so we need to check to see if 'it' is
// even set
return (it && tos.call(it) == '[object Object]');
}
/**
* Standard function test
* @param it
*/
function isFunction(it) {
return typeof it == 'function';
}
/**
* Creates a new {Array} with the same contents as array
* @param array {Array}