Skip to content

Commit

Permalink
refactor(adapter): put a cleaner way for inheritance
Browse files Browse the repository at this point in the history
  • Loading branch information
rochejul committed Feb 21, 2023
1 parent 2337040 commit 2b99d90
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 103 deletions.
43 changes: 19 additions & 24 deletions src/adapters/ApexTestWireAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/

import { TestWireAdapterTemplate } from "./TestWireAdapter";
import { generateAdapter } from "./adapter";

function buildErrorObject({ body, status, statusText }) {
if (status && (status < 400 || status > 599)) {
Expand All @@ -28,33 +28,28 @@ function buildErrorObject({ body, status, statusText }) {
};
}

class ApexTestWireAdapterTemplate extends TestWireAdapterTemplate {
static emit(value, filterFn) {
super.emit({ data: value, error: undefined }, filterFn);
}
export function buildApexTestWireAdapter() {
return class ApexTestWireAdapter extends generateAdapter() {
static emit(value, filterFn) {
super.emit({ data: value, error: undefined }, filterFn);
}

static emitError(errorOptions, filterFn) {
const err = buildErrorObject(errorOptions || {});
static emitError(errorOptions, filterFn) {
const err = buildErrorObject(errorOptions || {});

super.emit({ data: undefined, error: err }, filterFn);
}
super.emit({ data: undefined, error: err }, filterFn);
}

static error(body, status, statusText) {
const err = buildErrorObject({ body, status, statusText });

super.emit({ data: undefined, error: err });
}
static error(body, status, statusText) {
const err = buildErrorObject({ body, status, statusText });

constructor(dataCallback) {
super(dataCallback);
super.emit({ data: undefined, error: err });
}

this.emit({ data: undefined, error: undefined });
}
}
constructor(dataCallback) {
super(dataCallback);

export function buildApexTestWireAdapter() {
return class ApexTestWireAdapter extends ApexTestWireAdapterTemplate {
static _lastConfig = null;
static _wireInstances = new Set();
}
this.emit({ data: undefined, error: undefined });
}
};
}
43 changes: 19 additions & 24 deletions src/adapters/LdsTestWireAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/

import { TestWireAdapterTemplate } from "./TestWireAdapter";
import { generateAdapter } from "./adapter";

function buildErrorObject({ body, status, statusText }) {
if (status && (status < 400 || status > 599)) {
Expand All @@ -29,33 +29,28 @@ function buildErrorObject({ body, status, statusText }) {
};
}

class LdsTestWireAdapterTemplate extends TestWireAdapterTemplate {
static emit(value, filterFn) {
super.emit({ data: value, error: undefined }, filterFn);
}
export function buildLdsTestWireAdapter() {
return class LdsTestWireAdapter extends generateAdapter() {
static emit(value, filterFn) {
super.emit({ data: value, error: undefined }, filterFn);
}

static emitError(errorOptions, filterFn) {
const err = buildErrorObject(errorOptions || {});
static emitError(errorOptions, filterFn) {
const err = buildErrorObject(errorOptions || {});

super.emit({ data: undefined, error: err }, filterFn);
}
super.emit({ data: undefined, error: err }, filterFn);
}

static error(body, status, statusText) {
const err = buildErrorObject({ body, status, statusText });

super.emit({ data: undefined, error: err });
}
static error(body, status, statusText) {
const err = buildErrorObject({ body, status, statusText });

constructor(dataCallback) {
super(dataCallback);
super.emit({ data: undefined, error: err });
}

this.emit({ data: undefined, error: undefined })
}
}
constructor(dataCallback) {
super(dataCallback);

export function buildLdsTestWireAdapter() {
return class LdsTestWireAdapter extends LdsTestWireAdapterTemplate {
static _lastConfig = null;
static _wireInstances = new Set();
}
this.emit({ data: undefined, error: undefined })
}
};
}
57 changes: 2 additions & 55 deletions src/adapters/TestWireAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,62 +4,9 @@
* SPDX-License-Identifier: MIT
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/
export class TestWireAdapterTemplate {
static _lastConfig = null;
static _wireInstances = new Set();

static emit(value, filterFn) {
let instances = Array.from(this._wireInstances);

if (typeof filterFn === 'function') {
instances = instances.filter((instance) => filterFn(instance.getConfig()));
}

instances.forEach((instance) => instance.emit(value));
}

static getLastConfig() {
return this._lastConfig;
}

static resetLastConfig() {
this._lastConfig = null;
}

_dataCallback;
config = {};

constructor(dataCallback) {
this._dataCallback = dataCallback;
this.constructor._wireInstances.add(this);
}

update(config) {
this.config = config;
this.constructor._lastConfig = config;
}

connect() {
this.constructor._lastConfig = {};
this.constructor._wireInstances.add(this);
}

disconnect() {
this.constructor._wireInstances.delete(this);
}

emit(value) {
this._dataCallback(value);
}

getConfig() {
return this.config;
}
}
import { generateAdapter } from "./adapter";

export function buildTestWireAdapter() {
return class TestWireAdapter extends TestWireAdapterTemplate {
static _lastConfig = null;
static _wireInstances = new Set();
}
return generateAdapter();
}
54 changes: 54 additions & 0 deletions src/adapters/adapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
export function generateAdapter() {
let lastConfig = null;
const wireInstances = new Set();

return class TestWireAdapterTemplate {
static emit(value, filterFn) {
let instances = Array.from(wireInstances);

if (typeof filterFn === 'function') {
instances = instances.filter((instance) => filterFn(instance.getConfig()));
}

instances.forEach((instance) => instance.emit(value));
}

static getLastConfig() {
return lastConfig;
}

static resetLastConfig() {
lastConfig = null;
}

_dataCallback;
config = {};

constructor(dataCallback) {
this._dataCallback = dataCallback;
wireInstances.add(this);
}

update(config) {
this.config = config;
lastConfig = config;
}

connect() {
lastConfig = {};
wireInstances.add(this);
}

disconnect() {
wireInstances.delete(this);
}

emit(value) {
this._dataCallback(value);
}

getConfig() {
return this.config;
}
}
}

0 comments on commit 2b99d90

Please sign in to comment.