Skip to content

Commit 58eaeeb

Browse files
committed
Added examples
1 parent 419a3ea commit 58eaeeb

File tree

5 files changed

+232
-16
lines changed

5 files changed

+232
-16
lines changed

.eslintrc

+165
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
{
2+
"env": {
3+
"node": true,
4+
"es6": true
5+
},
6+
"rules": {
7+
"no-alert": 2,
8+
"no-array-constructor": 2,
9+
"no-bitwise": 2,
10+
"no-caller": 2,
11+
"no-catch-shadow": 2,
12+
"no-cond-assign": 2,
13+
"no-console": 1,
14+
"no-constant-condition": 2,
15+
"no-continue": 2,
16+
"no-control-regex": 2,
17+
"no-debugger": 1,
18+
"no-delete-var": 2,
19+
"no-div-regex": 2,
20+
"no-dupe-keys": 2,
21+
"no-dupe-args": 2,
22+
"no-duplicate-case": 2,
23+
"no-else-return": 0,
24+
"no-empty": 2,
25+
"no-empty-character-class": 2,
26+
"no-eq-null": 2,
27+
"no-eval": 2,
28+
"no-ex-assign": 2,
29+
"no-extend-native": 2,
30+
"no-extra-bind": 2,
31+
"no-extra-boolean-cast": 2,
32+
"no-extra-parens": 0,
33+
"no-extra-semi": 2,
34+
"no-fallthrough": 2,
35+
"no-floating-decimal": 2,
36+
"no-func-assign": 2,
37+
"no-implied-eval": 2,
38+
"no-inline-comments": 2,
39+
"no-inner-declarations": [2, "functions"],
40+
"no-invalid-regexp": 2,
41+
"no-irregular-whitespace": 2,
42+
"no-iterator": 2,
43+
"no-label-var": 2,
44+
"no-labels": 2,
45+
"no-lone-blocks": 2,
46+
"no-lonely-if": 2,
47+
"no-loop-func": 2,
48+
"no-mixed-requires": 0,
49+
"no-mixed-spaces-and-tabs": [2, false],
50+
"linebreak-style": [0, "unix"],
51+
"no-multi-spaces": 2,
52+
"no-multi-str": 2,
53+
"no-multiple-empty-lines": [2, {"max": 2}],
54+
"no-native-reassign": 2,
55+
"no-negated-in-lhs": 2,
56+
"no-nested-ternary": 2,
57+
"no-new": 2,
58+
"no-new-func": 2,
59+
"no-new-object": 2,
60+
"no-new-require": 2,
61+
"no-new-wrappers": 2,
62+
"no-obj-calls": 2,
63+
"no-octal": 2,
64+
"no-octal-escape": 2,
65+
"no-param-reassign": 0,
66+
"no-path-concat": 2,
67+
"no-plusplus": 2,
68+
"no-process-env": 2,
69+
"no-process-exit": 2,
70+
"no-proto": 2,
71+
"no-redeclare": 2,
72+
"no-regex-spaces": 2,
73+
"no-restricted-modules": 2,
74+
"no-return-assign": 2,
75+
"no-script-url": 2,
76+
"no-self-compare": 2,
77+
"no-sequences": 2,
78+
"no-shadow": 2,
79+
"no-shadow-restricted-names": 2,
80+
"no-spaced-func": 2,
81+
"no-sparse-arrays": 2,
82+
"no-sync": 0,
83+
"no-ternary": 0,
84+
"no-trailing-spaces": 2,
85+
"no-this-before-super": 2,
86+
"no-throw-literal": 0,
87+
"no-undef": 2,
88+
"no-undef-init": 2,
89+
"no-undefined": 0,
90+
"no-unexpected-multiline": 2,
91+
"no-underscore-dangle": 0,
92+
"no-unneeded-ternary": 2,
93+
"no-unreachable": 2,
94+
"no-unused-expressions": [2, { allowTernary: true }],
95+
"no-unused-vars": [2, {"vars": "all", "args": "after-used"}],
96+
"no-use-before-define": 0,
97+
"no-void": 2,
98+
"no-var": 0,
99+
"prefer-const": 2,
100+
"no-warning-comments": [1, { "terms": ["todo", "fixme", "xxx"], "location": "start" }],
101+
"no-with": 2,
102+
"array-bracket-spacing": [2, "never"],
103+
"accessor-pairs": 2,
104+
"block-scoped-var": 2,
105+
"brace-style": [2, "1tbs"],
106+
"camelcase": 0,
107+
"comma-dangle": [2, "never"],
108+
"comma-spacing": 2,
109+
"comma-style": 2,
110+
"complexity": [2, 11],
111+
"computed-property-spacing": [2, "never"],
112+
"consistent-return": 0,
113+
"consistent-this": [2, "that"],
114+
"constructor-super": 2,
115+
"curly": [2, "all"],
116+
"default-case": 2,
117+
"dot-location": 2,
118+
"dot-notation": [2, { "allowKeywords": true }],
119+
"eol-last": 2,
120+
"eqeqeq": 2,
121+
"func-names": 0,
122+
"func-style": [2, "expression"],
123+
"generator-star-spacing": 2,
124+
"guard-for-in": 2,
125+
"handle-callback-err": 2,
126+
"indent": [2, 2],
127+
"keyword-spacing": [2],
128+
"lines-around-comment": 2,
129+
"max-depth": [2, 4],
130+
"max-len": [2, 200, 2],
131+
"max-nested-callbacks": [2, 2],
132+
"max-params": [2, 6],
133+
"max-statements": [2, 50],
134+
"new-cap": 2,
135+
"new-parens": 2,
136+
"newline-after-var": 0,
137+
"object-curly-spacing": [2, "always"],
138+
"object-shorthand": 0,
139+
"one-var": 2,
140+
"operator-assignment": [2, "always"],
141+
"operator-linebreak": 2,
142+
"padded-blocks": 0,
143+
"quote-props": [2, "as-needed"],
144+
"quotes": [2, "single"],
145+
"radix": 2,
146+
"semi": 2,
147+
"semi-spacing": [2, {"before": false, "after": true}],
148+
"sort-vars": 0,
149+
"space-before-blocks": [2, "always"],
150+
"space-before-function-paren": [2, "always"],
151+
"space-in-brackets": 0,
152+
"space-in-parens": [2, "never"],
153+
"space-infix-ops": 2,
154+
"space-unary-ops": [2, { "words": true, "nonwords": false }],
155+
"spaced-comment": 2,
156+
"strict": 0,
157+
"use-isnan": 2,
158+
"valid-jsdoc": 0,
159+
"valid-typeof": 2,
160+
"vars-on-top": 2,
161+
"wrap-iife": 2,
162+
"wrap-regex": 2,
163+
"yoda": [2, "never"]
164+
}
165+
}

