-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathag-model.js
184 lines (159 loc) · 4.83 KB
/
ag-model.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import AGField from './ag-field.js';
const AsyncStreamEmitter = AGField.AsyncStreamEmitter;
// options.socket: The SocketCluster client socket to use to sync the model state.
// options.type: The resource type.
// options.id: The resource id.
// options.fields: An array of fields names required by this model.
function AGModel(options) {
AsyncStreamEmitter.call(this);
this.socket = options.socket;
this.type = options.type;
this.id = options.id;
this.fields = options.fields || [];
this.fieldTransformations = options.fieldTransformations || {};
this.defaultFieldValues = options.defaultFieldValues;
this.enableRebound = options.enableRebound || false;
this.agFields = {};
this.value = {
...this.defaultFieldValues,
id: this.id
};
this.isActive = true;
this.passiveMode = options.passiveMode || false;
this._symbol = Symbol();
if (!this.socket.agFields) {
this.socket.agFields = {};
}
if (!this.socket.fieldWatchers) {
this.socket.fieldWatchers = {};
}
if (this.enableRebound) {
if (!this.socket.lastPublisherId) {
this.socket.lastPublisherId = 1;
}
this.publisherId = String(this.socket.lastPublisherId++);
} else {
this.publisherId = null;
}
this.fields.forEach(field => this.addField(field));
this.isLoaded = Object.values(this.agFields).every(field => field.isLoaded);
}
AGModel.prototype = Object.create(AsyncStreamEmitter.prototype);
AGModel.AsyncStreamEmitter = AsyncStreamEmitter;
AGModel.prototype.getResourceId = function (field) {
return `${this.type}/${this.id}/${field}`;
};
AGModel.prototype.addField = function (field) {
if (!this.isActive || this.agFields[field]) return;
let resourceId = this.getResourceId(field);
let agField;
if (this.socket.agFields[resourceId] && this.socket.agFields[resourceId].isActive) {
agField = this.socket.agFields[resourceId];
} else {
let transformations = this.fieldTransformations[field] || {};
agField = new AGField({
socket: this.socket,
resourceType: this.type,
resourceId: this.id,
name: field,
transformations,
passiveMode: this.passiveMode,
publisherId: this.publisherId
});
this.socket.agFields[resourceId] = agField;
}
if (!this.socket.fieldWatchers[resourceId]) {
this.socket.fieldWatchers[resourceId] = {};
}
this.socket.fieldWatchers[resourceId][this._symbol] = true;
this.agFields[field] = agField;
this.value[field] = agField.value == null ? null : agField.value;
(async () => {
for await (let event of agField.listener('error')) {
this.emit('error', event);
}
})();
(async () => {
for await (let event of agField.listener('change')) {
this.value[event.field] = event.newValue;
this.emit('change', {
resourceType: this.type,
resourceId: this.id,
resourceField: event.field,
oldValue: event.oldValue,
newValue: event.newValue,
isRemote: event.isRemote
});
}
})();
(async () => {
for await (let event of agField.listener('load')) {
if (!this.isLoaded) {
this.isLoaded = Object.values(this.agFields).every(field => field.isLoaded);
if (this.isLoaded) {
this.emit('load', {});
}
}
}
})();
return agField;
};
AGModel.prototype.save = async function () {
let promises = [];
Object.values(this.agFields).forEach((agField) => {
agField.value = this.value[agField.name];
promises.push(agField.save());
});
return Promise.all(promises);
};
AGModel.prototype.update = async function (field, newValue) {
if (this.agFields[field]) {
return this.agFields[field].update(newValue);
}
let query = {
action: 'update',
type: this.type,
id: this.id,
field: field,
value: newValue
};
if (this.publisherId) {
query.publisherId = this.publisherId;
}
return this.socket.invoke('crud', query);
};
AGModel.prototype.delete = async function (field) {
let query = {
action: 'delete',
type: this.type,
id: this.id
};
if (this.publisherId) {
query.publisherId = this.publisherId;
}
if (field != null) {
if (this.agFields[field]) {
return this.agFields[field].delete();
}
query.field = field;
}
return this.socket.invoke('crud', query);
};
AGModel.prototype.destroy = async function () {
this.killAllListeners();
this.isActive = false;
await new Promise(resolve => setTimeout(resolve, 0));
Object.values(this.agFields).forEach((agField) => {
let resourceId = this.getResourceId(agField.name);
let watchers = this.socket.fieldWatchers[resourceId];
if (watchers) {
delete watchers[this._symbol];
}
if (!Object.getOwnPropertySymbols(watchers || {}).length) {
delete this.socket.fieldWatchers[resourceId];
delete this.socket.agFields[resourceId];
agField.destroy();
}
});
};
export default AGModel;