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

Remove added observer #30

Open
alexey13 opened this issue May 1, 2019 · 3 comments
Open

Remove added observer #30

alexey13 opened this issue May 1, 2019 · 3 comments

Comments

@alexey13
Copy link

alexey13 commented May 1, 2019

Good day! Thanks for awesome work. Trying to use it as state manager. I have a global proxy object - state and local states that observe global proxy object and render they logic.

//Global state
const state = ObservableSlim.create(STATE, true);

//Local observer
ObservableSlim.observe(state, function(changes) {
	renderComponent(changes)
});

//Remove all observers but i want to stop only local observer
ObservableSlim.remove(state)

I would like to have a option to remove only specific observer. Its not possible right?

@alexey13
Copy link
Author

alexey13 commented May 1, 2019

Something like that

//Local observer
const localObserver = ObservableSlim.observe(state, function(changes) {
	renderComponent(changes)
});

//Remove local observer
ObservableSlim.remove(localObserver)

@alexey13
Copy link
Author

alexey13 commented May 1, 2019

                observe: function(proxy, observer) {
			// loop over all the observables created by the _create() function
			var i = observables.length;
			while (i--) {
				if (observables[i].parentProxy === proxy) {
					observables[i].observers.push(observer);
					break;
				}
			};
                        //Added
			return observer;
		},

		/*
	        Remove observer
                params: proxy object and observer function
		*/
		observerRemove: function(proxy, observer) {
			var i = observables.length;
			while (i--) {
				if (observables[i].parentProxy === proxy) {
					var b = observables[i].observers.length;
					while(b--) {
						if (observables[i].observers[b] === observer) {
							observables[i].observers.splice(b,1);
							break;
						}
					}
					break;
				}
			};
			
		},

@alexey13
Copy link
Author

alexey13 commented May 1, 2019

Using new method

const localObserver = ObservableSlim.observe(state, function(changes) {
  ....
});
//Proxy object and observer function of that proxy object
ObservableSlim.observerRemove(state, localObserver);

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

1 participant