-
Notifications
You must be signed in to change notification settings - Fork 159
Better shoebox API #250
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
base: master
Are you sure you want to change the base?
Better shoebox API #250
Conversation
This adds a new higher-level API for the shoebox. ```js model() { let fastboot = this.get('fastboot'); return RSVP.hash({ // In this example, we generate a random number once on the server, // and then the client app will receive the same number. random: fastboot.withShoebox('my-random-number', () => Math.random()) }); } ``` `withShoebox` acts as a memoization service. On the server, the given callback will run and generate a value, which will be saved in the shoebox and then returned. On the client, the callback will not run if the value is already present in the shoebox. `withShoebox` returns a promise, because the callback is allowed to return a promise.
resolve(shoebox.retrieve(key)); | ||
return; | ||
} | ||
Promise.resolve(fn()).then(value => { |
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.
resolve(Promise.resolve(fn())...
not then(resolve, reject)
I love this API @ef4 |
let shoebox = this.get('fastboot._fastbootInfo.shoebox'); | ||
return shoebox && shoebox.hasOwnProperty(key); | ||
} else { | ||
return !!document.querySelector(`#shoebox-${key}`); |
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.
if shoebox is for random data (that we likely don't want displayed) should we use meta or script tags?
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 could also make them addressible via import
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.
Meta tags have limits on length that differ from browser to browser @stefanpenner
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.
ah so big datasets
So this immediately solves some problems I'm running into. What else (besides tests) needs to be done to get this in? |
It seems like the problem I see a common use case where you only want the client to access the shoebox data one time (on initial payload) and then use the callback every subsequent time the data is requested. Maybe it would make sense to add an argument flag or a separate API that would implement this idea of "single use" shoebox data? |
@steveszc it's my understanding that |
@BenKingBBC That hasn't been my experience using It does look like it caches the value after it is retrieved from the DOM, but subsequent calls to shoebox.retrieve for a given key would just get the cached value instead of going back to the DOM shoebox. I'm happy to open a separate issue if there is consensus that "single-use" use case for shoebox values should be handled by |
@ef4 We discussed as a team, and like and approve the API, would you have bandwidth to write tests so that we can land this? |
this would be really awesome! is there anything else blocking this except tests? or latest |
resolve(shoebox.retrieve(key)); | ||
return; | ||
} | ||
resolve(Promise.resolve(fn()).then(value => { |
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.
do you need wrapping Promise.resolve
? I think it will work without it too
Fix invalid syntax with deferRendering.
This is a good one to work on and get it in. I ll pick this one up if @ef4 is not working actively on it. |
This adds a new higher-level API for the shoebox.
withShoebox
acts as a memoization service. On the server, the given callback will run and generate a value, which will be saved in the shoebox and then returned. On the client, the callback will not run if the value is already present in the shoebox.withShoebox
returns a promise, because the callback is allowed to return a promise.