y-rb 0.3.2-x64-mingw32 → 0.4.0-x64-mingw32

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.
data/lib/y/doc.rb CHANGED
@@ -26,37 +26,26 @@ module Y
26
26
  #
27
27
  # @return [void]
28
28
  def commit
29
- current_transaction.commit
30
- end
31
-
32
- # The currently active transaction for this document
33
- # @return [Y::Transaction]
34
- def current_transaction
35
- @current_transaction ||= begin
36
- transaction = ydoc_transact
37
- transaction.document = self
38
- transaction
39
- end
29
+ current_transaction(&:commit)
40
30
  end
41
31
 
42
32
  # Create a diff between this document and another document. The diff is
43
33
  # created based on a state vector provided by the other document. It only
44
34
  # returns the missing blocks, as binary encoded sequence.
45
35
  #
46
- # @param [::Array<Int>] state The state to create the diff against
47
- # @return [::Array<Int>] Binary encoded diff
36
+ # @param state [::Array<Integer>] The state to create the diff against
37
+ # @return [::Array<Integer>] Binary encoded diff
48
38
  def diff(state = ZERO_STATE)
49
- ydoc_encode_diff_v1(state)
39
+ current_transaction { |tx| ydoc_encode_diff_v1(tx, state) }
50
40
  end
51
41
 
52
42
  # Creates a full diff for the current document. It is similar to {#diff},
53
43
  # but does not take a state. Instead it creates an empty state and passes it
54
44
  # to the encode_diff function.
55
45
  #
56
- # @return [::Array<Int>] Binary encoded diff
46
+ # @return [::Array<Integer>] Binary encoded diff
57
47
  def full_diff
58
- empty_state = Y::Doc.new.state
59
- ydoc_encode_diff_v1(empty_state)
48
+ diff
60
49
  end
61
50
 
62
51
  # Gets or creates a new array by name
@@ -65,11 +54,11 @@ module Y
65
54
  # from the provided array. If the array already exists and isn't
66
55
  # empty, elements are pushed to the end of the array.
67
56
  #
68
- # @param [String] name The name of the structure
69
- # @param [::Array] values Optional initial values
57
+ # @param name [String] The name of the structure
58
+ # @param values [::Array] Optional initial values
70
59
  # @return [Y::Array]
71
60
  def get_array(name, values = nil)
72
- array = current_transaction.get_array(name)
61
+ array = ydoc_get_or_insert_array(name)
73
62
  array.document = self
74
63
  array.concat(values) unless values.nil?
75
64
  array
@@ -81,11 +70,11 @@ module Y
81
70
  # pairs from the provided input hash. If the map already exists and isn't
82
71
  # empty, any existing keys are overridden and new keys are added.
83
72
  #
84
- # @param [String] name The name of the structure
85
- # @param [Hash] input Optional initial map key-value pairs
73
+ # @param name [String] The name of the structure
74
+ # @param input [Hash] Optional initial map key-value pairs
86
75
  # @return [Y::Map]
87
76
  def get_map(name, input = nil)
88
- map = current_transaction.get_map(name)
77
+ map = ydoc_get_or_insert_map(name)
89
78
  map.document = self
90
79
  input&.each { |key, value| map[key] = value }
91
80
  map
@@ -97,11 +86,11 @@ module Y
97
86
  # at creation time. If the text isn't new and not empty, appends the input
98
87
  # to the end of the text.
99
88
  #
100
- # @param [String] name The name of the structure
101
- # @param [String] input Optional initial text value
89
+ # @param name [String] The name of the structure
90
+ # @param input [String] Optional initial text value
102
91
  # @return [Y::Text]
103
92
  def get_text(name, input = nil)
104
- text = current_transaction.get_text(name)
93
+ text = ydoc_get_or_insert_text(name)
105
94
  text.document = self
106
95
  text << input unless input.nil?
107
96
  text
@@ -109,21 +98,31 @@ module Y
109
98
 
110
99
  # Gets or creates a new XMLElement by name
111
100
  #
112
- # @param [String] name The name of the structure
101
+ # @param name [String] The name of the structure
113
102
  # @return [Y::XMLElement]
114
103
  def get_xml_element(name)
