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

How does generator function for collection work? #283

Closed
caramboleyo opened this issue Nov 13, 2022 · 2 comments
Closed

How does generator function for collection work? #283

caramboleyo opened this issue Nov 13, 2022 · 2 comments

Comments

@caramboleyo
Copy link

caramboleyo commented Nov 13, 2022

In the readme you say collection can be an array or a generator function, but when i put a generator func in, it just loops infinite and the function actually gets never called:

import { Query } from 'https://esm.sh/mingo@6.1.2';

function* generatorStream() {
	console.log('generator start');
	yield { key: 1 };
	yield { key: 2 };
	yield { key: 3 };
}
console.log('START');
const query = new Query({ key: 2 });
const cursor = query.find(generatorStream);
console.log(cursor.next());
console.log('END');

As i read in another issue, i think this also will not be possible right? Because i get every data item from a file stream and of course have to wait until its here:

import { Query } from 'https://esm.sh/mingo@6.1.2';

async function* generatorStream() {
	console.log('generator start');
	yield Promise.resolve({ key: 1 });
	yield Promise.resolve({ key: 2 });
	yield Promise.resolve({ key: 3 });
}
console.log('START');
const query = new Query({ key: 2 });
const cursor = query.find(generatorStream);
console.log(await cursor.next());
// for await (const result of cursor) {}
console.log('END');
@kofrasa
Copy link
Owner

kofrasa commented Nov 13, 2022

You must invoke the generator. Since there is no general way to detect a generator, the user must explicitly initialize it. Also the library does not support async processing. Try the following.

console.log('START');
const query = new Query({ key: 2 });
const cursor = query.find(generatorStream());
console.log(cursor.next());
// for await (const result of cursor) {}
console.log('END');

@caramboleyo
Copy link
Author

Aaah, yes that works :)

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

2 participants