-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
73 lines (65 loc) · 1.64 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
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
/**
* The dispatcher is the central hub
* that manages all data flow in a Flux application.
*/
class Dispatcher {
constructor() {
this.effects = [];
}
/**
* Register storeEffect inside
* @param storeEffect
*/
register(storeEffect) {
this.effects.push(storeEffect);
}
/**
* Dispatch a new action to all registered stores
* @param action
*/
dispatch(action) {
this.effects.forEach(effect => {
effect(action);
});
}
}
class Store {
constructor({ initialData }) {
this.data = {};
this.subscribers = new Map();
this.amountSubscriptions = 0;
this.data = initialData;
}
_createSubscriptionId() {
this.amountSubscriptions++;
return `sub_${this.amountSubscriptions}`;
}
_notifySubscribers() {
this.subscribers.forEach((subscriberCallback) => {
subscriberCallback();
});
}
getData() {
return this.data;
}
updateData(updatedFieldsData) {
this.data = Object.assign({}, this.data, updatedFieldsData);
this._notifySubscribers();
return this.data;
}
subscribe(callback) {
const subId = this._createSubscriptionId();
this.subscribers.set(subId, callback);
return subId;
}
unsubscribe(id) {
return this.subscribers.delete(id);
}
}
var types = /*#__PURE__*/Object.freeze({
});
exports.Dispatcher = Dispatcher;
exports.Store = Store;
exports.Types = types;