Skip to content

Commit 1cfd983

Browse files
committed
fix(property): add missing support for observe method
1 parent 4c325bd commit 1cfd983

File tree

5 files changed

+51
-18
lines changed

5 files changed

+51
-18
lines changed

Diff for: docs/core-features/property.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ Property factory provides the most basic feature - it holds the property value.
99
## Usage
1010

1111
```typescript
12-
property(defaultValue: any, [connect: Function]): Object
12+
property(defaultValue: any, connect: Function, observe: Function): Object
1313
```
1414

1515
* **arguments**:
1616
* `defaultValue` - any value
1717
* `connect` - a connect callback function of the property descriptor
18+
* `observe` - an observe callback function of the property descriptor
1819
* **returns**:
1920
* a property descriptor, which resolves to value
2021

Diff for: docs/misc/api-reference.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,13 @@ define({ tagName: descriptors, ... }): { tagName: Wrapper, ... }
2727
## property
2828

2929
```typescript
30-
property(defaultValue: any, [connect: Function]): Object
30+
property(defaultValue: any, connect: Function, observe: Function): Object
3131
```
3232

3333
* **arguments**:
3434
* `defaultValue` - any value
3535
* `connect` - a connect callback function of the property descriptor
36+
* `observe` - an observe callback function of the property descriptor
3637
* **returns**:
3738
* a property descriptor, which resolves to value
3839

Diff for: src/property.js

+17-15
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const objectTransform = value => {
99
return value && Object.freeze(value);
1010
};
1111

12-
export default function property(value, connect) {
12+
export default function property(value, connect, observe) {
1313
const attrs = new WeakMap();
1414
const type = typeof value;
1515
let transform = defaultTransform;
@@ -38,7 +38,7 @@ export default function property(value, connect) {
3838

3939
return {
4040
get: (host, val = value) => val,
41-
set: (host, val, oldValue = value) => transform(val, oldValue),
41+
set: (host, val, lastValue = value) => transform(val, lastValue),
4242
connect:
4343
type !== "object" && type !== "undefined"
4444
? (host, key, invalidate) => {
@@ -57,21 +57,23 @@ export default function property(value, connect) {
5757
}
5858
: connect,
5959
observe:
60-
type !== "object" &&
61-
type !== "undefined" &&
62-
((host, val) => {
63-
const attrName = attrs.get(host);
60+
type !== "object" && type !== "undefined"
61+
? (host, val, lastValue) => {
62+
const attrName = attrs.get(host);
63+
64+
const attrValue = host.getAttribute(attrName);
65+
const nextValue = val === true ? "" : val;
6466

65-
const attrValue = host.getAttribute(attrName);
66-
const nextValue = val === true ? "" : val;
67+
if (nextValue === attrValue) return;
6768

68-
if (nextValue === attrValue) return;
69+
if (val !== 0 && !val) {
70+
host.removeAttribute(attrName);
71+
} else {
72+
host.setAttribute(attrName, nextValue);
73+
}
6974

70-
if (val !== 0 && !val) {
71-
host.removeAttribute(attrName);
72-
} else {
73-
host.setAttribute(attrName, nextValue);
74-
}
75-
}),
75+
if (observe) observe(host, val, lastValue);
76+
}
77+
: observe,
7678
};
7779
}

Diff for: test/spec/property.js

+28
Original file line numberDiff line numberDiff line change
@@ -286,4 +286,32 @@ describe("property:", () => {
286286
)(done);
287287
});
288288
});
289+
290+
describe("observe option", () => {
291+
it("is called for primitive default value", done => {
292+
const spy = jasmine.createSpy("observe");
293+
define("test-property-connect", {
294+
prop: property(0, null, spy),
295+
});
296+
297+
test("<test-property-connect></test-property-connect>")(() =>
298+
resolveRaf(() => {
299+
expect(spy).toHaveBeenCalledTimes(1);
300+
}),
301+
)(done);
302+
});
303+
304+
it("is called for object default value", done => {
305+
const spy = jasmine.createSpy("observe");
306+
define("test-property-connect", {
307+
prop: property({}, null, spy),
308+
});
309+
310+
test("<test-property-connect></test-property-connect>")(() =>
311+
resolveRaf(() => {
312+
expect(spy).toHaveBeenCalledTimes(1);
313+
}),
314+
)(done);
315+
});
316+
});
289317
});

Diff for: types/index.d.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
declare module 'hybrids' {
1+
declare module "hybrids" {
22
interface InvalidateOptions {
33
force?: boolean;
44
}
@@ -77,6 +77,7 @@ declare module 'hybrids' {
7777
function property<E, V>(
7878
value: V | null | undefined | ((value: any) => V),
7979
connect?: Descriptor<E, V>["connect"],
80+
observe?: Descriptor<E, V>["observe"],
8081
): Descriptor<E, V>;
8182

8283
function parent<E, V>(

0 commit comments

Comments
 (0)