Skip to content

Commit 64674db

Browse files
committed
refactor(adapter): put a cleaner way for inheritance
1 parent 6e3909f commit 64674db

File tree

4 files changed

+94
-103
lines changed

4 files changed

+94
-103
lines changed

src/adapters/ApexTestWireAdapter.js

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
66
*/
77

8-
import { TestWireAdapterTemplate } from "./TestWireAdapter";
8+
import { generateAdapter } from "./adapter";
99

1010
function buildErrorObject({ body, status, statusText }) {
1111
if (status && (status < 400 || status > 599)) {
@@ -28,33 +28,28 @@ function buildErrorObject({ body, status, statusText }) {
2828
};
2929
}
3030

31-
class ApexTestWireAdapterTemplate extends TestWireAdapterTemplate {
32-
static emit(value, filterFn) {
33-
super.emit({ data: value, error: undefined }, filterFn);
34-
}
31+
export function buildApexTestWireAdapter() {
32+
return class ApexTestWireAdapter extends generateAdapter() {
33+
static emit(value, filterFn) {
34+
super.emit({ data: value, error: undefined }, filterFn);
35+
}
3536

36-
static emitError(errorOptions, filterFn) {
37-
const err = buildErrorObject(errorOptions || {});
37+
static emitError(errorOptions, filterFn) {
38+
const err = buildErrorObject(errorOptions || {});
3839

39-
super.emit({ data: undefined, error: err }, filterFn);
40-
}
40+
super.emit({ data: undefined, error: err }, filterFn);
41+
}
4142

42-
static error(body, status, statusText) {
43-
const err = buildErrorObject({ body, status, statusText });
44-
45-
super.emit({ data: undefined, error: err });
46-
}
43+
static error(body, status, statusText) {
44+
const err = buildErrorObject({ body, status, statusText });
4745

48-
constructor(dataCallback) {
49-
super(dataCallback);
46+
super.emit({ data: undefined, error: err });
47+
}
5048

51-
this.emit({ data: undefined, error: undefined });
52-
}
53-
}
49+
constructor(dataCallback) {
50+
super(dataCallback);
5451

55-
export function buildApexTestWireAdapter() {
56-
return class ApexTestWireAdapter extends ApexTestWireAdapterTemplate {
57-
static _lastConfig = null;
58-
static _wireInstances = new Set();
59-
}
52+
this.emit({ data: undefined, error: undefined });
53+
}
54+
};
6055
}

src/adapters/LdsTestWireAdapter.js

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
66
*/
77

8-
import { TestWireAdapterTemplate } from "./TestWireAdapter";
8+
import { generateAdapter } from "./adapter";
99

1010
function buildErrorObject({ body, status, statusText }) {
1111
if (status && (status < 400 || status > 599)) {
@@ -29,33 +29,28 @@ function buildErrorObject({ body, status, statusText }) {
2929
};
3030
}
3131

32-
class LdsTestWireAdapterTemplate extends TestWireAdapterTemplate {
33-
static emit(value, filterFn) {
34-
super.emit({ data: value, error: undefined }, filterFn);
35-
}
32+
export function buildLdsTestWireAdapter() {
33+
return class LdsTestWireAdapter extends generateAdapter() {
34+
static emit(value, filterFn) {
35+
super.emit({ data: value, error: undefined }, filterFn);
36+
}
3637

37-
static emitError(errorOptions, filterFn) {
38-
const err = buildErrorObject(errorOptions || {});
38+
static emitError(errorOptions, filterFn) {
39+
const err = buildErrorObject(errorOptions || {});
3940

40-
super.emit({ data: undefined, error: err }, filterFn);
41-
}
41+
super.emit({ data: undefined, error: err }, filterFn);
42+
}
4243

43-
static error(body, status, statusText) {
44-
const err = buildErrorObject({ body, status, statusText });
45-
46-
super.emit({ data: undefined, error: err });
47-
}
44+
static error(body, status, statusText) {
45+
const err = buildErrorObject({ body, status, statusText });
4846

49-
constructor(dataCallback) {
50-
super(dataCallback);
47+
super.emit({ data: undefined, error: err });
48+
}
5149

52-
this.emit({ data: undefined, error: undefined })
53-
}
54-
}
50+
constructor(dataCallback) {
51+
super(dataCallback);
5552

56-
export function buildLdsTestWireAdapter() {
57-
return class LdsTestWireAdapter extends LdsTestWireAdapterTemplate {
58-
static _lastConfig = null;
59-
static _wireInstances = new Set();
60-
}
53+
this.emit({ data: undefined, error: undefined })
54+
}
55+
};
6156
}

src/adapters/TestWireAdapter.js

Lines changed: 2 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -4,62 +4,9 @@
44
* SPDX-License-Identifier: MIT
55
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
66
*/
7-
export class TestWireAdapterTemplate {
8-
static _lastConfig = null;
9-
static _wireInstances = new Set();
107

11-
static emit(value, filterFn) {
12-
let instances = Array.from(this._wireInstances);
13-
14-
if (typeof filterFn === 'function') {
15-
instances = instances.filter((instance) => filterFn(instance.getConfig()));
16-
}
17-
18-
instances.forEach((instance) => instance.emit(value));
19-
}
20-
21-
static getLastConfig() {
22-
return this._lastConfig;
23-
}
24-
25-
static resetLastConfig() {
26-
this._lastConfig = null;
27-
}
28-
29-
_dataCallback;
30-
config = {};
31-
32-
constructor(dataCallback) {
33-
this._dataCallback = dataCallback;
34-
this.constructor._wireInstances.add(this);
35-
}
36-
37-
update(config) {
38-
this.config = config;
39-
this.constructor._lastConfig = config;
40-
}
41-
42-
connect() {
43-
this.constructor._lastConfig = {};
44-
this.constructor._wireInstances.add(this);
45-
}
46-
47-
disconnect() {
48-
this.constructor._wireInstances.delete(this);
49-
}
50-
51-
emit(value) {
52-
this._dataCallback(value);
53-
}
54-
55-
getConfig() {
56-
return this.config;
57-
}
58-
}
8+
import { generateAdapter } from "./adapter";
599

6010
export function buildTestWireAdapter() {
61-
return class TestWireAdapter extends TestWireAdapterTemplate {
62-
static _lastConfig = null;
63-
static _wireInstances = new Set();
64-
}
11+
return generateAdapter();
6512
}

src/adapters/adapter.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
export function generateAdapter() {
2+
let lastConfig = null;
3+
const wireInstances = new Set();
4+
5+
return class TestWireAdapterTemplate {
6+
static emit(value, filterFn) {
7+
let instances = Array.from(wireInstances);
8+
9+
if (typeof filterFn === 'function') {
10+
instances = instances.filter((instance) => filterFn(instance.getConfig()));
11+
}
12+
13+
instances.forEach((instance) => instance.emit(value));
14+
}
15+
16+
static getLastConfig() {
17+
return lastConfig;
18+
}
19+
20+
static resetLastConfig() {
21+
lastConfig = null;
22+
}
23+
24+
_dataCallback;
25+
config = {};
26+
27+
constructor(dataCallback) {
28+
this._dataCallback = dataCallback;
29+
wireInstances.add(this);
30+
}
31+
32+
update(config) {
33+
this.config = config;
34+
lastConfig = config;
35+
}
36+
37+
connect() {
38+
lastConfig = {};
39+
wireInstances.add(this);
40+
}
41+
42+
disconnect() {
43+
wireInstances.delete(this);
44+
}
45+
46+
emit(value) {
47+
this._dataCallback(value);
48+
}
49+
50+
getConfig() {
51+
return this.config;
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)