examples/get-set.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/* eslint no-console: 0 */
2+
3+
console.log('example: get-set');
4+
// require atom
5+
const atom = require('../src/index.js');
6+
// set email in atom
7+
console.log('set email in atom');
8+
atom.set('user.email', '[email protected]');
9+
// get and console email
10+
console.log('get email from atom', atom.get('user.email'));
11+
console.log('\n');

examples/observer.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/* eslint no-console: 0 */
2+
3+
console.log('example: observer');
4+
5+
// require atom
6+
const atom = require('../src/index.js'),
7+
8+
// create a context
9+
myContext = {
10+
onUserEmailChange: function () {
11+
// executed after atom path set, get and console email
12+
console.log('atom.user.email has change:', atom.get('user.email'));
13+
console.log('\n');
14+
}
15+
};
16+
17+
// observe atom path
18+
console.log('observe atom email path');
19+
atom.on(myContext, {
20+
paths: 'user.email',
21+
run: 'onUserEmailChange'
22+
});
23+
24+
// set atom path
25+
console.log('set email in atom');
26+
atom.set('user.email', '[email protected]');

package.json

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
{
22
"name": "atom",
3-
"description": "Reactive Data Model",
3+
"description": "Reactive Data Model (es6)",
44
"version": "0.0.1",
55
"main": "src/index.js",
66
"authors": [
7-
"Miguel del Mazo <[email protected]>",
7+
"Miguel del Mazo <[email protected]>"
88
],
99
"license": "UNLICENSED",
10+
"scripts": {
11+
"examples": "node examples/get-set.js && node examples/observer.js",
12+
"lint": "./node_modules/.bin/eslint ./**/*.js"
13+
},
1014
"dependencies": {
1115
"lodash": "^4.17.4"
16+
},
17+
"devDependencies": {
18+
"eslint": "^4.3.0"
1219
}
1320
}

