1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const store_1 = require("./store");
|
4 | const debug = require("debug")("kubernetes:informer");
|
5 | class Informer {
|
6 | constructor(resource, opts, store) {
|
7 | this.resource = resource;
|
8 | this.opts = opts;
|
9 | this.store = new store_1.ObservableStoreDecorator(store || new store_1.InMemoryStore());
|
10 | }
|
11 | start() {
|
12 | const handler = (event) => {
|
13 | const { type, object } = event;
|
14 | switch (type) {
|
15 | case "ADDED":
|
16 | case "MODIFIED":
|
17 | debug("added or updated object %o: %o", object.kind, `${object.metadata.namespace}/${object.metadata.name}`);
|
18 | this.store.store(object);
|
19 | break;
|
20 | case "DELETED":
|
21 | debug("removed object %o: %s", object.kind, `${object.metadata.namespace}/${object.metadata.name}`);
|
22 | this.store.pull(object);
|
23 | break;
|
24 | }
|
25 | };
|
26 | const opts = Object.assign({ skipAddEventsOnResync: true, onResync: (objs) => {
|
27 | debug("resynced %d objects", objs.length);
|
28 | this.store.sync(objs);
|
29 | } }, this.opts);
|
30 | const watchHandle = this.resource.listWatch(handler, undefined, opts);
|
31 | return {
|
32 | waitForInitialList: () => watchHandle.initialized,
|
33 | waitUntilFinish: () => watchHandle.done,
|
34 | stop: watchHandle.stop,
|
35 | };
|
36 | }
|
37 | }
|
38 | exports.Informer = Informer;
|
39 |
|
\ | No newline at end of file |