Author: reebalazs
Date: Fri Dec 28 21:33:57 2007
New Revision: 50166
Modified:
kukit/kukit.js/branch/ree-service-layer-and-refactoring/ (props changed)
kukit/kukit.js/branch/ree-service-layer-and-refactoring/kukit/interfaces.js
kukit/kukit.js/branch/ree-service-layer-and-refactoring/kukit/service.js
kukit/kukit.js/branch/ree-service-layer-and-refactoring/tests/test_interfaces.js
Log:
Switch also the service layer to config dicts,
causes a better readable service.js and tests.
Modified:
kukit/kukit.js/branch/ree-service-layer-and-refactoring/kukit/interfaces.js
==============================================================================
--- kukit/kukit.js/branch/ree-service-layer-and-refactoring/kukit/interfaces.js
(original)
+++ kukit/kukit.js/branch/ree-service-layer-and-refactoring/kukit/interfaces.js
Fri Dec 28 21:33:57 2007
@@ -249,7 +249,7 @@
}; /* end NamedInterfaces */
/*
- * KSS service layer registry
+ * KSS service layer resource
*/
this.ServiceMethod = function(iface, methodName) {
@@ -257,43 +257,64 @@
this.iface = iface;
this.methodName = methodName;
this.registry = {};
- this.preferredProviders = null;
+ this.preferredSources = null;
- this.define = function(preferredProviders, checker, fallbackProvider) {
- if (this.preferredProviders) {
-;;; kukit.E = 'Attempt to double define method [';
+;;; this._makeError = function(componentName, name) {
+;;; kukit.E = 'In a call to ' + componentName + ', ';
+;;; kukit.E += name + ' must be provided for method [';
+;;; kukit.E += this.methodName + '] on interface [';
+;;; kukit.E += this.iface.interfaceName + ']';
+;;; return new Error(kukit.E);
+;;; };
+
+ this.require = function(config) {
+;;; // Diagnose if parameters are all right.
+;;; if (! config.preferredSources) {
+;;; throw this._makeError('ServiceMethod.require', 'config.getter');
+;;; };
+ if (this.preferredSources) {
+;;; kukit.E = 'ServiceMethod.require is called twice on [';
;;; kukit.E += this.methodName + '] on interface [';
;;; kukit.E += this.iface.interfaceName + ']';
throw new Error(kukit.E);
};
- this.preferredProviders = preferredProviders;
- this.checker = checker;
- this.fallbackProvider = fallbackProvider;
+ this.preferredSources = config.preferredSources;
+ this.checker = config.checker;
+ this.fallbackProvider = config.fallbackProvider;
};
- this.provide = function(providerName, providerVersion, getter) {
+ this.provide = function(config) {
+;;; // Diagnose if parameters are all right.
+;;; var componentName = 'SericeDescriptor.provide';
+;;; if (! config.getter) {
+;;; throw this._makeError(componentName, 'config.getter');
+;;; }
+;;; if (! config.sourceName) {
+;;; throw this._makeError(componentName, 'config.sourceName');
+;;; }
+;;; if (! config.sourceVersion) {
+;;; throw this._makeError(componentName, 'config.sourceVersion');
+;;; }
if (this.iface.finalized) {
;;; kukit.E = 'Attempt to provide implementation to method [';
;;; kukit.E += this.methodName + '] on already finalized interface [';
;;; kukit.E += this.iface.interfaceName + ']';
throw new Error(kukit.E);
- } else if (typeof(this.registry[providerName]) != 'undefined') {
-;;; kukit.E = 'Double registration by [' + providerName + '][';
+ } else if (typeof(this.registry[config.sourceName]) != 'undefined') {
+;;; kukit.E = 'Double registration by [' + config.sourceName + '][';
;;; kukit.E += providerVersion;
;;; kukit.E += '] of method [' + this.methodName;
;;; kukit.E += '] in interface [' + this.iface.interfaceName + ']';
throw new Error(kukit.E);
}
- this.registry[providerName] = {
- methodName: this.methodName,
- providerName: providerName,
- providerVersion: providerVersion,
- getter: getter};
+ // Use the config to set the registry
+ config.methodName = this.methodName;
+ this.registry[config.sourceName] = config;
};
this.finalize = function(loader) {
var func;
- if (! this.preferredProviders) {
+ if (! this.preferredSources) {
;;; kukit.E = 'Undefined method [';
;;; kukit.E += this.methodName + '] on interface [';
;;; kukit.E += this.iface.interfaceName + '], ';
@@ -302,8 +323,8 @@
throw new Error(kukit.E);
}
// loop through all preferred providers
- for (var i = 0; i < this.preferredProviders.length; i++) {
- var item = this.registry[this.preferredProviders[i]];
+ for (var i = 0; i < this.preferredSources.length; i++) {
+ var item = this.registry[this.preferredSources[i]];
// Did we have an item?
if (typeof(item) == 'undefined') {
continue;
@@ -329,7 +350,7 @@
;;; kukit.E += this.methodName + '] on interface [';
;;; kukit.E += this.iface.interfaceName + '], because no provider
found ';
;;; kukit.E += 'of the following preferences: [';
-;;; kukit.E += this.preferredProviders + ']';
+;;; kukit.E += this.preferredSources + ']';
throw new Error(kukit.E);
}
} else {
Modified:
kukit/kukit.js/branch/ree-service-layer-and-refactoring/kukit/service.js
==============================================================================
--- kukit/kukit.js/branch/ree-service-layer-and-refactoring/kukit/service.js
(original)
+++ kukit/kukit.js/branch/ree-service-layer-and-refactoring/kukit/service.js
Fri Dec 28 21:33:57 2007
@@ -1,64 +1,74 @@
-
new function() { // BEGIN CLOSURE service
- var core = kukit.interfaces.global.schema.core;
+var core = kukit.interfaces.global.schema.core;
- core.getAttribute('cssQuery').define(['base2', 'cssQuery'],
- // a parameter checker common for each implementation
- function(selector, inNodes) {
- // to eliminate possible errors
- if (typeof(inNodes) != 'undefined' && inNodes == null) {
-;;; kukit.E = 'Selection error in kukit.dom.cssQuery';
- throw new Error(kukit.E);
- }
- });
-
- // providers are also here now
-
- core.getAttribute('cssQuery').provide('cssQuery', '1.1',
- function() {
- // Detect if cssQuery is present
- //
- if (typeof(cssQuery) == 'undefined') {
- return;
- }
- // return the method plug
-;;; kukit.log('Using original cssQuery.');
- return function(selector, inNodes) {
- // global scope, always.
- // This is very bad. However the binding makes sure that
- // nodes once bound will never be bound again
- // (also, noticed the following issue: cssQuery, when
called
- // on an element, does not check the element itself.)
- var results = cssQuery(selector);
- return results;
- };
- });
-
-
- core.getAttribute('cssQuery').provide('base2', '1.1',
- function() {
- // Detect if base2 is present
- //
- if (typeof(base2) == 'undefined') {
- return;
- }
- // return the method plug
-;;; kukit.log('Using cssQuery from base2.');
- return function(selector, inNodes) {
- // global scope, always.
- // This is very bad. However the binding makes sure that
- // nodes once bound will never be bound again
- var results = base2.DOM.Document.matchAll(document,
selector);
- var nodes = [];
- for(var i = 0; i < results.length; i++) {
- nodes.push(results.item(i));
- }
- return nodes;
- };
- });
+// Requirements towards core services. It contains
+// kukit's preferences to choose between vendors
+// that implement a given functionality.
+
+core.getAttribute('cssQuery').require({
+ preferredSources: ['base2', 'cssQuery'],
+ // a parameter checker common for each implementation
+ checker: function(selector, inNodes) {
+ // to eliminate possible errors
+ if (typeof(inNodes) != 'undefined' && inNodes == null) {
+;;; kukit.E = 'Selection error in kukit.dom.cssQuery';
+ throw new Error(kukit.E);
+ }
+ }});
+
+// Third party code provides services. We have a few things
+// that we can detect ourselves. by inspecting if a given
+// library is loaded.
+
+core.getAttribute('cssQuery').provide({
+ sourceName:'cssQuery',
+ sourceVersion: '1.1',
+ getter: function() {
+ // Detect if cssQuery is present
+ //
+ if (typeof(cssQuery) == 'undefined') {
+ return;
+ }
+ // return the method plug
+;;; kukit.log('Using original cssQuery.');
+ return function(selector, inNodes) {
+ // global scope, always.
+ // This is very bad. However the binding makes sure that
+ // nodes once bound will never be bound again
+ // (also, noticed the following issue: cssQuery, when called
+ // on an element, does not check the element itself.)
+ var results = cssQuery(selector);
+ return results;
+ };
+ }});
+
+
+core.getAttribute('cssQuery').provide({
+ sourceName: 'base2',
+ sourceVersion: '1.1',
+ getter: function() {
+ // Detect if base2 is present
+ //
+ if (typeof(base2) == 'undefined') {
+ return;
+ }
+ // return the method plug
+;;; kukit.log('Using cssQuery from base2.');
+ return function(selector, inNodes) {
+ // global scope, always.
+ // This is very bad. However the binding makes sure that
+ // nodes once bound will never be bound again
+ var results = base2.DOM.Document.matchAll(document, selector);
+ var nodes = [];
+ for(var i = 0; i < results.length; i++) {
+ nodes.push(results.item(i));
+ }
+ return nodes;
+ };
+ }});
}() // END CLOSURE service
Modified:
kukit/kukit.js/branch/ree-service-layer-and-refactoring/tests/test_interfaces.js
==============================================================================
---
kukit/kukit.js/branch/ree-service-layer-and-refactoring/tests/test_interfaces.js
(original)
+++
kukit/kukit.js/branch/ree-service-layer-and-refactoring/tests/test_interfaces.js
Fri Dec 28 21:33:57 2007
@@ -173,27 +173,43 @@
// We have a singleton, called s.
var s = {};
var iface = this.interfaces.schema.dummyservice;
- // Define four methods, thie defines their necessity and preferred
providers
- iface.getAttribute('first').define(['corelib', 'extralib']);
- iface.getAttribute('second').define(['extralib', 'corelib']);
- iface.getAttribute('third').define(['corelib']);
- iface.getAttribute('fourth').define(['extralib']);
+ // Requires four methods, thie defines their necessity and preferred
providers
+ iface.getAttribute('first').require({
+ preferredSources: ['corelib', 'extralib']});
+ iface.getAttribute('second').require({
+ preferredSources: ['extralib', 'corelib']});
+ iface.getAttribute('third').require({
+ preferredSources: ['corelib']});
+ iface.getAttribute('fourth').require({
+ preferredSources: ['extralib']});
//
- // somewhere else, provider "core" provides the methods:
- iface.getAttribute('first').provide('corelib', '1.1',
- function() {return function() {return 'first/core';}});
- iface.getAttribute('second').provide('corelib', '1.1',
- function() {return function() {return 'second/core';}});
- iface.getAttribute('third').provide('corelib', '1.1',
- function() {return function() {return 'third/core';}});
+ // somewhere else, source "corelib" provides the methods:
+ iface.getAttribute('first').provide({
+ sourceName: 'corelib',
+ sourceVersion: '1.1',
+ getter: function() {return function() {return 'first/core';}}});
+ iface.getAttribute('second').provide({
+ sourceName: 'corelib',
+ sourceVersion: '1.1',
+ getter: function() {return function() {return 'second/core';}}});
+ iface.getAttribute('third').provide({
+ sourceName: 'corelib',
+ sourceVersion: '1.1',
+ getter: function() {return function() {return 'third/core';}}});
//
- // somewhere else, provider "extras" provides the methods:
- iface.getAttribute('first').provide('extralib', '12.4',
- function() {return function() {return 'first/extra';}});
- iface.getAttribute('second').provide('extralib', '12.4',
- function() {return function() {return 'second/extra';}});
- iface.getAttribute('fourth').provide('extralib', '12.4',
- function() {return function() {return 'fourth/extra';}});
+ // somewhere else, source "extras" provides the methods:
+ iface.getAttribute('first').provide({
+ sourceName: 'extralib',
+ sourceVersion: '12.4',
+ getter: function() {return function() {return 'first/extra';}}});
+ iface.getAttribute('second').provide({
+ sourceName: 'extralib',
+ sourceVersion: '12.4',
+ getter: function() {return function() {return 'second/extra';}}});
+ iface.getAttribute('fourth').provide({
+ sourceName: 'extralib',
+ sourceVersion: '12.4',
+ getter: function() {return function() {return 'fourth/extra';}}});
//
// Now finalize it.
iface.finalize(s);
@@ -212,16 +228,21 @@
// and a counter for the checker
var checker_counter = 0;
// Define three methods
- iface.getAttribute('first').define(['corelib', 'extralib'],
- function() {checker_counter++;});
+ iface.getAttribute('first').require({
+ preferredSources: ['corelib', 'extralib'],
+ checker: function() {checker_counter++;}});
//
// somewhere else, core provides the methods:
- iface.getAttribute('first').provide('corelib', '1.1',
- function() {return function() {return 'first/core';}});
+ iface.getAttribute('first').provide({
+ sourceName: 'corelib',
+ sourceVersion: '1.1',
+ getter: function() {return function() {return 'first/core';}}});
//
// somewhere else, extras provides the methods:
- iface.getAttribute('first').provide('extralib', '12.4',
- function() {return function() {return 'first/extra';}});
+ iface.getAttribute('first').provide({
+ sourceName: 'extralib',
+ sourceVersion: '12.4',
+ getter: function() {return function() {return 'first/extra';}}});
//
// Now finalize it.
iface.finalize(s);
@@ -233,15 +254,18 @@
this.testReverseOrder = function() {
// Method creation also works in reverse order:
- // a plugin first provides a method that another plugin later defines.
+ // a plugin first provides a method that another plugin later requires.
// We have a singleton, called s.
var s = {};
var iface = this.interfaces.schema.dummyservice;
// Provide the method first
- iface.getAttribute('first').provide('corelib', '1.1',
- function() {return function() {return 'first/core';}});
+ iface.getAttribute('first').provide({
+ sourceName: 'corelib',
+ sourceVersion: '1.1',
+ getter: function() {return function() {return 'first/core';}}});
// Define the method later
- iface.getAttribute('first').define(['corelib', 'extralib']);
+ iface.getAttribute('first').require({
+ preferredSources: ['corelib', 'extralib']});
// Now finalize it.
iface.finalize(s);
// We can call the methods on it,
@@ -253,10 +277,13 @@
var s = {};
var iface = this.interfaces.schema.dummyservice;
// Provide the method first
- iface.getAttribute('first').provide('corelib', '1.1',
- function() {return function() {return 'first/core';}});
+ iface.getAttribute('first').provide({
+ sourceName: 'corelib',
+ sourceVersion: '1.1',
+ getter: function() {return function() {return 'first/core';}}});
// Define the method later
- iface.getAttribute('first').define(['corelib', 'extralib']);
+ iface.getAttribute('first').require({
+ preferredSources: ['corelib', 'extralib']});
// Now finalize it.
iface.finalize(s);
// Finalize it again.
@@ -267,12 +294,14 @@
};
this.testNotDefined = function() {
- // A method is provided but not defined.
+ // A method is provided but not required.
var s = {};
var iface = this.interfaces.schema.dummyservice;
// Provide the method first
- iface.getAttribute('first').provide('corelib', '1.1',
- function() {return function() {return 'first/core';}});
+ iface.getAttribute('first').provide({
+ sourceName: 'corelib',
+ sourceVersion: '1.1',
+ getter: function() {return function() {return 'first/core';}}});
// Now finalize it.
this.assertThrows(function() {
iface.finalize(s);
@@ -281,41 +310,49 @@
};
this.testDoubleProvided = function() {
- // A method cannot be provided twice by the same provider.
+ // A method cannot be provided twice by the same source.
// Let's initialize a dimmytest interface.
var iface = this.interfaces.schema.dummyservice;
// Provide the method first
- iface.getAttribute('first').provide('corelib', '1.1',
- function() {return function() {return 'first/core';}});
+ iface.getAttribute('first').provide({
+ sourceName: 'corelib',
+ sourceVersion: '1.1',
+ getter: function() {return function() {return 'first/core';}}});
// Now provide it for the second time.
this.assertThrows(function() {
- iface.getAttribute('first').provide('corelib', '1.1',
- function() {return function() {return 'first/core';}})
+ iface.getAttribute('first').provide({
+ sourceName: 'corelib',
+ sourceVersion: '1.1',
+ getter: function() {return function() {return
'first/core';}}});
},
Error);
};
this.testDoubleDefined = function() {
- // A method can only be defined once.
+ // A method can only be required once.
var iface = this.interfaces.schema.dummyservice;
// Define the method first
- iface.getAttribute('first').define(['corelib', 'extralib']);
- // define it again.
+ iface.getAttribute('first').require({
+ preferredSources: ['corelib', 'extralib']});
+ // require it again.
this.assertThrows(function() {
- iface.getAttribute('first').defineMethod(['corelib', 'extralib'])
- },
+ iface.getAttribute('first').require({
+ preferredSources: ['corelib', 'extralib']})},
Error);
};
this.testNotProvided = function() {
- // No preferred provider can be found for a method.
+ // No preferred vendor can be found for a method.
var s = {};
var iface = this.interfaces.schema.dummyservice;
// Define the method first
- iface.getAttribute('first').define(['corelib', 'extralib']);
+ iface.getAttribute('first').require({
+ preferredSources: ['corelib', 'extralib']});
// Someone provides the method, but it's not good.
- iface.getAttribute('first').provide('craplib', '1.1',
- function() {return function() {return 'first/crap';}});
+ iface.getAttribute('first').provide({
+ sourceName: 'craplib',
+ sourceVersion: '1.1',
+ getter: function() {return function() {return 'first/crap';}}});
// Now finalize it.
this.assertThrows(function() {
iface.finalize(s);
@@ -343,15 +380,25 @@
var s = {};
var iface = this.interfaces.schema.dummyservice;
// Define the method with loader and fallback script
- iface.getAttribute('first').define(['corelib', 'extralib'], null,
'++resource++one');
- iface.getAttribute('second').define(['corelib', 'extralib'], null,
'++resource++two');
- iface.getAttribute('third').define(['corelib', 'extralib'], null,
'++resource++thr');
+ iface.getAttribute('first').require({
+ preferredSources: ['corelib', 'extralib'],
+ fallbackProvider: '++resource++one'});
+ iface.getAttribute('seconf').require({
+ preferredSources: ['corelib', 'extralib'],
+ fallbackProvider: '++resource++two'});
+ iface.getAttribute('third').require({
+ preferredSources: ['corelib', 'extralib'],
+ fallbackProvider: '++resource++three'});
// Someone provides the method, but it's not good.
- iface.getAttribute('first').provide('craplib', '1.1',
- function() {return function() {return 'first/crap';}});
+ iface.getAttribute('first').provide({
+ sourceName: 'craplib',
+ sourceVersion: '1.1',
+ getter: function() {return function() {return 'first/crap';}}});
// Third will be satisfied.
- iface.getAttribute('third').provide('extralib', '1.1',
- function() {return function() {return 'third/extra';}});
+ iface.getAttribute('third').provide({
+ sourceName: 'extralib',
+ sourceVersion: '12.4',
+ getter: function() {return function() {return 'third/extra';}}});
// Now finalize it. It goes without error.
iface.finalize(s, loader);
// We see the contents of the loader. Third is not in there.
_______________________________________________
Kukit-checkins mailing list
[email protected]
http://codespeak.net/mailman/listinfo/kukit-checkins