1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | import { HandlerContext } from "@atomist/automation-client";
|
18 | import { PreferenceStoreFactory } from "@atomist/sdm";
|
19 | import * as _ from "lodash";
|
20 | import {
|
21 | AbstractPreferenceStore,
|
22 | Preference,
|
23 | } from "./AbstractPreferenceStore";
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | export const InMemoryPreferenceStoreFactory: PreferenceStoreFactory = ctx => new InMemoryPreferenceStore(ctx);
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | export class InMemoryPreferenceStore extends AbstractPreferenceStore {
|
35 |
|
36 | private readonly store: { [key: string]: Preference } = {};
|
37 |
|
38 | constructor(context: HandlerContext) {
|
39 | super(context);
|
40 | }
|
41 |
|
42 | protected async doGet(name: string, namespace: string): Promise<Preference | undefined> {
|
43 | const key = this.scopeKey(name, namespace);
|
44 | return this.store[key];
|
45 | }
|
46 |
|
47 | protected async doPut(pref: Preference): Promise<void> {
|
48 | const key = this.scopeKey(pref.name, pref.namespace);
|
49 | this.store[key] = {
|
50 | ...pref,
|
51 | ttl: typeof pref.ttl === "number" ? Date.now() + pref.ttl : undefined,
|
52 | };
|
53 | }
|
54 |
|
55 | protected async doList(namespace: string): Promise<Preference[]> {
|
56 | const values: Preference[] = [];
|
57 | _.forEach(this.store, (v, k) => {
|
58 | if (!namespace || k.startsWith(`${namespace}_$_`)) {
|
59 | values.push(v);
|
60 | }
|
61 | });
|
62 | return values;
|
63 | }
|
64 |
|
65 | protected async doDelete(name: string, namespace: string): Promise<void> {
|
66 | const key = this.scopeKey(name, namespace);
|
67 | delete this.store[key];
|
68 | }
|
69 | }
|