ar_sync 1.1.0 → 1.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -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
data/lib/ar_sync/rails.rb CHANGED
@@ -47,8 +47,8 @@ module ArSync
47
47
  end
48
48
 
49
49
  def sync_call
50
- _api_call :sync do |model, current_user, query|
51
- ArSync.sync_serialize model, current_user, query
50
+ _api_call :sync do |schema, current_user, query|
51
+ ArSync.sync_serialize schema, current_user, query
52
52
  end
53
53
  end
54
54
 
@@ -69,15 +69,8 @@ module ArSync
69
69
  end
70
70
 
71
71
  def static_call
72
- _api_call :static do |model, current_user, query|
73
- case model
74
- when ArSync::Collection, ActiveRecord::Relation, Array
75
- ArSerializer.serialize model.to_a, query, context: current_user
76
- when ArSerializer::Serializable
77
- ArSerializer.serialize model, query, context: current_user
78
- else
79
- model
80
- end
72
+ _api_call :static do |schema, current_user, query|
73
+ ArSerializer.serialize schema, query, context: current_user
81
74
  end
82
75
  end
83
76
 
@@ -87,15 +80,19 @@ module ArSync
87
80
  if respond_to?(ArSync.config.current_user_method)
88
81
  current_user = send ArSync.config.current_user_method
89
82
  end
83
+ sch = schema
90
84
  responses = params[:requests].map do |request|
91
85
  begin
92
86
  api_name = request[:api]
93
- sch = schema
94
- info = sch.class._serializer_field_info api_name
95
- raise ArSync::ApiNotFound, "#{type.to_s.capitalize} API named `#{api_name}` not configured" unless info
96
- api_params = (request[:params].as_json || {}).transform_keys(&:to_sym)
97
- model = sch.instance_exec(current_user, **api_params, &info.data_block)
98
- { data: yield(model, current_user, request[:query].as_json) }
87
+ raise ArSync::ApiNotFound, "#{type.to_s.capitalize} API named `#{api_name}` not configured" unless sch.class._serializer_field_info api_name
88
+ query = {
89
+ api_name => {
90
+ as: :data,
91
+ params: request[:params].as_json,
92
+ attributes: request[:query].as_json
93
+ }
94
+ }
95
+ yield schema, current_user, query
99
96
  rescue StandardError => e
100
97
  { error: handle_exception(e) }
101
98
  end
@@ -1,3 +1,3 @@
1
1
  module ArSync
2
- VERSION = '1.1.0'
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) {