Comparing version 0.1.8 to 0.2.0
@@ -10,8 +10,9 @@ 'use strict'; | ||
exports.Query = _types.Query; | ||
exports.QueryId = _types.QueryId; | ||
exports.Command = _types.Command; | ||
var _Avenger = require('./Avenger'); | ||
var _mkAvenger = require('./mkAvenger'); | ||
var _Avenger2 = _interopRequireDefault(_Avenger); | ||
var _mkAvenger2 = _interopRequireDefault(_mkAvenger); | ||
exports['default'] = _Avenger2['default']; | ||
exports['default'] = _mkAvenger2['default']; |
192
lib/types.js
@@ -5,2 +5,4 @@ 'use strict'; | ||
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } | ||
@@ -20,2 +22,3 @@ | ||
exports.QueryId = QueryId; | ||
var Dependency = _tcomb2['default'].struct({ | ||
@@ -27,34 +30,13 @@ // dep on this query | ||
// (this should minimize size of dep. results): | ||
map: _tcomb2['default'].maybe(_tcomb2['default'].Function), | ||
// TODO(gio): unused. use Query.cacheParams instead | ||
// override cache params from this dep | ||
// cacheParams: t.maybe(t.list(t.String)), | ||
// run dependent query (this query) once for | ||
// each value returned by dependency query. | ||
// make sure the query (or this dep. `map` fn) returns an array. | ||
// optionally provide a function here to map multi-results. | ||
// defaults to an array, same size as input array | ||
multi: _tcomb2['default'].maybe(_tcomb2['default'].union([_tcomb2['default'].Boolean, _tcomb2['default'].Function])) | ||
map: _tcomb2['default'].maybe(_tcomb2['default'].Function) | ||
}, 'Dependency'); | ||
var Dependencies = _tcomb2['default'].maybe(_tcomb2['default'].refinement(_tcomb2['default'].dict(QueryId, Dependency), | ||
// TODO(gio): looks like a dumb/arbitrary limitation? | ||
// only a single `multi` dependency is allowed | ||
function (deps) { | ||
return Object.keys(deps).map(function (k) { | ||
return deps[k]; | ||
}).filter(function (_ref) { | ||
var multi = _ref.multi; | ||
return !!multi; | ||
}).length <= 1; | ||
}, 'Dependencies')); | ||
var Dependencies = _tcomb2['default'].dict(QueryId, Dependency); | ||
var CacheMode = _tcomb2['default'].enums.of([ | ||
// (default): results for this q. are never stored in cache | ||
var CacheStrategy = _tcomb2['default'].enums.of([ | ||
// (default): results for this query are never stored in cache | ||
'no', | ||
// results are stored and always returned from cache, | ||
// a re-fetch is always re-issued at every pass | ||
// a fetch is always re-issued at every pass | ||
'optimistic', | ||
@@ -64,98 +46,114 @@ | ||
// (without manual intervention, never re-fetched) | ||
'manual'], 'CacheMode'); | ||
'manual'], 'CacheStrategy'); | ||
var TcombType = _tcomb2['default'].irreducible('TcombType', _tcomb.isType); | ||
var CacheParams = _tcomb2['default'].dict(_tcomb2['default'].String, TcombType, 'CacheParams'); | ||
var QueryParams = _tcomb2['default'].dict(_tcomb2['default'].String, TcombType, 'QueryParams'); | ||
var Query = _tcomb2['default'].struct({ | ||
// here for simplicity for now | ||
id: QueryId, | ||
// define caching policy for this query | ||
cache: _tcomb2['default'].maybe(CacheMode), | ||
// caching strategy for this query | ||
cacheStrategy: _tcomb2['default'].maybe(CacheStrategy), | ||
// cache params should default to all params + all deps params | ||
// this overrides caching of state params. | ||
// optionally pass a function to map the cache param value | ||
// remember that cache params should be primitive | ||
cacheParams: _tcomb2['default'].maybe(CacheParams), | ||
// typing for params passed to `fetch()` | ||
// params default to all params + all deps params | ||
// these are the same key:values used to store | ||
// each `fetch()` result in cache | ||
params: _tcomb2['default'].maybe(QueryParams), | ||
// dictionary of deps. { [queryId]: dep.map(queryRes), ... } | ||
dependencies: Dependencies, | ||
// dictionary of deps. { [paramName]: { query: queryRef } } | ||
dependencies: _tcomb2['default'].maybe(Dependencies), | ||
// state: t.Object -> depFetchParams: t.Object -> Promise[t.Object] | ||
fetch: _tcomb2['default'].Function | ||
// state: query.params -> Promise<returnType> | ||
fetch: _tcomb2['default'].Function, | ||
// return type of this query `fetch()` | ||
returnType: _tcomb2['default'].maybe(TcombType) | ||
}, 'Query'); | ||
exports.Query = Query; | ||
var _upsetActualParams = {}; | ||
Object.defineProperty(Query.prototype, 'upsetActualParams', { | ||
get: function get() { | ||
var _this = this; | ||
if (_upsetActualParams[this.id]) { | ||
return _upsetActualParams[this.id]; | ||
} else { | ||
var _ret = (function () { | ||
var deps = _this.dependencies || {}; | ||
_upsetActualParams[_this.id] = _extends({}, Object.keys(_this.params || {}).filter(function (k) { | ||
return !deps[k]; | ||
}).reduce(function (ac, k) { | ||
var _extends2; | ||
return _extends({}, ac, (_extends2 = {}, _extends2[k] = _this.params[k], _extends2)); | ||
}, {}), Object.keys(deps).reduce(function (ac, k) { | ||
return _extends({}, ac, _this.dependencies[k].query.upsetActualParams); | ||
}, {})); | ||
return { | ||
v: _upsetActualParams[_this.id] | ||
}; | ||
})(); | ||
if (typeof _ret === 'object') return _ret.v; | ||
} | ||
} | ||
}); | ||
Dependency.meta.props.query = Query; | ||
var AvengerInput = _tcomb2['default'].dict(_tcomb2['default'].Any, Query, 'AvengerInput'); | ||
var Queries = _tcomb2['default'].dict(_tcomb2['default'].Any, Query, 'Queries'); | ||
exports.AvengerInput = AvengerInput; | ||
exports.Queries = Queries; | ||
var CommandId = QueryId; | ||
exports.CommandId = CommandId; | ||
var CommandParams = QueryParams; | ||
var Command = _tcomb2['default'].struct({ | ||
// an optional list of queries to invalidate | ||
// entire downset for these will be invalidated as well | ||
invalidates: _tcomb2['default'].maybe(AvengerInput), | ||
id: CommandId, | ||
// an optional set of queries to invalidate | ||
// if it is a function, commands params are passed as only argument | ||
invalidates: _tcomb2['default'].maybe(_tcomb2['default'].union([Queries, _tcomb2['default'].Function])), | ||
params: _tcomb2['default'].maybe(CommandParams), | ||
// actual command | ||
run: _tcomb2['default'].Function // state: t.Object -> Promise[t.Any] | ||
run: _tcomb2['default'].Function // ...t.Any -> Promise[t.Any] | ||
}, 'Command'); | ||
exports.Command = Command; | ||
var QueryNodeEdges = _tcomb2['default'].dict(_tcomb2['default'].String, _tcomb2['default'].Any, 'QueryNodeEdges'); // circular, fixed below | ||
var _invalidateParams = {}; | ||
Object.defineProperty(Command.prototype, 'invalidateParams', { | ||
get: function get() { | ||
var _this2 = this; | ||
// a single DAG node | ||
var QueryNode = _tcomb2['default'].struct({ | ||
query: Query, // the query | ||
parents: QueryNodeEdges, // dependencies | ||
children: QueryNodeEdges // dependants | ||
}, 'QueryNode'); | ||
if (_invalidateParams[this.id]) { | ||
return _invalidateParams[this.id]; | ||
} else { | ||
var _ret2 = (function () { | ||
var inv = _this2.invalidates || {}; | ||
_invalidateParams[_this2.id] = Object.keys(inv).reduce(function (ac, k) { | ||
return _extends({}, ac, inv[k].upsetActualParams); | ||
}, {}); | ||
return { | ||
v: _invalidateParams[_this2.id] | ||
}; | ||
})(); | ||
exports.QueryNode = QueryNode; | ||
QueryNodeEdges.meta.codomain = QueryNode; | ||
if (typeof _ret2 === 'object') return _ret2.v; | ||
} | ||
} | ||
}); | ||
var QueryNodes = QueryNodeEdges; | ||
exports.QueryNodes = QueryNodes; | ||
var StateKey = _tcomb2['default'].Any; | ||
exports.StateKey = StateKey; | ||
// export const StateKey = t.irreducible( | ||
// 'StateKey', | ||
// v => t.Nil.is(v) || t.String.is(v) || t.Number.is(v) || t.Boolean.is(v) || t.Date.is(v), | ||
// ); | ||
var StateKey = _tcomb2['default'].Any;exports.StateKey = StateKey; | ||
// TODO(gio): we are JSON.stringifying the world in cache keys instead | ||
var State = _tcomb2['default'].dict(_tcomb2['default'].String, StateKey, 'State'); | ||
exports.State = State; | ||
// cache internal state representation | ||
var CacheState = _tcomb2['default'].dict(_tcomb2['default'].String, _tcomb2['default'].dict(_tcomb2['default'].String, _tcomb2['default'].Any)); | ||
exports.CacheState = CacheState; | ||
var EmitMeta = _tcomb2['default'].struct({ | ||
id: QueryId, | ||
error: _tcomb2['default'].maybe(_tcomb2['default'].Boolean), | ||
cache: _tcomb2['default'].maybe(_tcomb2['default'].Boolean), | ||
loading: _tcomb2['default'].maybe(_tcomb2['default'].Boolean), | ||
multi: _tcomb2['default'].maybe(_tcomb2['default'].Boolean), | ||
multiIndex: _tcomb2['default'].maybe(_tcomb2['default'].Number), | ||
multiAll: _tcomb2['default'].maybe(_tcomb2['default'].Boolean) | ||
}, 'EmitMeta'); | ||
exports.EmitMeta = EmitMeta; | ||
// export const MinimizedCache = t.dict( | ||
// // dependant qId | ||
// t.String, | ||
// t.dict( | ||
// // dependency qId | ||
// t.String, | ||
// // mapped (minimized) value | ||
// t.Any | ||
// ), | ||
// 'MinimizedCache' | ||
// ); | ||
// export const Value = t.struct({ | ||
// val: t.Any, | ||
// meta: t.struct({ | ||
// cached: t.Boolean, | ||
// error: t.Boolean, | ||
// loading: t.Boolean | ||
// }) | ||
// }, 'Value'); | ||
exports.State = State; |
{ | ||
"name": "avenger", | ||
"version": "0.1.8", | ||
"version": "0.2.0", | ||
"main": "index.js", | ||
@@ -8,3 +8,3 @@ "scripts": { | ||
"build": "rm -rf lib && mkdir lib && babel --out-dir lib src", | ||
"lint": "eslint src test", | ||
"lint": "./node_modules/eslint/bin/eslint.js src", | ||
"preversion": "npm run lint && npm run test", | ||
@@ -24,3 +24,2 @@ "prepublish": "npm run build" | ||
"debug": "^2.2.0", | ||
"eventemitter3": "^1.1.1", | ||
"lodash": "^3.9.3", | ||
@@ -32,13 +31,15 @@ "tcomb": "^2.5.0" | ||
"babel-eslint": "^4.1.3", | ||
"babel-plugin-tcomb": "gcanti/babel-plugin-tcomb#e11396625e8d1e63d3cbfc21eaa46a388b93f47e", | ||
"babel-plugin-tcomb": "^0.1.3", | ||
"better-assert": "^1.0.2", | ||
"eslint": "^1.6.0", | ||
"eslint-config-buildo": "github:buildo/eslint-config", | ||
"expect": "^1.6.0", | ||
"mocha": "*", | ||
"mocha-bamboo-reporter": "^1.1.0", | ||
"node-libs-browser": "^0.5.2", | ||
"require-dir": "^0.3.0", | ||
"sinon": "^1.16.1", | ||
"webpack": "^1.9.10" | ||
}, | ||
"peerDependencies": { | ||
"rxjs": "^5.0.0-beta.2" | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
export { Query, Command } from './types'; | ||
export default from './Avenger'; | ||
export { Query, QueryId, Command } from './types'; | ||
export default from './mkAvenger'; |
160
src/types.js
@@ -6,3 +6,3 @@ import t, { isType } from 'tcomb'; | ||
// unique string id for the query | ||
const QueryId = t.String; | ||
export const QueryId = t.String; | ||
@@ -15,30 +15,13 @@ const Dependency = t.struct({ | ||
// (this should minimize size of dep. results): | ||
map: t.maybe(t.Function), | ||
// TODO(gio): unused. use Query.cacheParams instead | ||
// override cache params from this dep | ||
// cacheParams: t.maybe(t.list(t.String)), | ||
// run dependent query (this query) once for | ||
// each value returned by dependency query. | ||
// make sure the query (or this dep. `map` fn) returns an array. | ||
// optionally provide a function here to map multi-results. | ||
// defaults to an array, same size as input array | ||
multi: t.maybe(t.union([t.Boolean, t.Function])) | ||
map: t.maybe(t.Function) | ||
}, 'Dependency'); | ||
const Dependencies = t.maybe(t.refinement( | ||
t.dict(QueryId, Dependency), | ||
// TODO(gio): looks like a dumb/arbitrary limitation? | ||
// only a single `multi` dependency is allowed | ||
deps => Object.keys(deps).map(k => deps[k]).filter(({ multi }) => !!multi).length <= 1, | ||
'Dependencies' | ||
)); | ||
const Dependencies = t.dict(QueryId, Dependency); | ||
const CacheMode = t.enums.of([ | ||
// (default): results for this q. are never stored in cache | ||
const CacheStrategy = t.enums.of([ | ||
// (default): results for this query are never stored in cache | ||
'no', | ||
// results are stored and always returned from cache, | ||
// a re-fetch is always re-issued at every pass | ||
// a fetch is always re-issued at every pass | ||
'optimistic', | ||
@@ -49,89 +32,92 @@ | ||
'manual' | ||
], 'CacheMode'); | ||
], 'CacheStrategy'); | ||
const TcombType = t.irreducible('TcombType', isType); | ||
const CacheParams = t.dict(t.String, TcombType, 'CacheParams'); | ||
const QueryParams = t.dict(t.String, TcombType, 'QueryParams'); | ||
export const Query = t.struct({ | ||
// here for simplicity for now | ||
id: QueryId, | ||
// define caching policy for this query | ||
cache: t.maybe(CacheMode), | ||
// caching strategy for this query | ||
cacheStrategy: t.maybe(CacheStrategy), | ||
// cache params should default to all params + all deps params | ||
// this overrides caching of state params. | ||
// optionally pass a function to map the cache param value | ||
// remember that cache params should be primitive | ||
cacheParams: t.maybe(CacheParams), | ||
// typing for params passed to `fetch()` | ||
// params default to all params + all deps params | ||
// these are the same key:values used to store | ||
// each `fetch()` result in cache | ||
params: t.maybe(QueryParams), | ||
// dictionary of deps. { [queryId]: dep.map(queryRes), ... } | ||
dependencies: Dependencies, | ||
// dictionary of deps. { [paramName]: { query: queryRef } } | ||
dependencies: t.maybe(Dependencies), | ||
// state: t.Object -> depFetchParams: t.Object -> Promise[t.Object] | ||
fetch: t.Function | ||
// state: query.params -> Promise<returnType> | ||
fetch: t.Function, | ||
// return type of this query `fetch()` | ||
returnType: t.maybe(TcombType) | ||
}, 'Query'); | ||
const _upsetActualParams = {}; | ||
Object.defineProperty(Query.prototype, 'upsetActualParams', { | ||
get() { | ||
if (_upsetActualParams[this.id]) { | ||
return _upsetActualParams[this.id]; | ||
} else { | ||
const deps = this.dependencies || {}; | ||
_upsetActualParams[this.id] = { | ||
...Object.keys(this.params || {}).filter(k => !deps[k]).reduce((ac, k) => ({ | ||
...ac, [k]: this.params[k] | ||
}), {}), | ||
...Object.keys(deps).reduce((ac, k) => ({ | ||
...ac, ...this.dependencies[k].query.upsetActualParams | ||
}), {}) | ||
}; | ||
return _upsetActualParams[this.id]; | ||
} | ||
} | ||
}); | ||
Dependency.meta.props.query = Query; | ||
export const AvengerInput = t.dict(t.Any, Query, 'AvengerInput'); | ||
export const Queries = t.dict(t.Any, Query, 'Queries'); | ||
export const Command = t.struct({ | ||
// an optional list of queries to invalidate | ||
// entire downset for these will be invalidated as well | ||
invalidates: t.maybe(AvengerInput), | ||
export const CommandId = QueryId; | ||
// actual command | ||
run: t.Function // state: t.Object -> Promise[t.Any] | ||
}, 'Command'); | ||
const CommandParams = QueryParams; | ||
const QueryNodeEdges = t.dict(t.String, t.Any, 'QueryNodeEdges'); // circular, fixed below | ||
export const Command = t.struct({ | ||
id: CommandId, | ||
// a single DAG node | ||
export const QueryNode = t.struct({ | ||
query: Query, // the query | ||
parents: QueryNodeEdges, // dependencies | ||
children: QueryNodeEdges // dependants | ||
}, 'QueryNode'); | ||
// an optional set of queries to invalidate | ||
// if it is a function, commands params are passed as only argument | ||
invalidates: t.maybe(t.union([Queries, t.Function])), | ||
QueryNodeEdges.meta.codomain = QueryNode; | ||
params: t.maybe(CommandParams), | ||
export const QueryNodes = QueryNodeEdges; | ||
// actual command | ||
run: t.Function // ...t.Any -> Promise[t.Any] | ||
}, 'Command'); | ||
export const StateKey = t.Any; | ||
export const State = t.dict(t.String, StateKey, 'State'); | ||
const _invalidateParams = {}; | ||
Object.defineProperty(Command.prototype, 'invalidateParams', { | ||
get() { | ||
if (_invalidateParams[this.id]) { | ||
return _invalidateParams[this.id]; | ||
} else { | ||
const inv = this.invalidates || {}; | ||
_invalidateParams[this.id] = Object.keys(inv).reduce((ac, k) => ({ | ||
...ac, ...inv[k].upsetActualParams | ||
}), {}); | ||
return _invalidateParams[this.id]; | ||
} | ||
} | ||
}); | ||
// cache internal state representation | ||
export const CacheState = t.dict(t.String, t.dict(t.String, t.Any)); | ||
export const EmitMeta = t.struct({ | ||
id: QueryId, | ||
error: t.maybe(t.Boolean), | ||
cache: t.maybe(t.Boolean), | ||
loading: t.maybe(t.Boolean), | ||
multi: t.maybe(t.Boolean), | ||
multiIndex: t.maybe(t.Number), | ||
multiAll: t.maybe(t.Boolean) | ||
}, 'EmitMeta'); | ||
// export const MinimizedCache = t.dict( | ||
// // dependant qId | ||
// t.String, | ||
// t.dict( | ||
// // dependency qId | ||
// t.String, | ||
// // mapped (minimized) value | ||
// t.Any | ||
// ), | ||
// 'MinimizedCache' | ||
// export const StateKey = t.irreducible( | ||
// 'StateKey', | ||
// v => t.Nil.is(v) || t.String.is(v) || t.Number.is(v) || t.Boolean.is(v) || t.Date.is(v), | ||
// ); | ||
// export const Value = t.struct({ | ||
// val: t.Any, | ||
// meta: t.struct({ | ||
// cached: t.Boolean, | ||
// error: t.Boolean, | ||
// loading: t.Boolean | ||
// }) | ||
// }, 'Value'); | ||
export const StateKey = t.Any; // TODO(gio): we are JSON.stringifying the world in cache keys instead | ||
export const State = t.dict(t.String, StateKey, 'State'); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
11
0
3
45519
17
1081
+ Addedrxjs@5.5.12(transitive)
+ Addedsymbol-observable@1.0.1(transitive)
- Removedeventemitter3@^1.1.1
- Removedeventemitter3@1.2.0(transitive)