I have created an abstract wrapper class around Electron's native BrowserWindow.
It defines two abstract accessors for the BrowserWindow. But whenever I inherit from BaseWindow, the window turns out to be undefined (note comments)!
import { BrowserWindow, BrowserWindowConstructorOptions } from "electron";
abstract class BaseWindow {
public constructor(options?: BrowserWindowConstructorOptions) {
this.setWindow(new BrowserWindow(options));
console.log("From Base", this.getWindow()); // > From Base BrowserWindow { ... }
}
public abstract getWindow(): BrowserWindow;
protected abstract setWindow(newWindow: BrowserWindow): void;
}
class MainWindow extends BaseWindow{
private window!: BrowserWindow;
public override getWindow(): BrowserWindow {
return this.window;
}
protected override setWindow(newWindow: BrowserWindow): void {
this.window = newWindow;
}
public constructor() {
super({
//...
});
console.log("From Main", this.getWindow()); // > From Main undefined
}
}
// Inside ready event
const window = new MainWindow();
console.log("From App", window.getWindow()) // > From App undefined
Am I doing something wrong? Are there any ways of fixing it?
Related
I'm instantiating the service class inside the controller class, and the log method of the service is been used in the controller.
In spec file, I'm adding spy on the log method but the spy is not been called.
Here is my code
test.service.ts
export class TestService {
public log(msg: string): void {
console.log(msg);
}
}
test.controller.ts
import { TestService } from "../service/cart.service";
export class CartController {
private testService: TestService;
constructor() {
this.testService = new TestService();
}
public testFx():void {
this.testService.log("Here is a dummy msg.")
}
}
test.controller.spec.ts
import { TestController } from "./test.controller";
import { TestService } from "./test.service";
describe("Testing controller", () => {
private testController: TestController = new TestController();
private testService: TestService = new TestService();
it ("test function", () => {
spyOn(testService, "log");
testController.testFx();
expect(testService.log).toHaveBeenCalled();
});
})
Error: - Expected spy log to have been called.
Instead of creating a new class instance,
private testController: TestController = new TestController();
private testService: TestService = new TestService();
it ("test function", () => {
spyOn(testService, "log");
you can use escape hatch for it.
Try this:
private testController: TestController = new TestController();
it ("test function", () => {
spyOn(testController["testService"], "log");
Since the private, protected and public are the concept of typescript's syntactic sugar it has nothing to do when code compiles to javascript.
The more explaining answer is here.
I have an abstract class defined statically and an implementation of it retrieved dynamically
Ie
export abstract class Foo {
abstract get();
}
const dynamicClass: typeof Foo = ( function() {
return class Bar {
get: function() {
console.log('get');
}
constructor() {
super();
console.log('cons');
}
}
}();
This is working fine exept one thing : I cannot call the constructor without "cheating"
IE
new Bar() output cannot instantiate abstract class
I have resolved that by doing
// #ts-ignore
new Bar();
But I feel i could do better.
The whole usecase for that is that the funciton that create the class at runtime will act differently based on the system it is (dynamiccally loading extra libraries that i removed for the sake of simplicity)
The easiest thing you can do is to not use an explicit type annotation, let the compiler infer what it will for dynamicClass:
export abstract class Foo {
abstract get(): void;
}
const dynamicClass = (function() {
return class Bar extends Foo {
get() {
console.log('get');
}
constructor() {
super();
console.log('cons');
}
}
})();
new dynamicClass();
Playground Link
If you want to go the explicit route you can use a constructor signature that returns Foo, this should mostly remove the abstarctness of the constructor:
export abstract class Foo {
abstract get(): void;
}
const dynamicClass: new () => Foo = (function() {
return class Bar extends Foo {
get() {
console.log('get');
}
constructor() {
super();
console.log('cons');
}
}
})();
new dynamicClass();
Playground Link
I would like to create a class that will contain a few functions to be used in all the code.
I'm doing this:
MAIN CLASS
class ListaAbitudini extends StatefulWidget {
final Function scegliIcona;
ListaAbitudini({this.scegliIcona});
#override
State<StatefulWidget> createState() {
return new _ListaAbitudiniState();
}
}
class _ListaAbitudiniState extends State<ListaAbitudini> {
#override
Widget build(BuildContext context) {
widget.scegliIcona();
...
}
}
CLASS WITH THE FUNCTION
class FindIcone {
Icon scegliIcona(){ ... }
ListaAbitudini(scegliIcona);
}
The problem is that I have an error on this line: 'ListaAbitudini(scegliIcona);' saying that "ListaAbitudini must have a method body because FindIcone isn't abstract".
What am I doing wrong? HELP
The method you use is for parent widget to child widget. In your case you can Create new object of FindIcone and call the method.
class ListaAbitudini extends StatefulWidget {
#override
State<StatefulWidget> createState() {
return new _ListaAbitudiniState();
}
}
class _ListaAbitudiniState extends State<ListaAbitudini> {
#override
Widget build(BuildContext context) {
FindIcone().scegliIcona();
}
}
class FindIcone {
Icon scegliIcona() {
return Icon(Icons.ac_unit);
}
Also you can use provider.
Where can I change the renderer in this code?
import { WINDOW_WIDTH, WINDOW_HEIGHT } from './config'
import Game from './state/Game'
class App extends Phaser.Game {
constructor () {
super(WINDOW_WIDTH, WINDOW_HEIGHT, Phaser.AUTO)
this.state.add('Game', Game)
this.state.start('Game')
}
}
const SimpleGame = new App()
and game is
class Game extends Phaser.State {
constructor() {
super()
this.player = {}
}
}
In the constructor, more specifically in the super call to Phaser.Game's constructor:
class App extends Phaser.Game {
constructor () {
super(WINDOW_WIDTH, WINDOW_HEIGHT, Phaser.CANVAS);
// ...
}
}
Reference: https://photonstorm.github.io/phaser-ce/Phaser.Game.html
My Razor Page
public class IndexModel : BaseModel {
public void OnGet() {
BaseModelMethod();
}
public void LocalMethod() {}
}
calls a method in the base ViewModel
public class BaseModel : PageModel {
public void BaseModelMethod() {
// Do stuff
}
}
Is there a way to call back to the instance of LocalMethod in the calling Razor Page?
You have to define the function as a virtual function. Your BaseModel has to have the following form:
public class BaseModel : PageModel
{
public void BaseModelMethod()
{
LocalMethod();
}
public virtual void LocalMethod()
{
}
}
As you can see I creted the virtual function so that we will know what we kind of method we will call.
Now we can define our own version of LocalMethod like this:
public class IndexModel : BaseModel
{
public void OnGet()
{
BaseModelMethod();
}
public override void LocalMethod()
{
base.LocalMethod();
}
}