@lumjs/compat 1.4.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/v4/promise.js DELETED
@@ -1,392 +0,0 @@
1
- const {B,O,F} = require('@lumjs/core').types;
2
-
3
- /**
4
- * Build a *Promise* object with *Deferred* compatibility.
5
- *
6
- * @param {(object|boolean)} [options] Options for the `Promise` object.
7
- *
8
- * If options is a `boolean`, it's assumed to be the `jquery` option.
9
- *
10
- * @param {boolean} [options.jquery] Use `jQuery.Deferred` API?
11
- * - If `true`, use `this._extendJQuery(options)` to define methods.
12
- * - If `false`, use `this._extendInternal(options)` to define methods.
13
- *
14
- * @exports module:@lumjs/compat/v4/promise
15
- * @deprecated Use the Javascript `Promise` object directly.
16
- *
17
- * If you need compatibility with the `jQuery.Deferred` API,
18
- * just use the `jQuery.Deferred` API itself.
19
- *
20
- * If you need the `deferDone` or `deferFail` methods, use
21
- * this until the `@lumjs/defer` library is released.
22
- */
23
- function LumPromise(options)
24
- {
25
- if (typeof options === B)
26
- { // Assume the 'jquery' option was passed implicitly.
27
- options = {jquery: options};
28
- }
29
- else if (typeof options !== O || options === null)
30
- { // Ensure options is an object, and auto-select jQuery if it's loaded.
31
- options = {jquery: (typeof jQuery === O)};
32
- }
33
-
34
- if (options.jquery)
35
- {
36
- this._extendJQuery(options)
37
- }
38
- else
39
- {
40
- this._extendInternal(options);
41
- }
42
- }
43
-
44
- const lpp = LumPromise.prototype;
45
-
46
- /**
47
- * Add the methods from jQuery.Deferred to ourself.
48
- *
49
- * If jQuery is not loaded, this will throw an error.
50
- *
51
- * @alias module:@lumjs/compat/v4/promise#_extendJQuery
52
- */
53
- lpp._extendJQuery = function (options)
54
- {
55
- if (jQuery === undefined)
56
- {
57
- throw new Error("Cannot use 'jquery' without jQuery loaded.");
58
- }
59
- var def = jQuery.Deferred();
60
- for (var f in def)
61
- {
62
- this[f] = def[f];
63
- }
64
- }
65
-
66
- /**
67
- * Create our own internal represenation of the Deferred API.
68
- *
69
- * Setters: done(), fail(), always(), progress()
70
- * Promise chaining: then(), catch()
71
- * Triggers: resolve(), reject(), notify()
72
- * Targetted triggers: resolveWith(), rejectWith(), notifyWith()
73
- * Info: state()
74
- *
75
- * @alias module:@lumjs/compat/v4/promise#_extendInternal
76
- */
77
- lpp._extendInternal = function (options)
78
- {
79
- // A copy of this for use in closures.
80
- var self = this;
81
-
82
- // Private storage for the state of the Promise.
83
- var state = 'pending';
84
-
85
- // Private storage for the arguments used to resolve/reject.
86
- var final_args;
87
-
88
- // Private storage for the 'this' object to use in callbacks.
89
- var final_this = this;
90
-
91
- var callbacks =
92
- {
93
- always: [],
94
- done: [],
95
- fail: [],
96
- progress: [],
97
- };
98
-
99
- // Private function to run registered callbacks.
100
- function apply_callbacks (name, args, current_this)
101
- {
102
- if (args === undefined)
103
- {
104
- args = final_args;
105
- }
106
- if (current_this === undefined)
107
- {
108
- current_this = final_this;
109
- }
110
- var cbs = callbacks[name];
111
- if (cbs && cbs.length)
112
- {
113
- for (var i = 0; i < cbs.length; i++)
114
- {
115
- var cb = cbs[i];
116
- cb.apply(current_this, args);
117
- }
118
- }
119
- }
120
-
121
- // Private function to create a listener.
122
- function create_listener (name, validStates)
123
- {
124
- var listener = function ()
125
- {
126
- var args = Array.prototype.slice.call(arguments);
127
- for (var i = 0; i < args.length; i++)
128
- {
129
- if (typeof args[i] === O && Array.isArray(args[i]))
130
- { // An array of callbacks, recurse them.
131
- listener.apply(self, args[i]);
132
- }
133
- else if (typeof args[i] === F)
134
- {
135
- if (state === 'pending')
136
- { // Add the callback to the appropriate listener queue.
137
- callbacks[name].push(args[i]);
138
- }
139
- else if (validStates.indexOf(state) != -1)
140
- { // Execute the callback now.
141
- args[i].apply(final_this, final_args);
142
- }
143
- }
144
- else
145
- {
146
- console.warn("Unhandled parameter passed to "+name, args[i]);
147
- }
148
- }
149
- return self;
150
- }
151
- return listener;
152
- }
153
-
154
- // Add our event assignment methods.
155
- var meths =
156
- {
157
- done: ['resolved'],
158
- fail: ['rejected'],
159
- always: ['resolved', 'rejected'],
160
- progress: [],
161
- };
162
- for (var mname in meths)
163
- {
164
- var mstate = meths[mname];
165
- self[mname] = create_listener(mname, mstate);
166
- }
167
-
168
- // Add our trigger methods.
169
- self.resolve = function ()
170
- {
171
- if (state === 'pending')
172
- {
173
- var args = Array.prototype.slice.call(arguments);
174
- if (args.length === 1 && typeof args[0] === O &&
175
- typeof args[0].then === F)
176
- { // Passed a promise. We'll 'then' it, and resolve again from it.
177
- args[0].then(function ()
178
- {
179
- self.resolve.apply(self, arguments);
180
- });
181
- }
182
- else
183
- { // Not a promise, let's do this.
184
- state = 'resolved';
185
- final_args = args;
186
- apply_callbacks('always');
187
- apply_callbacks('done');
188
- }
189
- }
190
- }
191
-
192
- self.resolveWith = function (withObj)
193
- {
194
- if (state === 'pending')
195
- {
196
- final_this = withObj;
197
- var resolveArgs = Array.prototype.slice.call(arguments, 1);
198
- self.resolve.apply(self, resolveArgs);
199
- }
200
- }
201
-
202
- self.reject = function ()
203
- {
204
- if (state === 'pending')
205
- {
206
- var args = Array.prototype.slice.call(arguments);
207
- if (args.length === 1 && typeof args[0] === O &&
208
- typeof args[0].then === F)
209
- { // Passed a promise. We'll 'then' it, and resolve again from it.
210
- args[0].then(null, function ()
211
- {
212
- self.reject.apply(self, arguments);
213
- });
214
- }
215
- else
216
- { // Not a promise, let's do this.
217
- state = 'rejected';
218
- final_args = args;
219
- apply_callbacks('always');
220
- apply_callbacks('fail');
221
- }
222
- }
223
- }
224
-
225
- self.rejectWith = function (withObj)
226
- {
227
- if (state === 'pending')
228
- {
229
- final_this = withObj;
230
- var rejectArgs = Array.prototype.slice.call(arguments, 1);
231
- self.reject.apply(self, rejectArgs);
232
- }
233
- }
234
-
235
- self.notify = function ()
236
- {
237
- if (state === 'pending')
238
- {
239
- var args = Array.prototype.slice.call(arguments);
240
- if (args.length === 1 && typeof args[0] === O &&
241
- typeof args[0].then === F)
242
- { // Passed a promise. We'll 'then' it, and resolve again from it.
243
- args[0].then(null, null, function ()
244
- {
245
- self.notify.apply(self, arguments);
246
- });
247
- }
248
- else
249
- { // Not a promise, let's do this.
250
- apply_callbacks('progress', args);
251
- }
252
- }
253
- }
254
-
255
- self.notifyWith = function (withObj)
256
- {
257
- if (state === 'pending')
258
- {
259
- var args = Array.prototype.slice.call(arguments, 1);
260
- if (args.length === 1 && typeof args[0] === O &&
261
- typeof args[0].then === F)
262
- { // Passed a promise. We'll 'then' it, and resolve again from it.
263
- args[0].then(null, null, function ()
264
- {
265
- var subargs = Array.prototype.slice.call(arguments);
266
- subargs.unshift(withObj);
267
- self.notifyWith.apply(self, subargs);
268
- });
269
- }
270
- else
271
- { // Not a promise, let's do this.
272
- apply_callbacks('progress', args, withObj);
273
- }
274
- }
275
- }
276
-
277
- self.catch = function (failFilter)
278
- {
279
- return self.then(null, failFilter);
280
- }
281
-
282
- self.then = function (doneFilter, failFilter, progressFilter)
283
- {
284
- var newPromise = new LumPromise(false);
285
-
286
- function make_callback (filterSpec)
287
- {
288
- var callback = function ()
289
- {
290
- var useWith = (this !== self);
291
- var args = Array.prototype.slice.call(arguments);
292
- var result = filterSpec.filter.apply(this, args);
293
- if (useWith)
294
- {
295
- newPromise[filterSpec.methodWith](this, result);
296
- }
297
- else
298
- {
299
- newPromise[filterSpec.methodName](result);
300
- }
301
- }
302
- return callback;
303
- }
304
-
305
- var filterSpecs =
306
- {
307
- done:
308
- {
309
- filter: doneFilter,
310
- methodName: 'resolve',
311
- methodWith: 'resolveWith',
312
- },
313
- fail:
314
- {
315
- filter: failFilter,
316
- methodName: 'reject',
317
- methodWith: 'rejectWith',
318
- },
319
- progress:
320
- {
321
- filter: progressFilter,
322
- methodName: 'notify',
323
- methodWith: 'notifyWith',
324
- },
325
- };
326
-
327
- for (var onName in filterSpecs)
328
- {
329
- var filterSpec = filterSpecs[onName];
330
- if (typeof filterSpec.filter === F)
331
- {
332
- var callback = make_callback(filterSpec);
333
- self[onName](callback);
334
- }
335
- }
336
-
337
- return newPromise;
338
- }
339
-
340
- self.state = function ()
341
- {
342
- return state;
343
- }
344
- } // _extendInternal()
345
-
346
- /**
347
- * Execute a delayed 'done' action.
348
- *
349
- * @param mixed obj The object to send to the done().
350
- * @param str ts The textStatus to send to done().
351
- * @param mixed xhr Object to use as XHR (default is this.)
352
- * @param int timeout The timeout (in ms) defaults to 5.
353
- *
354
- * @alias module:@lumjs/compat/v4/promise#deferDone
355
- */
356
- lpp.deferDone = function (obj, ts, xhr, timeout)
357
- {
358
- var self = this;
359
- if (timeout === undefined)
360
- timeout = 5; // 5ms should be enough time to register .done events.
361
- if (xhr === undefined)
362
- xhr = self;
363
- self.doneTimer = setTimeout(function()
364
- {
365
- self.resolve(obj, ts, xhr);
366
- }, timeout);
367
- }
368
-
369
- /**
370
- * Execute a delayed 'fail' action.
371
- *
372
- * @param mixed error The message, code, or object to send to fail().
373
- * @param str ts The textStatus to send to fail().
374
- * @param mixed xhr Object to use as XHR (default is this.)
375
- * @param int timeout The timeout (in ms) defaults to 5.
376
- *
377
- * @alias module:@lumjs/compat/v4/promise#deferFail
378
- */
379
- lpp.deferFail = function (error, ts, xhr, timeout)
380
- {
381
- var self = this;
382
- if (timeout === undefined)
383
- timeout = 5;
384
- if (xhr === undefined)
385
- xhr = self;
386
- self.failTimer = setTimeout(function ()
387
- {
388
- self.reject(xhr, ts, error);
389
- }, timeout);
390
- }
391
-
392
- module.exports = LumPromise;
package/lib/v4/prop.js DELETED
@@ -1,164 +0,0 @@
1
- const core = require('@lumjs/core');
2
- const {isObj,isNil,notNil,isProperty,unbound,F} = core.types;
3
- const {cloneIfLocked} = core.obj;
4
-
5
- const {DESC,getDescriptor} = require('./descriptors');
6
-
7
- /**
8
- * A magic wrapper for Object.defineProperty()
9
- *
10
- * Rather than documenting the arguments in the usual manner, I'm
11
- * going to simply show all of the ways this method can be called.
12
- *
13
- * Anywhere the `target` parameter is shown, this parameter can be an `object` or `function`.
14
- * It's the target to which we're adding new properties.
15
- *
16
- * Anywhere the `property` parameter is shown, this parameter can be specified in two different
17
- * forms. The first and simplest is as a `string` in which case it's simply the name of the property we're adding.
18
- * The second more advanced form is as an `object`. If it is specified as an object, then it is a set of special options.
19
- * In this case, a property of that `property` object called `name` will be used as the name of the property.
20
- * If the `name` property is absent or `undefined`, it's the same as not passing the `property` parameter at all,
21
- * and a *bound* function will be returned, using the custom options as its bound defaults.
22
- *
23
- * See below the usage
24
- *
25
- * `Lum.prop(object)`
26
- *
27
- * Return a function that is a bound copy of this function with
28
- * the object as it's first parameter.
29
- *
30
- * `Lum.prop(object, property)`
31
- *
32
- * Add a property to the object which is mapped to a bound copy of
33
- * this function with the object as it's first parameter.
34
- *
35
- * `Lum.prop(object, property, function, function)`
36
- *
37
- * Add a getter and setter property with the default descriptor.
38
- *
39
- * `Lum.prop(object, property, function, function, object)`
40
- *
41
- * Add a getter and setter property with specified Descriptor options.
42
- * Do not use `get`, `set`, or `value` in the descriptor options.
43
- *
44
- * `Lum.prop(object, property, function, null, object)`
45
- *
46
- * Add a getter only with specified descriptor options.
47
- * Same restrictions to the descriptor options as above.
48
- * You can specify `{}` as the descriptor options to use the defaults.
49
- *
50
- * `Lum.prop(object, property, null, function, object)`
51
- *
52
- * Add a setter only with specified descriptor options.
53
- * Same restrictions as above, and again you can use `{}` for defaults.
54
- *
55
- * `Lum.prop(object, property, !null)`
56
- *
57
- * Add a property with the specified non-null value.
58
- * This uses the default descriptor.
59
- *
60
- * `Lum.prop(object, property, !null, object)`
61
- *
62
- * Add a property value with the specified descriptor options.
63
- * Has the same restrictions to the descriptor options as above.
64
- *
65
- * `Lum.prop(object, property, null, object)`
66
- *
67
- * Add a property using the descriptor object alone.
68
- * Use the newer and shorter `def()` function instead.
69
- *
70
- * `Lum.prop(object, property, Descriptor)`
71
- *
72
- * If you use `DESC.make()` or `descriptor()` to build a magic
73
- * descriptor object, you can pass it and it'll be used with
74
- * a few bonus features I need to document that aren't supported by
75
- * the otherwise equivalent `def(object,property,descriptor)` call.
76
- *
77
- * @exports module:@lumjs/compat/v4/prop
78
- * @deprecated Replaced by `@lumjs/core.def` method.
79
- */
80
- function prop(obj, name, arg1, arg2, arg3)
81
- {
82
- let opts;
83
-
84
- const isUnbound = unbound(this, true, true);
85
-
86
- if (isObj(name))
87
- { // A way to set some special options.
88
- opts = name;
89
- name = opts.name;
90
- }
91
- else if (isUnbound)
92
- { // Use the default options.
93
- opts = isObj(prop.options) ? prop.options : {};
94
- }
95
- else if (isObj(this))
96
- { // This is already a bound copy, so `this` is the options.
97
- opts = this;
98
- }
99
- else
100
- { // Something weird is going on here...
101
- throw new Error("Invalid `this` in a prop() function call");
102
- }
103
-
104
- if (isNil(name))
105
- { // A special case, returns a copy of this function bound to the object.
106
- return prop.bind(opts, obj);
107
- }
108
- else if (!isProperty(name))
109
- { // The property must in every other case be a string.
110
- throw new Error("property name must be a string or Symbol");
111
- }
112
-
113
- let desc;
114
-
115
- if (arg1 === undefined && arg2 === undefined)
116
- { // Another special case, the property is a bound version of this.
117
- return prop(obj, name, prop.bind(opts, obj));
118
- }
119
- else if (DESC.is(arg1) && arg2 === undefined)
120
- { // Yet another special case.
121
- if (arg1.isReady)
122
- { // Already has a value or get/set properties assigned.
123
- desc = arg1;
124
- }
125
- else
126
- { // We'll need to call setValue(), setGetter(), etc, then done().
127
- return arg1.whenDone(function()
128
- {
129
- return prop(obj, name, this);
130
- });
131
- }
132
- }
133
- else if (typeof arg1 === F && typeof arg2 === F)
134
- { // A getter and setter were specified.
135
- desc = getDescriptor(isObj(arg3) ? cloneIfLocked(arg3) : DESC.CONF);
136
- desc.setAccessor(arg1, arg2);
137
- }
138
- else if (isObj(arg3))
139
- { // A custom descriptor for an accessor, find the accessor.
140
- desc = getDescriptor(cloneIfLocked(arg3));
141
- if (typeof arg1 === F)
142
- { // A getter-only accessor.
143
- desc.setGetter(arg1);
144
- }
145
- else if (typeof arg2 === F)
146
- { // A setter-only accessor.
147
- desc.setSetter(arg2);
148
- }
149
- }
150
- else
151
- { // Not a getter/setter, likely a standard value.
152
- desc = getDescriptor(isObj(arg2) ? cloneIfLocked(arg2) : DESC.CONF);
153
-
154
- if (notNil(arg1))
155
- { // If you really want a null 'value', use a custom descriptor.
156
- desc.setValue(arg1);
157
- }
158
- }
159
-
160
- // If we reached here, we should have a valid descriptor now.
161
- return Object.defineProperty(obj, name, desc);
162
- }
163
-
164
- module.exports = prop;