Skip to content

Commit

Permalink
Wrap the controller returned by BaseController constructor in a Proxy…
Browse files Browse the repository at this point in the history
… that logs all non-stimulus-internal method calls and property accesses
  • Loading branch information
Sub-Xaero committed Jan 23, 2021
1 parent 47c3305 commit 8dfa8b3
Show file tree
Hide file tree
Showing 13 changed files with 70 additions and 11 deletions.
3 changes: 2 additions & 1 deletion dist/base_controller.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Controller } from "stimulus";
import { Context, Controller } from "stimulus";
export declare class BaseController extends Controller {
constructor(context: Context);
log(functionName: string, args?: {}): void;
}
//# sourceMappingURL=base_controller.d.ts.map
2 changes: 1 addition & 1 deletion dist/base_controller.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/stimulus-library.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/stimulus-library.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/stimulus-library.modern.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/stimulus-library.modern.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/stimulus-library.module.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/stimulus-library.module.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/stimulus-library.umd.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/stimulus-library.umd.js.map

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion playground/controllers/debug_controller.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ <h1 class="title is-1">Stimulus Library</h1>
<div class="has-text-centered">
<a href="../index.html">Back</a>

<p data-controller="debug">Lorem ipsum</p>
<p data-controller="debug">
<span data-debug-target="test" data-action="click->test#foo"> Lorem</span>
<span data-debug-target="test"> ipsum</span>
</p>

</div>
</div>
Expand Down
2 changes: 2 additions & 0 deletions playground/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import {

const application = Application.start();

application.debug = true;

application.register("async-block", AsyncBlockController);
application.register("auto-submit-form", AutoSubmitFormController);
application.register("autosize", AutosizeController);
Expand Down
53 changes: 53 additions & 0 deletions src/base_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,30 @@ import {Context, Controller} from "stimulus";

export class BaseController extends Controller {

constructor(context: Context) {
super(context);
return new Proxy(this, {
get: (obj, prop) => {
let returnVal = Reflect.get(obj, prop);
let self = this;
if (logProperty(prop.toString())) {
if (typeof returnVal == "function") {
return new Proxy(returnVal, {
apply(target: any, thisArg: any, argArray?: any): any {
self.log(prop.toString(), {
args: argArray,
});
return Reflect.apply(target, thisArg, argArray);
},
});
} else {
this.log(prop.toString());
}
}
return returnVal;
},
});
}

log(functionName: string, args: {} = {}): void {
// @ts-ignore
Expand All @@ -18,3 +42,32 @@ export class BaseController extends Controller {
}

}

function logProperty(prop: string): boolean {
switch (prop) {
case "application":
case "element":
case "constructor":
case "initialize":
case "log":
case "data":
case "valueDescriptorMap":
case "identifier":
return false;
}

if (/^.*?Target(s)?$/.test(prop)) {
return false;
}
if (/^.*?Value$/.test(prop)) {
return false;
}
if (/^.*?ValueChanged$/.test(prop)) {
return false;
}
if (/^.*?Class$/.test(prop)) {
return false;
}

return true;
}

0 comments on commit 8dfa8b3

Please sign in to comment.