github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/ui/app/utils/properties/watch.js (about) 1 import Ember from 'ember'; 2 import { get } from '@ember/object'; 3 import { assert } from '@ember/debug'; 4 import RSVP from 'rsvp'; 5 import { task } from 'ember-concurrency'; 6 import { AbortController } from 'fetch'; 7 import wait from 'nomad-ui/utils/wait'; 8 import Watchable from 'nomad-ui/adapters/watchable'; 9 import config from 'nomad-ui/config/environment'; 10 11 const isEnabled = config.APP.blockingQueries !== false; 12 13 export function watchRecord(modelName) { 14 return task(function*(id, throttle = 2000) { 15 assert( 16 'To watch a record, the record adapter MUST extend Watchable', 17 this.store.adapterFor(modelName) instanceof Watchable 18 ); 19 if (typeof id === 'object') { 20 id = get(id, 'id'); 21 } 22 while (isEnabled && !Ember.testing) { 23 const controller = new AbortController(); 24 try { 25 yield RSVP.all([ 26 this.store.findRecord(modelName, id, { 27 reload: true, 28 adapterOptions: { watch: true, abortController: controller }, 29 }), 30 wait(throttle), 31 ]); 32 } catch (e) { 33 yield e; 34 break; 35 } finally { 36 controller.abort(); 37 } 38 } 39 }).drop(); 40 } 41 42 export function watchRelationship(relationshipName) { 43 return task(function*(model, throttle = 2000) { 44 assert( 45 'To watch a relationship, the adapter of the model provided to the watchRelationship task MUST extend Watchable', 46 this.store.adapterFor(model.constructor.modelName) instanceof Watchable 47 ); 48 while (isEnabled && !Ember.testing) { 49 const controller = new AbortController(); 50 try { 51 yield RSVP.all([ 52 this.store 53 .adapterFor(model.constructor.modelName) 54 .reloadRelationship(model, relationshipName, { 55 watch: true, 56 abortController: controller, 57 }), 58 wait(throttle), 59 ]); 60 } catch (e) { 61 yield e; 62 break; 63 } finally { 64 controller.abort(); 65 } 66 } 67 }).drop(); 68 } 69 70 export function watchAll(modelName) { 71 return task(function*(throttle = 2000) { 72 assert( 73 'To watch all, the respective adapter MUST extend Watchable', 74 this.store.adapterFor(modelName) instanceof Watchable 75 ); 76 while (isEnabled && !Ember.testing) { 77 const controller = new AbortController(); 78 try { 79 yield RSVP.all([ 80 this.store.findAll(modelName, { 81 reload: true, 82 adapterOptions: { watch: true, abortController: controller }, 83 }), 84 wait(throttle), 85 ]); 86 } catch (e) { 87 yield e; 88 break; 89 } finally { 90 controller.abort(); 91 } 92 } 93 }).drop(); 94 } 95 96 export function watchQuery(modelName) { 97 return task(function*(params, throttle = 10000) { 98 assert( 99 'To watch a query, the adapter for the type being queried MUST extend Watchable', 100 this.store.adapterFor(modelName) instanceof Watchable 101 ); 102 while (isEnabled && !Ember.testing) { 103 const controller = new AbortController(); 104 try { 105 yield RSVP.all([ 106 this.store.query(modelName, params, { 107 reload: true, 108 adapterOptions: { watch: true, abortController: controller }, 109 }), 110 wait(throttle), 111 ]); 112 } catch (e) { 113 yield e; 114 break; 115 } finally { 116 controller.abort(); 117 } 118 } 119 }).drop(); 120 }