115
- xml_element = current_transaction.get_xml_element(name)
116
- xml_element&.document = self
104
+ xml_element = ydoc_get_or_insert_xml_element(name)
105
+ xml_element.document = self
117
106
  xml_element
118
107
  end
119
108
 
109
+ # Gets or creates a new XMLFragment by name
110
+ #
111
+ # @param name [String] The name of the fragment
112
+ # @return [Y::XMLFragment]
113
+ def get_xml_fragment(name)
114
+ xml_fragment = ydoc_get_or_insert_xml_fragment(name)
115
+ xml_fragment.document = self
116
+ xml_fragment
117
+ end
118
+
120
119
  # Gets or creates a new XMLText by name
121
120
  #
122
- # @param [String] name The name of the structure
123
- # @param [String] input Optional initial text value
121
+ # @param name [String] The name of the structure
122
+ # @param input [String] Optional initial text value
124
123
  # @return [Y::XMLText]
125
124
  def get_xml_text(name, input = nil)
126
- xml_text = current_transaction.get_xml_text(name)
125
+ xml_text = ydoc_get_or_insert_xml_text(name)
127
126
  xml_text.document = self
128
127
  xml_text << input unless input.nil?
129
128
  xml_text
@@ -132,17 +131,17 @@ module Y
132
131
  # Creates a state vector of this document. This can be used to compare the
133
132
  # state of two documents with each other and to later on sync them.
134
133
  #
135
- # @return [::Array<Int>] Binary encoded state vector
134
+ # @return [::Array<Integer>] Binary encoded state vector
136
135
  def state
137
- current_transaction.state
136
+ current_transaction(&:state)
138
137
  end
139
138
 
140
139
  # Synchronizes this document with the diff from another document
141
140
  #
142
- # @param [::Array<Int>] diff Binary encoded update
141
+ # @param diff [::Array<Integer>] Binary encoded update
143
142
  # @return [void]
144
143
  def sync(diff)
145
- current_transaction.apply(diff)
144
+ current_transaction { |tx| tx.apply(diff) }
146
145
  end
147
146
 
148
147
  # Restores a specific document from an update that contains full state
@@ -150,54 +149,43 @@ module Y
150
149
  # This is doing the same as {#sync}, but it exists to be explicit about
151
150
  # the intent. This is the companion to {#full_diff}.
152
151
  #
153
- # @param [::Array<Int>] full_diff Binary encoded update
152
+ # @param full_diff [::Array<Integer>] Binary encoded update
154
153
  # @return [void]
155
154
  def restore(full_diff)
156
- current_transaction.apply(full_diff)
155
+ current_transaction { |tx| tx.apply(full_diff) }
157
156
  end
158
157
 
159
- # rubocop:disable Metrics/MethodLength
160
-
161
- # Creates a new transaction and provides it to the given block
162
- #
163
- # @example Insert into text
164
- # doc = Y::Doc.new
165
- # text = doc.get_text("my text")
166
- #
167
- # doc.transact do
168
- # text << "Hello, World!"
169
- # end
170
- #
171
- # @yield [transaction]
172
- # @yieldparam [Y::Transaction] transaction
173
- # @yieldreturn [void]
174
- # @return [Y::Transaction]
158
+ # Creates a new transaction
175
159
  def transact
176
- current_transaction.commit
177
-
178
- if block_given?
179
- # create new transaction just for the lifetime of this block
180
- tmp_transaction = ydoc_transact
181
- tmp_transaction.document = self
182
-
183
- # override transaction for the lifetime of the block
184
- @current_transaction = tmp_transaction
185
-
186
- yield tmp_transaction
187
-
188
- tmp_transaction.commit
160
+ # 1. release potentially existing transaction
161
+ if @current_transaction
162
+ @current_transaction.free
163
+ @current_transaction = nil
189
164
  end
190
165
 
191
- # create new transaction
166
+ # 2. store new transaction in instance variable
192
167
  @current_transaction = ydoc_transact
193
168
  @current_transaction.document = self
194
169
 
195
- current_transaction
170
+ # 3. call block with reference to current_transaction
171
+ yield @current_transaction
172
+ ensure
173
+ @current_transaction&.free
174
+ @current_transaction = nil
196
175
  end
197
176
 
