Skip to content
This repository
Browse code

added support for expiring resource remotely

  • Loading branch information...
commit 8cb057caee21f32e7f6e44472ec8c7d0a46b67d6 1 parent c2b5f5e
Shajith Chacko shajith authored
2  AssetFile
@@ -2,7 +2,7 @@ require 'rake-pipeline-web-filters'
2 2
3 3 output "dist"
4 4
5   -files = ["vendor/lru.js", "base.js", "identity_map.js", "ember-resource.js"]
  5 +files = ["vendor/lru.js", "base.js", "remote_expiry.js", "identity_map.js", "ember-resource.js"]
6 6
7 7 input "src" do
8 8 match "{#{files.join(',')}}" do
52 spec/javascripts/remoteExpirySpec.js
... ... @@ -0,0 +1,52 @@
  1 +/*globals Ember, jasmine */
  2 +describe('remote expiry', function() {
  3 + var Resource, resource;
  4 + describe('on a resource with a remote expiry key', function() {
  5 + beforeEach(function() {
  6 + Resource = Ember.Resource.define().extend({
  7 + remoteExpiryKey: "foo"
  8 + });
  9 + resource = Resource.create();
  10 + spyOn(resource, 'subscribeForExpiry');
  11 + });
  12 +
  13 + it('should subscribe for expiry on fetch', function() {
  14 + Ember.sendEvent(resource, 'didFetch');
  15 + Ember.run.sync();
  16 + expect(resource.subscribeForExpiry).toHaveBeenCalled();
  17 + });
  18 + });
  19 +
  20 +
  21 + describe('on a resource with no remote expiry key', function() {
  22 + beforeEach(function() {
  23 + Resource = Ember.Resource.define().extend();
  24 + resource = Resource.create();
  25 + spyOn(resource, 'subscribeForExpiry');
  26 + });
  27 +
  28 + it('should not subscribe for expiry on fetch', function() {
  29 + Ember.sendEvent(resource, 'didFetch');
  30 + Ember.run.sync();
  31 + expect(resource.subscribeForExpiry).not.toHaveBeenCalled();
  32 + });
  33 + });
  34 +
  35 + describe("subscribing for expiry", function() {
  36 + beforeEach(function() {
  37 + Resource = Ember.Resource.define().extend({
  38 + remoteExpiryKey: "foo"
  39 + });
  40 + resource = Resource.create();
  41 + spyOn(Ember.Resource.PushTransport, 'subscribe');
  42 + Ember.sendEvent(resource, 'didFetch');
  43 + Ember.run.sync();
  44 + });
  45 +
  46 + it('should use the PushTransport', function() {
  47 + expect(Ember.Resource.PushTransport.subscribe).toHaveBeenCalledWith("foo", jasmine.any(Function));
  48 + });
  49 +
  50 + });
  51 +
  52 +});
4 src/ember-resource.js
@@ -1035,7 +1035,7 @@
1035 1035 var schema = expandSchema(options.schema);
1036 1036 schema = mergeSchemas(schema, this.schema);
1037 1037
1038   - var klass = this.extend(createSchemaProperties(schema));
  1038 + var klass = this.extend(createSchemaProperties(schema), Ember.Resource.RemoteExpiry);
1039 1039
1040 1040 var classOptions = {
1041 1041 schema: schema
@@ -1102,7 +1102,7 @@
1102 1102 }
1103 1103 }, Ember.Resource.Lifecycle.classMixin);
1104 1104
1105   - Ember.ResourceCollection = Ember.ArrayProxy.extend({
  1105 + Ember.ResourceCollection = Ember.ArrayProxy.extend(Ember.Resource.RemoteExpiry, {
1106 1106 isEmberResourceCollection: true,
1107 1107 type: Ember.required(),
1108 1108
57 src/remote_expiry.js
... ... @@ -0,0 +1,57 @@
  1 +(function(exports) {
  2 +
  3 + var Ember = exports.Ember, NullTransport = {
  4 + subscribe: Ember.K,
  5 + unsubscribe: Ember.K
  6 + };
  7 +
  8 + Ember.Resource.PushTransport = NullTransport;
  9 +
  10 + var RemoteExpiry = Ember.Mixin.create({
  11 + init: function() {
  12 + var ret = this._super(),
  13 + self = this,
  14 + remoteExpiryScope = this.get('remoteExpiryKey');
  15 +
  16 + if(!this.get('remoteExpiryKey')) { return; }
  17 +
  18 + this.set('_subscribedForExpiry', false);
  19 +
  20 + if(!remoteExpiryScope) {
  21 + return ret;
  22 + }
  23 +
  24 + Ember.addListener(this, 'didFetch', this, function() {
  25 + self.subscribeForExpiry();
  26 + });
  27 +
  28 + return ret;
  29 + },
  30 +
  31 + subscribeForExpiry: function() {
  32 + var remoteExpiryScope = this.get('remoteExpiryKey'),
  33 + updatedAt,
  34 + self = this;
  35 +
  36 + if(!remoteExpiryScope) {
  37 + return;
  38 + }
  39 +
  40 + Ember.Resource.PushTransport.subscribe(remoteExpiryScope, function(message) {
  41 + updatedAt = new Date(message.updated_at);
  42 + if(self.stale(updatedAt)) {
  43 + self.set('updatedAt', updatedAt);
  44 + self.expire();
  45 + }
  46 + });
  47 +
  48 + this.set('_subscribedForExpiry', true);
  49 + },
  50 +
  51 + stale: function(updatedAt) {
  52 + return !this.get('updatedAt') || (+this.get('updatedAt') < +updatedAt);
  53 + }
  54 + });
  55 +
  56 + Ember.Resource.RemoteExpiry = RemoteExpiry;
  57 +}(this));

0 comments on commit 8cb057c

Please sign in to comment.
Something went wrong with that request. Please try again.