@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.
Files changed (97) hide show
  1. package/.coveralls.yml +1 -0
  2. package/.github/workflows/opencommit.yml +33 -0
  3. package/.github/workflows/testing.yml +20 -0
  4. package/.gitmodules +0 -0
  5. package/README.md +98 -0
  6. package/behaviors/cache.dev.js +282 -0
  7. package/behaviors/changes.dev.js +337 -0
  8. package/behaviors/collect.dev.js +40 -0
  9. package/behaviors/examples/async.dev.js +17 -0
  10. package/behaviors/firebase.dev.js +283 -0
  11. package/behaviors/live.dev.js +67 -0
  12. package/behaviors/map.dev.js +54 -0
  13. package/behaviors/mapreduce.dev.js +68 -0
  14. package/behaviors/match.dev.js +66 -0
  15. package/behaviors/patch.dev.js +69 -0
  16. package/behaviors/rest.dev.js +340 -0
  17. package/behaviors/shorthand.dev.js +59 -0
  18. package/behaviors/stats.dev.js +672 -0
  19. package/dist/behaviors/index.js +142 -0
  20. package/dist/database/index.js +76 -0
  21. package/dist/databases/index.js +121 -0
  22. package/dist/entry/index.js +166 -0
  23. package/dist/index.js +93 -0
  24. package/dist/indexes/index.js +153 -0
  25. package/dist/store/index.js +97 -0
  26. package/dist/stores/index.js +90 -0
  27. package/dist/utilities/index.js +174 -0
  28. package/documentation/database/closing.md +3 -0
  29. package/documentation/database/getting.md +1 -0
  30. package/documentation/database/opening.md +5 -0
  31. package/documentation/database/removing.md +3 -0
  32. package/documentation/databases.md +21 -0
  33. package/documentation/entries.md +13 -0
  34. package/documentation/entry/adding.md +1 -0
  35. package/documentation/entry/getting.md +4 -0
  36. package/documentation/entry/putting.md +0 -0
  37. package/documentation/entry/removing.md +3 -0
  38. package/documentation/entry/updating.md +0 -0
  39. package/documentation/general/security.md +10 -0
  40. package/documentation/general/transaction/requests.md +3 -0
  41. package/documentation/general/transactions.md +5 -0
  42. package/documentation/index/creating.md +1 -0
  43. package/documentation/index/getting.md +1 -0
  44. package/documentation/index/iterating.md +1 -0
  45. package/documentation/index/removing.md +1 -0
  46. package/documentation/indexes.md +3 -0
  47. package/documentation/key/cursors.md +5 -0
  48. package/documentation/key/range/bounds.md +11 -0
  49. package/documentation/key/range/direction.md +1 -0
  50. package/documentation/key/ranges.md +3 -0
  51. package/documentation/keys.md +12 -0
  52. package/documentation/objectstore/clearing.md +1 -0
  53. package/documentation/objectstore/creating.md +1 -0
  54. package/documentation/objectstore/getting.md +1 -0
  55. package/documentation/objectstore/iteration.md +1 -0
  56. package/documentation/objectstore/removing.md +1 -0
  57. package/documentation/overview.md +5 -0
  58. package/documentation/stores.md +13 -0
  59. package/jest.config.js +12 -0
  60. package/package.json +40 -0
  61. package/src/behaviors/index.ts +140 -0
  62. package/src/database/index.ts +81 -0
  63. package/src/databases/index.ts +127 -0
  64. package/src/entry/index.ts +183 -0
  65. package/src/index/index.ts +61 -0
  66. package/src/index.ts +96 -0
  67. package/src/indexes/index.ts +151 -0
  68. package/src/store/index.ts +102 -0
  69. package/src/stores/index.ts +90 -0
  70. package/src/utilities/index.ts +349 -0
  71. package/tests/behaviors/behaviors.spec.ts +123 -0
  72. package/tests/database/database.spec.ts +177 -0
  73. package/tests/databases/databases.spec.ts +199 -0
  74. package/tests/entry/entry.spec.ts +252 -0
  75. package/tests/index/index.spec.ts +94 -0
  76. package/tests/indexes/indexes.spec.ts +203 -0
  77. package/tests/store/store.spec.ts +164 -0
  78. package/tests/stores/stores.spec.ts +148 -0
  79. package/tests/utilities/clone.spec.ts +48 -0
  80. package/tests/utilities/cloneError.spec.ts +33 -0
  81. package/tests/utilities/contains.spec.ts +28 -0
  82. package/tests/utilities/exists.spec.ts +21 -0
  83. package/tests/utilities/is.spec.ts +37 -0
  84. package/tests/utilities/isArray.spec.ts +21 -0
  85. package/tests/utilities/isBoolean.spec.ts +23 -0
  86. package/tests/utilities/isEmpty.spec.ts +45 -0
  87. package/tests/utilities/isFunction.spec.ts +30 -0
  88. package/tests/utilities/isNumber.spec.ts +29 -0
  89. package/tests/utilities/isObject.spec.ts +42 -0
  90. package/tests/utilities/isRegEx.spec.ts +33 -0
  91. package/tests/utilities/isString.spec.ts +25 -0
  92. package/tests/utilities/isnt.spec.ts +50 -0
  93. package/tests/utilities/randomId.spec.ts +39 -0
  94. package/tests/utilities/safeApply.spec.ts +49 -0
  95. package/tests/utilities/safeEach.spec.ts +38 -0
  96. package/tests/utilities/safeIterate.spec.ts +47 -0
  97. package/tsconfig.json +16 -0
