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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1998952425317eff3faa4eb8511f2ad0e40a5f5025dbdc3fae6b5ca7916c454f
4
- data.tar.gz: 2b5819d226d4f05892e6fda8a639d758d5579414e6e0ce7eb0fe2cddae978da4
3
+ metadata.gz: 5ce8f54d9f17ebdd9341d3ba5e1087bde5875d5a4f88b5eeddded0602aece480
4
+ data.tar.gz: 209d8c04edf0d06b42c853eea568e298ccd626438fb64b416a8cc8e82293f0b8
5
5
  SHA512:
6
- metadata.gz: d8c730609dedb72287c26ae09b39dfbe75aeb2b9e9c41b029e2c90212bb4253383a08e96fa975d00f56eb24154ba34dbe4fc13620654314e7fe061b227465a5b
7
- data.tar.gz: 5c5bed249fbc71e1c620aa1d0ef476efbb34492ee28554b86a158181d1f572e6449959b0e2fabd60af16f9b75e33b043e8d7d58dc8b50934949d88784cdc320a
6
+ metadata.gz: bb0b79346ca7062f69f77254ac4463aba3261ae8419937c64b0a0cb17715b5e97dc4035c76c863bdc87739fb1070ee21c27b034d908839309b83f26462038663
7
+ data.tar.gz: 2b633cdcff19283fd2626e41751de5e969888e905452f2a86621c39be9eed6a6c467e5abe98d194b120aa7096e370f95a5fdb80439829a47cf60047f9ed0d608
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ar_sync (1.0.4)
4
+ ar_sync (1.0.5)
5
5
  activerecord
6
6
  ar_serializer
7
7
 
@@ -1,29 +1,31 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- class ActionCableAdapter {
4
- constructor(actionCableClass) {
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
- const disconnected = () => {
11
- if (!this.connected)
9
+ ActionCableAdapter.prototype.subscribe = function (key, received) {
10
+ var _this = this;
11
+ var disconnected = function () {
12
+ if (!_this.connected)
12
13
  return;
13
- this.connected = false;
14
- this.ondisconnect();
14
+ _this.connected = false;
15
+ _this.ondisconnect();
15
16
  };
16
- const connected = () => {
17
- if (this.connected)
17
+ var connected = function () {
18
+ if (_this.connected)
18
19
  return;
19
- this.connected = true;
20
- this.onreconnect();
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;
@@ -1,79 +1,143 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- async function apiBatchFetch(endpoint, requests) {
4
- const headers = {
5
- 'Accept': 'application/json',
6
- 'Content-Type': 'application/json'
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
- const body = JSON.stringify({ requests });
9
- const option = { credentials: 'include', method: 'POST', headers, body };
10
- if (ArSyncApi.domain)
11
- endpoint = ArSyncApi.domain + endpoint;
12
- const res = await fetch(endpoint, option);
13
- if (res.status === 200)
14
- return res.json();
15
- throw new Error(res.statusText);
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
- class ApiFetcher {
18
- constructor(endpoint) {
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
- return new Promise((resolve, reject) => {
25
- this.batches.push([request, { resolve, reject }]);
26
- if (this.batchFetchTimer)
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
- this.batchFetchTimer = setTimeout(() => {
29
- this.batchFetchTimer = null;
30
- const compacts = {};
31
- const requests = [];
32
- const callbacksList = [];
33
- for (const batch of this.batches) {
34
- const request = batch[0];
35
- const callback = batch[1];
36
- const key = JSON.stringify(request);
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(request);
100
+ requests.push(request_1);
42
101
  callbacksList.push(compacts[key] = [callback]);
43
102
  }
44
103
  }
45
- this.batches = [];
46
- ArSyncApi._batchFetch(this.endpoint, requests).then((results) => {
47
- for (const i in callbacksList) {
48
- const result = results[i];
49
- const callbacks = callbacksList[i];
50
- for (const callback of callbacks) {
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
- const error = result.error || { type: 'Unknown Error' };
56
- callback.reject(Object.assign(Object.assign({}, error), { retry: false }));
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
- const error = { type: e.name, message: e.message, retry: true };
62
- for (const callbacks of callbacksList) {
63
- for (const callback of callbacks)
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
- const staticFetcher = new ApiFetcher('/static_api');
72
- const syncFetcher = new ApiFetcher('/sync_api');
73
- const ArSyncApi = {
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) => staticFetcher.fetch(request),
77
- syncFetch: (request) => syncFetcher.fetch(request),
140
+ fetch: function (request) { return staticFetcher.fetch(request); },
141
+ syncFetch: function (request) { return syncFetcher.fetch(request); },
78
142
  };
79
143
  exports.default = ArSyncApi;
@@ -1,104 +1,107 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- const ArSyncStore_1 = require("./ArSyncStore");
4
- const ConnectionManager_1 = require("./ConnectionManager");
5
- class ArSyncModel {
6
- constructor(request, option) {
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
- const setData = () => {
13
- this.data = this._ref.model.data;
14
- this.complete = this._ref.model.complete;
15
- this.notfound = this._ref.model.notfound;
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
- this.connected = status;
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
- const subscription = this.subscribe(event, (arg) => {
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
- static digData(data, path) {
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
- const key = path[0];
44
- const other = path.slice(1);
44
+ var key = path[0];
45
+ var other = path.slice(1);
45
46
  if (Array.isArray(data)) {
46
- return this.digData(data.find(el => el.id === key), other);
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
- const id = this._listenerSerial++;
56
- const subscription = this._ref.model.subscribe(event, callback);
57
- let unsubscribed = false;
58
- const unsubscribe = () => {
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 this._listeners[id];
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: this.data });
74
+ callback({ path: [], value: _this.data });
73
75
  }, 0);
74
76
  }
75
- return this._listeners[id] = { unsubscribe };
76
- }
77
- release() {
78
- for (const id in this._listeners)
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
- static retrieveRef(request, option) {
85
- const key = JSON.stringify([request, option]);
86
- let ref = this._cache[key];
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
- const model = new ArSyncStore_1.default(request, option);
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
- static _detach(ref) {
95
+ };
96
+ ArSyncModel._detach = function (ref) {
97
+ var _this = this;
95
98
  ref.count--;
96
- const timeout = this.cacheTimeout;
99
+ var timeout = this.cacheTimeout;
97
100
  if (ref.count !== 0)
98
101
  return;
99
- const timedout = () => {
102
+ var timedout = function () {
100
103
  ref.model.release();
101
- delete this._cache[ref.key];
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
- static _attach(ref) {
112
+ };
113
+ ArSyncModel._attach = function (ref) {
111
114
  ref.count++;
112
115
  if (ref.timer)
113
116
  clearTimeout(ref.timer);
114
- }
115
- static setConnectionAdapter(adapter) {
117
+ };
118
+ ArSyncModel.setConnectionAdapter = function (adapter) {
116
119
  ArSyncStore_1.default.connectionManager = new ConnectionManager_1.default(adapter);
117
- }
118
- static waitForLoad(...models) {
119
- return new Promise((resolve) => {
120
- let count = 0;
121
- for (const model of models) {
122
- model.onload(() => {
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;