Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Class members are inaccessible in tryLock #5

Closed
dancju opened this issue Oct 4, 2018 · 2 comments
Closed

Class members are inaccessible in tryLock #5

dancju opened this issue Oct 4, 2018 · 2 comments

Comments

@dancju
Copy link

dancju commented Oct 4, 2018

Class members are inaccessible due to the class not being bound in the execution context of tryLock.

Following snippet explains what I mean.

import { Cron, NestDistributedSchedule } from 'nest-schedule';

export class Foo extends NestDistributedSchedule {
  public lock: boolean;

  constructor() {
    super();
    this.lock = false;
  }

  public async tryLock(method: string) {
    if (this.lock) { // runtime error, because `this` is undefined
      throw new Error();
    }
    this.lock = true;
    return () => {
      this.lock = false;
    };
  }

  @Cron('*/1 * * * * *')
  public async cron(): Promise<void> {
    console.log(new Date());
  }
}

const foo = new Foo();

Perhaps .bind should be added to
https://github.com/miaowing/nest-schedule/blob/master/lib/JobExecutor.ts#L19

@saberistic
Copy link

I am trying to implement an advisory lock using postgresql and typeorm. I am facing same issue when trying to use the connection that is injected into the constructor.

@miaowing
Copy link
Owner

miaowing commented Oct 8, 2018

Thanks, I have fixed it in 0.4.1, @Nerddan @amirsaber

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants