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
disposer not called when resource is null #1099
Comments
Looks like we're using null inappropriately there: Lines 65 to 82 in c0d4472
|
Well, if we used undefined then it would not be called when resource is undefined... the example doesn't show that writeLockDisposer is allocating any resource, and if it does, how do you unallocate it if you don't get any handle? |
Thanks for your feedback. I can release the lock using a function I get passed from rwlock. That function should not be passed to the consuming code though (the argument to withWriteLock). Here's my actual code (its TS): import * as Promise from "bluebird";
import * as Rwlock from "rwlock";
class Library {
private _lock = new Rwlock();
private _ReportMeta: typeof Reports.ReportMeta;
constructor(private path: string) {
// TODO check if folder exists and is read+writable
this._ReportMeta = Reports.init(this.path);
}
listReports(): Promise<Reports.ReportMeta[]> {
return this.withWriteLock(() => {
const r = Math.random();
if (r < 1 / 3) {
return Promise.resolve([]);
} else if (r < 2 / 3) {
throw "exception";
} else {
return Promise.reject("crashed");
}
});
}
private writeLockDisposer(): Promise.Disposer<void> {
var releaseLock: Rwlock.Release; // this is just a parameterless void function
return new Promise((res, rej) => {
this._lock.writeLock(relFn => {
releaseLock = relFn;
res({}); // the releaseLock-Function is not a ressource, it does not make sense to pass it to "user-code"
});
}).disposer(releaseLock);
}
private withWriteLock<R, T>(fn: (r: R) => Promise<T>): Promise<T> {
return Promise.using<R, T>(this.writeLockDisposer(), fn);
}
} |
Ok I guess we could use special object identity instead of |
(NodeJS 6.1, Bluebird 3.3.5)
I tried using Promise.using with locks from rwlock. Imho in that scenario I do not really have a resource that i need to pass to my code, so I went with (removed everything related to rwlock to show the bluebird relevant part only):
I'm rather new to JS and Node, so I might just not know some conventions like "you never fulfill a promise with null", but I found this behaviour somewhat strange after reading the documentation:
The text was updated successfully, but these errors were encountered: