Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

slt

Package Overview
Dependencies
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

slt - npm Package Compare versions

Comparing version 1.2.0 to 1.3.0

lib/__tests__/data/deps.js

1

lib/__tests__/cascadeRules.js

@@ -53,3 +53,4 @@ "use strict";

});
// deal with conflicts
});
//# sourceMappingURL=cascadeRules.js.map

4

lib/__tests__/data/cascadeRules.js

@@ -12,5 +12,7 @@ "use strict";

return this.set("users", url);
}
},
"users": function users() {}
};
module.exports = exports["default"];
//# sourceMappingURL=cascadeRules.js.map

@@ -12,5 +12,5 @@ 'use strict';

it('should set Rules and State', function () {
var set = function set(route) {};
var rules = {
'route': function route(_route) {}
'route': set
};

@@ -26,4 +26,8 @@

slots = new _slots2['default'](rules, state);
expect(slots.getRules()).toEqual(rules);
expect(slots.getRules().route).toEqual(jasmine.any(Function));
expect(slots.rules).toEqual({
'route': {
set: set,
deps: []
}
});

@@ -30,0 +34,0 @@ expect(slots.getState().toJS()).toEqual(state);

@@ -29,3 +29,3 @@ "use strict";

var Branch = (function () {
function Branch(rules, state, ctx) {
function Branch(state, slots, ctx) {
var parent = arguments[3] === undefined ? null : arguments[3];

@@ -35,3 +35,3 @@

this.rules = rules;
this.slots = slots;
this.state = state;

@@ -53,3 +53,3 @@ this.ctx = ctx;

value: function newBranch(state) {
var branch = new Branch(this.rules, state, this.ctx, this);
var branch = new Branch(state, this.slots, this.ctx, this);
this.children.push(branch);

@@ -79,16 +79,21 @@ return branch;

log("SET %s TO %s", (0, _utils.insp)(path), (0, _utils.insp)(value));
var imValue = (0, _immutable.fromJS)(value);
var result = imValue.toJS ? this.state.mergeDeepIn(path, imValue) : this.state.setIn(path, imValue);
d("MERGED \n%s", (0, _utils.insp)(result));
var state = this.state;
d("MERGED \n%s", (0, _utils.insp)(state));
state = Branch.mergeValue(state, path, value); // TODO: deal with conflicts
var applyRules = function applyRules() {
var path = arguments[0] === undefined ? new _immutable.List() : arguments[0];
var value = arguments[1] === undefined ? new _immutable.Map() : arguments[1];
var value = arguments[1] === undefined ? {} : arguments[1];
var rule = _this.rules.get(path.toArray().join("."));
if ((0, _utils.isFunction)(rule)) {
var rule = _this.getSetRule(path);
if (rule) {
(function () {
var val = result.getIn(path);
log("RULE on path %s matched with value %s", (0, _utils.insp)(path), (0, _utils.insp)(val));
var branch = rule.call(_this.newBranch(result), (0, _utils.toJS)(val));
if ((0, _utils.isPromise)(branch)) {
log("RULE on path %s matched with value %s", (0, _utils.insp)(path), (0, _utils.insp)(value));
var branch = _this.newBranch(state);
rule.call(branch, value);
state = branch.state;
if (!(0, _utils.isPromise)(branch)) {
d("NEW BRANCH with state %s", (0, _utils.insp)(state));
state = branch.state;
d("RESULT is %s", (0, _utils.insp)(state));
} else {
log("PROMISE RETURNED");

@@ -102,18 +107,14 @@ branch.bind(_this.ctx); // out of call stack

});
} else {
d("NEW BRANCH with state %s", (0, _utils.insp)(result));
result = branch.getState();
d("RESULT is %s", (0, _utils.insp)(result));
}
})();
} else {
if ((0, _utils.isObject)(value)) {
Object.keys(value).forEach(function (k) {
return applyRules(path.push(k), value[k]);
});
}
}
if (!_immutable.Map.isMap(value)) {
return;
}
value.flip().toList().map(function (k) {
return applyRules(path.push(k), value.get(k));
});
};
applyRules(new _immutable.List(path), result);
this.state = result;
applyRules(new _immutable.List(path), value);
this.state = state;
return this;

@@ -131,3 +132,3 @@ }

v = tmp;
if (this.rules.get(p.join("."))) {
if (this.getRule(p)) {
path = p;

@@ -158,2 +159,22 @@ value = v;

}
}, {
key: "getRule",
value: function getRule(path) {
return this.slots.getRule(path);
}
}, {
key: "getSetRule",
value: function getSetRule(path) {
return this.slots.getSetRule(path);
}
}, {
key: "getDeps",
value: function getDeps(path) {
return this.slots.getDeps(path);
}
}], [{
key: "mergeValue",
value: function mergeValue(state, path, value) {
return (0, _utils.isObject)(value) ? state.mergeDeepIn(path, value) : state.setIn(path, value);
}
}]);

@@ -160,0 +181,0 @@

@@ -64,3 +64,3 @@ "use strict";

var prevState = this.state;
var branch = new _branch2["default"](this.rules, this.state, this);
var branch = new _branch2["default"](this.state, this.slots, this);
this.branches.push(branch);

@@ -67,0 +67,0 @@ this.slots._fire("beforeSet", prevState, this);

@@ -36,4 +36,6 @@ "use strict";

this.rules = Slots.validateRules(Slots.normalizeRules((0, _immutable.fromJS)(rules)));
this.rules = Object.keys(rules).reduce(function (res, key) {
res[key] = Slots.normalizeRule(rules[key]);
return res;
}, {});
this.state = (0, _immutable.fromJS)(state);

@@ -71,11 +73,3 @@ this.contexts = [];

this._fire("willCommit", ctx.state);
if (!ctx.promises.length) {
log("NO PROMISES LEFT FOR CONTEXT %s", (0, _utils.insp)(ctx.path));
this.contexts.splice(this.contexts.indexOf(ctx), 1);
}
log("COMMIT %s", (0, _utils.insp)(ctx.state));
if ((0, _immutable.is)(this.state, ctx.state)) {
log("NO STATE CHANGES IN CONTEXT %s", (0, _utils.insp)(ctx.path));
return this;
}
this.state = ctx.state;

@@ -177,13 +171,20 @@ this._fire("didCommit", prevState);

}, {
key: "getRules",
value: function getRules() {
return this.rules.toJS();
}
}, {
key: "getRule",
value: function getRule(path) {
path = Slots.makePath(path);
return this.rules[path.join(".")];
return this.rules[Slots.makeDottedPath(path)];
}
}, {
key: "getSetRule",
value: function getSetRule(path) {
var rule = this.getRule(path);
return rule && rule.set;
}
}, {
key: "getDeps",
value: function getDeps(path) {
var rule = this.getRule(path);
return rule && rule.deps;
}
}, {
key: "isEqual",

@@ -194,12 +195,19 @@ value: function isEqual(state) {

}], [{
key: "normalizeRules",
value: function normalizeRules(rule) {
key: "normalizeRule",
value: function normalizeRule(rule) {
if ((0, _utils.isFunction)(rule)) {
var fn = rule;
rule = {
"set": fn
};
}
if (!rule.deps) {
rule.deps = [];
}
if (!(0, _utils.isArray)(rule.deps)) {
throw new Error("Invalid rule");
}
return rule;
}
}, {
key: "validateRules",
value: function validateRules(rules) {
return rules;
}
}, {
key: "makePath",

@@ -210,2 +218,5 @@ value: function makePath(path) {

}
if (path.toArray) {
path = path.toArray();
}
return (0, _utils.isArray)(path) && path || (0, _utils.isString)(path) && path.split(".") || (function () {

@@ -215,2 +226,7 @@ throw new Error("path should be an array or dot-separated string or null,\n " + Object.prototype.toString.call(path) + " given");

}
}, {
key: "makeDottedPath",
value: function makeDottedPath(path) {
return Slots.makePath(path).join(".");
}
}]);

@@ -217,0 +233,0 @@

@@ -7,2 +7,3 @@ "use strict";

exports.isFunction = isFunction;
exports.isObject = isObject;
exports.isPromise = isPromise;

@@ -25,4 +26,8 @@ exports.isImmutable = isImmutable;

function isObject(v) {
return v !== null && typeof v === "object";
}
function isPromise(v) {
return isFunction(v.then);
return v && isFunction(v.then);
}

@@ -29,0 +34,0 @@

{
"name": "slt",
"version": "1.2.0",
"version": "1.3.0",
"description": "Take care of your state",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -32,3 +32,3 @@ ## DISCLAIMER

## Philosophy
In each web app we can distinguish 2 types of state data: first is the source (request, session) data and second is derivative (response, artefacts) data. Derivative data (response and additional artefacts such as errors/widgets/recommendations/comments to post ect.) dependends on request (HTTP or another type of request). The idea is to hold request data in the state and apply rule that will fetch data (derivative) for that request.
In each web app we can distinguish 2 types of state data: first is the source (request, session) data and second is derivative (response, artefacts) data. Derivative data (response and additional artefacts such as errors/widgets/recommendations/comments to post ect.) dependends on request (HTTP or another type of request). The idea is to hold request data in the state and apply rule that will fetch data (derivative) for that request. **This approach makes you reach fully consistent app state: it holds all data you need and all the reasons why it should hold it at any point of time.**

@@ -35,0 +35,0 @@ A great analogy is an `<img>` or `<script>` in HTML. Browser loads specified img just because of existence of this tag in the DOM (request state). The logic here is: we have `<img>` tag inserted in DOM node and it has its `src` attribute then fetch actual image from server and put it inside this node. `Slots` follows the same idea, as you can see in the following example.

@@ -45,2 +45,3 @@ import rules from "./data/cascadeRules";

});
// deal with conflicts
});

@@ -7,3 +7,7 @@ export default {

return this.set("users", url);
},
"users": function () {
}
}

