-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaranon.js
381 lines (316 loc) · 12.1 KB
/
maranon.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
var _ = require('lodash');
var CookieManager = require('./storage/CookieManager.js');
var Maranon = function(schema, enableStore, cacheBackupPeriod) {
var store = enableStore ? require('store') : { enabled: false };
var ONE_MINUTE = 60000;
var caches = {};
var manyToManys = {};
var properties = {};
var subscriptions = {};
var thiz = {};
function init() {
_.forOwn(schema.model, initTypeCache);
_.forOwn(schema.manyToMany, _.partial(initManyToMany, schema));
_.forOwn(schema.properties, initPropertyCache);
backupEvery(cacheBackupPeriod || ONE_MINUTE);
}
function initTypeCache(type, typeName) {
caches[typeName] = getBackedUpCache(typeName) || Cache(type);
if(!subscriptions[typeName]) subscriptions[typeName] = [];
addGettersAndSettersFor(type, typeName);
}
function getCacheBackupKey(typeName) {
return typeName + '_cache';
}
function getManyToManyBackupKey(manyToManyName) {
return manyToManyName + '_manyToMany';
}
function getPropertyBackupKey(propertyName) {
return propertyName + '_property';
}
function getBackedUpCache(typeName) {
if(!store.enabled) return;
var backup = store.get(getCacheBackupKey(typeName));
if(backup) return backup;
}
function getBackedUpManyToMany(manyToManyName) {
if(!store.enabled) return;
var backup = store.get(getManyToManyBackupKey(manyToManyName));
if(backup) return backup;
}
function getBackedUpProperty(property, propertyName) {
if(!store.enabled) return;
if(property.cookieBacked) return CookieManager.read(propertyName);
var backup = store.get(getPropertyBackupKey(propertyName));
if(backup) return backup;
}
function backupEvery(millis) {
store.enabled && setInterval(doBackup, millis);
}
function doBackup() {
_.forOwn(caches, backupCache);
_.forOwn(manyToManys, backupManyToMany);
}
function backupCache(cache, typeName) {
store.set(getCacheBackupKey(typeName), cache);
}
function backupManyToMany(manyToMany, manyToManyName) {
store.set(getManyToManyBackupKey(manyToManyName), manyToMany);
}
function initManyToMany(schema, manyToMany, manyToManyName) {
manyToManys[manyToManyName] = getBackedUpManyToMany() || new ManyToMany();
addManyToManyGettersAndSetters(schema, manyToMany, manyToManyName);
}
function initPropertyCache(property, propertyName) {
if(!subscriptions[getPropertySubscriptionKey(propertyName)]) subscriptions[getPropertySubscriptionKey(propertyName)] = [];
if(properties[propertyName]) return;
properties[propertyName] = getBackedUpProperty(property, propertyName);
var fnSuffix = _.capitalize(propertyName) + 'Property';
thiz['get' + fnSuffix] = _.partial(getProperty, propertyName);
thiz['put' + fnSuffix] = _.partial(putProperty, !property.doNotPersist, property.cookieBacked, propertyName);
thiz['delete' + fnSuffix] = _.partial(deleteProperty, !property.doNotPersist, propertyName);
}
function getProperty(key) {
return properties[key];
}
function putProperty(persist, cookieBacked, key, value) {
properties[key] = value;
if(persist) {
if(cookieBacked) {
CookieManager.createNonExpiringCookie(key, value);
} else {
if(store.enabled) store.set(getPropertyBackupKey(key), value);
}
}
invokeSubscribedActions(getPropertySubscriptionKey(key));
}
function getPropertySubscriptionKey(propertyName) {
return propertyName + '_property';
}
function deleteProperty(persisted, key) {
delete properties[key];
if(store.enabled && persisted) store.remove(getPropertyBackupKey(key));
}
function addManyToManyGettersAndSetters(schema, manyToMany, manyToManyName) {
var aTypeName = manyToMany.entityA;
var bTypeName = manyToMany.entityB;
var aType = schema.model[aTypeName];
var bType = schema.model[bTypeName];
var aTypeNameCapitalized = _.capitalize(aTypeName);
var bTypeNameCapitalized = _.capitalize(bTypeName);
var aPluralSuffix = _.capitalize(pluralise(aType, aTypeName));
var bPluralSuffix = _.capitalize(pluralise(bType, bTypeName));
thiz['get' + aTypeNameCapitalized + bPluralSuffix] = _.partial(getsManyToMany, manyToManyName, aTypeName, bTypeName);
thiz['get' + bTypeNameCapitalized + aPluralSuffix] = _.partial(getsManyToMany, manyToManyName, bTypeName, aTypeName);
thiz['put' + aTypeNameCapitalized + bPluralSuffix] = _.partial(putsManyToMany, manyToManyName, aTypeName, bTypeName, bType.idProperty);
thiz['put' + bTypeNameCapitalized + aPluralSuffix] = _.partial(putsManyToMany, manyToManyName, bTypeName, aTypeName, aType.idProperty);
}
function initHasManyRelations(schema, type, typeName) {
hasManyRelations[typeName] = {};
hasManyTypeNames.forEach(function(hasManyTypeName) {
hasManyRelations[typeName][hasManyTypeName] = {};
var getSetSuffix = _.capitalize(typeName) + pluralise(schema[hasManyTypeName], _.capitalize(hasManyTypeName));
thiz['get' + getSetSuffix] = _.partial(getsHasMany, typeName, hasManyTypeName);
thiz['put' + getSetSuffix] = x;
});
}
function Cache(type) {
var cache = {};
cache.populated = false;
cache.entities = {};
cache.idProperty = type.idProperty;
cache.indexes = {};
_.each(type.indexOn, function(property) {
cache.indexes[property] = {};
});
return cache;
}
function ManyToMany() {
var manyToMany = {};
manyToMany.populated = false;
manyToMany.pairs = [];
return manyToMany;
}
function pluralise(type, typeName) {
return typeName + (type.pluralSuffix || 's');
}
function addGettersAndSettersFor(type, typeName) {
var typeNameFnSuffix = _.capitalize(typeName);
var typeNameFnSuffixPlural = pluralise(type, typeNameFnSuffix);
// add getters for specified id property
thiz['get' + typeNameFnSuffix] = _.partial(get, typeName);
thiz['get' + typeNameFnSuffixPlural] = _.partial(gets, typeName);
thiz['getAll' + typeNameFnSuffixPlural] = _.partial(getAll, typeName);
// add getters for specified indexOn properties (return single element)
_.each(type.indexOn, _.partial(addIndexedGetForTypeAndProperty, typeNameFnSuffix, typeName));
// add getters for specified getBy properties (return array of 0 or more elements)
_.each(type.getBy, _.partial(addGetsForTypeAndProperty, typeNameFnSuffixPlural, typeName));
// add setters
thiz['put' + typeNameFnSuffix] = _.partial(putAndInvokeSubscribedActions, typeName);
thiz['put' + typeNameFnSuffixPlural] = _.partial(putsAndInvokeSubscribedActions, typeName);
}
function addIndexedGetForTypeAndProperty(typeNameFnSuffix, typeName, property) {
thiz['get' + typeNameFnSuffix + 'By' + _.capitalize(property)] = _.partial(getByIndexedProperty, typeName, property);
}
function addGetsForTypeAndProperty(typeNameFnSuffixPlural, typeName, property) {
thiz['get' + typeNameFnSuffixPlural + 'By' + _.capitalize(property)] = _.partial(getsByProperty, typeName, property);
}
function getFromCache(cache, id) {
return cache.entities[id];
}
function get(typeName, id) {
return getFromCache(caches[typeName], id);
}
function gets(typeName, ids) {
var cache = caches[typeName];
return arrayResult(_.chain(ids)
.map(_.partial(getFromCache, cache))
.compact()
.value(), cache);
}
function getAll(typeName) {
var cache = caches[typeName];
return cache ? arrayResult(_.values(cache.entities), cache) : [];
}
function arrayResult(arr, cache) {
return arr.length === 0 && !cache.populated ? undefined : arr;
}
function getByIndexedProperty(typeName, property, value) {
var cache = caches[typeName];
var id = cache.indexes[property][value];
return cache.entities[id];
}
function getsByProperty(typeName, property, value) {
var cache = caches[typeName];
return arrayResult(_.filter(cache.entities, looselyEquals(property, value)), cache);
}
function getsManyToMany(manyToManyName, fromTypeName, toTypeName, id) {
var manyToMany = manyToManys[manyToManyName];
var ids = _.chain(manyToMany.pairs)
.filter(looselyEquals(fromTypeName, id))
.pluck(toTypeName)
.value();
if(ids.length === 0) {
if(manyToMany.populated) return ids;
return;
}
return gets(toTypeName, ids);
}
function makeManyToManyPair(fromKey, toKey, fromValue, toValue) {
var newPair = {};
newPair[fromKey] = fromValue;
newPair[toKey] = toValue;
return newPair;
}
function putsManyToMany(manyToManyName, fromTypeName, toTypeName, toTypeIdProperty, fromId, toObjects) {
var newPairs = _.chain(toObjects)
.pluck(toTypeIdProperty)
.map(_.partial(makeManyToManyPair, fromTypeName, toTypeName, fromId))
.value();
manyToManys[manyToManyName].pairs = newPairs.concat(_.reject(manyToManys[manyToManyName].pairs, looselyEquals(fromTypeName, fromId)));
manyToManys[manyToManyName].populated = true;
}
function looselyEquals(property, value) {
return function(obj) {
return obj[property] == value;
};
}
function put(cache, entity) {
var id = entity[cache.idProperty];
cache.entities[id] = entity;
_.forOwn(cache.indexes, function(index, indexName) {
index[entity[indexName]] = id;
});
return entity;
}
function putAndInvokeSubscribedActions(typeName, entity) {
var cache = caches[typeName];
var rtn = put(cache, entity);
cache.populated = true;
invokeSubscribedActions(typeName);
return rtn;
}
function putsAndInvokeSubscribedActions(typeName, entities) {
var cache = caches[typeName];
var rtn = _.map(entities, _.partial(put, cache));
cache.populated = true;
invokeSubscribedActions(typeName);
return rtn;
}
function invokeSubscribedActions(typeName) {
_.invoke(subscriptions[typeName], 'action');
}
function invokeAllSubscribedActions() {
_.forOwn(subscriptions, function(v, k) { invokeSubscribedActions(k); });
}
function subscribe(id) {
return {
toDo: function(action) {
return {
onUpdatesTo: function(typeName) {
subscriptions[typeName].push({
id: id,
action: action
});
},
onUpdatesToProperty: function(propertyName) {
subscriptions[getPropertySubscriptionKey(propertyName)].push({
id: id,
action: action
});
}
};
}
};
}
function unsubscribe(id) {
return {
fromUpdatesTo: function(typeName) {
subscriptions[typeName] = _.reject(subscriptions[typeName], looselyEquals('id', id));
},
fromUpdatesToProperty: function(propertyName) {
subscriptions[getPropertySubscriptionKey(propertyName)] = _.reject(subscriptions[getPropertySubscriptionKey(propertyName)], looselyEquals('id', id));
},
fromAllUpdates: function() {
_.each(_.keys(subscriptions), unsubscribe(id).fromUpdatesTo);
_.each(_.keys(subscriptions), unsubscribe(id).fromUpdatesToProperty);
}
};
}
function wipe() {
wipeCaches();
wipeManyToManys();
wipeProperties();
invokeAllSubscribedActions();
init();
}
function wipeCaches() {
_.forOwn(caches, wipeCacheBackup);
caches = {};
}
function wipeCacheBackup(cache, typeName) {
store.remove(getCacheBackupKey(typeName));
}
function wipeManyToManys() {
_.forOwn(manyToManys, wipeManyToManyBackup);
manyToManys = {};
}
function wipeManyToManyBackup(manyToMany, manyToManyName) {
store.remove(getManyToManyBackupKey(manyToManyName));
}
function wipeProperties() {
_.forOwn(schema.properties, wipeProperty);
}
function wipeProperty(property, propertyName) {
if(property.doNotWipe) return;
delete properties[propertyName];
if(!property.doNotPersist) store.remove(getPropertyBackupKey(propertyName));
if(property.cookieBacked) CookieManager.deleteCookie(propertyName);
}
thiz.subscribe = subscribe;
thiz.unsubscribe = unsubscribe;
thiz.wipe = wipe;
init();
return thiz;
};
module.exports = Maranon;