| 49 | } |
| 50 | |
| 51 | getAny<T extends IDataCollection>(endpoint: string, accessor: IDataAccessor, |
| 52 | descriptor: IAnyDataDescriptor, query: Query, |
| 53 | subscribe: boolean, collectionFactory: () => T) { |
| 54 | // subscribe for changes if 'subscribe' is true |
| 55 | if (subscribe && !accessor) { |
| 56 | console.warn("subscribe call should be done after DataClient.open() for " + |
| 57 | "maintaining trace of observers"); |
| 58 | subscribe = false; |
| 59 | } |
| 60 | |
| 61 | const collection = collectionFactory(); |
| 62 | |
| 63 | const subscribePromise = subscribe ? collection.subscribe() : Promise.resolve(); |
| 64 | |
| 65 | subscribePromise.then(() => |
| 66 | // get the data from the rest api |
| 67 | this.restClient.get(endpoint, query).then((response) => { |
| 68 | const datalist = response[descriptor.restArrayField]; |
| 69 | // the response should always be an array |
| 70 | if (!Array.isArray(datalist)) { |
| 71 | console.error(`${datalist} is not an array when retrieving ${descriptor.restArrayField}`); |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | // fill up the collection with initial data |
| 76 | collection.initial(datalist); |
| 77 | }) |
| 78 | ); |
| 79 | |
| 80 | return collection; |
| 81 | } |
| 82 | |
| 83 | control(endpoint: string, method: string, params: ControlParams) { |
| 84 | return this.restClient.post(endpoint, { |