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

Tent: Scoped stores fire different events than the regular store #95

Closed
espy opened this issue Apr 12, 2016 · 7 comments
Closed

Tent: Scoped stores fire different events than the regular store #95

espy opened this issue Apr 12, 2016 · 7 comments

Comments

@espy
Copy link
Member

espy commented Apr 12, 2016

Scoped stores emit events differently than the regular store, and sometimes don't emit the correct events at all, instead only ever emitting update instead of remove, for example.

The pattern seems to be that add and remove are be mutually exclusive, only the first listener of the two ever works.

To reproduce

In an empty directory, npm init -y and hit enter until done. Install the tent release: npm i hoodie --save --no-optional. Open package.json and add this into the root object:

"scripts": {
  "start": "hoodie"
}

Add a public/index.html file that has <script src="/hoodie/client.js"></script> in it, then start the app with npm start -- --inMemory

Now, open the Hoodie app in an incognito browser tab and set up some listeners in the dev console, to observe that regular store works as expected:

hoodie.store.on('add', function(person){
  console.log('add')
})

hoodie.store.on('remove', function(person){
  console.log('remove')
})

hoodie.store.on('update', function(person){
  console.log('update')
})

hoodie.store.on('change', function(event, person){
  console.log('change ',event)
})

Then add a person:

hoodie.store().add({
   type: 'person',
   name: 'knut'
})

This logs add and change add, as expected.

Now reload the page, then do the same with a scoped store:

hoodie.store('person').on('add', function(person){
  console.log('add ')
})

hoodie.store('person').on('remove', function(person){
  console.log('remove')
})

hoodie.store('person').on('update', function(person){
  console.log('update')
})

hoodie.store('person').on('change', function(event, person){
  console.log('change ',event)
})

hoodie.store('person').add({
   name: 'horst'
})

This logs add, update, and change update, when it should log add and change add.

Here's the serious bit:

If you now hoodie.store('person').removeAll(), no remove event is fired at all, only update and change update. If you reload the page and do everything again, but register remove before add, you'll get remove, update, and change update whenever you remove something, but no add event when you add a person, only an update.

@sebbel
Copy link
Contributor

sebbel commented Apr 12, 2016

I could reproduce the error.

@gr2m
Copy link
Member

gr2m commented Apr 12, 2016

@sebbel can you try to create a test reproducing the error? Alex will be out until next week

@sebbel
Copy link
Contributor

sebbel commented Apr 12, 2016

I'd be happy to.

@gr2m
Copy link
Member

gr2m commented May 20, 2016

I wasn’t able to reproduce last bit:

If you reload the page and do everything again, but register remove before add, you'll get remove, update, and change update whenever you remove something, but no add event when you add a person, only an update.

When I run

hoodie.store('person').on('remove', function(person){
  console.log('remove')
})

hoodie.store('person').on('add', function(person){
  console.log('add ')
})

hoodie.store('person').on('update', function(person){
  console.log('update')
})

hoodie.store('person').on('change', function(event, person){
  console.log('change ',event)
})

hoodie.store('person').add({
   name: 'horst'
})

.then(function () {
  hoodie.store('person').removeAll()
}); 

I get this in my console log

update
change update
update
change update

But no remove log

@courajs
Copy link

courajs commented May 21, 2016

I did notice I was getting updates instead of removes, and had a small test repro here

@gr2m
Copy link
Member

gr2m commented Dec 26, 2016

please see the discussion on the .type property and the hoodie.store(scope) API: hoodiehq/discussion#106

@gr2m
Copy link
Member

gr2m commented Apr 26, 2017

this should be resolved now with hoodie.store.withIdPrefix()

@gr2m gr2m closed this as completed Apr 26, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants