-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathhooks.ts
128 lines (119 loc) · 3.3 KB
/
hooks.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/**
* hook-functions that can be extended by the plugin
*/
export const HOOKS = {
/**
* Runs before a plugin is added.
* Use this to block the usage of non-compatible plugins.
*/
preAddRxPlugin: [],
/**
* functions that run before the database is created
*/
preCreateRxDatabase: [],
/**
* runs after the database is created and prepared
* but before the instance is returned to the user
* @async
*/
createRxDatabase: [],
preCreateRxCollection: [],
createRxCollection: [],
createRxState: [],
/**
* runs at the end of the close-process of a collection
* @async
*/
postCloseRxCollection: [],
/**
* Runs after a collection is removed.
* @async
*/
postRemoveRxCollection: [],
/**
* functions that get the json-schema as input
* to do additionally checks/manipulation
*/
preCreateRxSchema: [],
/**
* functions that run after the RxSchema is created
* gets RxSchema as attribute
*/
createRxSchema: [],
prePrepareRxQuery: [],
preCreateRxQuery: [],
/**
* Runs before a query is send to the
* prepareQuery function of the storage engine.
*/
prePrepareQuery: [],
createRxDocument: [],
/**
* runs after a RxDocument is created,
* cannot be async
*/
postCreateRxDocument: [],
/**
* Runs before a RxStorageInstance is created
* gets the params of createStorageInstance()
* as attribute so you can manipulate them.
* Notice that you have to clone stuff before mutating the inputs.
*/
preCreateRxStorageInstance: [],
preStorageWrite: [],
/**
* runs on the document-data before the document is migrated
* {
* doc: Object, // original doc-data
* migrated: // migrated doc-data after run through migration-strategies
* }
*/
preMigrateDocument: [],
/**
* runs after the migration of a document has been done
*/
postMigrateDocument: [],
/**
* runs at the beginning of the close-process of a database
*/
preCloseRxDatabase: [],
/**
* runs after a database has been removed
* @async
*/
postRemoveRxDatabase: [],
postCleanup: [],
/**
* runs before the replication writes the rows to master
* but before the rows have been modified
* @async
*/
preReplicationMasterWrite: [],
/**
* runs after the replication has been sent to the server
* but before the new documents have been handled
* @async
*/
preReplicationMasterWriteDocumentsHandle: [],
};
export function runPluginHooks(hookKey: keyof typeof HOOKS, obj: any) {
if (HOOKS[hookKey].length > 0) {
HOOKS[hookKey].forEach(fun => (fun as any)(obj));
}
}
/**
* We do intentionally not run the hooks in parallel
* because that makes stuff unpredictable and we use runAsyncPluginHooks()
* only in places that are not that relevant for performance.
*/
export async function runAsyncPluginHooks(hookKey: keyof typeof HOOKS, obj: any): Promise<any> {
for (const fn of HOOKS[hookKey]) {
await (fn as any)(obj);
}
}
/**
* used in tests to remove hooks
*/
export function _clearHook(type: keyof typeof HOOKS, fun: Function) {
HOOKS[type] = HOOKS[type].filter(h => h !== fun);
}