Skip to content
This repository has been archived by the owner on Jun 9, 2020. It is now read-only.

Commit

Permalink
Merge pull request #16 from crossfilter/feat-destroy-method
Browse files Browse the repository at this point in the history
Feat destroy method
  • Loading branch information
tannerlinsley committed Apr 11, 2016
2 parents b9c5638 + f527394 commit 321db01
Show file tree
Hide file tree
Showing 10 changed files with 232 additions and 70 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "universe",
"version": "0.6.2",
"version": "0.6.3",
"description": "The fastest way to query and explore multivariate datasets",
"main": "src/universe.js",
"directories": {
Expand Down
37 changes: 23 additions & 14 deletions src/column.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ module.exports = function(service) {
}

// for storing info about queries and post aggregations
column.queries = [];
column.queries = []
service.columns.push(column)

column.promise = Promise.try(function() {
Expand Down Expand Up @@ -125,22 +125,31 @@ module.exports = function(service) {
return buildColumnKeys()

// Build the columnKeys
function buildColumnKeys(onAdd) {
function buildColumnKeys(changes) {
if (column.key === true) {
return Promise.resolve()
}
return Promise.resolve(column.dimension.bottom(Infinity))
.then(function(rows) {
var accessor = dimension.makeAccessor(column.key)
if (column.type === 'complex') {
column.values = _.uniq(_.flatten(_.map(rows, accessor)))
}
else if (column.type === 'array') {
column.values = _.uniq(_.flatten(_.map(rows, accessor)))
} else {
column.values = _.uniq(_.map(rows, accessor))
}
})

var accessor = dimension.makeAccessor(column.key)
column.values = column.values || []

return Promise.try(function(){
if (changes && changes.added) {
return Promise.resolve(changes.added)
} else {
return Promise.resolve(column.dimension.bottom(Infinity))
}
})
.then(function(rows) {
if (column.type === 'complex') {
var newValues = _.flatten(_.map(rows, accessor))
} else if (column.type === 'array') {
var newValues = _.flatten(_.map(rows, accessor))
} else {
var newValues = _.map(rows, accessor)
}
column.values = _.uniq(column.values.concat(newValues))
})
}
})

Expand Down
4 changes: 3 additions & 1 deletion src/crossfilter.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ module.exports = function(service) {
.then(function() {
return Promise.serial(_.map(service.dataListeners, function(listener) {
return function() {
return listener(true)
return listener({
added: data
})
}
}))
})
Expand Down
18 changes: 18 additions & 0 deletions src/destroy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict'

var Promise = require('q')
var _ = require('./lodash')

module.exports = function(service) {
return function destroy() {
return service.clear()
.then(function(){
service.cf.dataListeners = []
service.cf.filterListeners = []
return Promise.resolve(service.cf.remove())
})
.then(function(){
return service
})
}
}
5 changes: 3 additions & 2 deletions src/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,14 +216,15 @@ module.exports = function(service) {
}

function clearQuery() {
_.forEach(q.removeListners, function(l) {
_.forEach(q.removeListeners, function(l) {
l()
})
return Promise.try(function() {
return q.group.dispose()
})
.then(function() {
q.column.queries.splice(q.column.queries.indexOf(q), 1)
// Automatically recycle the column if there are no queries active on it
if (!q.column.queries.length) {
return service.clear(q.column.key)
}
Expand All @@ -235,7 +236,7 @@ module.exports = function(service) {

function postAggregateMethodWrap(postMethod) {
return function() {
var args = Array.prototype.slice.call(arguments);
var args = Array.prototype.slice.call(arguments)
var sub = {}
newQueryObj(sub, q)
args.unshift(sub, q)
Expand Down
1 change: 1 addition & 0 deletions src/universe.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ function universe(data, options) {
query: require('./query')(service),
filter: require('./filters')(service).filter,
clear: require('./clear')(service),
destroy: require('./destroy')(service),
onDataChange: onDataChange,
onFilter: onFilter,
})
Expand Down
23 changes: 5 additions & 18 deletions test/column.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,6 @@ describe('universe column', function() {
var diff1
var diff2
return u.then(function(u) {
return Promise.all([
u.column({
key: ['type', 'total']
}),
])
.then(function(){
return u
})
})
.then(function(u) {
diff1 = Date.now() - now
return u
})
.then(function(u) {
now = Date.now()
return Promise.all([
u.column({
key: ['type', 'total']
Expand Down Expand Up @@ -128,10 +113,12 @@ describe('universe column', function() {
key: ['type', 'total']
})
])
.then(function(){
return u
})
})
.then(function() {
var diff2 = Date.now() - now
expect((diff2 - diff1) < 10).to.equal(true)
.then(function(u) {
expect(u.columns.length).to.equal(1)
})
})

Expand Down
111 changes: 111 additions & 0 deletions test/destroy.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
var chai = require('chai')
var chaiAsPromised = require('chai-as-promised')

chai.use(chaiAsPromised)
var expect = chai.expect

var universe = require('../universe')
var crossfilter = require('crossfilter2')
var data = require('./data')

describe('universe clear', function() {

var u = universe(data)

beforeEach(function() {
return u.then(function(u) {
return u.clear()
})
})

it('can destroy the universe a few times over', function() {
return u.then(function(u) {
return u.query({
groupBy: 'type',
select: {
$count: true
}
})
})
.then(function(u) {
return u.universe.query({
groupBy: 'total',
select: {
$count: true
}
})
})
.then(function(u) {
return u.universe.query({
groupBy: 'tip',
select: {
$count: true
}
})
})
.then(function(u) {
return u.universe.destroy()
})
.then(function(u) {
return u.add(data)
})
.then(function(u) {
return u.query({
groupBy: 'type',
select: {
$count: true
}
})
})
.then(function(u) {
return u.universe.query({
groupBy: 'total',
select: {
$count: true
}
})
})
.then(function(u) {
return u.universe.query({
groupBy: 'tip',
select: {
$count: true
}
})
})
.then(function(u) {
return u.universe.destroy()
})
.then(function(u) {
return u.add(data)
})
.then(function(u) {
return u.query({
groupBy: 'type',
select: {
$count: true
}
})
})
.then(function(u) {
return u.universe.query({
groupBy: 'total',
select: {
$count: true
}
})
})
.then(function(u) {
return u.universe.query({
groupBy: 'tip',
select: {
$count: true
}
})
})
.then(function(u) {
return u.universe.destroy()
})
})

})
Loading

0 comments on commit 321db01

Please sign in to comment.