CoreManager.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. /*:: import type { AttributeMap, ObjectCache, OpsMap, State } from './ObjectStateMutations';*/
  2. /*:: import type ParseFile from './ParseFile';*/
  3. /*:: import type { FileSource } from './ParseFile';*/
  4. /*:: import type { Op } from './ParseOp';*/
  5. /*:: import type ParseObject from './ParseObject';*/
  6. /*:: import type { QueryJSON } from './ParseQuery';*/
  7. /*:: import type ParseUser from './ParseUser';*/
  8. /*:: import type { AuthData } from './ParseUser';*/
  9. /*:: import type { PushData } from './Push';*/
  10. /*:: import type { RequestOptions, FullOptions } from './RESTController';*/
  11. /*:: type AnalyticsController = {
  12. track: (name: string, dimensions: { [key: string]: string }) => Promise;
  13. };*/
  14. /*:: type CloudController = {
  15. run: (name: string, data: mixed, options: RequestOptions) => Promise;
  16. getJobsData: (options: RequestOptions) => Promise;
  17. startJob: (name: string, data: mixed, options: RequestOptions) => Promise;
  18. };*/
  19. /*:: type ConfigController = {
  20. current: () => Promise;
  21. get: () => Promise;
  22. save: (attrs: { [key: string]: any }) => Promise;
  23. };*/
  24. /*:: type FileController = {
  25. saveFile: (name: string, source: FileSource, options: FullOptions) => Promise;
  26. saveBase64: (name: string, source: FileSource, options: FullOptions) => Promise;
  27. download: (uri: string) => Promise;
  28. };*/
  29. /*:: type InstallationController = {
  30. currentInstallationId: () => Promise;
  31. };*/
  32. /*:: type ObjectController = {
  33. fetch: (object: ParseObject | Array<ParseObject>, forceFetch: boolean, options: RequestOptions) => Promise;
  34. save: (object: ParseObject | Array<ParseObject | ParseFile>, options: RequestOptions) => Promise;
  35. destroy: (object: ParseObject | Array<ParseObject>, options: RequestOptions) => Promise;
  36. };*/
  37. /*:: type ObjectStateController = {
  38. getState: (obj: any) => ?State;
  39. initializeState: (obj: any, initial?: State) => State;
  40. removeState: (obj: any) => ?State;
  41. getServerData: (obj: any) => AttributeMap;
  42. setServerData: (obj: any, attributes: AttributeMap) => void;
  43. getPendingOps: (obj: any) => Array<OpsMap>;
  44. setPendingOp: (obj: any, attr: string, op: ?Op) => void;
  45. pushPendingState: (obj: any) => void;
  46. popPendingState: (obj: any) => OpsMap;
  47. mergeFirstPendingState: (obj: any) => void;
  48. getObjectCache: (obj: any) => ObjectCache;
  49. estimateAttribute: (obj: any, attr: string) => mixed;
  50. estimateAttributes: (obj: any) => AttributeMap;
  51. commitServerChanges: (obj: any, changes: AttributeMap) => void;
  52. enqueueTask: (obj: any, task: () => Promise) => Promise;
  53. clearAllState: () => void;
  54. duplicateState: (source: any, dest: any) => void;
  55. };*/
  56. /*:: type PushController = {
  57. send: (data: PushData, options: RequestOptions) => Promise;
  58. };*/
  59. /*:: type QueryController = {
  60. find: (className: string, params: QueryJSON, options: RequestOptions) => Promise;
  61. aggregate: (className: string, params: any, options: RequestOptions) => Promise;
  62. };*/
  63. /*:: type RESTController = {
  64. request: (method: string, path: string, data: mixed, options: RequestOptions) => Promise;
  65. ajax: (method: string, url: string, data: any, headers?: any, options: FullOptions) => Promise;
  66. };*/
  67. /*:: type SchemaController = {
  68. purge: (className: string) => Promise;
  69. get: (className: string, options: RequestOptions) => Promise;
  70. delete: (className: string, options: RequestOptions) => Promise;
  71. create: (className: string, params: any, options: RequestOptions) => Promise;
  72. update: (className: string, params: any, options: RequestOptions) => Promise;
  73. send(className: string, method: string, params: any, options: RequestOptions): Promise;
  74. };*/
  75. /*:: type SessionController = {
  76. getSession: (token: RequestOptions) => Promise;
  77. };*/
  78. /*:: type StorageController = {
  79. async: 0;
  80. getItem: (path: string) => ?string;
  81. setItem: (path: string, value: string) => void;
  82. removeItem: (path: string) => void;
  83. getItemAsync?: (path: string) => Promise;
  84. setItemAsync?: (path: string, value: string) => Promise;
  85. removeItemAsync?: (path: string) => Promise;
  86. clear: () => void;
  87. } | {
  88. async: 1;
  89. getItem?: (path: string) => ?string;
  90. setItem?: (path: string, value: string) => void;
  91. removeItem?: (path: string) => void;
  92. getItemAsync: (path: string) => Promise;
  93. setItemAsync: (path: string, value: string) => Promise;
  94. removeItemAsync: (path: string) => Promise;
  95. clear: () => void;
  96. };*/
  97. /*:: type LocalDatastoreController = {
  98. fromPinWithName: (name: string) => ?any;
  99. pinWithName: (name: string, objects: any) => void;
  100. unPinWithName: (name: string) => void;
  101. getAllContents: () => ?any;
  102. clear: () => void;
  103. };*/
  104. /*:: type UserController = {
  105. setCurrentUser: (user: ParseUser) => Promise;
  106. currentUser: () => ?ParseUser;
  107. currentUserAsync: () => Promise;
  108. signUp: (user: ParseUser, attrs: AttributeMap, options: RequestOptions) => Promise;
  109. logIn: (user: ParseUser, options: RequestOptions) => Promise;
  110. become: (options: RequestOptions) => Promise;
  111. hydrate: (userJSON: AttributeMap) => Promise;
  112. logOut: (options: RequestOptions) => Promise;
  113. me: (options: RequestOptions) => Promise;
  114. requestPasswordReset: (email: string, options: RequestOptions) => Promise;
  115. updateUserOnDisk: (user: ParseUser) => Promise;
  116. upgradeToRevocableSession: (user: ParseUser, options: RequestOptions) => Promise;
  117. linkWith: (user: ParseUser, authData: AuthData) => Promise;
  118. removeUserFromDisk: () => Promise;
  119. };*/
  120. /*:: type HooksController = {
  121. get: (type: string, functionName?: string, triggerName?: string) => Promise;
  122. create: (hook: mixed) => Promise;
  123. delete: (hook: mixed) => Promise;
  124. update: (hook: mixed) => Promise;
  125. send: (method: string, path: string, body?: mixed) => Promise;
  126. };*/
  127. /*:: type WebSocketController = {
  128. onopen: () => void;
  129. onmessage: (message: any) => void;
  130. onclose: () => void;
  131. onerror: (error: any) => void;
  132. send: (data: any) => void;
  133. close: () => void;
  134. }*/
  135. /*:: type Config = {
  136. AnalyticsController?: AnalyticsController,
  137. CloudController?: CloudController,
  138. ConfigController?: ConfigController,
  139. FileController?: FileController,
  140. InstallationController?: InstallationController,
  141. ObjectController?: ObjectController,
  142. ObjectStateController?: ObjectStateController,
  143. PushController?: PushController,
  144. QueryController?: QueryController,
  145. RESTController?: RESTController,
  146. SchemaController?: SchemaController,
  147. SessionController?: SessionController,
  148. StorageController?: StorageController,
  149. LocalDatastoreController?: LocalDatastoreController,
  150. UserController?: UserController,
  151. HooksController?: HooksController,
  152. WebSocketController?: WebSocketController,
  153. };*/
  154. /*
  155. * Copyright (c) 2015-present, Parse, LLC.
  156. * All rights reserved.
  157. *
  158. * This source code is licensed under the BSD-style license found in the
  159. * LICENSE file in the root directory of this source tree. An additional grant
  160. * of patent rights can be found in the PATENTS file in the same directory.
  161. *
  162. * @flow
  163. */
  164. const config
  165. /*: Config & { [key: string]: mixed }*/
  166. = {
  167. // Defaults
  168. IS_NODE: typeof process !== 'undefined' && !!process.versions && !!process.versions.node && !process.versions.electron,
  169. REQUEST_ATTEMPT_LIMIT: 5,
  170. SERVER_URL: 'https://api.parse.com/1',
  171. SERVER_AUTH_TYPE: null,
  172. SERVER_AUTH_TOKEN: null,
  173. LIVEQUERY_SERVER_URL: null,
  174. VERSION: 'js' + "2.6.0",
  175. APPLICATION_ID: null,
  176. JAVASCRIPT_KEY: null,
  177. MASTER_KEY: null,
  178. USE_MASTER_KEY: false,
  179. PERFORM_USER_REWRITE: true,
  180. FORCE_REVOCABLE_SESSION: false
  181. };
  182. function requireMethods(name
  183. /*: string*/
  184. , methods
  185. /*: Array<string>*/
  186. , controller
  187. /*: any*/
  188. ) {
  189. methods.forEach(func => {
  190. if (typeof controller[func] !== 'function') {
  191. throw new Error(`${name} must implement ${func}()`);
  192. }
  193. });
  194. }
  195. module.exports = {
  196. get: function (key
  197. /*: string*/
  198. )
  199. /*: any*/
  200. {
  201. if (config.hasOwnProperty(key)) {
  202. return config[key];
  203. }
  204. throw new Error('Configuration key not found: ' + key);
  205. },
  206. set: function (key
  207. /*: string*/
  208. , value
  209. /*: any*/
  210. )
  211. /*: void*/
  212. {
  213. config[key] = value;
  214. },
  215. /* Specialized Controller Setters/Getters */
  216. setAnalyticsController(controller
  217. /*: AnalyticsController*/
  218. ) {
  219. requireMethods('AnalyticsController', ['track'], controller);
  220. config['AnalyticsController'] = controller;
  221. },
  222. getAnalyticsController()
  223. /*: AnalyticsController*/
  224. {
  225. return config['AnalyticsController'];
  226. },
  227. setCloudController(controller
  228. /*: CloudController*/
  229. ) {
  230. requireMethods('CloudController', ['run', 'getJobsData', 'startJob'], controller);
  231. config['CloudController'] = controller;
  232. },
  233. getCloudController()
  234. /*: CloudController*/
  235. {
  236. return config['CloudController'];
  237. },
  238. setConfigController(controller
  239. /*: ConfigController*/
  240. ) {
  241. requireMethods('ConfigController', ['current', 'get', 'save'], controller);
  242. config['ConfigController'] = controller;
  243. },
  244. getConfigController()
  245. /*: ConfigController*/
  246. {
  247. return config['ConfigController'];
  248. },
  249. setFileController(controller
  250. /*: FileController*/
  251. ) {
  252. requireMethods('FileController', ['saveFile', 'saveBase64'], controller);
  253. config['FileController'] = controller;
  254. },
  255. getFileController()
  256. /*: FileController*/
  257. {
  258. return config['FileController'];
  259. },
  260. setInstallationController(controller
  261. /*: InstallationController*/
  262. ) {
  263. requireMethods('InstallationController', ['currentInstallationId'], controller);
  264. config['InstallationController'] = controller;
  265. },
  266. getInstallationController()
  267. /*: InstallationController*/
  268. {
  269. return config['InstallationController'];
  270. },
  271. setObjectController(controller
  272. /*: ObjectController*/
  273. ) {
  274. requireMethods('ObjectController', ['save', 'fetch', 'destroy'], controller);
  275. config['ObjectController'] = controller;
  276. },
  277. getObjectController()
  278. /*: ObjectController*/
  279. {
  280. return config['ObjectController'];
  281. },
  282. setObjectStateController(controller
  283. /*: ObjectStateController*/
  284. ) {
  285. requireMethods('ObjectStateController', ['getState', 'initializeState', 'removeState', 'getServerData', 'setServerData', 'getPendingOps', 'setPendingOp', 'pushPendingState', 'popPendingState', 'mergeFirstPendingState', 'getObjectCache', 'estimateAttribute', 'estimateAttributes', 'commitServerChanges', 'enqueueTask', 'clearAllState'], controller);
  286. config['ObjectStateController'] = controller;
  287. },
  288. getObjectStateController()
  289. /*: ObjectStateController*/
  290. {
  291. return config['ObjectStateController'];
  292. },
  293. setPushController(controller
  294. /*: PushController*/
  295. ) {
  296. requireMethods('PushController', ['send'], controller);
  297. config['PushController'] = controller;
  298. },
  299. getPushController()
  300. /*: PushController*/
  301. {
  302. return config['PushController'];
  303. },
  304. setQueryController(controller
  305. /*: QueryController*/
  306. ) {
  307. requireMethods('QueryController', ['find', 'aggregate'], controller);
  308. config['QueryController'] = controller;
  309. },
  310. getQueryController()
  311. /*: QueryController*/
  312. {
  313. return config['QueryController'];
  314. },
  315. setRESTController(controller
  316. /*: RESTController*/
  317. ) {
  318. requireMethods('RESTController', ['request', 'ajax'], controller);
  319. config['RESTController'] = controller;
  320. },
  321. getRESTController()
  322. /*: RESTController*/
  323. {
  324. return config['RESTController'];
  325. },
  326. setSchemaController(controller
  327. /*: SchemaController*/
  328. ) {
  329. requireMethods('SchemaController', ['get', 'create', 'update', 'delete', 'send', 'purge'], controller);
  330. config['SchemaController'] = controller;
  331. },
  332. getSchemaController()
  333. /*: SchemaController*/
  334. {
  335. return config['SchemaController'];
  336. },
  337. setSessionController(controller
  338. /*: SessionController*/
  339. ) {
  340. requireMethods('SessionController', ['getSession'], controller);
  341. config['SessionController'] = controller;
  342. },
  343. getSessionController()
  344. /*: SessionController*/
  345. {
  346. return config['SessionController'];
  347. },
  348. setStorageController(controller
  349. /*: StorageController*/
  350. ) {
  351. if (controller.async) {
  352. requireMethods('An async StorageController', ['getItemAsync', 'setItemAsync', 'removeItemAsync'], controller);
  353. } else {
  354. requireMethods('A synchronous StorageController', ['getItem', 'setItem', 'removeItem'], controller);
  355. }
  356. config['StorageController'] = controller;
  357. },
  358. setLocalDatastoreController(controller
  359. /*: LocalDatastoreController*/
  360. ) {
  361. requireMethods('LocalDatastoreController', ['pinWithName', 'fromPinWithName', 'unPinWithName', 'getAllContents', 'clear'], controller);
  362. config['LocalDatastoreController'] = controller;
  363. },
  364. getLocalDatastoreController()
  365. /*: LocalDatastoreController*/
  366. {
  367. return config['LocalDatastoreController'];
  368. },
  369. setLocalDatastore(store
  370. /*: any*/
  371. ) {
  372. config['LocalDatastore'] = store;
  373. },
  374. getLocalDatastore() {
  375. return config['LocalDatastore'];
  376. },
  377. getStorageController()
  378. /*: StorageController*/
  379. {
  380. return config['StorageController'];
  381. },
  382. setAsyncStorage(storage
  383. /*: any*/
  384. ) {
  385. config['AsyncStorage'] = storage;
  386. },
  387. getAsyncStorage() {
  388. return config['AsyncStorage'];
  389. },
  390. setWebSocketController(controller
  391. /*: WebSocketController*/
  392. ) {
  393. config['WebSocketController'] = controller;
  394. },
  395. getWebSocketController()
  396. /*: WebSocketController*/
  397. {
  398. return config['WebSocketController'];
  399. },
  400. setUserController(controller
  401. /*: UserController*/
  402. ) {
  403. requireMethods('UserController', ['setCurrentUser', 'currentUser', 'currentUserAsync', 'signUp', 'logIn', 'become', 'logOut', 'me', 'requestPasswordReset', 'upgradeToRevocableSession', 'linkWith'], controller);
  404. config['UserController'] = controller;
  405. },
  406. getUserController()
  407. /*: UserController*/
  408. {
  409. return config['UserController'];
  410. },
  411. setLiveQueryController(controller
  412. /*: any*/
  413. ) {
  414. requireMethods('LiveQueryController', ['setDefaultLiveQueryClient', 'getDefaultLiveQueryClient', '_clearCachedDefaultClient'], controller);
  415. config['LiveQueryController'] = controller;
  416. },
  417. getLiveQueryController()
  418. /*: any*/
  419. {
  420. return config['LiveQueryController'];
  421. },
  422. setHooksController(controller
  423. /*: HooksController*/
  424. ) {
  425. requireMethods('HooksController', ['create', 'get', 'update', 'remove'], controller);
  426. config['HooksController'] = controller;
  427. },
  428. getHooksController()
  429. /*: HooksController*/
  430. {
  431. return config['HooksController'];
  432. }
  433. };