Comments (5)
@edelooff Have you come across this issue in your usage of Amplfy?
First, I really appreciate the detailed report - it made it very easy to follow your points as you made them.
Having looked at it, I am not sure its something that would actually be able to happen. Given the synchronous nature of the code between lines 19 and 23, as far as I know, in a normal JavaScript run loop, it would be impossible for code outside of this method to remove a subscriber between the execution of those lines. If I am thinking about this incorrectly, please let me know – but I am fairly confident this is not a bug that could happen.
from amplify.
@dcneiner I've not come across this issue in my usage of Amplify, no.
This mostly came up as I discussed the code with a friend, and from closer inspection is based on a false assumption I made with regards to JavaScript asynchronicity. I had assumed JS was fully asynchronous, meaning that every function can be interrupted at any point to switch context to another (or they run on two threads simultaneously). However, this seems to be not the case:
This describes how the whole method (from line 7 to 32) will run without any interruption, contrary to my initial statement in the issue. This means there's indeed no race condition and everything works as intended. False alarm and apologies from my side. Bringing assumptions from other languages doesn't quite work out.
Should it not have been obvious, JavaScript's not really my area of expertise. By this point I'm mostly trying to understand some of the decisions that were made with better understanding than my own.
However, this makes me curious about the array slice that's done on line 23. Is making a copy of the subscriber list necessary? Getting a reference to the subscribers from the full list makes access a little faster and a lot easier on the eyes, but given that there's no concurrent (async) access to the subscriber list, it will not change during execution of the loop. This would mean the copy is a waste of memory, right? That is, the .slice()
portion could be left out and functionality remains identical.
from amplify.
Ah, ok – that makes sense!
However, this makes me curious about the array slice that's done on line 23. Is making a copy of the subscriber list necessary? Getting a reference to the subscribers from the full list makes access a little faster and a lot easier on the eyes, but given that there's no concurrent (async) access to the subscriber list, it will not change during execution of the loop. This would mean the copy is a waste of memory, right? That is, the .slice() portion could be left out and functionality remains identical.
No, this code is in place because we are using a for loop – and since a callback could request a synchronous unsubscribe in the middle of a synchronous publish cycle, it is important the indexes do not change until our for loop is completed. The relevant test that shows this in action is here: https://github.com/appendto/amplify/blob/master/test/core/unit.js#L221
from amplify.
Ah yes of course, that was pretty obvious. Thanks for the quick response.
from amplify.
Sure thing @edelooff! Thanks for the interest in the code!
from amplify.
Related Issues (20)
- Amplify 1.1.2 Uncaught TypeError in new Chrome
- Add browser support documentation [was: "IE8 support"] HOT 1
- amplify.js incompatible with FormData
- losing the data in local storage that I set on a HTTP page when I move to a HTTPS page HOT 1
- Subscribe to event groups (namespaces)
- is amplify still being developed? HOT 5
- null being passed to handleResponse on error instead of _xhr HOT 1
- Any plan to support chrome.storage in the future?
- Not an issue, a suggestion... for pubsub HOT 1
- support for commonjs HOT 1
- amplify.store() examples in the docs is not working HOT 1
- ajax request types are taking longer than default jquery ajax calls
- need clear all subscription method
- Publish core as a separate module
- Offline DB Support HOT 1
- Provide a 'baseURL' option HOT 1
- userData domain level persistance not working HOT 1
- Publish/Subscribe across iframes HOT 4
- BUG: amplify.request on IE 9 not work HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from amplify.