-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
112 lines (98 loc) · 3.49 KB
/
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
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
import localforage from 'localforage';
// // you can access the serializer and thedrivers by:
// import { getSerializerPromise, getDriverPromise } from './utils';
// getSerializerPromise();
// getDriverPromise(localforage.WEBSQL);
export interface LocalForagePlugin extends LocalForage {
_baseMethods: { [methodName: string]: () => any };
_pluginPrivateVariables: {
listOfImportantThings: any[];
callCount: number;
};
}
function handleMethodCall(
localforageInstance: LocalForagePlugin,
methodName: string,
args: any,
) {
return localforageInstance.ready().then(function() {
console.log('Invoking ' + methodName + ' with arguments: ', args);
const promise = localforageInstance._baseMethods[methodName].apply(
localforageInstance,
args,
);
promise.then(
function(result: any) {
console.log(
'Invoking ' + methodName + ' resolved with: ',
result,
);
},
function(err: Error) {
console.log('Invoking ' + methodName + ' rejected with: ', err);
},
);
return promise;
});
}
// wraps the localForage methods of the WrappedLibraryMethods array and
// allows you to execute code before & after their invocation
function wireUpMethods(localforageInstance: LocalForagePlugin) {
const WrappedLibraryMethods = [
'clear',
'getItem',
'iterate',
'key',
'keys',
'length',
'removeItem',
'setItem',
];
function wireUpMethod(
localforageInstance: LocalForagePlugin,
methodName: string,
) {
localforageInstance._baseMethods =
localforageInstance._baseMethods || {};
localforageInstance._baseMethods[
methodName
] = (localforageInstance as any)[methodName] as () => any;
(localforageInstance as any)[methodName] = function() {
return handleMethodCall(this, methodName, arguments);
};
}
for (let i = 0, len = WrappedLibraryMethods.length; i < len; i++) {
const methodName = WrappedLibraryMethods[i];
wireUpMethod(localforageInstance, methodName);
}
}
// place your plugin initialization logic here
// useful in case that you need to preserve a state
function setup(localforageInstance: LocalForagePlugin) {
if (!localforageInstance._pluginPrivateVariables) {
localforageInstance._pluginPrivateVariables = {
callCount: 0,
listOfImportantThings: [],
};
// in case you need to observe the invocation of some methods
wireUpMethods(localforageInstance);
}
}
// this will be available as `localforage.pluginMethod('test');`
export function localforagePluginBoilerplate(/*option*/) {
const localforageInstance = this;
// this will initialize your plugin lazily
// after the first invocation of your method
setup(localforageInstance);
console.log('Hello world from the plugin method!');
return Promise.resolve('Hello world result!');
}
// add your plugin method to every localForage instance
export function extendPrototype(localforage: LocalForage) {
const localforagePrototype = Object.getPrototypeOf(localforage);
if (localforagePrototype) {
localforagePrototype.pluginMethod = localforagePluginBoilerplate;
}
return localforage as LocalForagePlugin;
}
export const extendPrototypeResult = extendPrototype(localforage);