This repository has been archived by the owner on Aug 31, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_index.js
133 lines (120 loc) · 3.22 KB
/
_index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { path, set, view, lensPath, equals } from 'ramda';
export default class Store {
constructor(data = {}) {
this.listeners = {};
this.data = this.mergeStores(data);
}
mergeListemers(store) {
for (const path of store.listeners) {
const fn = store.listeners[path];
this.subscribe(path, fn);
}
store.listeners = {};
}
mergeStores(data) {
for (const key in data) {
const value = data[key];
if (value instanceof Store) {
this.mergeListeners(value);
data[key] = value.get();
} else if (data[key].constructor.name === 'Object') {
this.mergeStores(data[key]);
} else if (Array.isArray(data[key])) {
for (let item of data[key]) {
this.mergeStores(item);
}
}
}
return data;
}
destroy() {
this.data = undefined;
this.listeners = {};
}
subscribeAll(userPaths, fn) {
let unsubscribers = [];
for (const userPath of userPaths) {
const wrappedSubscriber = (newValue, oldValue) => {
fn(userPath, newValue, oldValue);
};
unsubscribers.push(this.subscribe(userPath, wrappedSubscriber));
}
return () => {
for (const unsubscribe of unsubscribers) {
unsubscribe();
}
unsubscribers = [];
};
}
watchAll(userPaths, fn) {
return this.subscribeAll(userPaths, fn);
}
subscribe(userPath, fn) {
if (typeof userPath === 'function') {
fn = userPath;
userPath = '';
}
if (!Array.isArray(this.listeners[userPath])) {
this.listeners[userPath] = [];
}
this.listeners[userPath].push(fn);
let pathSplit = userPath.split('.');
if (userPath === '') {
pathSplit = [];
}
fn(path(pathSplit, this.data));
return this.unsubscribe(fn);
}
watch(userPath, fn) {
return this.subscribe(userPath, fn);
}
unsubscribe(fn) {
return () => {
for (const currentPath in this.listeners) {
this.listeners[currentPath] = this.listeners[currentPath].filter((current) => current !== fn);
if (this.listeners[currentPath].length === 0) {
delete this.listeners[currentPath];
}
}
};
}
unwatch(fn) {
return this.unsubscribe(fn);
}
update(userPath, fn) {
if (typeof userPath === 'function') {
fn = userPath;
userPath = '';
}
let pathSplit = userPath.split('.');
if (userPath === '') {
pathSplit = [];
}
const lens = lensPath(pathSplit);
let newValue = fn(view(lens, this.data));
this.data = set(lens, newValue, this.data);
for (const currentPath in this.listeners) {
if (
userPath.substr(0, currentPath.length) === currentPath ||
currentPath.substr(0, userPath.length) === userPath
) {
let currentPathSplit = currentPath.split('.');
if (currentPath === '') {
currentPathSplit = [];
}
for (const listener of this.listeners[currentPath]) {
listener(path(currentPathSplit, this.data));
}
}
}
}
set(userPath, fn) {
return this.update(userPath, fn);
}
get(userPath) {
if (typeof userPath === 'undefined' || userPath === '') {
return this.data;
}
return path(userPath.split('.'), this.data);
}
}