Predicate matching

This commit is contained in:
Mo Bitar
2018-07-03 19:29:44 -05:00
parent b270618b53
commit 428458e821
3 changed files with 16 additions and 16 deletions

View File

@@ -149,7 +149,8 @@ class AuthManager extends SFAuthManager {
configureUserPrefs() { configureUserPrefs() {
let prefsContentType = "SN|UserPreferences"; let prefsContentType = "SN|UserPreferences";
this.singletonManager.registerSingleton({content_type: prefsContentType}, (resolvedSingleton) => { let contentTypePredicate = new SFPredicate("content_type", "=", prefsContentType);
this.singletonManager.registerSingleton([contentTypePredicate], (resolvedSingleton) => {
this.userPreferences = resolvedSingleton; this.userPreferences = resolvedSingleton;
this.userPreferencesDidChange(); this.userPreferencesDidChange();
}, (valueCallback) => { }, (valueCallback) => {

View File

@@ -21,7 +21,10 @@ class NativeExtManager {
resolveExtensionsManager() { resolveExtensionsManager() {
this.singletonManager.registerSingleton({content_type: "SN|Component", package_info: {identifier: this.extensionsManagerIdentifier}}, (resolvedSingleton) => { let contentTypePredicate = new SFPredicate("content_type", "=", "SN|Component");
let packagePredicate = new SFPredicate("package_info.identifier", "=", this.extensionsManagerIdentifier);
this.singletonManager.registerSingleton([contentTypePredicate, packagePredicate], (resolvedSingleton) => {
// Resolved Singleton // Resolved Singleton
this.systemExtensions.push(resolvedSingleton.uuid); this.systemExtensions.push(resolvedSingleton.uuid);
@@ -91,7 +94,10 @@ class NativeExtManager {
resolveBatchManager() { resolveBatchManager() {
this.singletonManager.registerSingleton({content_type: "SN|Component", package_info: {identifier: this.batchManagerIdentifier}}, (resolvedSingleton) => { let contentTypePredicate = new SFPredicate("content_type", "=", "SN|Component");
let packagePredicate = new SFPredicate("package_info.identifier", "=", this.batchManagerIdentifier);
this.singletonManager.registerSingleton([contentTypePredicate, packagePredicate], (resolvedSingleton) => {
// Resolved Singleton // Resolved Singleton
this.systemExtensions.push(resolvedSingleton.uuid); this.systemExtensions.push(resolvedSingleton.uuid);

View File

@@ -40,14 +40,14 @@ class SingletonManager {
}) })
} }
registerSingleton(predicate, resolveCallback, createBlock) { registerSingleton(predicates, resolveCallback, createBlock) {
/* /*
predicate: a key/value pair that specifies properties that should match in order for an item to be considered a predicate predicate: a key/value pair that specifies properties that should match in order for an item to be considered a predicate
resolveCallback: called when one or more items are deleted and a new item becomes the reigning singleton resolveCallback: called when one or more items are deleted and a new item becomes the reigning singleton
createBlock: called when a sync is complete and no items are found. The createBlock should create the item and return it. createBlock: called when a sync is complete and no items are found. The createBlock should create the item and return it.
*/ */
this.singletonHandlers.push({ this.singletonHandlers.push({
predicate: predicate, predicates: predicates,
resolutionCallback: resolveCallback, resolutionCallback: resolveCallback,
createBlock: createBlock createBlock: createBlock
}); });
@@ -58,20 +58,20 @@ class SingletonManager {
savedItems = savedItems || []; savedItems = savedItems || [];
for(let singletonHandler of this.singletonHandlers) { for(let singletonHandler of this.singletonHandlers) {
var predicate = singletonHandler.predicate; var predicates = singletonHandler.predicates;
let retrievedSingletonItems = this.filterItemsWithPredicate(retrievedItems, predicate); let retrievedSingletonItems = this.modelManager.filterItemsWithPredicates(retrievedItems, predicates);
// We only want to consider saved items count to see if it's more than 0, and do nothing else with it. // We only want to consider saved items count to see if it's more than 0, and do nothing else with it.
// This way we know there was some action and things need to be resolved. The saved items will come up // This way we know there was some action and things need to be resolved. The saved items will come up
// in filterItemsWithPredicate(this.modelManager.allItems) and be deleted anyway // in filterItemsWithPredicate(this.modelManager.allItems) and be deleted anyway
let savedSingletonItemsCount = this.filterItemsWithPredicate(savedItems, predicate).length; let savedSingletonItemsCount = this.modelManager.filterItemsWithPredicates(savedItems, predicates).length;
if(retrievedSingletonItems.length > 0 || savedSingletonItemsCount > 0) { if(retrievedSingletonItems.length > 0 || savedSingletonItemsCount > 0) {
/* /*
Check local inventory and make sure only 1 similar item exists. If more than 1, delete newest Check local inventory and make sure only 1 similar item exists. If more than 1, delete newest
Note that this local inventory will also contain whatever is in retrievedItems. Note that this local inventory will also contain whatever is in retrievedItems.
*/ */
var allExtantItemsMatchingPredicate = this.filterItemsWithPredicate(this.modelManager.allItems, predicate); var allExtantItemsMatchingPredicate = this.modelManager.itemsMatchingPredicates(predicates);
/* /*
Delete all but the earliest created Delete all but the earliest created
@@ -122,13 +122,6 @@ class SingletonManager {
} }
} }
} }
filterItemsWithPredicate(items, predicate) {
return items.filter((candidate) => {
return candidate.satisfiesPredicate(predicate);
})
}
} }
angular.module('app').service('singletonManager', SingletonManager); angular.module('app').service('singletonManager', SingletonManager);