Skip to content

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion app/services/fastboot.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/* global FastBoot */
import Ember from "ember";

const { deprecate, computed, get } = Ember;
const { deprecate, computed, get, RSVP } = Ember;
const { deprecatingAlias, readOnly } = computed;
const { Promise } = RSVP;

const RequestObject = Ember.Object.extend({
init() {
Expand Down Expand Up @@ -57,6 +58,15 @@ const Shoebox = Ember.Object.extend({
this.set(key, shoeboxItem);

return shoeboxItem;
},

has(key) {
if (this.get('fastboot.isFastBoot')) {
let shoebox = this.get('fastboot._fastbootInfo.shoebox');
return shoebox && shoebox.hasOwnProperty(key);
} else {
return !!document.querySelector(`#shoebox-${key}`);
Copy link
Contributor

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?

Copy link
Contributor

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

Copy link
Member

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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah so big datasets

}
}
});

Expand Down Expand Up @@ -95,5 +105,21 @@ export default Ember.Service.extend({
deferRendering(promise) {
Ember.assert('deferRendering requires a promise or thennable object', typeof promise.then === 'function');
this._fastbootInfo.deferRendering(promise);
},

withShoebox(key, fn) {
return new Promise((resolve, reject) => {
let shoebox = this.get('shoebox');
if (shoebox.has(key)) {
resolve(shoebox.retrieve(key));
return;
}
Promise.resolve(fn()).then(value => {
Copy link
Contributor

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)

if (this.get('isFastBoot')) {
shoebox.put(key, value);
}
return value;
}).then(resolve, reject);
});
}
});