@@ -6,7 +6,6 @@ import Slots from "../slots";

it ('should set Rules and State', () => {
let set = function (route) {
};
let rules = {
"route": (route) => {
}
"route": set
};

@@ -22,4 +21,8 @@

slots = new Slots(rules, state);
expect(slots.getRules()).toEqual(rules);
expect(slots.getRules().route).toEqual(jasmine.any(Function));
expect(slots.rules).toEqual( {
"route": {
set: set,
deps: []
}
});

@@ -26,0 +29,0 @@ expect(slots.getState().toJS()).toEqual(state);

import { fromJS, is, Map, List} from "immutable";
import debug from "debug";
import { toJS, isFunction, isPromise, insp } from "./utils";
import { toJS, isFunction, isPromise, insp, isObject } from "./utils";
import Slots from "./slots";

@@ -9,4 +9,4 @@ const d = debug("slt");

class Branch {
constructor(rules, state, ctx, parent = null) {
this.rules = rules;
constructor(state, slots, ctx, parent = null) {
this.slots = slots;
this.state = state;

@@ -25,3 +25,3 @@ this.ctx = ctx;

newBranch(state) {
let branch = new Branch(this.rules, state, this.ctx, this);
let branch = new Branch(state, this.slots, this.ctx, this);
this.children.push(branch);

@@ -40,13 +40,17 @@ return branch;

log("SET %s TO %s", insp(path), insp(value));
let imValue = fromJS(value);
let result = imValue.toJS ? this.state.mergeDeepIn(path, imValue)
: this.state.setIn(path, imValue);
d("MERGED \n%s", insp(result));
const applyRules = (path = new List(), value = new Map()) => {
let rule = this.rules.get(path.toArray().join("."));
if (isFunction(rule)) {
let val = result.getIn(path);
log("RULE on path %s matched with value %s", insp(path), insp(val));
let branch = rule.call(this.newBranch(result), toJS(val));
if (isPromise(branch)) {
let state = this.state;
d("MERGED \n%s", insp(state));
state = Branch.mergeValue(state, path, value); // TODO: deal with conflicts
const applyRules = (path = new List(), value = {}) => {
let rule = this.getSetRule(path);
if (rule) {
log("RULE on path %s matched with value %s", insp(path), insp(value));
let branch = this.newBranch(state);
rule.call(branch, value);
state = branch.state;
if (!isPromise(branch)) {
d("NEW BRANCH with state %s", insp(state));
state = branch.state;
d("RESULT is %s", insp(state));
} else {
log("PROMISE RETURNED");

@@ -60,15 +64,12 @@ branch.bind(this.ctx); // out of call stack

});
} else {
d("NEW BRANCH with state %s", insp(result));
result = branch.getState();
d("RESULT is %s", insp(result));
}
}
if (!Map.isMap(value)) {
return;
else {
if (isObject(value)) {
Object.keys(value).forEach(k => applyRules(path.push(k), value[k]));
}
}
value.flip().toList().map((k) => applyRules(path.push(k), value.get(k)));
};
applyRules(new List(path), result);
this.state = result;
applyRules(new List(path), value);
this.state = state;
return this;

@@ -85,3 +86,3 @@ }

v = tmp;
if (this.rules.get(p.join("."))) {
if (this.getRule(p)) {
path = p;

@@ -107,4 +108,21 @@ value = v;

}
getRule(path) {
return this.slots.getRule(path);
}
getSetRule(path) {
return this.slots.getSetRule(path);
}
getDeps(path) {
return this.slots.getDeps(path);
}
static mergeValue(state, path, value) {
return (isObject(value)) ?
state.mergeDeepIn(path, value) : state.setIn(path, value);
}
}
export default Branch;

@@ -32,3 +32,3 @@ import { fromJS, is, Map, List} from "immutable";

let prevState = this.state;
let branch = new Branch(this.rules, this.state, this);
let branch = new Branch(this.state, this.slots, this);
this.branches.push(branch);

@@ -35,0 +35,0 @@ this.slots._fire("beforeSet", prevState, this);

@@ -10,7 +10,6 @@ import { fromJS, is, Map, List} from "immutable";

constructor(rules = {}, state = {}, aliases = {}) {
this.rules =
Slots.validateRules(
Slots.normalizeRules(
fromJS(rules)));
this.rules = Object.keys(rules).reduce((res, key) => {
res[key] = Slots.normalizeRule(rules[key]);
return res;
}, {});
this.state = fromJS(state);

@@ -40,11 +39,3 @@ this.contexts = [];

this._fire("willCommit", ctx.state);
if (!ctx.promises.length) {
log("NO PROMISES LEFT FOR CONTEXT %s", insp(ctx.path));
this.contexts.splice(this.contexts.indexOf(ctx), 1);
}
log("COMMIT %s", insp(ctx.state));
if (is(this.state, ctx.state)) {
log("NO STATE CHANGES IN CONTEXT %s", insp(ctx.path));
return this;
}
this.state = ctx.state;

@@ -121,11 +112,18 @@ this._fire("didCommit", prevState);

getRules() {
return this.rules.toJS();
}
getRule(path) {
path = Slots.makePath(path);
return this.rules[path.join(".")];
return this.rules[Slots.makeDottedPath(path)];
}
getSetRule(path) {
let rule = this.getRule(path);
return rule && rule.set;
}
getDeps(path) {
let rule = this.getRule(path);
return rule && rule.deps;
}
isEqual(state) {

@@ -135,10 +133,18 @@ return is(fromJS(state), this.state);

static normalizeRules(rule) {
static normalizeRule(rule) {
if (isFunction(rule)) {
let fn = rule;
rule = {
"set": fn
};
}
if (!rule.deps) {
rule.deps = [];
}
if (!isArray(rule.deps)) {
throw new Error("Invalid rule");
}
return rule;
}
static validateRules (rules) {
return rules;
}
static makePath(path) {

@@ -148,2 +154,5 @@ if (path === null) {

}
if (path.toArray) {
path = path.toArray();
}
return isArray(path) && path || isString(path) && path.split('.') ||

@@ -155,4 +164,8 @@ (() => { throw new Error (

static makeDottedPath(path) {
return Slots.makePath(path).join(".");
}
}
export default Slots;

@@ -7,4 +7,8 @@ import util from "util";

export function isObject(v) {
return v !== null && typeof v === 'object';
}
export function isPromise(v) {
return isFunction(v.then);
return v && isFunction(v.then);
}

@@ -11,0 +15,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc