ar_sync 1.1.1 → 1.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/core/hooks.js CHANGED
@@ -18,7 +18,7 @@ function checkHooks() {
18
18
  if (!useState)
19
19
  throw 'uninitialized. needs `initializeHooks({ useState, useEffect, useMemo, useRef })`';
20
20
  }
21
- var initialResult = [null, { complete: false, notfound: undefined, connected: true }];
21
+ var initialResult = [null, { complete: false, notfound: undefined, connected: true, destroyed: false }];
22
22
  function useArSyncModel(request) {
23
23
  var _a;
24
24
  checkHooks();
@@ -33,13 +33,13 @@ function useArSyncModel(request) {
33
33
  }
34
34
  var model = new ArSyncModel_1.default(request, { immutable: true });
35
35
  function update() {
36
- var complete = model.complete, notfound = model.notfound, connected = model.connected, data = model.data;
36
+ var complete = model.complete, notfound = model.notfound, connected = model.connected, destroyed = model.destroyed, data = model.data;
37
37
  setResult(function (resultWas) {
38
38
  var dataWas = resultWas[0], statusWas = resultWas[1];
39
- var statusPersisted = statusWas.complete === complete && statusWas.notfound === notfound && statusWas.connected === connected;
39
+ var statusPersisted = statusWas.complete === complete && statusWas.notfound === notfound && statusWas.connected === connected && statusWas.destroyed === destroyed;
40
40
  if (dataWas === data && statusPersisted)
41
41
  return resultWas;
42
- var status = statusPersisted ? statusWas : { complete: complete, notfound: notfound, connected: connected };
42
+ var status = statusPersisted ? statusWas : { complete: complete, notfound: notfound, connected: connected, destroyed: destroyed };
43
43
  return [data, status];
44
44
  });
45
45
  }
@@ -51,6 +51,7 @@ function useArSyncModel(request) {
51
51
  }
52
52
  model.subscribe('load', update);
53
53
  model.subscribe('change', update);
54
+ model.subscribe('destroy', update);
54
55
  model.subscribe('connection', update);
55
56
  return function () { return model.release(); };
56
57
  }, [requestString]);
@@ -1,9 +1,10 @@
1
- source "https://rubygems.org"
1
+ source 'https://rubygems.org'
2
2
 
3
3
  git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
4
 
5
5
  # Specify your gem's dependencies in ar_sync.gemspec
6
- gemspec path: ".."
6
+ gemspec path: '..'
7
7
 
8
- gem "activerecord", "~> 6.0.0"
8
+ gem 'sqlite3', '~> 1.4'
9
+ gem 'activerecord', '~> 6.0'
9
10
  gem 'ar_serializer', github: 'tompng/ar_serializer'
@@ -1,9 +1,10 @@
1
- source "https://rubygems.org"
1
+ source 'https://rubygems.org'
2
2
 
3
3
  git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
4
 
5
5
  # Specify your gem's dependencies in ar_sync.gemspec
6
- gemspec path: ".."
6
+ gemspec path: '..'
7
7
 
8
- gem "activerecord", "~> 7.0.0"
8
+ gem 'sqlite3', '~> 1.4'
9
+ gem 'activerecord', '~> 7.0.0'
9
10
  gem 'ar_serializer', github: 'tompng/ar_serializer'
@@ -0,0 +1,10 @@
1
+ source 'https://rubygems.org'
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in ar_sync.gemspec
6
+ gemspec path: '..'
7
+
8
+ gem 'sqlite3', '~> 1.4'
9
+ gem 'activerecord', '~> 7.1.0'
10
+ gem 'ar_serializer', github: 'tompng/ar_serializer'
@@ -173,12 +173,8 @@ module ArSync::ModelBase::ClassMethods
173
173
 
174
174
  _sync_define :id
175
175
 
176
- _sync_define :sync_keys, type: [:string] do |current_user|
177
- ArSync.sync_keys self, current_user
178
- end
179
-
180
176
  serializer_defaults namespace: :sync do |current_user|
181
- { id: id, sync_keys: ArSync.sync_keys(self, current_user) }
177
+ { _sync: _sync_field(current_user) }
182
178
  end
183
179
 
184
180
  after_initialize do
data/lib/ar_sync/core.rb CHANGED
@@ -41,7 +41,7 @@ module ArSync
41
41
  e = { action: action }
42
42
  e[:field] = field if field
43
43
  if model
44
- e[:class_name] = model.class.base_class.name
44
+ e[:class] = model.class.base_class.name
45
45
  e[:id] = model.id
46
46
  end
47
47
  event = ["#{key}#{path}", e]
@@ -53,10 +53,6 @@ module ArSync
53
53
  end
54
54
  end
55
55
 
56
- def self.sync_keys(model, user)
57
- [sync_key(model), sync_key(model, user)]
58
- end
59
-
60
56
  def self.sync_key(model, to_user = nil, signature: true)
61
57
  if model.is_a? ArSync::Collection
62
58
  key = [to_user&.id, model.klass.name, model.name].join '/'
@@ -91,7 +87,7 @@ module ArSync
91
87
  serialized = ArSerializer.serialize target, query, context: user, use: :sync
92
88
  return serialized if target.is_a? ArSync::ModelBase
93
89
  {
94
- sync_keys: ArSync.sync_keys(target, user),
90
+ _sync: { keys: [ArSync.sync_key(target), ArSync.sync_key(target, user)] },
95
91
  ordering: target.ordering,
96
92
  collection: serialized
97
93
  }
@@ -1,7 +1,18 @@
1
1
  module ArSync::ModelBase::InstanceMethods
2
+ def sync_keys(current_user)
3
+ [ArSync.sync_key(self), ArSync.sync_key(self, current_user)]
4
+ end
5
+
6
+ def _sync_field(current_user)
7
+ { id:, keys: sync_keys(current_user) }
8
+ end
9
+
2
10
  def _sync_notify(action)
3
11
  _sync_notify_parent action
4
- _sync_notify_self if self.class._sync_self? && action == :update
12
+ if self.class._sync_self?
13
+ _sync_notify_self if action == :update
14
+ _sync_notify_self_destroy if action == :destroy
15
+ end
5
16
  end
6
17
 
7
18
  def _sync_current_watch_values
@@ -121,4 +132,8 @@ module ArSync::ModelBase::InstanceMethods
121
132
  end
122
133
  ArSync.sync_send to: self, action: :update, model: self
123
134
  end
135
+
136
+ def _sync_notify_self_destroy
137
+ ArSync.sync_send to: self, action: :destroy, path: :_destroy, model: nil
138
+ end
124
139
  end
@@ -1,3 +1,3 @@
1
1
  module ArSync
2
- VERSION = '1.1.1'
2
+ VERSION = '1.1.2'
3
3
  end
@@ -1,15 +1,15 @@
1
- import ArSyncStore from './ArSyncStore'
1
+ import { ArSyncStore, Request } from './ArSyncStore'
2
2
  import ArSyncConnectionManager from './ConnectionManager'
3
3
  import ConnectionAdapter from './ConnectionAdapter'
4
4
 
5
- interface Request { api: string; query: any; params?: any }
6
5
  type Path = Readonly<(string | number)[]>
7
6
  interface Change { path: Path; value: any }
8
7
  type ChangeCallback = (change: Change) => void
9
8
  type LoadCallback = () => void
10
9
  type ConnectionCallback = (status: boolean) => void
11
- type SubscriptionType = 'load' | 'change' | 'connection'
10
+ type SubscriptionType = 'load' | 'change' | 'connection' | 'destroy'
12
11
  type SubscriptionCallback = ChangeCallback | LoadCallback | ConnectionCallback
12
+ type ArSyncModelRef = { key: string; count: number; timer: number | null; model: ArSyncStore }
13
13
 
14
14
  type PathFirst<P extends Readonly<any[]>> = ((...args: P) => void) extends (first: infer First, ...other: any) => void ? First : never
15
15
 
@@ -30,11 +30,12 @@ type DigResult<Data, P extends Readonly<any[]>> =
30
30
  : undefined
31
31
 
32
32
  export default class ArSyncModel<T> {
33
- private _ref
33
+ private _ref: ArSyncModelRef
34
34
  private _listenerSerial: number
35
35
  private _listeners
36
- complete: boolean
36
+ complete = false
37
37
  notfound?: boolean
38
+ destroyed = false
38
39
  connected: boolean
39
40
  data: T | null
40
41
  static _cache: { [key: string]: { key: string; count: number; timer: number | null; model } } = {}
@@ -43,16 +44,17 @@ export default class ArSyncModel<T> {
43
44
  this._ref = ArSyncModel.retrieveRef(request, option)
44
45
  this._listenerSerial = 0
45
46
  this._listeners = {}
46
- this.complete = false
47
47
  this.connected = ArSyncStore.connectionManager.networkStatus
48
48
  const setData = () => {
49
49
  this.data = this._ref.model.data
50
50
  this.complete = this._ref.model.complete
51
51
  this.notfound = this._ref.model.notfound
52
+ this.destroyed = this._ref.model.destroyed
52
53
  }
53
54
  setData()
54
55
  this.subscribe('load', setData)
55
56
  this.subscribe('change', setData)
57
+ this.subscribe('destroy', setData)
56
58
  this.subscribe('connection', (status: boolean) => {
57
59
  this.connected = status
58
60
  })
@@ -107,12 +109,11 @@ export default class ArSyncModel<T> {
107
109
  for (const id in this._listeners) this._listeners[id].unsubscribe()
108
110
  this._listeners = {}
109
111
  ArSyncModel._detach(this._ref)
110
- this._ref = null
111
112
  }
112
113
  static retrieveRef(
113
114
  request: Request,
114
115
  option?: { immutable: boolean }
115
- ): { key: string; count: number; timer: number | null; model } {
116
+ ): ArSyncModelRef {
116
117
  const key = JSON.stringify([request, option])
117
118
  let ref = this._cache[key]
118
119
  if (!ref) {