Skip to content

Commit de52a82

Browse files
committed
style: implement PR suggestions
1 parent 5f661c1 commit de52a82

File tree

9 files changed

+44
-45
lines changed

9 files changed

+44
-45
lines changed

Diff for: docs/basics/descriptor.md

+29-29
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@ get: (host: Element, lastValue: any) => {
6565
}
6666
```
6767

68-
* **arguments**:
69-
* `host` - an element instance
70-
* `lastValue` - last cached value of the property
71-
* **returns (required)**:
72-
* `nextValue` - a value of the current state of the property
68+
- **arguments**:
69+
- `host` - an element instance
70+
- `lastValue` - last cached value of the property
71+
- **returns (required)**:
72+
- `nextValue` - a value of the current state of the property
7373

7474
`get` calculates the current property value. The returned value is always cached. The cache mechanism works between properties defined by the library (even between different elements). If your `get` method does not use other properties, it won't be called again (then, the only way to update the value is to assert new value or call `invalidate` from `connect` method).
7575

@@ -79,8 +79,8 @@ In the following example, the `get` method of the `name` property is called agai
7979

8080
```javascript
8181
const MyElement = {
82-
firstName: 'John',
83-
lastName: 'Smith',
82+
firstName: "John",
83+
lastName: "Smith",
8484
name: {
8585
get: ({ firstName, lastName }) => `${firstName} ${lastName}`,
8686
},
@@ -102,12 +102,12 @@ set: (host: Element, value: any, lastValue: any) => {
102102
}
103103
```
104104

105-
* **arguments**:
106-
* `host` - an element instance
107-
* `value` - a value passed to assertion (ex., `el.myProperty = 'new value'`)
108-
* `lastValue` - last cached value of the property
109-
* **returns (required)**:
110-
* `nextValue` - a value of the property, which replaces cached value
105+
- **arguments**:
106+
- `host` - an element instance
107+
- `value` - a value passed to assertion (ex., `el.myProperty = 'new value'`)
108+
- `lastValue` - last cached value of the property
109+
- **returns (required)**:
110+
- `nextValue` - a value of the property, which replaces cached value
111111

112112
Every assertion of the property calls `set` method (like `myElement.property = 'new value'`). If returned `nextValue` is not equal to `lastValue`, cache of the property invalidates. However, `set` method does not trigger `get` method automatically. Only the next access to the property (like `const value = myElement.property`) calls `get` method. Then `get` takes `nextValue` from `set` as the `lastValue` argument, calculates `value` and returns it.
113113

@@ -118,7 +118,7 @@ const MyElement = {
118118
power: {
119119
set: (host, value) => value ** value,
120120
},
121-
}
121+
};
122122

123123
myElement.power = 10; // calls 'set' method and set cache to 100
124124
console.log(myElement.power); // Cache returns 100
@@ -142,12 +142,12 @@ connect: (host: Element, key: string, invalidate: Function) => {
142142
}
143143
```
144144

145-
* **arguments**:
146-
* `host` - an element instance
147-
* `key` - a property key name
148-
* `invalidate` - a callback function, which invalidates cached value
149-
* **returns (not required):**
150-
* `disconnect` - a function (without arguments)
145+
- **arguments**:
146+
- `host` - an element instance
147+
- `key` - a property key name
148+
- `invalidate` - a callback function, which invalidates cached value
149+
- **returns (not required):**
150+
- `disconnect` - a function (without arguments)
151151

152152
When you insert, remove or relocate an element in the DOM tree, `connect` or `disconnect` is called synchronously (in the `connectedCallback` and `disconnectedCallback` callbacks of the Custom Elements API).
153153

@@ -156,21 +156,21 @@ You can use `connect` to attach event listeners, initialize property value (usin
156156
If the third party code is responsible for the property value, you can use `invalidate` callback to notify that value should be recalculated (within next access). For example, it can be used to connect to async web APIs or external libraries:
157157

158158
```javascript
159-
import reduxStore from './store';
159+
import reduxStore from "./store";
160160

161161
const MyElement = {
162162
name: {
163163
get: () => reduxStore.getState().name,
164-
connect: (host, key, invalidate) => reduxStore.subscribe(() => invalidate()),
164+
connect: (host, key, invalidate) => reduxStore.subscribe(invalidate),
165165
},
166166
};
167167
```
168168

169169
`invalidate` can take an options object argument.
170170

171-
| Option | Type | Default | Description |
172-
| ------ | ---- | ------- | ----------- |
173-
| force | `boolean` | false | When true, the invalidate call will always trigger a rerender, even if the property's identity has not changed. |
171+
| Option | Type | Default | Description |
172+
| ------ | --------- | ------- | --------------------------------------------------------------------------------------------------------------- |
173+
| force | `boolean` | false | When true, the invalidate call will always trigger a rerender, even if the property's identity has not changed. |
174174

175175
> Click and play with [redux](redux.js.org) library integration example:
176176
>
@@ -187,10 +187,10 @@ observe: (host: Element, value: any, lastValue: any) => {
187187
}
188188
```
189189

190-
* **arguments**:
191-
* `host` - an element instance
192-
* `value` - current value of the property
193-
* `lastValue` - last cached value of the property
190+
- **arguments**:
191+
- `host` - an element instance
192+
- `value` - current value of the property
193+
- `lastValue` - last cached value of the property
194194

195195
When property cache invalidates (directly by the assertion or when one of the dependency invalidates) and `observe` method is set, the change detection mechanism adds the property to the internal queue. Within the next animation frame (using `requestAnimationFrame`) properties from the queue are checked if they have changed, and if they did, `observe` method of the property is called. It means, that `observe` method is asynchronous by default, and it is only called for properties, which value is different in the time of execution of the queue (in the `requestAnimationFrame` call).
196196

Diff for: src/cache.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -189,12 +189,11 @@ function deleteEntry(entry) {
189189
gcList.add(entry);
190190
}
191191

192-
function invalidateEntry(entry, options = {}) {
192+
function invalidateEntry(entry, options) {
193193
entry.depState = 0;
194194
dispatchDeep(entry);
195195

196196
if (options.clearValue) {
197-
entry.value = undefined;
198197
values.delete(entry);
199198
}
200199

@@ -207,7 +206,7 @@ function invalidateEntry(entry, options = {}) {
207206
}
208207
}
209208

210-
export function invalidate(target, key, options) {
209+
export function invalidate(target, key, options = {}) {
211210
if (contexts.size) {
212211
throw Error(
213212
`Invalidating property in chain of get calls is forbidden: '${key}'`,
@@ -218,7 +217,7 @@ export function invalidate(target, key, options) {
218217
invalidateEntry(entry, options);
219218
}
220219

221-
export function invalidateAll(target, clearValue, deleteValue) {
220+
export function invalidateAll(target, options = {}) {
222221
if (contexts.size) {
223222
throw Error(
224223
"Invalidating all properties in chain of get calls is forbidden",
@@ -228,7 +227,7 @@ export function invalidateAll(target, clearValue, deleteValue) {
228227
const targetMap = entries.get(target);
229228
if (targetMap) {
230229
targetMap.forEach(entry => {
231-
invalidateEntry(entry, { clearValue, deleteEntry: deleteValue });
230+
invalidateEntry(entry, options);
232231
});
233232
}
234233
}

Diff for: src/children.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export default function children(
2727
return walk(host, fn, options);
2828
},
2929
connect(host, key, invalidate) {
30-
const observer = new MutationObserver(() => invalidate());
30+
const observer = new MutationObserver(invalidate);
3131

3232
observer.observe(host, {
3333
childList: true,

Diff for: src/define.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ function compile(Hybrid, descriptors) {
7373
if (config.connect) {
7474
callbacks.push(host =>
7575
config.connect(host, key, options => {
76-
cache.invalidate(host, key, options);
76+
cache.invalidate(host, key, {
77+
force: options && options.force === true,
78+
});
7779
}),
7880
);
7981
}

Diff for: src/parent.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export default function parent(hybridsOrFn) {
2626
connect(host, key, invalidate) {
2727
const target = host[key];
2828
if (target) {
29-
return () => invalidate();
29+
return invalidate;
3030
}
3131
return false;
3232
},

Diff for: src/store.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1047,7 +1047,7 @@ function clear(model, clearValue = true) {
10471047
"Model definition must be used before - passed argument is probably not a model definition",
10481048
);
10491049
}
1050-
cache.invalidateAll(bootstrap(model), clearValue, true);
1050+
cache.invalidateAll(bootstrap(model), { clearValue, deleteEntry: true });
10511051
}
10521052
}
10531053

Diff for: test/spec/cache.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ describe("cache:", () => {
185185

186186
expect(getEntries(target).length).toBe(1);
187187

188-
invalidateAll(target, true);
188+
invalidateAll(target, { clearValue: true });
189189
expect(getEntries(target)).toEqual([
190190
jasmine.objectContaining({
191191
value: undefined,

Diff for: test/spec/define.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ describe("define:", () => {
357357
one: "text",
358358
two: {
359359
get: () => "test",
360-
observe: () => spy(),
360+
observe: spy,
361361
},
362362
};
363363

@@ -377,18 +377,18 @@ describe("define:", () => {
377377

378378
it("updates elements in shadowRoot", done => {
379379
test("<div></div>")(el => {
380-
const connectSpy = jasmine.createSpy();
380+
const connect = jasmine.createSpy();
381381

382382
el.attachShadow({ mode: "open" });
383383
const child = document.createElement("test-define-multiple");
384384
el.shadowRoot.appendChild(child);
385385

386386
define("test-define-multiple", {
387-
one: { get: () => "text", connect: () => connectSpy() },
387+
one: { get: () => "text", connect },
388388
});
389389

390390
return Promise.resolve().then(() => {
391-
expect(connectSpy).toHaveBeenCalledTimes(1);
391+
expect(connect).toHaveBeenCalledTimes(1);
392392
expect(child.one).toBe("text");
393393
});
394394
})(done);

Diff for: types/index.d.ts

-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ export as namespace hybrids;
44

55
declare namespace hybrids {
66
interface InvalidateOptions {
7-
clearValue?: boolean;
8-
deleteEntry?: boolean;
97
force?: boolean;
108
}
119

0 commit comments

Comments
 (0)