@buley/dash 0.0.30
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/.coveralls.yml +1 -0
- package/.github/workflows/opencommit.yml +33 -0
- package/.github/workflows/testing.yml +20 -0
- package/.gitmodules +0 -0
- package/README.md +98 -0
- package/behaviors/cache.dev.js +282 -0
- package/behaviors/changes.dev.js +337 -0
- package/behaviors/collect.dev.js +40 -0
- package/behaviors/examples/async.dev.js +17 -0
- package/behaviors/firebase.dev.js +283 -0
- package/behaviors/live.dev.js +67 -0
- package/behaviors/map.dev.js +54 -0
- package/behaviors/mapreduce.dev.js +68 -0
- package/behaviors/match.dev.js +66 -0
- package/behaviors/patch.dev.js +69 -0
- package/behaviors/rest.dev.js +340 -0
- package/behaviors/shorthand.dev.js +59 -0
- package/behaviors/stats.dev.js +672 -0
- package/dist/behaviors/index.js +142 -0
- package/dist/database/index.js +76 -0
- package/dist/databases/index.js +121 -0
- package/dist/entry/index.js +166 -0
- package/dist/index.js +93 -0
- package/dist/indexes/index.js +153 -0
- package/dist/store/index.js +97 -0
- package/dist/stores/index.js +90 -0
- package/dist/utilities/index.js +174 -0
- package/documentation/database/closing.md +3 -0
- package/documentation/database/getting.md +1 -0
- package/documentation/database/opening.md +5 -0
- package/documentation/database/removing.md +3 -0
- package/documentation/databases.md +21 -0
- package/documentation/entries.md +13 -0
- package/documentation/entry/adding.md +1 -0
- package/documentation/entry/getting.md +4 -0
- package/documentation/entry/putting.md +0 -0
- package/documentation/entry/removing.md +3 -0
- package/documentation/entry/updating.md +0 -0
- package/documentation/general/security.md +10 -0
- package/documentation/general/transaction/requests.md +3 -0
- package/documentation/general/transactions.md +5 -0
- package/documentation/index/creating.md +1 -0
- package/documentation/index/getting.md +1 -0
- package/documentation/index/iterating.md +1 -0
- package/documentation/index/removing.md +1 -0
- package/documentation/indexes.md +3 -0
- package/documentation/key/cursors.md +5 -0
- package/documentation/key/range/bounds.md +11 -0
- package/documentation/key/range/direction.md +1 -0
- package/documentation/key/ranges.md +3 -0
- package/documentation/keys.md +12 -0
- package/documentation/objectstore/clearing.md +1 -0
- package/documentation/objectstore/creating.md +1 -0
- package/documentation/objectstore/getting.md +1 -0
- package/documentation/objectstore/iteration.md +1 -0
- package/documentation/objectstore/removing.md +1 -0
- package/documentation/overview.md +5 -0
- package/documentation/stores.md +13 -0
- package/jest.config.js +12 -0
- package/package.json +40 -0
- package/src/behaviors/index.ts +140 -0
- package/src/database/index.ts +81 -0
- package/src/databases/index.ts +127 -0
- package/src/entry/index.ts +183 -0
- package/src/index/index.ts +61 -0
- package/src/index.ts +96 -0
- package/src/indexes/index.ts +151 -0
- package/src/store/index.ts +102 -0
- package/src/stores/index.ts +90 -0
- package/src/utilities/index.ts +349 -0
- package/tests/behaviors/behaviors.spec.ts +123 -0
- package/tests/database/database.spec.ts +177 -0
- package/tests/databases/databases.spec.ts +199 -0
- package/tests/entry/entry.spec.ts +252 -0
- package/tests/index/index.spec.ts +94 -0
- package/tests/indexes/indexes.spec.ts +203 -0
- package/tests/store/store.spec.ts +164 -0
- package/tests/stores/stores.spec.ts +148 -0
- package/tests/utilities/clone.spec.ts +48 -0
- package/tests/utilities/cloneError.spec.ts +33 -0
- package/tests/utilities/contains.spec.ts +28 -0
- package/tests/utilities/exists.spec.ts +21 -0
- package/tests/utilities/is.spec.ts +37 -0
- package/tests/utilities/isArray.spec.ts +21 -0
- package/tests/utilities/isBoolean.spec.ts +23 -0
- package/tests/utilities/isEmpty.spec.ts +45 -0
- package/tests/utilities/isFunction.spec.ts +30 -0
- package/tests/utilities/isNumber.spec.ts +29 -0
- package/tests/utilities/isObject.spec.ts +42 -0
- package/tests/utilities/isRegEx.spec.ts +33 -0
- package/tests/utilities/isString.spec.ts +25 -0
- package/tests/utilities/isnt.spec.ts +50 -0
- package/tests/utilities/randomId.spec.ts +39 -0
- package/tests/utilities/safeApply.spec.ts +49 -0
- package/tests/utilities/safeEach.spec.ts +38 -0
- package/tests/utilities/safeIterate.spec.ts +47 -0
- package/tsconfig.json +16 -0
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
var dashLive = (function (environment) {
|
|
2
|
+
"use strict";
|
|
3
|
+
var that,
|
|
4
|
+
liveMap = {},
|
|
5
|
+
live = function(ste) {
|
|
6
|
+
var ctx = ste.context,
|
|
7
|
+
fn = function(live_ctx) {
|
|
8
|
+
if (!liveMap[ ste.context.liveid ]) {
|
|
9
|
+
return live_ctx;
|
|
10
|
+
}
|
|
11
|
+
var ditto = that.clone(ste);
|
|
12
|
+
ditto.type = that.contains(['get.entries', 'update.entries', 'remove.entries'], ditto.method) ? 'notify' : 'resolve';
|
|
13
|
+
ditto.context = live_ctx.context;
|
|
14
|
+
ditto.context.zombie = 'braaains';
|
|
15
|
+
liveMap[ ste.context.liveid ][ditto.type](ditto);
|
|
16
|
+
};
|
|
17
|
+
fn.ready = false;
|
|
18
|
+
return fn;
|
|
19
|
+
};
|
|
20
|
+
return [ function (state) {
|
|
21
|
+
that = this;
|
|
22
|
+
if(this.isnt(state.context.live, true)) {
|
|
23
|
+
return state;
|
|
24
|
+
}
|
|
25
|
+
state.context.liveid = this.random();
|
|
26
|
+
var lives = live(this.clone(state));
|
|
27
|
+
liveMap[ state.context.liveid ] = lives;
|
|
28
|
+
if (this.isArray(state.context.changes)) {
|
|
29
|
+
state.context.changes.push(lives);
|
|
30
|
+
} else if (this.isFunction(state.context.changes)) {
|
|
31
|
+
state.context.changes = [state.context.changes, lives];
|
|
32
|
+
} else {
|
|
33
|
+
state.context.changes = [lives];
|
|
34
|
+
}
|
|
35
|
+
return state;
|
|
36
|
+
}, function (state) {
|
|
37
|
+
that = this;
|
|
38
|
+
if(this.isEmpty(state.context.liveid)) {
|
|
39
|
+
return state;
|
|
40
|
+
}
|
|
41
|
+
var promise = state.promise,
|
|
42
|
+
deferred = this.deferred(),
|
|
43
|
+
removeChanges = function(ste) {
|
|
44
|
+
if(that.isArray(ste.context.changes)) {
|
|
45
|
+
that.each(ste.context.changes, function(el, i) {
|
|
46
|
+
if (that.is(ste, liveMap[ ste.context.liveid ])) {
|
|
47
|
+
delete ste.context.changes[ i ];
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
delete ste.context.liveid;
|
|
52
|
+
return ste;
|
|
53
|
+
};
|
|
54
|
+
if (this.contains(['resolve', 'error'], state.type)) {
|
|
55
|
+
liveMap[ state.context.liveid ] = deferred;
|
|
56
|
+
}
|
|
57
|
+
state.promise = deferred.promise;
|
|
58
|
+
promise(function(ctx) {
|
|
59
|
+
deferred.resolve(removeChanges(ctx));
|
|
60
|
+
}, function(ctx) {
|
|
61
|
+
deferred.error(removeChanges(ctx));
|
|
62
|
+
}, function(ctx) {
|
|
63
|
+
deferred.notify(removeChanges(ctx));
|
|
64
|
+
});
|
|
65
|
+
return removeChanges(state);
|
|
66
|
+
} ];
|
|
67
|
+
}(self));
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
var dashMap = (function (environment) {
|
|
2
|
+
"use strict";
|
|
3
|
+
var mapMap = {}; //heh
|
|
4
|
+
return [ function (state) {
|
|
5
|
+
if(this.isEmpty(state.context.map) || !this.isEmpty(state.context.reduce)) {
|
|
6
|
+
return state;
|
|
7
|
+
}
|
|
8
|
+
state.context.mapid = this.random();
|
|
9
|
+
mapMap[ state.context.mapid ] = this.isArray(state.context.map) ? state.context.map : [state.context.map];
|
|
10
|
+
delete state.context.map;
|
|
11
|
+
return state;
|
|
12
|
+
}, function (state) {
|
|
13
|
+
if(this.isEmpty(state.context.mapid) || !this.isEmpty(state.context.reduce)) {
|
|
14
|
+
return state;
|
|
15
|
+
}
|
|
16
|
+
if (this.exists(state.context.entry)) {
|
|
17
|
+
var deferred = this.deferred(),
|
|
18
|
+
result = state.context.entry,
|
|
19
|
+
promise = state.promise,
|
|
20
|
+
results = [],
|
|
21
|
+
promises = [],
|
|
22
|
+
that = this;
|
|
23
|
+
this.each(mapMap[ state.context.mapid ], function(fn) {
|
|
24
|
+
result = that.apply(fn, [ result ]);
|
|
25
|
+
if (that.isFunction(result)) {
|
|
26
|
+
promises.push(result);
|
|
27
|
+
} else {
|
|
28
|
+
results.push(result);
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
if (this.isEmpty(promises)) {
|
|
32
|
+
state.context.mapped = this.is(results.length, 1) ? results[0] : results;
|
|
33
|
+
} else {
|
|
34
|
+
this.each(promises, function(pro) {
|
|
35
|
+
promise(function(result) {
|
|
36
|
+
results.push(result);
|
|
37
|
+
});
|
|
38
|
+
promise = pro;
|
|
39
|
+
});
|
|
40
|
+
state.context.promise = promise(function(ctx) {
|
|
41
|
+
ctx.mapped = that.is(results.length, 1) ? results[0] : results;
|
|
42
|
+
deferred.resolve(ctx);
|
|
43
|
+
}, function(ctx) {
|
|
44
|
+
deferred.reject(ctx);
|
|
45
|
+
}, function(ctx) {
|
|
46
|
+
deferred.notify(ctx);
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
delete mapMap[ state.context.mapid ];
|
|
50
|
+
delete state.context.mapid;
|
|
51
|
+
}
|
|
52
|
+
return state;
|
|
53
|
+
} ];
|
|
54
|
+
}(self));
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
var dashMapReduce = (function (environment) {
|
|
2
|
+
"use strict";
|
|
3
|
+
var mapReduceMap = {};
|
|
4
|
+
return [ function (state) {
|
|
5
|
+
if(this.isEmpty(state.context.map) || this.isEmpty(state.context.reduce)) {
|
|
6
|
+
return state;
|
|
7
|
+
}
|
|
8
|
+
state.context.mapReduceId = this.random();
|
|
9
|
+
mapReduceMap[ state.context.mapReduceId ] = mapReduceMap[ state.context.mapReduceId ] || {};
|
|
10
|
+
mapReduceMap[ state.context.mapReduceId ].intermediate = mapReduceMap[ state.context.mapReduceId ].intermediate || null;
|
|
11
|
+
mapReduceMap[ state.context.mapReduceId ].mappers = this.isArray(state.context.map) ? state.context.map : [state.context.map];
|
|
12
|
+
mapReduceMap[ state.context.mapReduceId ].reducers = this.isArray(state.context.reduce) ? state.context.reduce : [state.context.reduce];
|
|
13
|
+
delete state.context.mapReduce;
|
|
14
|
+
return state;
|
|
15
|
+
}, function (state) {
|
|
16
|
+
if(this.isEmpty(state.context.mapReduceId)) {
|
|
17
|
+
return state;
|
|
18
|
+
}
|
|
19
|
+
var deferred = this.deferred(),
|
|
20
|
+
result = state.context.entry,
|
|
21
|
+
promise = state.promise,
|
|
22
|
+
results = [],
|
|
23
|
+
maps = [],
|
|
24
|
+
finalized,
|
|
25
|
+
promises = [],
|
|
26
|
+
that = this;
|
|
27
|
+
if (this.is(state.type, 'notify') && this.exists(state.context.entry)) {
|
|
28
|
+
this.each(mapReduceMap[ state.context.mapReduceId ].mappers, function(mapper) {
|
|
29
|
+
result = that.apply(mapper, [ result ]);
|
|
30
|
+
if (that.isFunction(result)) {
|
|
31
|
+
promises.push(result);
|
|
32
|
+
} else {
|
|
33
|
+
maps.push(result);
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
this.each(mapReduceMap[ state.context.mapReduceId ].reducers, function(reducer) {
|
|
37
|
+
result = that.apply(reducer, [ mapReduceMap[ state.context.mapReduceId ].intermediate, result ]);
|
|
38
|
+
if (that.isFunction(result)) {
|
|
39
|
+
promises.push(result);
|
|
40
|
+
} else {
|
|
41
|
+
mapReduceMap[ state.context.mapReduceId ].intermediate = result;
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
if (this.isEmpty(promises)) {
|
|
45
|
+
state.context.mapped = maps;
|
|
46
|
+
} else {
|
|
47
|
+
this.each(promises, function(pro) {
|
|
48
|
+
promise(function(result) {
|
|
49
|
+
results.push(result);
|
|
50
|
+
});
|
|
51
|
+
promise = pro;
|
|
52
|
+
});
|
|
53
|
+
state.context.promise = promise(function(ctx) {
|
|
54
|
+
state.context = ctx;
|
|
55
|
+
delete mapReduceMap[ state.context.mapReduceId ];
|
|
56
|
+
delete state.context.mapReduceId;
|
|
57
|
+
deferred.resolve(state);
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
} else if ( this.is(state.type, 'resolve')) {
|
|
61
|
+
state.context.reduced = mapReduceMap[ state.context.mapReduceId ].intermediate;
|
|
62
|
+
}
|
|
63
|
+
if ( state.context.mapReduceId ) {
|
|
64
|
+
delete state.context.mapReduceId;
|
|
65
|
+
}
|
|
66
|
+
return state;
|
|
67
|
+
} ];
|
|
68
|
+
}(self));
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
var dashMatch = (function (environment) {
|
|
2
|
+
"use strict";
|
|
3
|
+
return [ null, function (state) {
|
|
4
|
+
if(this.isEmpty(state.context.match)) {
|
|
5
|
+
return state;
|
|
6
|
+
}
|
|
7
|
+
var promise = state.promise,
|
|
8
|
+
deferred = this.deferred(),
|
|
9
|
+
that = this,
|
|
10
|
+
reduce = function(expression, context) {
|
|
11
|
+
var maybeReduce = function(expr) {
|
|
12
|
+
if (that.isFunction(expr)) {
|
|
13
|
+
expr = that.apply(expr, [context], that);
|
|
14
|
+
}
|
|
15
|
+
if (that.isObject(expr)) {
|
|
16
|
+
that.iterate(expr, function(key, value) {
|
|
17
|
+
expr[key] = maybeReduce(value, context);
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
return expr;
|
|
21
|
+
}
|
|
22
|
+
return maybeReduce(expression, context);
|
|
23
|
+
},
|
|
24
|
+
match = function(expr, data) {
|
|
25
|
+
var matches = true;
|
|
26
|
+
if (that.isEmpty(data)) {
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
var ok = true, any = false;
|
|
30
|
+
that.iterate(expr, function(key, val) {
|
|
31
|
+
if ( !that.exists(data[key]) ) {
|
|
32
|
+
ok = false;
|
|
33
|
+
}
|
|
34
|
+
if ( that.isObject(val) ) {
|
|
35
|
+
ok = match(val, data[key]);
|
|
36
|
+
if ( ok ) {
|
|
37
|
+
any = true;
|
|
38
|
+
}
|
|
39
|
+
} else if ( that.isRegEx(val) ) {
|
|
40
|
+
if ( that.isnt(data[key], val) && !that.exists(data[key]) || null === data[ key ].match(val) ) {
|
|
41
|
+
ok = false;
|
|
42
|
+
} else {
|
|
43
|
+
any = true;
|
|
44
|
+
}
|
|
45
|
+
} else if ( data[key] !== val) {
|
|
46
|
+
ok = false;
|
|
47
|
+
} else {
|
|
48
|
+
any = true;
|
|
49
|
+
}
|
|
50
|
+
} );
|
|
51
|
+
return that.is(state.context.any, true) ? any : ok;
|
|
52
|
+
},
|
|
53
|
+
reduced;
|
|
54
|
+
promise(function(st) {
|
|
55
|
+
reduced = reduce(st.context.match, st.context);
|
|
56
|
+
if ( 'notify' === st.type && !match(reduced, st.context.entry) ) {
|
|
57
|
+
st.type = null;
|
|
58
|
+
deferred.reject(st);
|
|
59
|
+
} else {
|
|
60
|
+
deferred.resolve(st);
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
state.promise = deferred.promise;
|
|
64
|
+
return state;
|
|
65
|
+
} ];
|
|
66
|
+
}(self));
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
var dashPatch = (function (environment) {
|
|
2
|
+
"use strict";
|
|
3
|
+
//TODO: Support array of sequential patches
|
|
4
|
+
var patchMap = {};
|
|
5
|
+
return [ function (state) {
|
|
6
|
+
if(this.isEmpty(state.context.patch)) {
|
|
7
|
+
return state;
|
|
8
|
+
}
|
|
9
|
+
var outside = this.deferred(),
|
|
10
|
+
inside = this.deferred(),
|
|
11
|
+
result,
|
|
12
|
+
promise = state.promise,
|
|
13
|
+
promises = [],
|
|
14
|
+
that = this;
|
|
15
|
+
state.context.patchid = this.random();
|
|
16
|
+
patchMap[ state.context.patchid ] = this.isArray(state.context.patch) ? { before: state.context.patch[0], after: state.context.patch[1] } : { before: state.context.patch, after: state.context.patch };
|
|
17
|
+
state.promise = outside.promise;
|
|
18
|
+
result = this.apply(patchMap[ state.context.patchid ].before, [ state ]);
|
|
19
|
+
if (this.is(result.promise, state.promise)) {
|
|
20
|
+
result.promise = promise;
|
|
21
|
+
state = result;
|
|
22
|
+
} else {
|
|
23
|
+
result(function(ctx) {
|
|
24
|
+
state.context.promise = promise;
|
|
25
|
+
outside.resolve(ctx);
|
|
26
|
+
}, function(ctx) {
|
|
27
|
+
state.context.promise = promise;
|
|
28
|
+
outside.reject(ctx);
|
|
29
|
+
}, function(ctx) {
|
|
30
|
+
state.context.promise = promise;
|
|
31
|
+
outside.notify(ctx);
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
return state;
|
|
35
|
+
}, function (state) {
|
|
36
|
+
if(this.isEmpty(state.context.patchid)) {
|
|
37
|
+
return state;
|
|
38
|
+
}
|
|
39
|
+
if (!patchMap[ state.context.patchid ].after) {
|
|
40
|
+
delete state.context.patchid;
|
|
41
|
+
delete patchMap[ state.context.patchid ];
|
|
42
|
+
return state;
|
|
43
|
+
}
|
|
44
|
+
var outside = this.deferred(),
|
|
45
|
+
inside = this.deferred(),
|
|
46
|
+
result,
|
|
47
|
+
promise = state.promise,
|
|
48
|
+
promises = [],
|
|
49
|
+
that = this;
|
|
50
|
+
state.promise = inside;
|
|
51
|
+
result = this.apply(patchMap[ state.context.patchid ].after, [ state ]);
|
|
52
|
+
if (this.is(result.promise, state.promise)) {
|
|
53
|
+
result.promise = promise;
|
|
54
|
+
state = result;
|
|
55
|
+
} else {
|
|
56
|
+
state.context.promise = outside.promise;
|
|
57
|
+
result(function(ctx2) {
|
|
58
|
+
delete patchMap[ ctx2.context.patchid ];
|
|
59
|
+
delete ctx2.context.patchid;
|
|
60
|
+
outside.resolve(ctx2);
|
|
61
|
+
}, function(ctx2) {
|
|
62
|
+
outside.reject(ctx2);
|
|
63
|
+
}, function(ctx2) {
|
|
64
|
+
outside.notify(ctx2);
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
return state;
|
|
68
|
+
} ];
|
|
69
|
+
}(self));
|
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
self.dashRest = self.dashRest || (function (environment) {
|
|
2
|
+
"use strict";
|
|
3
|
+
var that,
|
|
4
|
+
rest = {},
|
|
5
|
+
ajax = function( context ) {
|
|
6
|
+
var request_type = context.method,
|
|
7
|
+
url = context.url,
|
|
8
|
+
input = context.data,
|
|
9
|
+
params = context.params,
|
|
10
|
+
callback = context.callback,
|
|
11
|
+
fallbacks = ['MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP', 'Microsoft.XMLHTTP'],
|
|
12
|
+
request,
|
|
13
|
+
serialize = function (data) {
|
|
14
|
+
var queryString = '',
|
|
15
|
+
attr;
|
|
16
|
+
if ('string' !== typeof data) {
|
|
17
|
+
for (attr in data) {
|
|
18
|
+
if (data.hasOwnProperty(attr)) {
|
|
19
|
+
queryString += "&" + encodeURIComponent(attr) + '=' + encodeURIComponent(data[attr]);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
queryString = queryString.replace(/^&/, '');
|
|
23
|
+
} else {
|
|
24
|
+
queryString = data;
|
|
25
|
+
}
|
|
26
|
+
return queryString;
|
|
27
|
+
},
|
|
28
|
+
qs = serialize(params),
|
|
29
|
+
formencoded = serialize(input),
|
|
30
|
+
i = 0,
|
|
31
|
+
error = null;
|
|
32
|
+
if (environment.XMLHttpRequest) {
|
|
33
|
+
request = new XMLHttpRequest();
|
|
34
|
+
} else {
|
|
35
|
+
for (i = 0; i < fallbacks.length; i++) {
|
|
36
|
+
try {2
|
|
37
|
+
request = new ActiveXObject(fallbacks[i]);
|
|
38
|
+
break;
|
|
39
|
+
} catch (e) {}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
request.addEventListener('readystatechange', function (e) {
|
|
43
|
+
if ('function' === typeof callback && 4 === request.readyState && null !== request.status.toString().match(/^2/)) {
|
|
44
|
+
var json;
|
|
45
|
+
try {
|
|
46
|
+
json = JSON.parse(request.responseText);
|
|
47
|
+
} catch(e) {
|
|
48
|
+
//not json (or bad json)
|
|
49
|
+
}
|
|
50
|
+
error = null;
|
|
51
|
+
callback(json || request.responseText, error, e, request);
|
|
52
|
+
} else if ('function' === typeof callback && 4 === request.readyState && null !== request.status.toString().match(/^[34]/)) {
|
|
53
|
+
var json;
|
|
54
|
+
//TODO: Handle this
|
|
55
|
+
try {
|
|
56
|
+
json = JSON.parse(request.responseText);
|
|
57
|
+
} catch(e) {
|
|
58
|
+
//not json (or bad json)
|
|
59
|
+
}
|
|
60
|
+
error = request.status.toString();
|
|
61
|
+
callback(json || request.responseText, error, e, request);
|
|
62
|
+
}
|
|
63
|
+
}, true);
|
|
64
|
+
|
|
65
|
+
if (request_type.toUpperCase() === 'GET') {
|
|
66
|
+
request.open(request_type, url + (qs ? '?' + qs : ''), true);
|
|
67
|
+
request.send();
|
|
68
|
+
} else {
|
|
69
|
+
request.open(request_type, url + (qs ? '?' + qs : ''), true);
|
|
70
|
+
if (false === context.json) {
|
|
71
|
+
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
|
|
72
|
+
request.send(formencoded);
|
|
73
|
+
} else {
|
|
74
|
+
request.setRequestHeader('Content-Type', 'application/javascript');
|
|
75
|
+
request.send(JSON.stringify(input));
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
},
|
|
80
|
+
get = function( context ) {
|
|
81
|
+
context.method = 'GET';
|
|
82
|
+
ajax(context);
|
|
83
|
+
},
|
|
84
|
+
post = function( context ) {
|
|
85
|
+
context.method = 'POST';
|
|
86
|
+
ajax(context);
|
|
87
|
+
},
|
|
88
|
+
put = function( context ) {
|
|
89
|
+
context.method = 'PUT';
|
|
90
|
+
ajax(context);
|
|
91
|
+
},
|
|
92
|
+
remove = function( context ) {
|
|
93
|
+
context.method = 'DELETE';
|
|
94
|
+
ajax(context);
|
|
95
|
+
},
|
|
96
|
+
whichMethod = function(signature) {
|
|
97
|
+
if ( that.contains( [ 'get.entry', 'get.entries', 'get.index', 'get.database', 'get.store' ], signature)) {
|
|
98
|
+
return 'GET';
|
|
99
|
+
} else if ( that.contains([ 'remove.entry', 'remove.entries', 'remove.index', 'remove.database', 'remove.store' ], signature)) {
|
|
100
|
+
return 'DELETE';
|
|
101
|
+
} else if ( that.contains([ 'add.entry' ], signature)) {
|
|
102
|
+
return 'POST';
|
|
103
|
+
} else if ( that.contains([ 'update.entry', 'update.entries' ], signature)) {
|
|
104
|
+
return 'PUT';
|
|
105
|
+
} else {
|
|
106
|
+
return null;
|
|
107
|
+
}
|
|
108
|
+
},
|
|
109
|
+
scripts = ( !! environment.document) ? environment.document.getElementsByTagName("script") : [],
|
|
110
|
+
libraryScript = scripts[scripts.length - 1] || null,
|
|
111
|
+
libraryPath =( null !== libraryScript && null === libraryScript.src.match(/chrome-extension/) ) ? libraryScript.src : null,
|
|
112
|
+
workerEnvironment = null !== environment.constructor.toString().match(/WorkerGlobalScope/),
|
|
113
|
+
worker,
|
|
114
|
+
workQueue = {},
|
|
115
|
+
workRegister = function (worker, message, context, success, error, notify) {
|
|
116
|
+
var id = that.random(),
|
|
117
|
+
callback = function (e) {
|
|
118
|
+
var data = e.data,
|
|
119
|
+
queued = workQueue[data.uid];
|
|
120
|
+
if (undefined !== queued) {
|
|
121
|
+
switch (e.data.type) {
|
|
122
|
+
case 'success':
|
|
123
|
+
delete workQueue[data.uid];
|
|
124
|
+
worker.removeEventListener('message', callback);
|
|
125
|
+
that.apply(success, [data]);
|
|
126
|
+
break;
|
|
127
|
+
case 'error':
|
|
128
|
+
delete workQueue[data.uid];
|
|
129
|
+
worker.removeEventListener('message', callback);
|
|
130
|
+
that.apply(error, [data]);
|
|
131
|
+
break;
|
|
132
|
+
case 'abort':
|
|
133
|
+
that.apply(notify, [data]);
|
|
134
|
+
break;
|
|
135
|
+
default:
|
|
136
|
+
break;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
},
|
|
140
|
+
clean = function(obj) {
|
|
141
|
+
if (that.isFunction(obj)) {
|
|
142
|
+
return undefined;
|
|
143
|
+
} else if (that.isObject(obj)) {
|
|
144
|
+
that.iterate(obj, function(key, val) {
|
|
145
|
+
obj[ key ] = clean(val);
|
|
146
|
+
});
|
|
147
|
+
} else if (that.isArray(obj)) {
|
|
148
|
+
that.each(obj, function(v, i) {
|
|
149
|
+
obj[i] = clean(v);
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
return obj;
|
|
153
|
+
};
|
|
154
|
+
workQueue[id] = {
|
|
155
|
+
success: success,
|
|
156
|
+
error: error,
|
|
157
|
+
notify: notify
|
|
158
|
+
};
|
|
159
|
+
worker.addEventListener('message', callback);
|
|
160
|
+
worker.postMessage({
|
|
161
|
+
method: message,
|
|
162
|
+
context: clean(context),
|
|
163
|
+
uid: id
|
|
164
|
+
});
|
|
165
|
+
return id;
|
|
166
|
+
},
|
|
167
|
+
workDispatch = function (message, context) {
|
|
168
|
+
var defd = that.deferred(),
|
|
169
|
+
callbacks = {
|
|
170
|
+
on_success: context.on_success,
|
|
171
|
+
on_error: context.on_error,
|
|
172
|
+
on_abort: context.on_abort,
|
|
173
|
+
on_complete: context.on_complete,
|
|
174
|
+
on_upgrade_needed: context.on_upgrade_needed,
|
|
175
|
+
on_blocked: context.on_blocked,
|
|
176
|
+
on_close: context.on_close
|
|
177
|
+
},
|
|
178
|
+
getData = function (data) {
|
|
179
|
+
if( that.isObject(data) ) {
|
|
180
|
+
that.iterate(callbacks, function (key, val) {
|
|
181
|
+
if(!!val) {
|
|
182
|
+
data[key] = val;
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
return data;
|
|
187
|
+
};
|
|
188
|
+
that.iterate(callbacks, function (key, val) {
|
|
189
|
+
delete context[key];
|
|
190
|
+
});
|
|
191
|
+
workRegister(worker, message, context, function (data) {
|
|
192
|
+
var add_ctx = that.clone(context);
|
|
193
|
+
add_ctx.data = data.context.entry;
|
|
194
|
+
add_ctx.resting = true;
|
|
195
|
+
that.api.add.entry(add_ctx)(function(added_ctx) {
|
|
196
|
+
delete added_ctx.resting;
|
|
197
|
+
defd.resolve(getData(added_ctx));
|
|
198
|
+
}, function(added_ctx) {
|
|
199
|
+
delete added_ctx.resting;
|
|
200
|
+
defd.reject(getData(added_ctx));
|
|
201
|
+
}, function(added_ctx) {
|
|
202
|
+
delete added_ctx.resting;
|
|
203
|
+
defd.notify(getData(added_ctx));
|
|
204
|
+
})
|
|
205
|
+
}, function (data) {
|
|
206
|
+
defd.reject(getData(data));
|
|
207
|
+
}, function (data) {
|
|
208
|
+
defd.notify(getData(data));
|
|
209
|
+
});
|
|
210
|
+
return defd.promise;
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
if (true === workerEnvironment) {
|
|
214
|
+
environment.addEventListener('message', function (e) {
|
|
215
|
+
var input = e.data,
|
|
216
|
+
method = input.method,
|
|
217
|
+
value = input.value,
|
|
218
|
+
key = input.key,
|
|
219
|
+
context = input.context,
|
|
220
|
+
params = context.params,
|
|
221
|
+
expires = input.expires,
|
|
222
|
+
end = function (ctx) {
|
|
223
|
+
ctx.type = ctx.type || 'success';
|
|
224
|
+
environment.postMessage(ctx);
|
|
225
|
+
},
|
|
226
|
+
callback = function(sig) {
|
|
227
|
+
return function(data, error) {
|
|
228
|
+
delete input.context.callback;
|
|
229
|
+
if (!!error) {
|
|
230
|
+
input.context.error = error;
|
|
231
|
+
input.context.message = data;
|
|
232
|
+
input.type = 'error';
|
|
233
|
+
} else {
|
|
234
|
+
input.context.entry = data;
|
|
235
|
+
}
|
|
236
|
+
end(input);
|
|
237
|
+
}
|
|
238
|
+
};
|
|
239
|
+
if (method === 'GET' || method === 'PUT' || method === 'POST' || method === 'DELETE') {
|
|
240
|
+
context.callback = callback(method);
|
|
241
|
+
if ( method === 'GET' ) {
|
|
242
|
+
get(context);
|
|
243
|
+
} else if ( method === 'POST' ) {
|
|
244
|
+
post(context);
|
|
245
|
+
} else if ( method === 'PUT' ) {
|
|
246
|
+
put(context);
|
|
247
|
+
} else if ( method === 'DELETE' ) {
|
|
248
|
+
remove(context);
|
|
249
|
+
}
|
|
250
|
+
} else {
|
|
251
|
+
input.type = 'error';
|
|
252
|
+
end({
|
|
253
|
+
type: 'abort',
|
|
254
|
+
context: input,
|
|
255
|
+
error: 'No such method'
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
}, false);
|
|
259
|
+
} else {
|
|
260
|
+
return function(libraryPath) {
|
|
261
|
+
worker = !!libraryPath ? new Worker(libraryPath) : null
|
|
262
|
+
return [ function (state) {
|
|
263
|
+
that = this;
|
|
264
|
+
if(this.isnt(state.context.rest, true) || this.exists(state.context.resting)) {
|
|
265
|
+
return state;
|
|
266
|
+
}
|
|
267
|
+
state.context.restid = this.random();
|
|
268
|
+
rest[ state.context.restid ] = {
|
|
269
|
+
url: state.context.url,
|
|
270
|
+
params: state.context.params ? state.context.params : null
|
|
271
|
+
}
|
|
272
|
+
delete state.context.url;
|
|
273
|
+
delete state.context.params;
|
|
274
|
+
return state;
|
|
275
|
+
}, function (state) {
|
|
276
|
+
if(this.isnt(state.context.rest, true) || this.exists(state.context.resting)) {
|
|
277
|
+
return state;
|
|
278
|
+
}
|
|
279
|
+
var promise = state.promise,
|
|
280
|
+
outward = this.deferred(),
|
|
281
|
+
inward,
|
|
282
|
+
update = false,
|
|
283
|
+
args;
|
|
284
|
+
if (this.contains(['add.entry', 'update.entry', 'update.entries', 'remove.entry', 'remove.entries'], state.method)) {
|
|
285
|
+
if (this.contains(['notify', 'resolve'], state.type)) {
|
|
286
|
+
update = true;
|
|
287
|
+
}
|
|
288
|
+
} else {
|
|
289
|
+
if ((this.is('error', state.type) || (this.isEmpty(state.context.entry) && this.contains(['get.entry', 'get.entries'], state.method))) && (this.is(state.context.rest, true) || this.is(state.context.fallback, true))) {
|
|
290
|
+
update = true;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
args = rest[ state.context.restid ];
|
|
294
|
+
if (update) {
|
|
295
|
+
state.promise = outward.promise;
|
|
296
|
+
state.context.url = args.url;0
|
|
297
|
+
if (that.isFunction(state.context.url)) {
|
|
298
|
+
state.context.url = that.apply(state.context.url, [that.clone(state)]);
|
|
299
|
+
}
|
|
300
|
+
if (that.isFunction(state.context.params)) {
|
|
301
|
+
state.context.params = that.apply(state.context.params, [that.clone(state)]);
|
|
302
|
+
}
|
|
303
|
+
state.context.params = args.params;
|
|
304
|
+
inward = workDispatch( whichMethod(state.method), state.context);
|
|
305
|
+
inward(function(ctx2){
|
|
306
|
+
ctx2.url = args.url;
|
|
307
|
+
ctx2.params = args.params;
|
|
308
|
+
state.context = ctx2;
|
|
309
|
+
state.type = 'resolve';
|
|
310
|
+
delete rest[ ctx2.restid ];
|
|
311
|
+
delete ctx2.restid;
|
|
312
|
+
outward.resolve(state.context);
|
|
313
|
+
}, function(ctx2) {
|
|
314
|
+
ctx2.url = args.url;
|
|
315
|
+
ctx2.params = args.params;
|
|
316
|
+
delete rest[ ctx2.restid ];
|
|
317
|
+
delete ctx2.restid;
|
|
318
|
+
state.context = ctx2;
|
|
319
|
+
state.type = 'error';
|
|
320
|
+
outward.reject(state.context);
|
|
321
|
+
}, function(ctx2) {
|
|
322
|
+
ctx2.url = args.url;
|
|
323
|
+
ctx2.params = args.params;
|
|
324
|
+
delete rest[ ctx2.restid ];
|
|
325
|
+
delete ctx2.restid;
|
|
326
|
+
state.type = 'notify';
|
|
327
|
+
state.context = ctx2;
|
|
328
|
+
outward.notify(state.context);
|
|
329
|
+
});
|
|
330
|
+
} else {
|
|
331
|
+
state.context.url = args.url;
|
|
332
|
+
state.context.params = args.params;
|
|
333
|
+
delete rest[ state.context.restid ];
|
|
334
|
+
delete state.context.restid;
|
|
335
|
+
}
|
|
336
|
+
return state;
|
|
337
|
+
} ];
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
}(self));
|