@@ -0,0 +1,337 @@
1
+ var dashChanges = (function (environment) {
2
+ "use strict";
3
+ var callbackMap = {},
4
+ changeMap = {},
5
+ that,
6
+ update = function(type, ctx) {
7
+ if (that.exists(ctx.primary_key) || that.exists(ctx.key)) {
8
+ if (that.contains(['update.entries', 'update.entry'], type)) {
9
+ var key = ctx.primary_key || ctx.key;
10
+ changeMap[ctx.database].stores[ctx.store].entries = changeMap[ctx.database].stores[ctx.store].entries || {};
11
+ changeMap[ctx.database].stores[ctx.store].entries[key] = changeMap[ctx.database].stores[ctx.store].entries[key] || {
12
+ callbacks: []
13
+ };
14
+ if (that.exists(ctx.entry)) {
15
+ changeMap[ctx.database].stores[ctx.store].entries[key].data = ctx.entry;
16
+ }
17
+ }
18
+ }
19
+ },
20
+ unregister = function(type, ctx) {
21
+ if (that.contains(['remove.database'], type)) {
22
+ delete changeMap[ctx.database];
23
+ } else if (that.exists(ctx.store)) {
24
+ if (that.contains(['remove.store'], type)) {
25
+ delete changeMap[ctx.database].stores[ctx.store];
26
+ } else if (that.exists(ctx.index)) {
27
+ if (that.contains(['remove.index'], type)) {
28
+ delete changeMap[ctx.database].stores[ctx.store].indexes[ctx.index];
29
+ } else if (that.exists(ctx.key) && that.isnt(ctx.primary_key, ctx.key)) {
30
+ if (that.contains(['remove.entries', 'remove.entry', 'clear.store'], type)) {
31
+ delete changeMap[ctx.database].stores[ctx.store].indexes[ctx.index].entries[ ctx.key ];
32
+ delete changeMap[ctx.database].stores[ctx.store].entries[ ctx.primary_key ];
33
+ }
34
+ }
35
+ } else if (that.exists(ctx.primary_key) || that.exists(ctx.key)) {
36
+ if (that.contains(['remove.entries', 'remove.entry', 'clear.store'], type)) {
37
+ var key = ctx.primary_key || ctx.key;
38
+ if (that.exists(changeMap[ctx.database])) {
39
+ delete changeMap[ctx.database].stores[ctx.store].entries[key];
40
+ }
41
+ }
42
+ }
43
+ }
44
+ },
45
+ register = function(type, ctx) {
46
+ var obj = ctx.changeid;
47
+ changeMap[ctx.database] = changeMap[ctx.database] || {
48
+ stores: {},
49
+ callbacks: []
50
+ };
51
+ if (that.contains(['get.databases','get.database'], type)) {
52
+ changeMap[ctx.database].callbacks.push(obj);
53
+ }
54
+ if (that.exists(ctx.store)) {
55
+ changeMap[ctx.database].stores[ctx.store] = changeMap[ctx.database].stores[ctx.store] || {
56
+ callbacks: [],
57
+ indexes: {}
58
+ };
59
+ if (that.contains(['get.stores','get.store'], type)) {
60
+ changeMap[ctx.database].stores[ctx.store].callbacks.push(obj);
61
+ }
62
+ if (that.exists(ctx.index)) {
63
+ changeMap[ctx.database].stores[ctx.store].indexes[ctx.index] = changeMap[ctx.database].stores[ctx.store].indexes[ctx.index] || {
64
+ callbacks: [],
65
+ entries: {}
66
+ };
67
+ if (that.contains(['get.index','get.indexes'], type)) {
68
+ if (that.exists(ctx.idx)) {
69
+ changeMap[ctx.database].stores[ctx.store].indexes[ctx.index].data = ctx.idx;
70
+ }
71
+ if (!that.contains(changeMap[ctx.database].stores[ctx.store].indexes[ctx.index].callbacks, obj)) {
72
+ changeMap[ctx.database].stores[ctx.store].indexes[ctx.index].callbacks.push(obj);
73
+ }
74
+ }
75
+ if (that.exists(ctx.key) && that.isnt(ctx.primary_key, ctx.key)) {
76
+ if (that.is(ctx.live, true) || that.contains(['get.entry','get.entries', 'update.entry', 'remove.entry', 'update.entries'], type)) {
77
+ changeMap[ctx.database].stores[ctx.store].indexes[ctx.index].entries[ ctx.key ] = changeMap[ctx.database].stores[ctx.store].indexes[ctx.index].entries[ ctx.key ] || {
78
+ callbacks: []
79
+ };
80
+ if (that.exists(ctx.entry)) {
81
+ changeMap[ctx.database].stores[ctx.store].indexes[ctx.index].entries[ ctx.key ].data = ctx.entry;
82
+ }
83
+ if (!that.contains(changeMap[ctx.database].stores[ctx.store].indexes[ctx.index].entries[ ctx.key ].callbacks, obj)) {
84
+ changeMap[ctx.database].stores[ctx.store].indexes[ctx.index].entries[ ctx.key ].callbacks.push(obj);
85
+ }
86
+ }
87
+ }
88
+ }
89
+ if (that.exists(ctx.primary_key) || that.exists(ctx.key)) {
90
+ if (that.is(ctx.live, true) || that.contains(['get.entry','get.entries', 'update.entries', 'update.entry'], type)) {
91
+ var key = ctx.primary_key || ctx.key;
92
+ changeMap[ctx.database].stores[ctx.store].entries = changeMap[ctx.database].stores[ctx.store].entries || {};
93
+ changeMap[ctx.database].stores[ctx.store].entries[key] = changeMap[ctx.database].stores[ctx.store].entries[key] || {
94
+ callbacks: []
95
+ };
96
+ if (that.exists(ctx.entry)) {
97
+ changeMap[ctx.database].stores[ctx.store].entries[key].data = ctx.entry;
98
+ }
99
+ if (!that.contains(changeMap[ctx.database].stores[ctx.store].entries[key].callbacks, obj)) {
100
+ changeMap[ctx.database].stores[ctx.store].entries[key].callbacks.push(obj);
101
+ }
102
+ }
103
+ }
104
+ }
105
+ },
106
+ inquire = function(type, ctx) {
107
+ var listeners = [],
108
+ previous = null,
109
+ current = null,
110
+ obj = ctx.changeid,
111
+ key;
112
+ changeMap[ctx.database] = changeMap[ctx.database] || {
113
+ stores: {},
114
+ callbacks: []
115
+ };
116
+ if (that.contains(['remove.database', 'add.index', 'add.store'], type)) {
117
+ that.each(changeMap[ctx.database].callbacks, function(callback) {
118
+ if (!that.contains(listeners, callback)) {
119
+ listeners.push(callback);
120
+ }
121
+ });
122
+ previous = changeMap[ctx.database].data;
123
+ current = ctx.db;
124
+ }
125
+ if (that.exists(ctx.store)) {
126
+ changeMap[ctx.database].stores[ctx.store] = changeMap[ctx.database].stores[ctx.store] || {
127
+ callbacks: [],
128
+ indexes: {}
129
+ };
130
+ if (that.contains(['remove.database', 'remove.store', 'clear.store', 'add.index'], type)) {
131
+ that.each(changeMap[ctx.database].stores[ctx.store].callbacks, function(callback) {
132
+ if (!that.contains(listeners, callback)) {
133
+ listeners.push(callback);
134
+ }
135
+ });
136
+ previous = changeMap[ctx.database].stores[ctx.store].data;
137
+ current = ctx.objectstore;
138
+ }
139
+ if (that.exists(ctx.index)) {
140
+ changeMap[ctx.database].stores[ctx.store].indexes[ctx.index] = changeMap[ctx.database].stores[ctx.store].indexes[ctx.index] || {
141
+ callbacks: [],
142
+ entries: {}
143
+ };
144
+ if (that.contains(['remove.index', 'remove.store', 'remove.database', 'clear.store'], type)) {
145
+ that.each(changeMap[ctx.database].stores[ctx.store].indexes[ctx.index].callbacks, function(callback) {
146
+ if (!that.contains(listeners, callback)) {
147
+ listeners.push(callback);
148
+ }
149
+ });
150
+ previous = changeMap[ctx.database].stores[ctx.store].indexes[ctx.index].data;
151
+ current = ctx.idx;
152
+ }
153
+ if (that.exists(ctx.key) && that.isnt(ctx.primary_key, ctx.key)) {
154
+ if (that.is(ctx.live, true) || that.contains(['get.entries', 'get.entry', 'remove.store', 'clear.store', 'remove.database', 'update.entries', 'update.entry', 'remove.entries', 'remove.entry'], type)) {
155
+ changeMap[ctx.database].stores[ctx.store].indexes[ctx.index].entries[ ctx.key ] = changeMap[ctx.database].stores[ctx.store].indexes[ctx.index].entries[ ctx.key ] || {
156
+ callbacks: []
157
+ };
158
+ previous = changeMap[ctx.database].stores[ctx.store].indexes[ctx.index].entries[ ctx.key ].data;
159
+ current = ctx.entry;
160
+ that.each(changeMap[ctx.database].stores[ctx.store].indexes[ctx.index].entries[ ctx.key ].callbacks, function(callback) {
161
+ if (!that.contains(listeners, callback)) {
162
+ listeners.push(callback);
163
+ }
164
+ });
165
+ }
166
+ }
167
+ }
168
+ if (that.exists(ctx.primary_key) || that.exists(ctx.key)) {
169
+ if (that.contains(['update.entries', 'update.entry', 'remove.entries', 'remove.entry', 'remove.database', 'remove.store', 'clear.store'], type)) {
170
+ key = ctx.primary_key || ctx.key;
171
+ changeMap[ctx.database].stores[ctx.store].entries = changeMap[ctx.database].stores[ctx.store].entries || {};
172
+ changeMap[ctx.database].stores[ctx.store].entries[key] = changeMap[ctx.database].stores[ctx.store].entries[key] || {
173
+ callbacks: []
174
+ };
175
+ previous = changeMap[ctx.database].stores[ctx.store].entries[key].data;
176
+ current = ctx.entry;
177
+ that.each(changeMap[ctx.database].stores[ctx.store].entries[key].callbacks, function(callback) {
178
+ if (!that.contains(listeners, callback)) {
179
+ listeners.push(callback);
180
+ }
181
+ });
182
+ }
183
+ }
184
+ }
185
+ return {
186
+ listeners: listeners,
187
+ current: current,
188
+ previous: previous
189
+ };
190
+ },
191
+ notSame = function(a, b) {
192
+ if ( that.isArray(a) ) {
193
+ a = a.join('');
194
+ }
195
+ if ( that.isArray(b) ) {
196
+ b = b.join('');
197
+ }
198
+ a = a ? a.toString() : a;
199
+ b = b ? b.toString() : b;
200
+ return that.isnt(a, b);
201
+ },
202
+ notify = function(ctx, method, type) {
203
+ var inquiry = inquire(method, ctx),
204
+ listeners = inquiry.listeners || [],
205
+ current = inquiry.current || {},
206
+ previous = inquiry.previous || {},
207
+ difference = function(one, two, shallow) {
208
+ var diff = {};
209
+ one = one || {};
210
+ two = two || {};
211
+ if (that.isObject(one)) {
212
+ that.iterate(one, function(key, val) {
213
+ if (notSame(val, two[key])) {
214
+ if ( that.isEmpty(val) || that.isEmpty(one[key])) {
215
+ diff[ key ] = [val, one[key]];
216
+ } else if ( that.isnt(shallow, true) && ( ( that.exists(two[key]) && that.isObject(two[key]) ) || that.isObject(val))) {
217
+ diff[ key ] = difference(val, two[key], shallow);
218
+ } else {
219
+ diff[ key ] = [val, two[key]];
220
+ }
221
+ }
222
+ });
223
+ } else if (that.isArray(one)) {
224
+ if (that.isArray(two)) {
225
+ that.each(one, function(val, i) {
226
+ diff[ i ] = diff[ i ] || [];
227
+ if ( that.isEmpty(val) || that.isEmpty(two[i])) {
228
+ diff[ key ] = [val, two[i]];
229
+ } if ( that.isnt(shallow, true) && (that.exists(one[i]) && that.isObject(one[i]) ) || that.isObject(val) ) {
230
+ diff[i] = difference(val, two[i], shallow);
231
+ } else {
232
+ diff[i] = [one[i], val];
233
+ }
234
+ });
235
+ }
236
+ }
237
+ if (that.isObject(two)) {
238
+ that.iterate(two, function(key, val) {
239
+ if (notSame(val, one[key]) && that.isEmpty(one[ key ])) {
240
+ if ( that.isEmpty(val) || that.isEmpty(two[key])) {
241
+ diff[ key ] = [val, two[key]];
242
+ } if ( that.isnt(shallow, true) && (that.exists(one[key]) && that.isObject(one[key]) ) || that.isObject(val) ) {
243
+ diff[ key ] = difference(one[key], val, shallow);
244
+ } else {
245
+ diff[ key ] = [one[key], val];
246
+ }
247
+ }
248
+ });
249
+ } else if (that.isArray(two)) {
250
+ if (that.isArray(two)) {
251
+ that.each(two, function(val, i) {
252
+ diff[ i ] = diff[ i ] || [];
253
+ if ( that.isEmpty(val) || that.isEmpty(one[i])) {
254
+ diff[ i ] = [val, one[i]];
255
+ } if ( that.isnt(shallow, true) && (that.exists(one[i]) && that.isObject(one[i]) ) || that.isObject(val) ) {
256
+ diff[ i ] = difference(one[i], val, shallow);
257
+ } else {
258
+ diff[ i ] = [one[i], val];
259
+ }
260
+ });
261
+ }
262
+ }
263
+ return diff;
264
+ },
265
+ diff = (that.is(ctx.diff, true)) ? difference(current, previous, ctx.shallow ? true : false) : null,
266
+ args = { context: ctx, method: method, type: type, current: current, previous: previous };
267
+ if(that.is(ctx.diff, true)) {
268
+ args.difference = that.isEmpty(diff) ? null : diff;
269
+ }
270
+ if (that.isnt(ctx.diff, true) || that.isnt(args.difference, null)) {
271
+ that.each(listeners, function(id, i) {
272
+ var listens = callbackMap[id];
273
+ if(that.isArray(listens)) {
274
+ that.each(listens, function(listen, z) {
275
+ if ( false === that.apply(listen, [ args ]) ) {
276
+ delete listeners[i][z];
277
+ delete callbackMap[id][z];
278
+ }
279
+ });
280
+ } else {
281
+ if ( false === that.apply(callbackMap[id], [ args ]) ) {
282
+ delete listeners[i];
283
+ delete callbackMap[id];
284
+ }
285
+ }
286
+ });
287
+ }
288
+ return ctx;
289
+ };
290
+ return [ function(state) {
291
+ that = this;
292
+ var id = this.random();
293
+ state.context.changeid = id;
294
+ if(!this.isFunction(state.context.changes) && !this.isArray(state.context.changes)) {
295
+ return state;
296
+ }
297
+ callbackMap[ id ] = this.clone(state.context.changes);
298
+ return state;
299
+ }, function (state) {
300
+ if (this.exists(state.context.zombie)) {
301
+ return state;
302
+ }
303
+ that = this;
304
+ var promise = state.promise,
305
+ outward = this.deferred(),
306
+ doTick = function(ste) {
307
+ if (!ste || !!ste.context.changing) {
308
+ return ste;
309
+ }
310
+ ste.context.changing = true;
311
+ notify(ste.context, ste.method, ste.type);
312
+ unregister(ste.method, ste.context);
313
+ if (!!state.context.changes) {
314
+ register(ste.method, ste.context);
315
+ }
316
+ if(that.is('resolve', 'notify', ste.type)) {
317
+ update(ste.method, ste.context);
318
+ }
319
+ if(that.is('resolve', ste.type)) {
320
+ if ( !that.isEmpty(callbackMap[ ste.context.changeid ]) ) {
321
+ ste.context.changes = callbackMap[ ste.context.changeid ];
322
+ }
323
+ delete ste.context.changeid;
324
+ }
325
+ return ste;
326
+ };
327
+ promise(function(ste) {
328
+ outward.resolve(doTick(ste));
329
+ }, function(ste) {
330
+ outward.reject(ste);
331
+ }, function(ste) {
332
+ outward.notify(doTick(ste));
333
+ });
334
+ state.promise = outward.promise;
335
+ return state;
336
+ } ];
337
+ }(self));
@@ -0,0 +1,40 @@
1
+ var dashCollect = (function(environment) {
2
+ "use strict";
3
+ var collections = {};
4
+ return [function(state) {
5
+ if (this.isnt(state.context.collect, true)) {
6
+ return state;
7
+ }
8
+ state.context.collector = this.random();
9
+ collections[state.context.collector] = [];
10
+ state.context.collection = collections[state.context.collector];
11
+ return state;
12
+ }, function(state) {
13
+ if (this.isnt(state.context.collect, true)) {
14
+ return state;
15
+ }
16
+ var promise = state.promise,
17
+ deferred = this.deferred(),
18
+ that = this;
19
+ promise(function(ste) {
20
+ if (that.contains(['notify', 'error'], ste.type)) {
21
+ if (that.exists(ste.context.entry)) {
22
+ collections[ste.context.collector].push(ste.context.entry);
23
+ deferred.notify(ste);
24
+ } else if (that.exists(ste.context.error)) {
25
+ collections[ste.context.collector].push(ste.context.error);
26
+ deferred.reject(ste);
27
+ }
28
+ } else if ("resolve" === ste.type) {
29
+ if (that.exists(ste.context.collector)) {
30
+ ste.context.collection = that.clone(collections[ste.context.collector]);
31
+ }
32
+ delete ste.context.collector;
33
+ deferred.resolve(ste);
34
+ delete collections[ste.context.collector];
35
+ }
36
+ });
37
+ state.promise = deferred.promise;
38
+ return state;
39
+ }];
40
+ }(self));
@@ -0,0 +1,17 @@
1
+ window.dashAsync = window.dashAsync || (function (environment) {
2
+ "use strict";
3
+ return [ null, function (state) {
4
+ if(this.isnt(state.context.async, true)) {
5
+ return state;
6
+ }
7
+ var promise = state.promise,
8
+ deferred = this.deferred();
9
+ promise(function(ste) {
10
+ setTimeout( function() {
11
+ deferred.resolve(ste);
12
+ }, 3000 );
13
+ });
14
+ state.promise = deferred.promise;
15
+ return state;
16
+ } ];
17
+ }(self));
@@ -0,0 +1,283 @@
1
+ var dashFirebase = (function (environment) {
2
+ "use strict";
3
+ var that,
4
+ child = function( context ) {
5
+ var deferred = that.deferred();
6
+ context.method = 'child';
7
+ return deferred.promise;
8
+ },
9
+ set = function( context ) {
10
+ var deferred = that.deferred(),
11
+ ref = firebase[ [context.firebase, context.database, context.store ].join('/') ].child(context.primary_key);
12
+ context.method = 'set';
13
+ ref.set( context.entry, function(err) {
14
+ if(that.err, null) {
15
+ deferred.resolve(context.entry);
16
+ } else {
17
+ deferred.reject(err);
18
+ }
19
+ } );
20
+ return deferred.promise;
21
+ },
22
+ update = function( context ) {
23
+ var deferred = that.deferred(),
24
+ ref = firebase[ [context.firebase, context.database, context.store ].join('/') ].child(context.primary_key);
25
+ context.method = 'update';
26
+ ref.update(context.entry, function() {
27
+ if(that.err, null) {
28
+ deferred.resolve(context.entry);
29
+ } else {
30
+ deferred.reject(err);
31
+ }
32
+ });
33
+ return deferred.promise;
34
+ },
35
+ remove = function( context ) {
36
+ var deferred = that.deferred(),
37
+ ref = firebase[ [context.firebase, context.database, context.store ].join('/') ].child(context.primary_key);
38
+ context.method = 'remove';
39
+ ref.remove(function() {
40
+ if(that.err, null) {
41
+ deferred.resolve(context.entry);
42
+ } else {
43
+ deferred.reject(err);
44
+ }
45
+ });
46
+ return deferred.promise;
47
+ },
48
+ whichMethod = function(signature) {
49
+ if ( that.contains( [ 'get.entry', 'get.entries', 'get.index', 'get.database', 'get.store' ], signature)) {
50
+ return 'child';
51
+ } else if ( that.contains([ 'remove.entry', 'remove.entries', 'remove.index', 'remove.database', 'remove.store' ], signature)) {
52
+ return 'remove';
53
+ } else if ( that.contains([ 'add.entry' ], signature)) {
54
+ return 'set';
55
+ } else if ( that.contains([ 'update.entry', 'update.entries' ], signature)) {
56
+ return 'update';
57
+ } else {
58
+ return null;
59
+ }
60
+ },
61
+ scripts = ( !! environment.document) ? environment.document.getElementsByTagName("script") : [],
62
+ libraryScript = scripts[scripts.length - 1] || null,
63
+ libraryPath =( null !== libraryScript && null === libraryScript.src.match(/chrome-extension/) ) ? libraryScript.src : null,
64
+ workerEnvironment = null !== environment.constructor.toString().match(/WorkerGlobalScope/),
65
+ worker = !!workerEnvironment && !!libraryPath && null !== libraryPath.match(/firebase/) ? new Worker(libraryPath) : null,
66
+ workQueue = {},
67
+ firebases = {},
68
+ workRegister = function (worker, message, context, success, error, notify) {
69
+ var id = that.random(),
70
+ callback = function (e) {
71
+ var data = e.data,
72
+ queued = workQueue[data.uid];
73
+ if (undefined !== queued) {
74
+ switch (e.data.type) {
75
+ case 'success':
76
+ delete workQueue[data.uid];
77
+ worker.removeEventListener('message', callback);
78
+ that.apply(success, [data]);
79
+ break;
80
+ case 'error':
81
+ delete workQueue[data.uid];
82
+ worker.removeEventListener('message', callback);
83
+ that.apply(error, [data]);
84
+ break;
85
+ case 'abort':
86
+ that.apply(notify, [data]);
87
+ break;
88
+ default:
89
+ break;
90
+ }
91
+ }
92
+ },
93
+ clean = function(obj) {
94
+ if (that.isFunction(obj)) {
95
+ return undefined;
96
+ } else if (that.isObject(obj)) {
97
+ that.iterate(obj, function(key, val) {
98
+ obj[ key ] = clean(val);
99
+ });
100
+ } else if (that.isArray(obj)) {
101
+ that.each(obj, function(v, i) {
102
+ obj[i] = clean(v);
103
+ });
104
+ }
105
+ return obj;
106
+ };
107
+ workQueue[id] = {
108
+ success: success,
109
+ error: error,
110
+ notify: notify
111
+ };
112
+ worker.addEventListener('message', callback);
113
+ worker.postMessage({
114
+ method: message,
115
+ context: clean(context),
116
+ uid: id
117
+ });
118
+ return id;
119
+ },
120
+ workDispatch = function (message, context) {
121
+ var defd = that.deferred(),
122
+ callbacks = {
123
+ on_success: context.on_success,
124
+ on_error: context.on_error,
125
+ on_abort: context.on_abort,
126
+ on_complete: context.on_complete,
127
+ on_upgrade_needed: context.on_upgrade_needed,
128
+ on_blocked: context.on_blocked,
129
+ on_close: context.on_close
130
+ },
131
+ getData = function (data) {
132
+ if( that.isObject(data) ) {
133
+ that.iterate(callbacks, function (key, val) {
134
+ if(!!val) {
135
+ data[key] = val;
136
+ }
137
+ });
138
+ }
139
+ return data;
140
+ };
141
+ that.iterate(callbacks, function (key, val) {
142
+ delete context[key];
143
+ });
144
+ workRegister(worker, message, context, function (data) {
145
+ var add_ctx = that.clone(context);
146
+ add_ctx.data = data.context.entry;
147
+ add_ctx.firebaseing = true;
148
+ that.api.add.entry(add_ctx)(function(added_ctx) {
149
+ delete added_ctx.firebaseing;
150
+ defd.resolve(getData(added_ctx));
151
+ }, function(added_ctx) {
152
+ delete added_ctx.firebaseing;
153
+ defd.reject(getData(added_ctx));
154
+ }, function(added_ctx) {
155
+ delete added_ctx.firebaseing;
156
+ defd.notify(getData(added_ctx));
157
+ })
158
+ }, function (data) {
159
+ defd.reject(getData(data));
160
+ }, function (data) {
161
+ defd.notify(getData(data));
162
+ });
163
+ return defd.promise;
164
+ };
165
+
166
+ if (true === workerEnvironment) {
167
+ importScripts('https://cdn.firebase.com/js/client/1.0.6/firebase.js');
168
+ console.log('firebase',Firebase);
169
+ environment.addEventListener('message', function (e) {
170
+ var input = e.data,
171
+ method = input.method,
172
+ value = input.value,
173
+ key = input.key,
174
+ context = input.context,
175
+ params = context.params,
176
+ expires = input.expires,
177
+ end = function (ctx) {
178
+ ctx.type = ctx.type || 'success';
179
+ environment.postMessage(ctx);
180
+ },
181
+ callback = function(sig) {
182
+ return function(data, error) {
183
+ delete input.context.callback;
184
+ if (!!error) {
185
+ input.context.error = error;
186
+ input.context.message = data;
187
+ input.type = 'error';
188
+ } else {
189
+ input.context.entry = data;
190
+ }
191
+ end(input);
192
+ }
193
+ };
194
+ if ( 'undefined' === typeof firebases[input.context.firebase] ) {
195
+ firebases[input.context.firebase] = new Firebase([context.firebase, context.database, context.store ].join('/'));
196
+ }
197
+ if (method === 'set' || method === 'update' || method === 'remove') {
198
+ context.callback = callback(method);
199
+ if ( method === 'set' ) {
200
+ set(context);
201
+ } else if ( method === 'child' ) {
202
+ child(context);
203
+ } else if ( method === 'update' ) {
204
+ update(context);
205
+ } else if ( method === 'remove' ) {
206
+ remove(context);
207
+ }
208
+ } else {
209
+ input.type = 'error';
210
+ end({
211
+ type: 'abort',
212
+ context: input,
213
+ error: 'No such method'
214
+ });
215
+ }
216
+ }, false);
217
+ } else {
218
+ return [ function (state) {
219
+ that = this;
220
+ if(this.isnt(state.context.firebase, true) || this.exists(state.context.firebaseing)) {
221
+ return state;
222
+ }
223
+ state.context.firebaseid = this.random();
224
+ firebase[ state.context.firebaseid ] = {
225
+ url: state.context.url,
226
+ params: state.context.params ? state.context.params : null
227
+ }
228
+ delete state.context.url;
229
+ delete state.context.params;
230
+ return state;
231
+ }, function (state) {
232
+ if(this.isnt(state.context.firebase, true) || this.exists(state.context.firebaseing)) {
233
+ return state;
234
+ }
235
+ var promise = state.promise,
236
+ outward = this.deferred(),
237
+ inward,
238
+ update = false,
239
+ args;
240
+ if (this.contains(['add.entry', 'update.entry', 'update.entries', 'remove.entry', 'remove.entries'], state.method)) {
241
+ if (this.contains(['notify', 'resolve'], state.type)) {
242
+ update = true;
243
+ }
244
+ } else {
245
+ if ((this.is('error', state.type) || (this.isEmpty(state.context.entry) && this.contains(['get.entry', 'get.entries'], state.method))) && (this.is(state.context.firebase, true) || this.is(state.context.fallback, true))) {
246
+ update = true;
247
+ }
248
+ }
249
+ args = firebase[ state.context.firebaseid ];
250
+ if (update) {
251
+ state.promise = outward.promise;
252
+ state.context.url = args.url;0
253
+ if (that.isFunction(state.context.url)) {
254
+ state.context.url = that.apply(state.context.url, [that.clone(state)]);
255
+ }
256
+ if (that.isFunction(state.context.params)) {
257
+ state.context.params = that.apply(state.context.params, [that.clone(state)]);
258
+ }
259
+ state.context.params = args.params;
260
+ inward = workDispatch( whichMethod(state.method), state.context);
261
+ inward(function(ctx2){
262
+ state.context = ctx2;
263
+ state.type = 'resolve';
264
+ outward.resolve(state.context);
265
+ }, function(ctx2) {
266
+ state.context = ctx2;
267
+ state.type = 'error';
268
+ outward.reject(state.context);
269
+ }, function(ctx2) {
270
+ state.type = 'notify';
271
+ state.context = ctx2;
272
+ outward.notify(state.context);
273
+ });
274
+ } else {
275
+ state.context.url = args.url;
276
+ state.context.params = args.params;
277
+ delete firebase[ state.context.firebaseid ];
278
+ delete state.context.firebaseid;
279
+ }
280
+ return state;
281
+ } ];
282
+ }
283
+ }(self));