src/index.js

+21-14
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ const _ = require('lodash'),
1717
},
1818

1919
parseDefinitionPaths = (definition) => {
20-
definition.paths = _.isArray(definition.paths) ? _.flattenDeep(definition.paths) : [definition.paths];
20+
definition.paths = _.isArray(definition.paths)
21+
? _.flattenDeep(definition.paths)
22+
: [definition.paths];
2123
},
2224

2325
parseDefinitionCallbacks = (definition) => {
24-
definition.run = _.parseArray(definition.run);
26+
definition.run = parseArray(definition.run);
2527
},
2628

2729
isValidDefinition = (definition) => {
@@ -33,7 +35,7 @@ const _ = require('lodash'),
3335
},
3436

3537
isValidDefinitionCallbacks = (run) => {
36-
return _.every(run, (runCallback) => _.isString(run) || _.isArray(run) || _.isFunction(run));
38+
return _.every(run, (runCallback) => _.isString(runCallback) || _.isArray(runCallback) || _.isFunction(runCallback));
3739
},
3840

3941
// paths
@@ -47,9 +49,7 @@ const _ = require('lodash'),
4749
triggerChanges = () => {
4850
const atomPaths = _.uniq(changedPaths);
4951
changedPaths = [];
50-
console.group('atom.triggerChanges', { atom }, { atomPaths });
5152
triggerChangesInContexts(atomPaths);
52-
console.groupEnd('atom.triggerChanges');
5353
},
5454

5555
triggerChangesInContexts = (atomPaths) => {
@@ -89,7 +89,11 @@ const _ = require('lodash'),
8989

9090
triggerChangesCallback = (context, definition) => {
9191
_.each(definition.run, (callback) => {
92-
_.run(context, callback, definition.options);
92+
if (_.isFunction(callback)) {
93+
callback.call(context);
94+
} else if (_.isString(callback) && _.isFunction(context[callback])) {
95+
context[callback].call(context);
96+
}
9397
});
9498
},
9599

@@ -107,9 +111,12 @@ const _ = require('lodash'),
107111
if (_.isString(path) && !_.isEqual(get(path), value)) {
108112
_.set(atom, path, value);
109113
triggerChangesDebounced(path);
110-
console.log('atom.set', { atom }, { path, value, atom });
111114
}
112-
};
115+
},
116+
117+
// helpers
118+
119+
parseArray = (arr) => _.isArray(arr) ? arr : [arr];
113120

114121
let changedPaths = [],
115122
onChangeTimer;
@@ -120,13 +127,13 @@ module.exports = {
120127

121128
on (context, definitions) {
122129
const contextIdx = getContextIdx(context);
123-
_.each(_.parseArray(definitions), (definition) => {
130+
_.each(parseArray(definitions), (definition) => {
124131
parseDefinitionPaths(definition);
125132
parseDefinitionCallbacks(definition);
126133
if (isValidDefinition(definition)) {
127134
contexts[contextIdx]._observers.push(definition);
128135
} else {
129-
console.warning('atom.on > invalid definitions', definitions);
136+
throw new Error('atom.on > invalid definitions', definitions);
130137
}
131138
});
132139
},
@@ -194,7 +201,7 @@ module.exports = {
194201
// setters
195202

196203
concat (path, value = []) {
197-
set(path, getClone(path, []).concat(_.parseArray(value)));
204+
set(path, getClone(path, []).concat(parseArray(value)));
198205
},
199206

200207
pop (path, defaultValue) {
@@ -210,14 +217,14 @@ module.exports = {
210217
},
211218

212219
remove (path, predicate) {
213-
let collection = getClone(path);
214-
const result = _.remove(collection, predicate);
220+
const collection = getClone(path),
221+
result = _.remove(collection, predicate);
215222
set(path, collection);
216223
return result;
217224
},
218225

219226
reset (path, value = []) {
220-
set(path, _.parseArray(value));
227+
set(path, parseArray(value));
221228
},
222229

223230
set,

0 commit comments

Comments
 (0)