198
- # rubocop:enable Metrics/MethodLength
177
+ # @!visibility private
178
+ def current_transaction(&block)
179
+ raise "provide a block" unless block
199
180
 
200
- # @!method ydoc_encode_diff_v1
181
+ # 1. instance variable is set, just use it
182
+ return yield @current_transaction if @current_transaction
183
+
184
+ # 2. forward block to transact
185
+ transact(&block) unless @current_transaction
186
+ end
187
+
188
+ # @!method ydoc_encode_diff_v1(tx, state_vector)
201
189
  # Encodes the diff of current document state vs provided state
202
190
  #
203
191
  # @example Create transaction on doc
@@ -216,5 +204,23 @@ module Y
216
204
  #
217
205
  # @return [Y::Transaction] The transaction object
218
206
  # @!visibility private
207
+
208
+ # @!method ydoc_get_or_insert_xml_element(name)
209
+ # Creates a new XMLText for the document
210
+ #
211
+ # @return [Y::XMLElement]
212
+ # @!visibility private
213
+
214
+ # @!method ydoc_get_or_insert_xml_fragment(name)
215
+ # Creates a new XMLFragment for the document
216
+ #
217
+ # @return [Y::XMLFragment]
218
+ # @!visibility private
219
+
220
+ # @!method ydoc_get_or_insert_xml_text(name)
221
+ # Creates a new XMLText for the document
222
+ #
223
+ # @return [Y::XMLText]
224
+ # @!visibility private
219
225
  end
220
226
  end
data/lib/y/map.rb CHANGED
@@ -27,7 +27,7 @@ module Y
27
27
 
28
28
  # Create a new map instance
29
29
  #
30
- # @param [Y::Doc] doc
30
+ # @param doc [Y::Doc]
31
31
  def initialize(doc = nil)
32
32
  @document = doc || Y::Doc.new
33
33
 
@@ -36,8 +36,8 @@ module Y
36
36
 
37
37
  # Attach a listener to get notified about any changes to the map
38
38
  #
39
- # @param [Proc] callback
40
- # @param [Block] block
39
+ # @param callback [Proc]
40
+ # @param block [Block]
41
41
  # @return [Integer]
42
42
  def attach(callback, &block)
43
43
  return ymap_observe(callback) unless callback.nil?
@@ -49,7 +49,7 @@ module Y
49
49
  #
50
50
  # @return [Self]
51
51
  def clear
52
- ymap_clear(transaction)
52
+ document.current_transaction { |tx| ymap_clear(tx) }
53
53
  self
54
54
  end
55
55
 
@@ -70,10 +70,10 @@ module Y
70
70
  # m.delete(:nosuch) { |key| "Key #{key} not found" }# => "Key nosuch not found"
71
71
  # m # => {}
72
72
  #
73
- # @param [String, Symbol] key
73
+ # @param key [String|Symbol]
74
74
  # @return [void]
75
75
  def delete(key)
76
- value = ymap_remove(transaction, key)
76
+ value = document.current_transaction { |tx| ymap_remove(tx, key) }
77
77
  if block_given? && key?(key)
78
78
  yield key
79
79
  else
@@ -85,7 +85,7 @@ module Y
85
85
 
86
86
  # Detach listener
87
87
  #
88
- # @param [Integer] subscription_id
88
+ # @param subscription_id [Integer]
89
89
  # @return [void]
90
90
  def detach(subscription_id)
91
91
  ymap_unobserve(subscription_id)
@@ -93,38 +93,38 @@ module Y
93
93
 
94
94
  # @return [void]
95
95
  def each(&block)
96
- ymap_each(block)
96
+ document.current_transaction { |tx| ymap_each(tx, block) }
97
97
  end
98
98
 
99
99
  # @return [true|false]
100
100
  def key?(key)
101
- ymap_contains(key)
101
+ document.current_transaction { |tx| ymap_contains(tx, key) }
102
102
  end
103
103
 
104
104
  alias has_key? key?
105
105
 
106
106
  # @return [Object]
107
107
  def [](key)
108
- ymap_get(key)
108
+ document.current_transaction { |tx| ymap_get(tx, key) }
109
109
  end
110
110
 
111
111
  # @return [void]
112
112
  def []=(key, val)
113
- ymap_insert(transaction, key, val)
113
+ document.current_transaction { |tx| ymap_insert(tx, key, val) }
114
114
  end
115
115
 
116
116
  # Returns size of map
117
117
  #
118
118
  # @return [Integer]
119
119
  def size
120
- ymap_size
120
+ document.current_transaction { |tx| ymap_size(tx) }
121
121
  end
122
122
 
123
123
  # Returns a Hash representation of this map
124
124
  #
125
125
  # @return [Hash]
126
126
  def to_h
127
- ymap_to_h
127
+ document.current_transaction { |tx| ymap_to_h(tx) }
128
128
  end
129
129
 
130
130
  # Returns a JSON representation of map
@@ -134,69 +134,66 @@ module Y
134
134
  to_h.to_json
135
135
  end
136
136
 
137
- private
138
-
139
- # @!method ymap_clear()
137
+ # @!method ymap_clear(tx)
140
138
  # Removes all key-value pairs from Map
139
+ #
140
+ # @param tx [Y::Transaction]
141
141
 
142
- # @!method ymap_contains(key)
142
+ # @!method ymap_contains(tx, key)
143
143
  # Check if a certain key is in the Map
144
144
  #
145
- # @param [String|Symbol] key
145
+ # @param tx [Y::Transaction]
146
+ # @param key [String|Symbol]
146
147
  # @return [Boolean] True, if and only if the key exists
147
148
 
148
- # @!method ymap_each(proc)
149
+ # @!method ymap_each(tx, proc)
149
150
  # Iterates over all key-value pairs in Map by calling the provided proc
150
151
  # with the key and the value as arguments.
151
152
  #
152
- # @param [Proc<String, Any>] proc A proc that is called for every element
153
+ # @param tx [Y::Transaction]
154
+ # @param proc [Proc<String|Any>] A proc that is called for every element
153
155
 
154
- # @!method ymap_get(key)
156
+ # @!method ymap_get(tx, key)
155
157
  # Returns stored value for key or nil if none is present
156
158
  #
157
- # @param [String|Symbol] key
158
- # @return [Any|Nil] Value or nil
159
+ # @param tx [Y::Transaction]
160
+ # @param key [String|Symbol]
161
+ # @return [Object|nil] Value or nil
159
162
 
160
- # @!method ymap_insert(transaction, key, value)
163
+ # @!method ymap_insert(tx, key, value)
161
164
  # Insert value for key. In case the key already exists, the previous value
162
165
  # will be overwritten.
163
166
  #
164
- # @param [Y::Transaction] transaction
165
- # @param [String|Symbol] key
166
- # @param [Any] value
167
+ # @param tx [Y::Transaction]
168
+ # @param key [String|Symbol]
169
+ # @param value [Object]
167
170
 
168
171
  # @!method ymap_observe(callback)
169
172
  #
170
- # @param [Proc] callback
173
+ # @param callback [Proc]
171
174
  # @return [Integer]
172
175
 
173
- # @!method ymap_remove(transaction, key)
176
+ # @!method ymap_remove(tx, key)
174
177
  # Removes key-value pair from Map if key exists.
175
178
  #
176
- # @param [Y::Transaction] transaction
177
- # @param [String|Symbol] key
179
+ # @param tx [Y::Transaction]
180
+ # @param key [String|Symbol]
178
181
 
179
- # @!method ymap_size()
182
+ # @!method ymap_size(tx)
180
183
  # Returns number of key-value pairs stored in map
181
184
  #
185
+ # @param tx [Y::Transaction]
182
186
  # @return [Integer] Number of key-value pairs
183
187
 
184
- # @!method ymap_to_h()
188
+ # @!method ymap_to_h(tx)
185
189
  # Returns a Hash representation of the Map
186
190
  #
191
+ # @param tx [Y::Transaction]
187
192
  # @return [Hash] Hash representation of Map
188
193
 
189
194
  # @!method ymap_unobserve(subscription_id)
190
195
  #
191
- # @param [Integer] subscription_id
196
+ # @param subscription_id [Integer]
192
197
  # @return [void]
193
-
194
- # A reference to the current active transaction of the document this map
195
- # belongs to.
196
- #
197
- # @return [Y::Transaction] A transaction object
198
- def transaction
199
- document.current_transaction
200
- end
201
198
  end
202
199
  end