Skip to content

Commit

Permalink
feat(wire-service): update wire method arg to an object (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
yungcheng authored Jan 30, 2018
1 parent 6d7ae13 commit 6eae53d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
8 changes: 4 additions & 4 deletions packages/lwc-wire-service/src/__tests__/wired-value.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ describe("wired-value.js", () => {
const observer = wiredValue.getObserver();
observer.next(expected);
expect(cmp.target.mock.calls).toHaveLength(1);
expect(cmp.target.mock.calls[0][0]).toBe(null);
expect(cmp.target.mock.calls[0][1]).toBe(expected);
expect(cmp.target.mock.calls[0][0].error).toBe(null);
expect(cmp.target.mock.calls[0][0].data).toBe(expected);
});
it("wired method - error invokes method with error, no value", () => {
const expected = {};
Expand All @@ -179,8 +179,8 @@ describe("wired-value.js", () => {
const observer = wiredValue.getObserver();
observer.error(expected);
expect(cmp.target.mock.calls).toHaveLength(1);
expect(cmp.target.mock.calls[0][0]).toBe(expected);
expect(cmp.target.mock.calls[0][1]).toBe(undefined);
expect(cmp.target.mock.calls[0][0].error).toBe(expected);
expect(cmp.target.mock.calls[0][0].data).toBe(undefined);
});
it("wired method - complete invokes completeHandler, does not invoke method", () => {
const expected = { value: "foo" };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ export default class WiredMethod extends Element {
@api propName;
@track state = {};
@wire('test', { propName: '$propName', fields: ['Name'] })
wiredMethod(err, data) {
if (err) {
this.state.error = err;
wiredMethod({error, data}) {
if (error) {
this.state.error = error;
this.state.Name = undefined;
} else {
this.state.Name = data.Name;
Expand Down
17 changes: 15 additions & 2 deletions packages/lwc-wire-service/src/wired-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,25 @@ export class WiredValue {
getObserver() {
if (!this.observer) {
if (this.isMethod) {
const wireMethod = this.cmp[this.propName];
this.observer = {
next: value => {
this.cmp[this.propName](null, value);
// TODO: deprecate (error, data) args
if (wireMethod.length === 2) {
console.warn('[DEPRECATE] @wire function no longer supports two arguments (error, data), please update your code to use ({error, data}) instead.');
wireMethod.call(this.cmp, null, value);
} else {
wireMethod.call(this.cmp, { data: value, error: null });
}
},
error: err => {
this.cmp[this.propName](err, undefined);
// TODO: deprecate (error, data) args
if (wireMethod.length === 2) {
console.warn('[DEPRECATE] @wire function no longer supports two arguments (error, data), please update your code to use ({error, data}) instead.');
wireMethod.call(this.cmp, err, undefined);
} else {
wireMethod.call(this.cmp, { data: undefined, error: err });
}
},
complete: () => {
this.completeHandler();
Expand Down

0 comments on commit 6eae53d

Please sign in to comment.