@geodaoyu/accessor 1.0.0 → 1.0.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.
- package/README.md +281 -281
- package/package.json +27 -26
- package/src/accessor.ts +8 -11
- package/test/watch.test.js +46 -0
package/README.md
CHANGED
|
@@ -1,282 +1,282 @@
|
|
|
1
|
-
# Accessor
|
|
2
|
-
|
|
3
|
-
Class: `Accessor`
|
|
4
|
-
|
|
5
|
-
Accessor is an abstract class that facilitates the access to instance properties as well as a mechanism to watch for property changes. Every sub-class of Accessor defines properties that are directly accessible or by using the **get()** and **set()** methods. It is possible to watch for a property changes by using the **watch()** method.
|
|
6
|
-
|
|
7
|
-
## Installation
|
|
8
|
-
|
|
9
|
-
``` shell
|
|
10
|
-
npm install @geodaoyu/accessor
|
|
11
|
-
```
|
|
12
|
-
|
|
13
|
-
## Property Overview
|
|
14
|
-
|
|
15
|
-
| Name | Type | Summary | Class |
|
|
16
|
-
| ------------- | ------------------------------------------------------------ | ---------------------- | -------- |
|
|
17
|
-
| declaredClass | [String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) | The name of the class. | Accessor |
|
|
18
|
-
|
|
19
|
-
### Property Details
|
|
20
|
-
|
|
21
|
-
<table><tr><td bgcolor=#ddd><b>declaredClass</b> <span>String</span> <span>readonly</span></td></tr></table>
|
|
22
|
-
|
|
23
|
-
The name of the class.
|
|
24
|
-
|
|
25
|
-
## Method Overview
|
|
26
|
-
|
|
27
|
-
| Name | Return Type | Summary | Class |
|
|
28
|
-
| ------- | ----------- | --------------------------------------------- | -------- |
|
|
29
|
-
| get() | * | Gets the value of a property. | Accessor |
|
|
30
|
-
| set() | * | Sets the value of a property. | Accessor |
|
|
31
|
-
| watch() | WatchHandle | Watches for property changes on the instance. | Accessor |
|
|
32
|
-
|
|
33
|
-
### Method Details
|
|
34
|
-
|
|
35
|
-
<table><tr><td bgcolor=#ddd><b>get(path){*}</b></td></tr></table>
|
|
36
|
-
|
|
37
|
-
Gets the value of a property.
|
|
38
|
-
|
|
39
|
-
The name of the property can refer to a property in the instance.
|
|
40
|
-
|
|
41
|
-
```javascript
|
|
42
|
-
view.get("scale");
|
|
43
|
-
```
|
|
44
|
-
|
|
45
|
-
It can also be a path to a property deeper in the instance. `get()` returns `undefined` if a property in the path doesn't exist.
|
|
46
|
-
|
|
47
|
-
```javascript
|
|
48
|
-
var title = map.get("basemap.title");
|
|
49
|
-
|
|
50
|
-
// equivalent of
|
|
51
|
-
var title = map.basemap && map.basemap.title || undefined;
|
|
52
|
-
```
|
|
53
|
-
|
|
54
|
-
Parameter:
|
|
55
|
-
|
|
56
|
-
| **path** | [String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) |
|
|
57
|
-
| -------------------------------- | ------------------------------------------------------------ |
|
|
58
|
-
| The path of the property to get. | |
|
|
59
|
-
|
|
60
|
-
Returns:
|
|
61
|
-
|
|
62
|
-
| Type | Description |
|
|
63
|
-
| ---- | --------------------- |
|
|
64
|
-
| * | The property's value. |
|
|
65
|
-
|
|
66
|
-
<table><tr><td bgcolor=#ddd><b>set(path, value){*}</b></td></tr></table>
|
|
67
|
-
|
|
68
|
-
Sets the value of a property.
|
|
69
|
-
|
|
70
|
-
Call `set()` with a property name and a value to change the value of the property.
|
|
71
|
-
|
|
72
|
-
``` javascript
|
|
73
|
-
// setting the basemap of the map
|
|
74
|
-
map.set("basemap", "topo-vector");
|
|
75
|
-
// is equivalent to
|
|
76
|
-
map.basemap = "topo-vector";
|
|
77
|
-
|
|
78
|
-
// currying set
|
|
79
|
-
var updateViewScale = view.set.bind(view, "scale");
|
|
80
|
-
updateViewScale(5000);
|
|
81
|
-
```
|
|
82
|
-
|
|
83
|
-
`set()` can be called with the path to a property and a value. The property is not set if a property in the path doesn't exist.
|
|
84
|
-
|
|
85
|
-
``` javascript
|
|
86
|
-
// updating the title of the basemap
|
|
87
|
-
map.set("basemap.title", "World Topographic Map");
|
|
88
|
-
|
|
89
|
-
// is equivalent to
|
|
90
|
-
if (map.basemap != null) {
|
|
91
|
-
map.basemap.title = "World Topographic Map";
|
|
92
|
-
}
|
|
93
|
-
```
|
|
94
|
-
|
|
95
|
-
An object with key-value pairs may be passed into `set()` to update multiple properties at once.
|
|
96
|
-
|
|
97
|
-
```javascript
|
|
98
|
-
// setting a viewpoint on the view
|
|
99
|
-
view.set({
|
|
100
|
-
center: [-4.4861, 48.3904],
|
|
101
|
-
scale: 5000
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
// currying set
|
|
105
|
-
var updateView = view.set.bind(view);
|
|
106
|
-
|
|
107
|
-
updateView({
|
|
108
|
-
center: [-4.4861, 48.3904],
|
|
109
|
-
scale: 5000
|
|
110
|
-
});
|
|
111
|
-
```
|
|
112
|
-
|
|
113
|
-
Parameters:
|
|
114
|
-
|
|
115
|
-
| **path** | [String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) \| [Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) |
|
|
116
|
-
| ------------------------------------------------------------ | ------------------------------------------------------------ |
|
|
117
|
-
| The path to the property to set, or an object of key-value pairs. | |
|
|
118
|
-
| **value** | * |
|
|
119
|
-
| *The new value to set on the property. | |
|
|
120
|
-
|
|
121
|
-
Returns:
|
|
122
|
-
|
|
123
|
-
| Type | Description |
|
|
124
|
-
| ---- | ------------- |
|
|
125
|
-
| * | The instance. |
|
|
126
|
-
|
|
127
|
-
<table><tr><td bgcolor=#ddd><b>watch(path, callback){WatchHandle}</b></td></tr></table>
|
|
128
|
-
|
|
129
|
-
Watches for property changes on the instance.
|
|
130
|
-
|
|
131
|
-
Watching for property changes is essential for tracking changes on objects. To start watching for changes on a property, call `watch()` with the property name and a callback function that will execute each time the property changes.
|
|
132
|
-
|
|
133
|
-
``` javascript
|
|
134
|
-
var handle = mapview.watch("scale", function(newValue, oldValue, propertyName, target) {
|
|
135
|
-
console.log(propertyName + " changed from " + oldValue + " to " + newValue);
|
|
136
|
-
})
|
|
137
|
-
```
|
|
138
|
-
|
|
139
|
-
To stop watching for changes, call the `remove()` method on the object that `watch()` returns.
|
|
140
|
-
|
|
141
|
-
``` javascript
|
|
142
|
-
handle.remove();
|
|
143
|
-
```
|
|
144
|
-
|
|
145
|
-
It is important to store the resulting objects from `watch()` to properly clean up the references.
|
|
146
|
-
|
|
147
|
-
```javascript
|
|
148
|
-
var viewHandles = [];
|
|
149
|
-
function setView(view) {
|
|
150
|
-
// remove the handles for the current view.
|
|
151
|
-
viewHandles.forEach(function(handle) {
|
|
152
|
-
handle.remove();
|
|
153
|
-
});
|
|
154
|
-
viewHandles.length = 0;
|
|
155
|
-
|
|
156
|
-
this.view = view;
|
|
157
|
-
|
|
158
|
-
// watch for properties on the newly set view.
|
|
159
|
-
if (view) {
|
|
160
|
-
viewHandles.push(
|
|
161
|
-
view.watch("scale", scaleWatcher);
|
|
162
|
-
);
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
setView(mapView);
|
|
167
|
-
setView(null);
|
|
168
|
-
```
|
|
169
|
-
|
|
170
|
-
Like `get()` and `set()`, it is possible to watch for a property deep in the object hierarchy by passing a path. If a property in the path doesn't exist the watch callback is called with `undefined`.
|
|
171
|
-
|
|
172
|
-
``` javascript
|
|
173
|
-
var view = new SceneView({
|
|
174
|
-
map: new Map({
|
|
175
|
-
basemap: "streets-vector"
|
|
176
|
-
})
|
|
177
|
-
});
|
|
178
|
-
|
|
179
|
-
view.watch("map.basemap.title", function(newValue, oldValue) {
|
|
180
|
-
console.log("basemap's title changed from " + oldValue + " to " + newValue);
|
|
181
|
-
});
|
|
182
|
-
|
|
183
|
-
view.map.basemap = "topo-vector";
|
|
184
|
-
// output: "basemap's title changed from Streets to Topographic"
|
|
185
|
-
|
|
186
|
-
view.map = null;
|
|
187
|
-
// output: "basemap's title changed from Topographic to undefined"
|
|
188
|
-
```
|
|
189
|
-
|
|
190
|
-
Pass a comma delimited list of property paths, or an array of property paths, to watch multiple properties with the same callback. Use the third parameter of the callback call to determine what property changed.
|
|
191
|
-
|
|
192
|
-
``` javascript
|
|
193
|
-
view.watch("center, scale, rotation", function(newValue, oldValue, propertyName) {
|
|
194
|
-
console.log(propertyName + " changed");
|
|
195
|
-
});
|
|
196
|
-
|
|
197
|
-
// equivalent of
|
|
198
|
-
view.watch(["center", "scale", "rotation"], function(newValue, oldValue, propertyName) {
|
|
199
|
-
console.log(propertyName + " changed");
|
|
200
|
-
});
|
|
201
|
-
|
|
202
|
-
// equivalent of
|
|
203
|
-
var callback = function(newValue, oldValue, propertyName) {
|
|
204
|
-
console.log(propertyName + " changed");
|
|
205
|
-
}
|
|
206
|
-
view.watch("center", callback);
|
|
207
|
-
view.watch("scale", callback);
|
|
208
|
-
view.watch("rotation", callback);
|
|
209
|
-
```
|
|
210
|
-
|
|
211
|
-
`Accessor` doesn't call the watch callbacks for a property immediately after its value changes. Instead, when a property's value changes and if that property is watched, `Accessor` schedules a notification which is then processed at a later time. Properties that change frequently like `view.scale` can be watched without having to throttle the callback.
|
|
212
|
-
|
|
213
|
-
``` javascript
|
|
214
|
-
// Divides the view.scale three times
|
|
215
|
-
view.watch("scale", function(newValue, oldValue) {
|
|
216
|
-
console.log("view's scale changed from " + oldValue + " to " + newValue);
|
|
217
|
-
});
|
|
218
|
-
console.log("current view scale: " + view.scale);
|
|
219
|
-
view.scale = view.scale / 2;
|
|
220
|
-
view.scale = view.scale / 2;
|
|
221
|
-
view.scale = view.scale / 2;
|
|
222
|
-
console.log("current view scale: " + view.scale);
|
|
223
|
-
|
|
224
|
-
// output the following:
|
|
225
|
-
// current view scale: 36978595.474472
|
|
226
|
-
// current view scale: 4622324.434309
|
|
227
|
-
// view's scale changed from 36978595.474472 to 4622324.434309
|
|
228
|
-
```
|
|
229
|
-
|
|
230
|
-
Parameters:
|
|
231
|
-
|
|
232
|
-
| **path** | [String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) \| String[] |
|
|
233
|
-
| ------------------------------------------------------------ | ------------------------------------------------------------ |
|
|
234
|
-
| The property or properties to watch. Multiple properties can be specified as a comma-separated list. | |
|
|
235
|
-
| **callback** | watchCallback |
|
|
236
|
-
| The callback to execute when the property value has changed. | |
|
|
237
|
-
|
|
238
|
-
Returns:
|
|
239
|
-
|
|
240
|
-
| Type | Description |
|
|
241
|
-
| ----------- | -------------- |
|
|
242
|
-
| WatchHandle | A watch handle |
|
|
243
|
-
|
|
244
|
-
## Type Definitions
|
|
245
|
-
|
|
246
|
-
<table><tr><td bgcolor=#ddd><b>watchCallback(newValue, oldValue, propertyName, target)</b></td></tr></table>
|
|
247
|
-
|
|
248
|
-
Callback to be called when a watched property changes.
|
|
249
|
-
|
|
250
|
-
Parameters:
|
|
251
|
-
|
|
252
|
-
| **newValue** | * |
|
|
253
|
-
| ------------------------------------------------- | ------------------------------------------------------------ |
|
|
254
|
-
| The new value of the watched property. | |
|
|
255
|
-
| **oldValue** | * |
|
|
256
|
-
| The old value of the watched property. | |
|
|
257
|
-
| **propertyName** | [String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) |
|
|
258
|
-
| The property name. | |
|
|
259
|
-
| **target** | Accessor |
|
|
260
|
-
| The object containing the property being watched. | |
|
|
261
|
-
|
|
262
|
-
<table><tr><td bgcolor=#ddd><b>WatchHandle</b> <span>Object</span></td></tr></table>
|
|
263
|
-
|
|
264
|
-
Represents a watch created when an object invokes **watch()**.
|
|
265
|
-
|
|
266
|
-
Property:
|
|
267
|
-
|
|
268
|
-
| **remove** | [Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function) |
|
|
269
|
-
| ------------------------- | ------------------------------------------------------------ |
|
|
270
|
-
| Removes the watch handle. | |
|
|
271
|
-
|
|
272
|
-
Example:
|
|
273
|
-
|
|
274
|
-
``` javascript
|
|
275
|
-
var handle = map.watch('basemap', function(newVal){
|
|
276
|
-
// Each time the value of map.basemap changes, it is logged in the console
|
|
277
|
-
console.log("new basemap: ", newVal);
|
|
278
|
-
});
|
|
279
|
-
|
|
280
|
-
// When remove() is called on the watch handle, the map no longer watches for changes to basemap
|
|
281
|
-
handle.remove();
|
|
1
|
+
# Accessor
|
|
2
|
+
|
|
3
|
+
Class: `Accessor`
|
|
4
|
+
|
|
5
|
+
Accessor is an abstract class that facilitates the access to instance properties as well as a mechanism to watch for property changes. Every sub-class of Accessor defines properties that are directly accessible or by using the **get()** and **set()** methods. It is possible to watch for a property changes by using the **watch()** method.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
``` shell
|
|
10
|
+
npm install @geodaoyu/accessor
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Property Overview
|
|
14
|
+
|
|
15
|
+
| Name | Type | Summary | Class |
|
|
16
|
+
| ------------- | ------------------------------------------------------------ | ---------------------- | -------- |
|
|
17
|
+
| declaredClass | [String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) | The name of the class. | Accessor |
|
|
18
|
+
|
|
19
|
+
### Property Details
|
|
20
|
+
|
|
21
|
+
<table><tr><td bgcolor=#ddd><b>declaredClass</b> <span>String</span> <span>readonly</span></td></tr></table>
|
|
22
|
+
|
|
23
|
+
The name of the class.
|
|
24
|
+
|
|
25
|
+
## Method Overview
|
|
26
|
+
|
|
27
|
+
| Name | Return Type | Summary | Class |
|
|
28
|
+
| ------- | ----------- | --------------------------------------------- | -------- |
|
|
29
|
+
| get() | * | Gets the value of a property. | Accessor |
|
|
30
|
+
| set() | * | Sets the value of a property. | Accessor |
|
|
31
|
+
| watch() | WatchHandle | Watches for property changes on the instance. | Accessor |
|
|
32
|
+
|
|
33
|
+
### Method Details
|
|
34
|
+
|
|
35
|
+
<table><tr><td bgcolor=#ddd><b>get(path){*}</b></td></tr></table>
|
|
36
|
+
|
|
37
|
+
Gets the value of a property.
|
|
38
|
+
|
|
39
|
+
The name of the property can refer to a property in the instance.
|
|
40
|
+
|
|
41
|
+
```javascript
|
|
42
|
+
view.get("scale");
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
It can also be a path to a property deeper in the instance. `get()` returns `undefined` if a property in the path doesn't exist.
|
|
46
|
+
|
|
47
|
+
```javascript
|
|
48
|
+
var title = map.get("basemap.title");
|
|
49
|
+
|
|
50
|
+
// equivalent of
|
|
51
|
+
var title = map.basemap && map.basemap.title || undefined;
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Parameter:
|
|
55
|
+
|
|
56
|
+
| **path** | [String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) |
|
|
57
|
+
| -------------------------------- | ------------------------------------------------------------ |
|
|
58
|
+
| The path of the property to get. | |
|
|
59
|
+
|
|
60
|
+
Returns:
|
|
61
|
+
|
|
62
|
+
| Type | Description |
|
|
63
|
+
| ---- | --------------------- |
|
|
64
|
+
| * | The property's value. |
|
|
65
|
+
|
|
66
|
+
<table><tr><td bgcolor=#ddd><b>set(path, value){*}</b></td></tr></table>
|
|
67
|
+
|
|
68
|
+
Sets the value of a property.
|
|
69
|
+
|
|
70
|
+
Call `set()` with a property name and a value to change the value of the property.
|
|
71
|
+
|
|
72
|
+
``` javascript
|
|
73
|
+
// setting the basemap of the map
|
|
74
|
+
map.set("basemap", "topo-vector");
|
|
75
|
+
// is equivalent to
|
|
76
|
+
map.basemap = "topo-vector";
|
|
77
|
+
|
|
78
|
+
// currying set
|
|
79
|
+
var updateViewScale = view.set.bind(view, "scale");
|
|
80
|
+
updateViewScale(5000);
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
`set()` can be called with the path to a property and a value. The property is not set if a property in the path doesn't exist.
|
|
84
|
+
|
|
85
|
+
``` javascript
|
|
86
|
+
// updating the title of the basemap
|
|
87
|
+
map.set("basemap.title", "World Topographic Map");
|
|
88
|
+
|
|
89
|
+
// is equivalent to
|
|
90
|
+
if (map.basemap != null) {
|
|
91
|
+
map.basemap.title = "World Topographic Map";
|
|
92
|
+
}
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
An object with key-value pairs may be passed into `set()` to update multiple properties at once.
|
|
96
|
+
|
|
97
|
+
```javascript
|
|
98
|
+
// setting a viewpoint on the view
|
|
99
|
+
view.set({
|
|
100
|
+
center: [-4.4861, 48.3904],
|
|
101
|
+
scale: 5000
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
// currying set
|
|
105
|
+
var updateView = view.set.bind(view);
|
|
106
|
+
|
|
107
|
+
updateView({
|
|
108
|
+
center: [-4.4861, 48.3904],
|
|
109
|
+
scale: 5000
|
|
110
|
+
});
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
Parameters:
|
|
114
|
+
|
|
115
|
+
| **path** | [String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) \| [Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) |
|
|
116
|
+
| ------------------------------------------------------------ | ------------------------------------------------------------ |
|
|
117
|
+
| The path to the property to set, or an object of key-value pairs. | |
|
|
118
|
+
| **value** | * |
|
|
119
|
+
| *The new value to set on the property. | |
|
|
120
|
+
|
|
121
|
+
Returns:
|
|
122
|
+
|
|
123
|
+
| Type | Description |
|
|
124
|
+
| ---- | ------------- |
|
|
125
|
+
| * | The instance. |
|
|
126
|
+
|
|
127
|
+
<table><tr><td bgcolor=#ddd><b>watch(path, callback){WatchHandle}</b></td></tr></table>
|
|
128
|
+
|
|
129
|
+
Watches for property changes on the instance.
|
|
130
|
+
|
|
131
|
+
Watching for property changes is essential for tracking changes on objects. To start watching for changes on a property, call `watch()` with the property name and a callback function that will execute each time the property changes.
|
|
132
|
+
|
|
133
|
+
``` javascript
|
|
134
|
+
var handle = mapview.watch("scale", function(newValue, oldValue, propertyName, target) {
|
|
135
|
+
console.log(propertyName + " changed from " + oldValue + " to " + newValue);
|
|
136
|
+
})
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
To stop watching for changes, call the `remove()` method on the object that `watch()` returns.
|
|
140
|
+
|
|
141
|
+
``` javascript
|
|
142
|
+
handle.remove();
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
It is important to store the resulting objects from `watch()` to properly clean up the references.
|
|
146
|
+
|
|
147
|
+
```javascript
|
|
148
|
+
var viewHandles = [];
|
|
149
|
+
function setView(view) {
|
|
150
|
+
// remove the handles for the current view.
|
|
151
|
+
viewHandles.forEach(function(handle) {
|
|
152
|
+
handle.remove();
|
|
153
|
+
});
|
|
154
|
+
viewHandles.length = 0;
|
|
155
|
+
|
|
156
|
+
this.view = view;
|
|
157
|
+
|
|
158
|
+
// watch for properties on the newly set view.
|
|
159
|
+
if (view) {
|
|
160
|
+
viewHandles.push(
|
|
161
|
+
view.watch("scale", scaleWatcher);
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
setView(mapView);
|
|
167
|
+
setView(null);
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
Like `get()` and `set()`, it is possible to watch for a property deep in the object hierarchy by passing a path. If a property in the path doesn't exist the watch callback is called with `undefined`.
|
|
171
|
+
|
|
172
|
+
``` javascript
|
|
173
|
+
var view = new SceneView({
|
|
174
|
+
map: new Map({
|
|
175
|
+
basemap: "streets-vector"
|
|
176
|
+
})
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
view.watch("map.basemap.title", function(newValue, oldValue) {
|
|
180
|
+
console.log("basemap's title changed from " + oldValue + " to " + newValue);
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
view.map.basemap = "topo-vector";
|
|
184
|
+
// output: "basemap's title changed from Streets to Topographic"
|
|
185
|
+
|
|
186
|
+
view.map = null;
|
|
187
|
+
// output: "basemap's title changed from Topographic to undefined"
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
Pass a comma delimited list of property paths, or an array of property paths, to watch multiple properties with the same callback. Use the third parameter of the callback call to determine what property changed.
|
|
191
|
+
|
|
192
|
+
``` javascript
|
|
193
|
+
view.watch("center, scale, rotation", function(newValue, oldValue, propertyName) {
|
|
194
|
+
console.log(propertyName + " changed");
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
// equivalent of
|
|
198
|
+
view.watch(["center", "scale", "rotation"], function(newValue, oldValue, propertyName) {
|
|
199
|
+
console.log(propertyName + " changed");
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
// equivalent of
|
|
203
|
+
var callback = function(newValue, oldValue, propertyName) {
|
|
204
|
+
console.log(propertyName + " changed");
|
|
205
|
+
}
|
|
206
|
+
view.watch("center", callback);
|
|
207
|
+
view.watch("scale", callback);
|
|
208
|
+
view.watch("rotation", callback);
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
`Accessor` doesn't call the watch callbacks for a property immediately after its value changes. Instead, when a property's value changes and if that property is watched, `Accessor` schedules a notification which is then processed at a later time. Properties that change frequently like `view.scale` can be watched without having to throttle the callback.
|
|
212
|
+
|
|
213
|
+
``` javascript
|
|
214
|
+
// Divides the view.scale three times
|
|
215
|
+
view.watch("scale", function(newValue, oldValue) {
|
|
216
|
+
console.log("view's scale changed from " + oldValue + " to " + newValue);
|
|
217
|
+
});
|
|
218
|
+
console.log("current view scale: " + view.scale);
|
|
219
|
+
view.scale = view.scale / 2;
|
|
220
|
+
view.scale = view.scale / 2;
|
|
221
|
+
view.scale = view.scale / 2;
|
|
222
|
+
console.log("current view scale: " + view.scale);
|
|
223
|
+
|
|
224
|
+
// output the following:
|
|
225
|
+
// current view scale: 36978595.474472
|
|
226
|
+
// current view scale: 4622324.434309
|
|
227
|
+
// view's scale changed from 36978595.474472 to 4622324.434309
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
Parameters:
|
|
231
|
+
|
|
232
|
+
| **path** | [String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) \| String[] |
|
|
233
|
+
| ------------------------------------------------------------ | ------------------------------------------------------------ |
|
|
234
|
+
| The property or properties to watch. Multiple properties can be specified as a comma-separated list. | |
|
|
235
|
+
| **callback** | watchCallback |
|
|
236
|
+
| The callback to execute when the property value has changed. | |
|
|
237
|
+
|
|
238
|
+
Returns:
|
|
239
|
+
|
|
240
|
+
| Type | Description |
|
|
241
|
+
| ----------- | -------------- |
|
|
242
|
+
| WatchHandle | A watch handle |
|
|
243
|
+
|
|
244
|
+
## Type Definitions
|
|
245
|
+
|
|
246
|
+
<table><tr><td bgcolor=#ddd><b>watchCallback(newValue, oldValue, propertyName, target)</b></td></tr></table>
|
|
247
|
+
|
|
248
|
+
Callback to be called when a watched property changes.
|
|
249
|
+
|
|
250
|
+
Parameters:
|
|
251
|
+
|
|
252
|
+
| **newValue** | * |
|
|
253
|
+
| ------------------------------------------------- | ------------------------------------------------------------ |
|
|
254
|
+
| The new value of the watched property. | |
|
|
255
|
+
| **oldValue** | * |
|
|
256
|
+
| The old value of the watched property. | |
|
|
257
|
+
| **propertyName** | [String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) |
|
|
258
|
+
| The property name. | |
|
|
259
|
+
| **target** | Accessor |
|
|
260
|
+
| The object containing the property being watched. | |
|
|
261
|
+
|
|
262
|
+
<table><tr><td bgcolor=#ddd><b>WatchHandle</b> <span>Object</span></td></tr></table>
|
|
263
|
+
|
|
264
|
+
Represents a watch created when an object invokes **watch()**.
|
|
265
|
+
|
|
266
|
+
Property:
|
|
267
|
+
|
|
268
|
+
| **remove** | [Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function) |
|
|
269
|
+
| ------------------------- | ------------------------------------------------------------ |
|
|
270
|
+
| Removes the watch handle. | |
|
|
271
|
+
|
|
272
|
+
Example:
|
|
273
|
+
|
|
274
|
+
``` javascript
|
|
275
|
+
var handle = map.watch('basemap', function(newVal){
|
|
276
|
+
// Each time the value of map.basemap changes, it is logged in the console
|
|
277
|
+
console.log("new basemap: ", newVal);
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
// When remove() is called on the watch handle, the map no longer watches for changes to basemap
|
|
281
|
+
handle.remove();
|
|
282
282
|
```
|
package/package.json
CHANGED
|
@@ -1,26 +1,27 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@geodaoyu/accessor",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"description": "Implement esri/core/Accessor by myself.",
|
|
5
|
-
"main": "index.js",
|
|
6
|
-
"module": "dist/index.js",
|
|
7
|
-
"scripts": {
|
|
8
|
-
"
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
"
|
|
13
|
-
"
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
"
|
|
17
|
-
"
|
|
18
|
-
"
|
|
19
|
-
|
|
20
|
-
"
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
"
|
|
25
|
-
|
|
26
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@geodaoyu/accessor",
|
|
3
|
+
"version": "1.0.2",
|
|
4
|
+
"description": "Implement esri/core/Accessor by myself.",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"module": "dist/index.js",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "npx tsc",
|
|
9
|
+
"test": "mocha"
|
|
10
|
+
},
|
|
11
|
+
"keywords": [
|
|
12
|
+
"Accessor",
|
|
13
|
+
"Esri",
|
|
14
|
+
"ArcGIS"
|
|
15
|
+
],
|
|
16
|
+
"author": "GeoDaoyu",
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"type": "module",
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "https://github.com/GeoDaoyu/Accessor.git"
|
|
22
|
+
},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"mocha": "^11.7.1",
|
|
25
|
+
"typescript": "^5.8.3"
|
|
26
|
+
}
|
|
27
|
+
}
|
package/src/accessor.ts
CHANGED
|
@@ -95,18 +95,15 @@ class Accessor {
|
|
|
95
95
|
}
|
|
96
96
|
}
|
|
97
97
|
pathArray.forEach((item) => {
|
|
98
|
-
let handle;
|
|
99
98
|
const dotIndex = item.indexOf(".");
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
path: item,
|
|
107
|
-
|
|
108
|
-
};
|
|
109
|
-
}
|
|
99
|
+
const handle =
|
|
100
|
+
dotIndex !== -1
|
|
101
|
+
? this[item.slice(0, dotIndex)].watch(
|
|
102
|
+
item.slice(dotIndex + 1),
|
|
103
|
+
callback
|
|
104
|
+
)
|
|
105
|
+
: { path: item, callback };
|
|
106
|
+
|
|
110
107
|
handles.push(handle);
|
|
111
108
|
this._handles.add(handle);
|
|
112
109
|
});
|
package/test/watch.test.js
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
import Accessor from "../dist/index.js";
|
|
2
2
|
import assert from "assert";
|
|
3
|
+
class Counter extends Accessor {
|
|
4
|
+
constructor() {
|
|
5
|
+
super();
|
|
6
|
+
this.number = 0;
|
|
7
|
+
}
|
|
8
|
+
setNumber = (value) => {
|
|
9
|
+
this.number = value;
|
|
10
|
+
};
|
|
11
|
+
}
|
|
3
12
|
|
|
4
13
|
describe("#watch()", function () {
|
|
5
14
|
it("watch property change", function () {
|
|
@@ -119,4 +128,41 @@ describe("#watch()", function () {
|
|
|
119
128
|
view.zoom = 5;
|
|
120
129
|
assert.deepStrictEqual(result, []);
|
|
121
130
|
});
|
|
131
|
+
/**
|
|
132
|
+
* 子类上的方法也可以被监听
|
|
133
|
+
*/
|
|
134
|
+
it("watch subclass member", function () {
|
|
135
|
+
const counter = new Counter();
|
|
136
|
+
const result = [];
|
|
137
|
+
const callback = (newValue, oldValue, propertyName, target) => {
|
|
138
|
+
result.push(newValue, oldValue, propertyName, target);
|
|
139
|
+
};
|
|
140
|
+
counter.number = 4;
|
|
141
|
+
counter.watch("number", callback);
|
|
142
|
+
counter.setNumber(5);
|
|
143
|
+
|
|
144
|
+
assert.deepStrictEqual(result, [5, 4, "number", counter]);
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* 子类上的方法 监听变更次数
|
|
149
|
+
*/
|
|
150
|
+
it("watch subclass member changed times", function () {
|
|
151
|
+
const counter = new Counter();
|
|
152
|
+
let times = 0;
|
|
153
|
+
const callback = () => {
|
|
154
|
+
times++;
|
|
155
|
+
};
|
|
156
|
+
counter.watch("number", callback);
|
|
157
|
+
counter.number = 1; // +1;
|
|
158
|
+
counter.number = 1; // +1;
|
|
159
|
+
counter.set("number", 2); // +1;
|
|
160
|
+
counter.set("number", 2); // +1;
|
|
161
|
+
counter.set({ number: 3 }); // +1;
|
|
162
|
+
counter.set({ number: 3 }); // +1;
|
|
163
|
+
counter.setNumber(4); // +1;
|
|
164
|
+
counter.setNumber(4); // +1;
|
|
165
|
+
|
|
166
|
+
assert.equal(times, 8);
|
|
167
|
+
});
|
|
122
168
|
});
|