add an ensurefn method that makes sure the resource is initialized before the function is called