-
Notifications
You must be signed in to change notification settings - Fork 43
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
Next #43
Next #43
Conversation
make contactsList readonly
Please resolve the conflicts. |
Something is wrong with this PR. The diff shows my recent commits. Could you please send a clean PR with just your commits. When you go to https://github.com/tinode/tinode-js/pull/43/files it should show just the changes that you made. |
.gitignore
Outdated
@@ -1,3 +1,5 @@ | |||
.DS_Store | |||
node_modules | |||
version.json | |||
.idea/ | |||
umd/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you want to exclude built files?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a mistake, please exclude that line
Yes I'm aware, I reverted my commit since there were too many a change. Testing the code right now. |
@or-else the problem I have right now, after getting the latest version, the |
Specifically here: https://github.com/tinode/tinode-js/blob/next/src/tinode.js#L5513 Why not enumerate |
I've been working on this feature for the last week tinode/chat#443 (item 10). In order for this feature to work I need to get rid of duplicate caching of topics (and I want to add |
I got your intention after reading further 👍🏻 For now I added a getter named |
Thanks! |
get: function() { | ||
const ret = [] | ||
for (let idx in this._tinode._cache) { | ||
if (idx.indexOf('topic:') !== 0) continue |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code style for if
is
if (condition) {
....;
}
Please follow the project code style.
for (let idx in this._tinode._cache) { | ||
if (idx.indexOf('topic:') !== 0) continue | ||
const c = this._tinode._cache[idx] | ||
if (!c.isComm()) continue |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Likewise, please follow the project code style.
@@ -5493,6 +5493,26 @@ TopicMe.prototype = Object.create(Topic.prototype, { | |||
writable: false | |||
}, | |||
|
|||
contactsList: { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BTW, this is pretty much the same as contacts()
. Essentially you are adding the same method as contacts
just under a different name.
So, why add |
It's not the best of solutions, preferably one would like to have a persistent access to a readonly list which gets updated, but, it's also okay to have a stateless list of contacts especially now that they're cached in a not so easily retrievable way. The rationale is to have a synchronous access,
Both can do, I find getter more convenient, as it's not writable neither. |
To illustrate further, if you have an awaitable function that you'd call within the CB, then the CB needs to be async, which makes the whole Another simple, perhaps better fix here could be to see if the CB is awaitable and await it right there, keep the flow synchronous. |
function isPromise(value) {
return value && value.then && typeof value.then === 'function';
} inside contacts: {
value: async function(callback, filter, context) {
this._tinode.cacheMap('topic', (c, idx) => {
if (c.isComm() && (!filter || filter(c))) {
let ret = callback.call(context, c, idx);
if (isPromise(ret)) // <==
ret = await ret;
}
});
},
enumerable: true,
configurable: true,
writable: true
} This naturally needs the |
I don't understand. Here is what you are proposing: contactsList: {
get: function() {
const ret = []
for (let idx in this._tinode._cache) {
if (idx.indexOf('topic:') !== 0) continue
const c = this._tinode._cache[idx]
if (!c.isComm()) continue
ret.push(c)
}
return ret
},
}, Here is the original method: contacts: {
value: function(callback, filter, context) {
this._tinode.cacheMap('topic', (c, idx) => {
if (c.isComm() && (!filter || filter(c))) {
callback.call(context, c, idx);
}
});
},
}, The difference is that your method returns an array, while the original calls the callback right away. You can get exactly the same result with the original method: const ret = [];
me.contacts((c) => ret.push(c)); So, why create confusion by adding a duplicate method? |
What if inside the CB you needed to await an async function? In order to make that work, you'd need to make the CB itself async, and JS will happily accept and execute it, but it won't wait for it to finish if you don't use await, hence P.s. |
You can always add an
In case of your proposal the return value of |
You cannot await inside CB, that's a syntax error. You can await inside the CB only when the CB itself is also async, but then by doing so I think you're aware that in JS, an aynac can be called with or without If you read above, I also proposed to make That's more than enough solutions, I stated the reason, the rationale and practical cases. Having contacts taking a CB and merely looping it doesn't add any value but only complications. Now, you could:
|
Works fine: |
The It was said multiple times, if we make the CB async, then the |
I didn't expect this little thing drag this long, either way there are many ways to "work around" this imposed limitation, but that is what it is at its core, unnecessary. If your original idea had been to make |
async function slowMethod(name) { // <---- this function is async
const result = await Promise.resolve(name);
console.log("Hello ", result);
};
["Alice", "Bob", "Carol"].forEach((name) => { // <----- this CALLBACK is NOT async.
slowMethod(name); // <<<---- this async function is inside a CALLBACK.
});
Correct. And it's used inside a callback which is NOT async. And
Your proposal does not make sense to me. I cannot see why it is better than the way it's done now. It's different but I see no benefits. It would require refactoring without a clear purpose. |
In JS one can call an async function but if wanted to await it, the caller function needs to be async as well. Do you follow this? You can call without awaiting, which means That code will not work IF the CB was async, of course you can do it in multiple steps and complicate things further (work around it)... All for no reason anyways 😀 |
I just demonstrated to you how you can do it without making the callback async. Please stop repeating the same thing. You have this very simple example which shows how you can do it. If you think the code snippet does not do what you want, show. There is no point in going in circles. |
Fix for tinode/chat#602