-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
33 lines (27 loc) · 924 Bytes
/
index.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
import { createHash } from 'crypto';
import Io, { registerPlugins, runRegisterFunctions } from './io';
import { IoOptions } from './types';
let instances: {[key: string]: Io} = {};
const emptyHash = createHash('md5').update('').digest('hex');
function io(options?: IoOptions): Io {
const hash = createHash('md5').update(JSON.stringify(options || '')).digest('hex');
if (instances[hash]) {
return instances[hash];
}
instances[hash] = new Io(options);
if (!instances[emptyHash]) {
instances[emptyHash] = instances[hash];
}
return instances[hash];
}
// utility function, should really be used just for testing
io.clearInstances = (): void => {
instances = {};
};
io.registerPlugins = (...registerFunctions: ((io: Io) => void)[]): void => {
registerPlugins(...registerFunctions);
for (const hash in instances) {
runRegisterFunctions(instances[hash], registerFunctions);
}
};
export = io;