|
| 1 | +/// <reference path="../typings/main.d.ts" /> |
| 2 | + |
| 3 | +import ApolloClient from 'apollo-client'; |
| 4 | + |
| 5 | +import { |
| 6 | + isEqual, |
| 7 | + noop, |
| 8 | + forIn, |
| 9 | +} from 'lodash'; |
| 10 | + |
| 11 | +export declare interface ApolloOptionsQueries { |
| 12 | + context: any; |
| 13 | +}; |
| 14 | + |
| 15 | +export declare interface ApolloOptionsMutations { |
| 16 | + context: any; |
| 17 | +}; |
| 18 | + |
| 19 | +export declare interface ApolloOptions { |
| 20 | + client: ApolloClient; |
| 21 | + queries?(opts: ApolloOptionsQueries): any; |
| 22 | + mutations?(opts: ApolloOptionsMutations): any; |
| 23 | +}; |
| 24 | + |
| 25 | +export function Apollo({ |
| 26 | + client, |
| 27 | + queries, |
| 28 | + mutations, |
| 29 | +}: ApolloOptions) { |
| 30 | + if (!(client instanceof ApolloClient)) { |
| 31 | + throw new Error('Client must be a ApolloClient instance'); |
| 32 | + } |
| 33 | + |
| 34 | + const { watchQuery } = client; |
| 35 | + |
| 36 | + // noop by default |
| 37 | + queries = queries || noop; |
| 38 | + mutations = mutations || noop; |
| 39 | + |
| 40 | + // holds latest values to track changes |
| 41 | + const lastQueryVariables = {}; |
| 42 | + |
| 43 | + return (sourceTarget: any) => { |
| 44 | + const target = sourceTarget; |
| 45 | + |
| 46 | + const oldHooks = {}; |
| 47 | + const hooks = { |
| 48 | + /** |
| 49 | + * Initialize the component |
| 50 | + * after Angular initializes the data-bound input properties. |
| 51 | + */ |
| 52 | + ngOnInit() { |
| 53 | + // use component's context |
| 54 | + const component = this; |
| 55 | + handleQueries(component, (key, { query, variables }) => { |
| 56 | + assign(component, key, { query, variables }); |
| 57 | + }); |
| 58 | + }, |
| 59 | + /** |
| 60 | + * Detect and act upon changes that Angular can or won't detect on its own. |
| 61 | + * Called every change detection run. |
| 62 | + */ |
| 63 | + ngDoCheck() { |
| 64 | + // use component's context |
| 65 | + const component = this; |
| 66 | + handleQueries(component, (queryName, { query, variables }) => { |
| 67 | + // check if query needs to be rerun |
| 68 | + if (!equalVariablesOf(queryName, variables)) { |
| 69 | + assign(component, queryName, { query, variables }); |
| 70 | + } |
| 71 | + }); |
| 72 | + }, |
| 73 | + }; |
| 74 | + |
| 75 | + // attach hooks |
| 76 | + forIn(hooks, (hook, name) => { |
| 77 | + wrapPrototype(name, hook); |
| 78 | + }); |
| 79 | + |
| 80 | + /** |
| 81 | + * Gets the result of the `queries` method |
| 82 | + * from decorator's options with component's context to compile variables. |
| 83 | + * |
| 84 | + * Then goes through all defined queries and calls a `touch` function. |
| 85 | + * |
| 86 | + * @param {any} component Component's context |
| 87 | + * @param {Function} touch Receives name and options |
| 88 | + */ |
| 89 | + function handleQueries(component: any, touch: Function) { |
| 90 | + forIn(queries(component), (opts: any, queryName: string) => { |
| 91 | + touch(queryName, opts); |
| 92 | + }); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Assings WatchQueryHandle to component |
| 97 | + * |
| 98 | + * @param {any} component Component's context |
| 99 | + * @param {string} queryName Query's name |
| 100 | + * @param {Object} options Query's options |
| 101 | + */ |
| 102 | + function assign(component: any, queryName: string, { query, variables }) { |
| 103 | + // save variables so they can be used in futher comparasion |
| 104 | + lastQueryVariables[queryName] = variables; |
| 105 | + // assign to component's context |
| 106 | + component[queryName] = watchQuery({ query, variables }); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Compares current variables with previous ones. |
| 111 | + * |
| 112 | + * @param {string} queryName Query's name |
| 113 | + * @param {any} variables current variables |
| 114 | + * @return {boolean} comparasion result |
| 115 | + */ |
| 116 | + function equalVariablesOf(queryName: string, variables: any): boolean { |
| 117 | + return isEqual(lastQueryVariables[queryName], variables); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Creates a new prototype method which is a wrapper function |
| 122 | + * that calls new function before old one. |
| 123 | + * |
| 124 | + * @param {string} name [description] |
| 125 | + * @param {Function} func [description] |
| 126 | + */ |
| 127 | + function wrapPrototype(name: string, func: Function) { |
| 128 | + oldHooks[name] = sourceTarget.prototype[name]; |
| 129 | + // create a wrapper |
| 130 | + target.prototype[name] = function(...args) { |
| 131 | + // to call a new prototype method |
| 132 | + func.apply(this, args); |
| 133 | + |
| 134 | + // call the old prototype method |
| 135 | + if (oldHooks[name]) { |
| 136 | + oldHooks[name].apply(this, args); |
| 137 | + } |
| 138 | + }; |
| 139 | + } |
| 140 | + |
| 141 | + // return decorated target |
| 142 | + return target; |
| 143 | + }; |
| 144 | +} |
0 commit comments