From 4e6655e1975a8e84cea6737ef7afbe293533c309 Mon Sep 17 00:00:00 2001 From: Jonathan Eiten Date: Thu, 17 Aug 2017 14:45:20 -0400 Subject: [PATCH] refined publish functionality --- README.md | 4 ++++ index.js | 59 +++++++++++++++++++++++++++++++++------------------- package.json | 10 ++++----- 3 files changed, 47 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 84a35cd..468bac0 100644 --- a/README.md +++ b/README.md @@ -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. \ No newline at end of file diff --git a/index.js b/index.js index d7013b1..1c74f53 100644 --- a/index.js +++ b/index.js @@ -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, @@ -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() { @@ -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); @@ -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. @@ -256,6 +269,10 @@ DataSourceBase.prototype = { } }; +function toCamelCase(hyphenAndNextChar) { + return hyphenAndNextChar[1].toUpperCase(); +} + function DataSourceError(message) { this.message = message; } diff --git a/package.json b/package.json index 498c156..9a127df 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "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": { @@ -8,14 +8,14 @@ }, "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 (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"