ar_sync 1.0.5 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/src/core/hooks.ts CHANGED
@@ -23,13 +23,13 @@ function checkHooks() {
23
23
 
24
24
  interface ModelStatus { complete: boolean; notfound?: boolean; connected: boolean }
25
25
  export type DataAndStatus<T> = [T | null, ModelStatus]
26
- export interface Request { api: string; params?: any; query: any }
26
+ export interface Request { api: string; params?: any; id?: number; query: any }
27
27
 
28
28
  const initialResult: DataAndStatus<any> = [null, { complete: false, notfound: undefined, connected: true }]
29
29
  export function useArSyncModel<T>(request: Request | null): DataAndStatus<T> {
30
30
  checkHooks()
31
31
  const [result, setResult] = useState<DataAndStatus<T>>(initialResult)
32
- const requestString = JSON.stringify(request && request.params)
32
+ const requestString = JSON.stringify(request?.id ?? request?.params)
33
33
  const prevRequestStringRef = useRef(requestString)
34
34
  useEffect(() => {
35
35
  prevRequestStringRef.current = requestString
@@ -79,10 +79,10 @@ export function useArSyncFetch<T>(request: Request | null): DataStatusUpdate<T>
79
79
  checkHooks()
80
80
  const [state, setState] = useState<FetchState<T>>(initialFetchState)
81
81
  const query = request && request.query
82
- const params = request && request.params
82
+ const resourceIdentifier = request?.id ?? request?.params
83
83
  const requestString = useMemo(() => {
84
- return JSON.stringify(extractParams(query, [params]))
85
- }, [query, params])
84
+ return JSON.stringify(extractParams(query, [resourceIdentifier]))
85
+ }, [query, resourceIdentifier])
86
86
  const prevRequestStringRef = useRef(requestString)
87
87
  const loader = useMemo(() => {
88
88
  let lastLoadId = 0
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ar_sync
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.5
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - tompng
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-01-24 00:00:00.000000000 Z
11
+ date: 2022-04-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -101,6 +101,7 @@ executables: []
101
101
  extensions: []
102
102
  extra_rdoc_files: []
103
103
  files:
104
+ - ".github/workflows/test.yml"
104
105
  - ".gitignore"
105
106
  - ".travis.yml"
106
107
  - Gemfile
@@ -127,6 +128,8 @@ files:
127
128
  - core/DataType.js
128
129
  - core/hooks.d.ts
129
130
  - core/hooks.js
131
+ - gemfiles/Gemfile-rails-6
132
+ - gemfiles/Gemfile-rails-7
130
133
  - index.d.ts
131
134
  - index.js
132
135
  - lib/ar_sync.rb
@@ -134,7 +137,6 @@ files:
134
137
  - lib/ar_sync/collection.rb
135
138
  - lib/ar_sync/config.rb
136
139
  - lib/ar_sync/core.rb
137
- - lib/ar_sync/field.rb
138
140
  - lib/ar_sync/instance_methods.rb
139
141
  - lib/ar_sync/rails.rb
140
142
  - lib/ar_sync/type_script.rb
@@ -159,7 +161,7 @@ homepage: https://github.com/tompng/ar_sync
159
161
  licenses:
160
162
  - MIT
161
163
  metadata: {}
162
- post_install_message:
164
+ post_install_message:
163
165
  rdoc_options: []
164
166
  require_paths:
165
167
  - lib
@@ -174,8 +176,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
174
176
  - !ruby/object:Gem::Version
175
177
  version: '0'
176
178
  requirements: []
177
- rubygems_version: 3.1.2
178
- signing_key:
179
+ rubygems_version: 3.3.3
180
+ signing_key:
179
181
  specification_version: 4
180
182
  summary: ActiveRecord - JavaScript Sync
181
183
  test_files: []
data/lib/ar_sync/field.rb DELETED
@@ -1,96 +0,0 @@
1
- class ArSync::Field
2
- attr_reader :name
3
- def initialize(name)
4
- @name = name
5
- end
6
-
7
- def skip_propagation?(_parent, _child, _path)
8
- false
9
- end
10
-
11
- def action_convert(action)
12
- action
13
- end
14
-
15
- def order_param; end
16
- end
17
-
18
- class ArSync::DataField < ArSync::Field
19
- def type
20
- :data
21
- end
22
-
23
- def data(parent, _child, to_user:, **)
24
- ArSync.serialize parent, name, user: to_user
25
- end
26
-
27
- def path(_child)
28
- []
29
- end
30
-
31
- def action_convert(_action)
32
- :update
33
- end
34
-
35
- def skip_propagation?(_parent, _child, path)
36
- !path.nil?
37
- end
38
- end
39
-
40
- class ArSync::HasOneField < ArSync::Field
41
- def type
42
- :one
43
- end
44
-
45
- def data(_parent, child, action:, **)
46
- child._sync_data new_record: action == :create
47
- end
48
-
49
- def path(_child)
50
- [name]
51
- end
52
- end
53
-
54
- class ArSync::HasManyField < ArSync::Field
55
- attr_reader :limit, :order, :association, :propagate_when
56
- def type
57
- :many
58
- end
59
-
60
- def initialize(name, association: nil, limit: nil, order: nil, propagate_when: nil)
61
- super name
62
- @limit = limit
63
- @order = order
64
- @association = association || name
65
- @propagate_when = propagate_when
66
- end
67
-
68
- def skip_propagation?(parent, child, _path)
69
- return false unless limit
70
- return !propagate_when.call(child) if propagate_when
71
- ids = parent.send(association).order(id: order).limit(limit).ids
72
- if child.destroyed?
73
- ids.size == limit && (order == :asc ? ids.max < child.id : child.id < ids.min)
74
- else
75
- !ids.include? child.id
76
- end
77
- end
78
-
79
- def data(_parent, child, action:, **)
80
- child._sync_data new_record: action == :create
81
- end
82
-
83
- def order_param
84
- { limit: limit, order: order } if order
85
- end
86
-
87
- def path(child)
88
- [name, child.id]
89
- end
90
- end
91
-
92
- class ArSync::CollectionField < ArSync::HasManyField
93
- def path(child)
94
- [child.id]
95
- end
96
- end