Skip to content

Commit

Permalink
refined publish functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Eiten committed Aug 17, 2017
1 parent 85da80c commit 4e6655e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 26 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# fin-hypergrid-data-source-base

Abstract base class for Hypergrid data source modules.

Version 1.0.1 implements publish-subscribe and deprecates Data Controllers.

All support for Data Controllers has been removed from Hypergrid as of version 3.0.0.
59 changes: 38 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ function DataSourceBase() {}
DataSourceBase.extend = require('extend-me');
var pubsubstar = require('pubsubstar');

var regexHyphenation = /[-_]\w/g;

DataSourceBase.prototype = {
constructor: DataSourceBase.prototype.constructor,

Expand All @@ -24,6 +26,14 @@ DataSourceBase.prototype = {
this.dataSource = dataSource;
},

postInitialize: function() {
if (this.dataSource) {
this.dataSource.nextSource = this;
} else {
DataSourceBase.prototype.origin = this;
}
},

// GETTERS/SETTERS

get schema() {
Expand Down Expand Up @@ -52,18 +62,6 @@ DataSourceBase.prototype = {
}
},

getFields: function() {
if (this.dataSource) {
return this.dataSource.getFields.apply(this.dataSource, arguments);
}
},

getHeaders: function() {
if (this.dataSource) {
return this.dataSource.getHeaders.apply(this.dataSource, arguments);
}
},

setValue: function() {
if (this.dataSource) {
return this.dataSource.setValue.apply(this.dataSource, arguments);
Expand Down Expand Up @@ -182,23 +180,38 @@ DataSourceBase.prototype = {

unsubscribe: pubsubstar.unsubscribe,

publish: function(topics, message) {
var results = [];
publish: function(topic, message) {
var dataSource, nextDataSourcePropertyName,
results = [];

if (!(typeof topic === 'string' && topic.indexOf('*') < 0)) {
throw new TypeError('DataSourceBase#publish expects topic to be a string primitive sans wildcards.');
}

if (this.publishDirection[topic]) {
dataSource = this;
nextDataSourcePropertyName = 'dataSource';
} else {
dataSource = this.origin;
nextDataSourcePropertyName = 'nextSource';
}

for (var subscribers, dataSource = this; dataSource; dataSource = dataSource.dataSource) {
results.concat(pubsubstar.publish.call(this, topics, message));
for (; dataSource; dataSource = dataSource[nextDataSourcePropertyName]) {
var methodName = topic.replace(regexHyphenation, toCamelCase)
if (typeof dataSource[methodName] === 'function') {
results.push(dataSource[methodName](message));
} else {
results.push(pubsubstar.publish.call(dataSource, topic, message));
}
}

return results;
},

publishDirection: {}, // truthy value means call top-down; otherwise call bottom-up

// OTHER METHODS

apply: function() {
throw new DataSourceError('Nothing to apply.');
},

// DEBUGGING AIDS

/**
* Get new object with name and index given the name or the index.
Expand Down Expand Up @@ -256,6 +269,10 @@ DataSourceBase.prototype = {
}
};

function toCamelCase(hyphenAndNextChar) {
return hyphenAndNextChar[1].toUpperCase();
}

function DataSourceError(message) {
this.message = message;
}
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
{
"name": "fin-hypergrid-data-source-base",
"version": "1.0.0",
"version": "1.0.1",
"description": "Base class for Hypergrid cascading \"data source\" modules.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/openfin/fin-hypergrid-data-source-base.git"
"url": "git+https://github.com/fin-hypergrid/fin-hypergrid-data-source-base.git"
},
"author": "Jonathan Eiten <[email protected]> (http://openfin.co)",
"author": "Jonathan Eiten",
"license": "MIT",
"bugs": {
"url": "https://github.com/openfin/fin-hypergrid/issues"
"url": "https://github.com/fin-hypergrid/fin-hypergrid-data-source-base/issues"
},
"homepage": "https://github.com/openfin/fin-hypergrid-data-source-base#readme",
"homepage": "https://github.com/fin-hypergrid/fin-hypergrid-data-source-base#readme",
"dependencies": {
"extend-me": "^2.3",
"pubsubstar": "^1.0.1"
Expand Down

0 comments on commit 4e6655e

Please sign in to comment.