-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathbasePlugin.js
More file actions
323 lines (267 loc) · 8.2 KB
/
basePlugin.js
File metadata and controls
323 lines (267 loc) · 8.2 KB
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
/** @license MIT License (c) copyright B Cavalier & J Hann */
/**
* Base wire plugin that provides properties, init, and destroy facets, and
* a proxy for plain JS objects.
*
* 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
*/
(function(define) { 'use strict';
define(function(require) {
var when, object, functional, pipeline, instantiate, createInvoker,
whenAll, obj, pluginInstance, undef, universalApply;
when = require('when');
object = require('../object');
functional = require('../functional');
pipeline = require('../pipeline');
instantiate = require('../instantiate');
createInvoker = require('../invoker');
universalApply = require('../universalApply');
whenAll = when.all;
obj = {};
function asArray(it) {
return Array.isArray(it) ? it : [it];
}
function invoke(func, proxy, args, wire) {
return when(wire(args, func, proxy.path),
function (resolvedArgs) {
return proxy.invoke(func, asArray(resolvedArgs));
}
);
}
function invokeAll(facet, wire) {
var options = facet.options;
if(typeof options == 'string') {
return invoke(options, facet, [], wire);
} else {
var promises, funcName;
promises = [];
for(funcName in options) {
promises.push(invoke(funcName, facet, options[funcName], wire));
}
return whenAll(promises);
}
}
//
// Mixins
//
function mixin(target, src) {
var name, s;
for(name in src) {
s = src[name];
if(!(name in target) || (target[name] !== s && (!(name in obj) || obj[name] !== s))) {
target[name] = s;
}
}
return target;
}
function doMixin(target, introduction, wire) {
introduction = typeof introduction == 'string'
? wire.resolveRef(introduction)
: wire(introduction);
return when(introduction, mixin.bind(null, target));
}
function mixinFacet(resolver, facet, wire) {
var target, intros;
target = facet.target;
intros = facet.options;
if(!Array.isArray(intros)) {
intros = [intros];
}
resolver.resolve(when.reduce(intros, function(target, intro) {
return doMixin(target, intro, wire);
}, target));
}
/**
* Factory that handles cases where you need to create an object literal
* that has a property whose name would trigger another wire factory.
* For example, if you need an object literal with a property named "create",
* which would normally cause wire to try to construct an instance using
* a constructor or other function, and will probably result in an error,
* or an unexpected result:
* myObject: {
* create: "foo"
* ...
* }
*
* You can use the literal factory to force creation of an object literal:
* myObject: {
* literal: {
* create: "foo"
* }
* }
*
* which will result in myObject.create == "foo" rather than attempting
* to create an instance of an AMD module whose id is "foo".
*/
function literalFactory(resolver, spec /*, wire */) {
resolver.resolve(spec.options);
}
/**
* @deprecated Use create (instanceFactory) instead
* @param resolver
* @param componentDef
* @param wire
*/
function protoFactory(resolver, componentDef, wire) {
var parentRef, promise;
parentRef = componentDef.options;
promise = typeof parentRef === 'string'
? wire.resolveRef(parentRef)
: wire(parentRef);
resolver.resolve(promise.then(Object.create));
}
function propertiesFacet(resolver, facet, wire) {
var properties, path, setProperty, propertiesSet;
properties = facet.options;
path = facet.path;
setProperty = facet.set.bind(facet);
propertiesSet = when.map(Object.keys(facet.options), function(key) {
return wire(properties[key], facet.path)
.then(function(wiredProperty) {
setProperty(key, wiredProperty);
}
);
});
resolver.resolve(propertiesSet);
}
function invokerFactory(resolver, componentDef, wire) {
var invoker = wire(componentDef.options).then(function (invokerContext) {
// It'd be nice to use wire.getProxy() then proxy.invoke()
// here, but that means the invoker must always return
// a promise. Not sure that's best, so for now, just
// call the method directly
return createInvoker(invokerContext.method, invokerContext.args);
});
resolver.resolve(invoker);
}
function invokerFacet(resolver, facet, wire) {
resolver.resolve(invokeAll(facet, wire));
}
function cloneFactory(resolver, componentDef, wire) {
var sourceRef, options, cloned;
if (wire.resolver.isRef(componentDef.options.source)) {
sourceRef = componentDef.options.source;
options = componentDef.options;
}
else {
sourceRef = componentDef.options;
options = {};
}
cloned = wire(sourceRef).then(function (ref) {
return when(wire.getProxy(ref), function (proxy) {
if (!proxy.clone) {
throw new Error('No clone function found for ' + componentDef.id);
}
return proxy.clone(options);
});
});
resolver.resolve(cloned);
}
function getArgs(create, wire) {
return create.args ? wire(asArray(create.args)) : [];
}
function moduleFactory(resolver, componentDef, wire) {
resolver.resolve(wire.loadModule(componentDef.options));
}
/**
* Factory that uses an AMD module either directly, or as a
* constructor or plain function to create the resulting item.
*
* @param {Object} resolver resolver to resolve with the created component
* @param {Object} componentDef portion of the spec for the component to be created
* @param {function} wire
*/
function instanceFactory(resolver, componentDef, wire) {
var create, args, isConstructor, module, instance, constructorName;
create = componentDef.options;
if (typeof create == 'string') {
module = wire.loadModule(create);
} else if(wire.resolver.isRef(create)) {
module = wire(create);
args = getArgs(create, wire);
constructorName = create.constructorName;
} else if(object.isObject(create) && create.module) {
module = wire.resolver.isRef(create.module)
? wire(create.module)
: wire.loadModule(create.module);
args = getArgs(create, wire);
isConstructor = create.isConstructor;
constructorName = create.constructorName;
} else {
module = create;
}
instance = when.join(module, args).spread(createInstance);
resolver.resolve(instance);
// Load the module, and use it to create the object
function createInstance(module, args) {
// We'll either use the module directly, or we need
// to instantiate/invoke it.
return constructorName
? universalApply(constructorName, module, args)
: typeof module == 'function'
? instantiate(module, args, isConstructor)
: Object.create(module);
}
}
function composeFactory(resolver, componentDef, wire) {
var options, promise;
options = componentDef.options;
if(typeof options == 'string') {
promise = pipeline(undef, options, wire);
} else {
// Assume it's an array of things that will wire to functions
promise = when(wire(options), function(funcArray) {
return functional.compose(funcArray);
});
}
resolver.resolve(promise);
}
pluginInstance = {
factories: {
module: moduleFactory,
create: instanceFactory,
literal: literalFactory,
prototype: protoFactory,
clone: cloneFactory,
compose: composeFactory,
invoker: invokerFactory
},
facets: {
// properties facet. Sets properties on components
// after creation.
properties: {
configure: propertiesFacet
},
mixin: {
configure: mixinFacet
},
// init facet. Invokes methods on components during
// the "init" stage.
init: {
initialize: invokerFacet
},
// ready facet. Invokes methods on components during
// the "ready" stage.
ready: {
ready: invokerFacet
},
// destroy facet. Registers methods to be invoked
// on components when the enclosing context is destroyed
destroy: {
destroy: invokerFacet
}
}
};
// "introduce" is deprecated, but preserved here for now.
pluginInstance.facets.introduce = pluginInstance.facets.mixin;
return function(/* options */) {
return pluginInstance;
};
});
})(typeof define == 'function'
? define
: function(factory) { module.exports = factory(require); }
);