Skip to content

Commit

Permalink
fixed local delete method to work with paths and object notation
Browse files Browse the repository at this point in the history
  • Loading branch information
almeynman committed Feb 23, 2016
1 parent c832c0a commit 636bf30
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 40 deletions.
12 changes: 6 additions & 6 deletions examples/todos/src/Todo.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ export default createContainer(Todo, {

const deleteTodo$ = intents.get('deleteTodo')
deleteTodo$.
// TODO what is a path syntax here
subscribe(index => model.local.delete({
todos: {
[index]: null
}
}))
subscribe(index => model.local.delete(['todos', index]))
// subscribe(index => model.local.delete({
// todos: {
// [index]: null
// }
// }))

return {
toggleCompleted$,
Expand Down
43 changes: 23 additions & 20 deletions lib/Provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ var _invariant = require('invariant');

var _invariant2 = _interopRequireDefault(_invariant);

var _merge = require('lodash/merge');

var _merge2 = _interopRequireDefault(_merge);

var _isPlainObject = require('lodash/isPlainObject');

var _isPlainObject2 = _interopRequireDefault(_isPlainObject);

var _isArray = require('lodash/isArray');

var _isArray2 = _interopRequireDefault(_isArray);

var _utils = require('./utils');

var _PropTypes = require('./PropTypes');
Expand Down Expand Up @@ -86,35 +86,38 @@ var Provider = exports.Provider = function (_Component) {
_this.model.local = _this.model.withoutDataSource();
// local updates
var previousSet = _this.model.local.set;
var normalizePathValues = function normalizePathValues(pathValues) {
(0, _invariant2["default"])(pathValues, 'set must accept either falcor\'s pathValue object\n or a plain object');
_this.model.local.set = function () {
for (var _len = arguments.length, pathValues = Array(_len), _key = 0; _key < _len; _key++) {
pathValues[_key] = arguments[_key];
}

(0, _invariant2["default"])(pathValues && pathValues.length > 0, 'set must accept either\n falcor\'s pathValue object or a plain object');
var finalPathValues = undefined;
if (pathValues.length === 1 && (0, _isPlainObject2["default"])(pathValues[0]) && !pathValues[0].path) {
finalPathValues = (0, _utils.toPathValues)(pathValues[0]);
} else {
finalPathValues = pathValues;
}
return finalPathValues;
};
_this.model.local.set = function () {
for (var _len = arguments.length, pathValues = Array(_len), _key = 0; _key < _len; _key++) {
pathValues[_key] = arguments[_key];
}

var finalPathValues = normalizePathValues(pathValues);
previousSet.apply(_this.model.local, finalPathValues).then(function () {});
};

_this.model.local["delete"] = function () {
for (var _len2 = arguments.length, pathValues = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
pathValues[_key2] = arguments[_key2];
for (var _len2 = arguments.length, paths = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
paths[_key2] = arguments[_key2];
}

var finalPathValues = normalizePathValues(pathValues);
finalPathValues.forEach(function (pV) {
return pV.value = { $type: 'atom', '$expires': 0 };
(0, _invariant2["default"])(paths && paths.length > 0, 'delete must accept either\n falcor\'s paths or a plain object');
var finalPaths = undefined;
if ((0, _isPlainObject2["default"])(paths[0])) {
finalPaths = (0, _utils.toPaths)(paths[0], true, true);
}
if ((0, _isArray2["default"])(paths[0])) {
finalPaths = paths;
}
var pathValues = finalPaths.map(function (path) {
return { path: path, value: { $type: 'atom', '$expires': 0 } };
});
previousSet.apply(_this.model.local, finalPathValues).then(function () {});
previousSet.apply(_this.model.local, pathValues).then(function () {});
};

// create intents
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "refar",
"version": "0.3.1",
"version": "0.3.2",
"description": "Falcor + Rx + React connector library",
"main": "lib/index.js",
"scripts": {
Expand Down
30 changes: 17 additions & 13 deletions src/Provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { Component, PropTypes, Children } from 'react'
import { BehaviorSubject } from 'rxjs/subject/BehaviorSubject'
import { Subject } from 'rxjs/Subject'
import invariant from 'invariant'
import merge from 'lodash/merge'
import isPlainObject from 'lodash/isPlainObject'
import isArray from 'lodash/isArray'

import { toPathValues } from './utils'
import { toPathValues, toPaths } from './utils'
import { modelType } from './PropTypes'

let didWarnAboutReceivingModel = false
Expand Down Expand Up @@ -55,26 +55,30 @@ export class Provider extends Component {
this.model.local = this.model.withoutDataSource()
// local updates
const previousSet = this.model.local.set
const normalizePathValues = pathValues => {
invariant(pathValues, `set must accept either falcor's pathValue object
or a plain object`)
this.model.local.set = (...pathValues) => {
invariant(pathValues && pathValues.length > 0, `set must accept either
falcor's pathValue object or a plain object`)
let finalPathValues
if (pathValues.length === 1 && isPlainObject(pathValues[0]) && !pathValues[0].path) {
finalPathValues = toPathValues(pathValues[0])
} else {
finalPathValues = pathValues
}
return finalPathValues
}
this.model.local.set = (...pathValues) => {
const finalPathValues = normalizePathValues(pathValues)
previousSet.apply(this.model.local, finalPathValues).then(() => {})
}

this.model.local.delete = (...pathValues) => {
const finalPathValues = normalizePathValues(pathValues)
finalPathValues.forEach(pV => pV.value = { $type: 'atom', '$expires': 0 })
previousSet.apply(this.model.local, finalPathValues).then(() => {})
this.model.local.delete = (...paths) => {
invariant(paths && paths.length > 0, `delete must accept either
falcor's paths or a plain object`)
let finalPaths
if (isPlainObject(paths[0])) {
finalPaths = toPaths(paths[0], true, true)
}
if (isArray(paths[0])) {
finalPaths = paths
}
const pathValues = finalPaths.map(path => ({ path, value: { $type: 'atom', '$expires': 0 } }))
previousSet.apply(this.model.local, pathValues).then(() => {})
}

// create intents
Expand Down

0 comments on commit 636bf30

Please sign in to comment.