ar_sync 1.0.4 → 1.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/core/ActionCableAdapter.js +19 -17
- data/core/ArSyncApi.js +112 -48
- data/core/ArSyncModel.js +69 -60
- data/core/ArSyncStore.js +401 -314
- data/core/ConnectionManager.js +43 -38
- data/core/hooks.d.ts +4 -0
- data/core/hooks.js +61 -36
- data/lib/ar_sync/type_script.rb +9 -8
- data/lib/ar_sync/version.rb +1 -1
- data/src/core/DataType.ts +1 -1
- data/src/core/hooks.ts +29 -5
- data/tsconfig.json +2 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5ce8f54d9f17ebdd9341d3ba5e1087bde5875d5a4f88b5eeddded0602aece480
|
4
|
+
data.tar.gz: 209d8c04edf0d06b42c853eea568e298ccd626438fb64b416a8cc8e82293f0b8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bb0b79346ca7062f69f77254ac4463aba3261ae8419937c64b0a0cb17715b5e97dc4035c76c863bdc87739fb1070ee21c27b034d908839309b83f26462038663
|
7
|
+
data.tar.gz: 2b633cdcff19283fd2626e41751de5e969888e905452f2a86621c39be9eed6a6c467e5abe98d194b120aa7096e370f95a5fdb80439829a47cf60047f9ed0d608
|
data/Gemfile.lock
CHANGED
data/core/ActionCableAdapter.js
CHANGED
@@ -1,29 +1,31 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
|
4
|
-
|
3
|
+
var ActionCableAdapter = /** @class */ (function () {
|
4
|
+
function ActionCableAdapter(actionCableClass) {
|
5
5
|
this.connected = true;
|
6
6
|
this.actionCableClass = actionCableClass;
|
7
|
-
this.subscribe(Math.random().toString(), ()
|
7
|
+
this.subscribe(Math.random().toString(), function () { });
|
8
8
|
}
|
9
|
-
subscribe(key, received) {
|
10
|
-
|
11
|
-
|
9
|
+
ActionCableAdapter.prototype.subscribe = function (key, received) {
|
10
|
+
var _this = this;
|
11
|
+
var disconnected = function () {
|
12
|
+
if (!_this.connected)
|
12
13
|
return;
|
13
|
-
|
14
|
-
|
14
|
+
_this.connected = false;
|
15
|
+
_this.ondisconnect();
|
15
16
|
};
|
16
|
-
|
17
|
-
if (
|
17
|
+
var connected = function () {
|
18
|
+
if (_this.connected)
|
18
19
|
return;
|
19
|
-
|
20
|
-
|
20
|
+
_this.connected = true;
|
21
|
+
_this.onreconnect();
|
21
22
|
};
|
22
23
|
if (!this._cable)
|
23
24
|
this._cable = this.actionCableClass.createConsumer();
|
24
|
-
return this._cable.subscriptions.create({ channel: 'SyncChannel', key }, { received, disconnected, connected });
|
25
|
-
}
|
26
|
-
ondisconnect() { }
|
27
|
-
onreconnect() { }
|
28
|
-
|
25
|
+
return this._cable.subscriptions.create({ channel: 'SyncChannel', key: key }, { received: received, disconnected: disconnected, connected: connected });
|
26
|
+
};
|
27
|
+
ActionCableAdapter.prototype.ondisconnect = function () { };
|
28
|
+
ActionCableAdapter.prototype.onreconnect = function () { };
|
29
|
+
return ActionCableAdapter;
|
30
|
+
}());
|
29
31
|
exports.default = ActionCableAdapter;
|
data/core/ArSyncApi.js
CHANGED
@@ -1,79 +1,143 @@
|
|
1
1
|
"use strict";
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
3
|
+
__assign = Object.assign || function(t) {
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
5
|
+
s = arguments[i];
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
7
|
+
t[p] = s[p];
|
8
|
+
}
|
9
|
+
return t;
|
7
10
|
};
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
11
|
+
return __assign.apply(this, arguments);
|
12
|
+
};
|
13
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
14
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
15
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
16
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
17
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
18
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
19
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
20
|
+
});
|
21
|
+
};
|
22
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
23
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
24
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
25
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
26
|
+
function step(op) {
|
27
|
+
if (f) throw new TypeError("Generator is already executing.");
|
28
|
+
while (_) try {
|
29
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
30
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
31
|
+
switch (op[0]) {
|
32
|
+
case 0: case 1: t = op; break;
|
33
|
+
case 4: _.label++; return { value: op[1], done: false };
|
34
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
35
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
36
|
+
default:
|
37
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
38
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
39
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
40
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
41
|
+
if (t[2]) _.ops.pop();
|
42
|
+
_.trys.pop(); continue;
|
43
|
+
}
|
44
|
+
op = body.call(thisArg, _);
|
45
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
46
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
47
|
+
}
|
48
|
+
};
|
49
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
50
|
+
function apiBatchFetch(endpoint, requests) {
|
51
|
+
return __awaiter(this, void 0, void 0, function () {
|
52
|
+
var headers, body, option, res;
|
53
|
+
return __generator(this, function (_a) {
|
54
|
+
switch (_a.label) {
|
55
|
+
case 0:
|
56
|
+
headers = {
|
57
|
+
'Accept': 'application/json',
|
58
|
+
'Content-Type': 'application/json'
|
59
|
+
};
|
60
|
+
body = JSON.stringify({ requests: requests });
|
61
|
+
option = { credentials: 'include', method: 'POST', headers: headers, body: body };
|
62
|
+
if (ArSyncApi.domain)
|
63
|
+
endpoint = ArSyncApi.domain + endpoint;
|
64
|
+
return [4 /*yield*/, fetch(endpoint, option)];
|
65
|
+
case 1:
|
66
|
+
res = _a.sent();
|
67
|
+
if (res.status === 200)
|
68
|
+
return [2 /*return*/, res.json()];
|
69
|
+
throw new Error(res.statusText);
|
70
|
+
}
|
71
|
+
});
|
72
|
+
});
|
16
73
|
}
|
17
|
-
|
18
|
-
|
74
|
+
var ApiFetcher = /** @class */ (function () {
|
75
|
+
function ApiFetcher(endpoint) {
|
19
76
|
this.batches = [];
|
20
77
|
this.batchFetchTimer = null;
|
21
78
|
this.endpoint = endpoint;
|
22
79
|
}
|
23
|
-
fetch(request) {
|
24
|
-
|
25
|
-
|
26
|
-
|
80
|
+
ApiFetcher.prototype.fetch = function (request) {
|
81
|
+
var _this = this;
|
82
|
+
return new Promise(function (resolve, reject) {
|
83
|
+
_this.batches.push([request, { resolve: resolve, reject: reject }]);
|
84
|
+
if (_this.batchFetchTimer)
|
27
85
|
return;
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
for (
|
34
|
-
|
35
|
-
|
36
|
-
|
86
|
+
_this.batchFetchTimer = setTimeout(function () {
|
87
|
+
_this.batchFetchTimer = null;
|
88
|
+
var compacts = {};
|
89
|
+
var requests = [];
|
90
|
+
var callbacksList = [];
|
91
|
+
for (var _i = 0, _a = _this.batches; _i < _a.length; _i++) {
|
92
|
+
var batch = _a[_i];
|
93
|
+
var request_1 = batch[0];
|
94
|
+
var callback = batch[1];
|
95
|
+
var key = JSON.stringify(request_1);
|
37
96
|
if (compacts[key]) {
|
38
97
|
compacts[key].push(callback);
|
39
98
|
}
|
40
99
|
else {
|
41
|
-
requests.push(
|
100
|
+
requests.push(request_1);
|
42
101
|
callbacksList.push(compacts[key] = [callback]);
|
43
102
|
}
|
44
103
|
}
|
45
|
-
|
46
|
-
ArSyncApi._batchFetch(
|
47
|
-
for (
|
48
|
-
|
49
|
-
|
50
|
-
for (
|
104
|
+
_this.batches = [];
|
105
|
+
ArSyncApi._batchFetch(_this.endpoint, requests).then(function (results) {
|
106
|
+
for (var i in callbacksList) {
|
107
|
+
var result = results[i];
|
108
|
+
var callbacks = callbacksList[i];
|
109
|
+
for (var _i = 0, callbacks_1 = callbacks; _i < callbacks_1.length; _i++) {
|
110
|
+
var callback = callbacks_1[_i];
|
51
111
|
if (result.data) {
|
52
112
|
callback.resolve(result.data);
|
53
113
|
}
|
54
114
|
else {
|
55
|
-
|
56
|
-
callback.reject(
|
115
|
+
var error = result.error || { type: 'Unknown Error' };
|
116
|
+
callback.reject(__assign(__assign({}, error), { retry: false }));
|
57
117
|
}
|
58
118
|
}
|
59
119
|
}
|
60
|
-
}).catch(e
|
61
|
-
|
62
|
-
for (
|
63
|
-
|
120
|
+
}).catch(function (e) {
|
121
|
+
var error = { type: e.name, message: e.message, retry: true };
|
122
|
+
for (var _i = 0, callbacksList_1 = callbacksList; _i < callbacksList_1.length; _i++) {
|
123
|
+
var callbacks = callbacksList_1[_i];
|
124
|
+
for (var _a = 0, callbacks_2 = callbacks; _a < callbacks_2.length; _a++) {
|
125
|
+
var callback = callbacks_2[_a];
|
64
126
|
callback.reject(error);
|
127
|
+
}
|
65
128
|
}
|
66
129
|
});
|
67
130
|
}, 16);
|
68
131
|
});
|
69
|
-
}
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
132
|
+
};
|
133
|
+
return ApiFetcher;
|
134
|
+
}());
|
135
|
+
var staticFetcher = new ApiFetcher('/static_api');
|
136
|
+
var syncFetcher = new ApiFetcher('/sync_api');
|
137
|
+
var ArSyncApi = {
|
74
138
|
domain: null,
|
75
139
|
_batchFetch: apiBatchFetch,
|
76
|
-
fetch: (request)
|
77
|
-
syncFetch: (request)
|
140
|
+
fetch: function (request) { return staticFetcher.fetch(request); },
|
141
|
+
syncFetch: function (request) { return syncFetcher.fetch(request); },
|
78
142
|
};
|
79
143
|
exports.default = ArSyncApi;
|
data/core/ArSyncModel.js
CHANGED
@@ -1,104 +1,107 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
3
|
+
var ArSyncStore_1 = require("./ArSyncStore");
|
4
|
+
var ConnectionManager_1 = require("./ConnectionManager");
|
5
|
+
var ArSyncModel = /** @class */ (function () {
|
6
|
+
function ArSyncModel(request, option) {
|
7
|
+
var _this = this;
|
7
8
|
this._ref = ArSyncModel.retrieveRef(request, option);
|
8
9
|
this._listenerSerial = 0;
|
9
10
|
this._listeners = {};
|
10
11
|
this.complete = false;
|
11
12
|
this.connected = ArSyncStore_1.default.connectionManager.networkStatus;
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
var setData = function () {
|
14
|
+
_this.data = _this._ref.model.data;
|
15
|
+
_this.complete = _this._ref.model.complete;
|
16
|
+
_this.notfound = _this._ref.model.notfound;
|
16
17
|
};
|
17
18
|
setData();
|
18
19
|
this.subscribe('load', setData);
|
19
20
|
this.subscribe('change', setData);
|
20
|
-
this.subscribe('connection', (status)
|
21
|
-
|
21
|
+
this.subscribe('connection', function (status) {
|
22
|
+
_this.connected = status;
|
22
23
|
});
|
23
24
|
}
|
24
|
-
onload(callback) {
|
25
|
+
ArSyncModel.prototype.onload = function (callback) {
|
25
26
|
this.subscribeOnce('load', callback);
|
26
|
-
}
|
27
|
-
subscribeOnce(event, callback) {
|
28
|
-
|
27
|
+
};
|
28
|
+
ArSyncModel.prototype.subscribeOnce = function (event, callback) {
|
29
|
+
var subscription = this.subscribe(event, function (arg) {
|
29
30
|
callback(arg);
|
30
31
|
subscription.unsubscribe();
|
31
32
|
});
|
32
33
|
return subscription;
|
33
|
-
}
|
34
|
-
dig(path) {
|
34
|
+
};
|
35
|
+
ArSyncModel.prototype.dig = function (path) {
|
35
36
|
return ArSyncModel.digData(this.data, path);
|
36
|
-
}
|
37
|
-
|
37
|
+
};
|
38
|
+
ArSyncModel.digData = function (data, path) {
|
38
39
|
function dig(data, path) {
|
39
40
|
if (path.length === 0)
|
40
41
|
return data;
|
41
42
|
if (data == null)
|
42
43
|
return data;
|
43
|
-
|
44
|
-
|
44
|
+
var key = path[0];
|
45
|
+
var other = path.slice(1);
|
45
46
|
if (Array.isArray(data)) {
|
46
|
-
return this.digData(data.find(el
|
47
|
+
return this.digData(data.find(function (el) { return el.id === key; }), other);
|
47
48
|
}
|
48
49
|
else {
|
49
50
|
return this.digData(data[key], other);
|
50
51
|
}
|
51
52
|
}
|
52
53
|
return dig(data, path);
|
53
|
-
}
|
54
|
-
subscribe(event, callback) {
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
54
|
+
};
|
55
|
+
ArSyncModel.prototype.subscribe = function (event, callback) {
|
56
|
+
var _this = this;
|
57
|
+
var id = this._listenerSerial++;
|
58
|
+
var subscription = this._ref.model.subscribe(event, callback);
|
59
|
+
var unsubscribed = false;
|
60
|
+
var unsubscribe = function () {
|
59
61
|
unsubscribed = true;
|
60
62
|
subscription.unsubscribe();
|
61
|
-
delete
|
63
|
+
delete _this._listeners[id];
|
62
64
|
};
|
63
65
|
if (this.complete) {
|
64
66
|
if (event === 'load')
|
65
|
-
setTimeout(()
|
67
|
+
setTimeout(function () {
|
66
68
|
if (!unsubscribed)
|
67
69
|
callback();
|
68
70
|
}, 0);
|
69
71
|
if (event === 'change')
|
70
|
-
setTimeout(()
|
72
|
+
setTimeout(function () {
|
71
73
|
if (!unsubscribed)
|
72
|
-
callback({ path: [], value:
|
74
|
+
callback({ path: [], value: _this.data });
|
73
75
|
}, 0);
|
74
76
|
}
|
75
|
-
return this._listeners[id] = { unsubscribe };
|
76
|
-
}
|
77
|
-
release() {
|
78
|
-
for (
|
77
|
+
return this._listeners[id] = { unsubscribe: unsubscribe };
|
78
|
+
};
|
79
|
+
ArSyncModel.prototype.release = function () {
|
80
|
+
for (var id in this._listeners)
|
79
81
|
this._listeners[id].unsubscribe();
|
80
82
|
this._listeners = {};
|
81
83
|
ArSyncModel._detach(this._ref);
|
82
84
|
this._ref = null;
|
83
|
-
}
|
84
|
-
|
85
|
-
|
86
|
-
|
85
|
+
};
|
86
|
+
ArSyncModel.retrieveRef = function (request, option) {
|
87
|
+
var key = JSON.stringify([request, option]);
|
88
|
+
var ref = this._cache[key];
|
87
89
|
if (!ref) {
|
88
|
-
|
89
|
-
ref = this._cache[key] = { key, count: 0, timer: null, model };
|
90
|
+
var model = new ArSyncStore_1.default(request, option);
|
91
|
+
ref = this._cache[key] = { key: key, count: 0, timer: null, model: model };
|
90
92
|
}
|
91
93
|
this._attach(ref);
|
92
94
|
return ref;
|
93
|
-
}
|
94
|
-
|
95
|
+
};
|
96
|
+
ArSyncModel._detach = function (ref) {
|
97
|
+
var _this = this;
|
95
98
|
ref.count--;
|
96
|
-
|
99
|
+
var timeout = this.cacheTimeout;
|
97
100
|
if (ref.count !== 0)
|
98
101
|
return;
|
99
|
-
|
102
|
+
var timedout = function () {
|
100
103
|
ref.model.release();
|
101
|
-
delete
|
104
|
+
delete _this._cache[ref.key];
|
102
105
|
};
|
103
106
|
if (timeout) {
|
104
107
|
ref.timer = setTimeout(timedout, timeout);
|
@@ -106,28 +109,34 @@ class ArSyncModel {
|
|
106
109
|
else {
|
107
110
|
timedout();
|
108
111
|
}
|
109
|
-
}
|
110
|
-
|
112
|
+
};
|
113
|
+
ArSyncModel._attach = function (ref) {
|
111
114
|
ref.count++;
|
112
115
|
if (ref.timer)
|
113
116
|
clearTimeout(ref.timer);
|
114
|
-
}
|
115
|
-
|
117
|
+
};
|
118
|
+
ArSyncModel.setConnectionAdapter = function (adapter) {
|
116
119
|
ArSyncStore_1.default.connectionManager = new ConnectionManager_1.default(adapter);
|
117
|
-
}
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
120
|
+
};
|
121
|
+
ArSyncModel.waitForLoad = function () {
|
122
|
+
var models = [];
|
123
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
124
|
+
models[_i] = arguments[_i];
|
125
|
+
}
|
126
|
+
return new Promise(function (resolve) {
|
127
|
+
var count = 0;
|
128
|
+
for (var _i = 0, models_1 = models; _i < models_1.length; _i++) {
|
129
|
+
var model = models_1[_i];
|
130
|
+
model.onload(function () {
|
123
131
|
count++;
|
124
132
|
if (models.length == count)
|
125
133
|
resolve(models);
|
126
134
|
});
|
127
135
|
}
|
128
136
|
});
|
129
|
-
}
|
130
|
-
}
|
137
|
+
};
|
138
|
+
ArSyncModel._cache = {};
|
139
|
+
ArSyncModel.cacheTimeout = 10 * 1000;
|
140
|
+
return ArSyncModel;
|
141
|
+
}());
|
131
142
|
exports.default = ArSyncModel;
|
132
|
-
ArSyncModel._cache = {};
|
133
|
-
ArSyncModel.cacheTimeout = 10 * 1000;
|