github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/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, replace = false) { 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 replace, 58 }), 59 wait(throttle), 60 ]); 61 } catch (e) { 62 yield e; 63 break; 64 } finally { 65 controller.abort(); 66 } 67 } 68 }).drop(); 69 } 70 71 export function watchNonStoreRecords(modelName) { 72 return task(function* (model, asyncCallbackName, throttle = 5000) { 73 assert( 74 'To watch a non-store records, the adapter of the model provided to the watchNonStoreRecords task MUST extend Watchable', 75 this.store.adapterFor(modelName) instanceof Watchable 76 ); 77 while (isEnabled && !Ember.testing) { 78 const controller = new AbortController(); 79 try { 80 yield model[asyncCallbackName](); 81 yield wait(throttle); 82 } catch (e) { 83 yield e; 84 break; 85 } finally { 86 controller.abort(); 87 } 88 } 89 }).drop(); 90 } 91 92 export function watchAll(modelName) { 93 return task(function* (throttle = 2000) { 94 assert( 95 'To watch all, the respective adapter MUST extend Watchable', 96 this.store.adapterFor(modelName) instanceof Watchable 97 ); 98 while (isEnabled && !Ember.testing) { 99 const controller = new AbortController(); 100 try { 101 yield RSVP.all([ 102 this.store.findAll(modelName, { 103 reload: true, 104 adapterOptions: { watch: true, abortController: controller }, 105 }), 106 wait(throttle), 107 ]); 108 } catch (e) { 109 yield e; 110 break; 111 } finally { 112 controller.abort(); 113 } 114 } 115 }).drop(); 116 } 117 118 export function watchQuery(modelName) { 119 return task(function* (params, throttle = 10000) { 120 assert( 121 'To watch a query, the adapter for the type being queried MUST extend Watchable', 122 this.store.adapterFor(modelName) instanceof Watchable 123 ); 124 while (isEnabled && !Ember.testing) { 125 const controller = new AbortController(); 126 try { 127 yield RSVP.all([ 128 this.store.query(modelName, params, { 129 reload: true, 130 adapterOptions: { watch: true, abortController: controller }, 131 }), 132 wait(throttle), 133 ]); 134 } catch (e) { 135 yield e; 136 break; 137 } finally { 138 controller.abort(); 139 } 140 } 141 }).drop(); 142 }