-
Notifications
You must be signed in to change notification settings - Fork 268
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: keep storage to server and move evaluation to renderer
Signed-off-by: lstocchi <lstocchi@redhat.com>
- Loading branch information
Showing
21 changed files
with
587 additions
and
387 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/********************************************************************** | ||
* Copyright (C) 2023 Red Hat, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
***********************************************************************/ | ||
import type { ContextKeyValue } from '@podman-desktop/api'; | ||
import type { Context } from '../context/context.js'; | ||
|
||
export interface IContext { | ||
getValue<T extends ContextKeyValue = ContextKeyValue>(key: string): T | undefined; | ||
} | ||
|
||
export interface ContextInfo { | ||
readonly id: number; | ||
readonly parent?: Context | null; | ||
readonly extension: string | null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/********************************************************************** | ||
* Copyright (C) 2023 Red Hat, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
***********************************************************************/ | ||
import type { ApiSenderType } from './api.js'; | ||
import type { ContextInfo } from './api/context-into.js'; | ||
import { Context } from './context/context.js'; | ||
|
||
export class ContextRegistry { | ||
private contexts: Map<string, Context>; | ||
|
||
constructor(private apiSender: ApiSenderType) { | ||
this.contexts = new Map(); | ||
} | ||
|
||
registerContext(extension: string): void { | ||
if (!this.contexts.has(extension)) { | ||
const ctx = new Context(this.contexts.size, null, extension, this.apiSender); | ||
this.contexts.set(extension, ctx); | ||
} | ||
} | ||
|
||
unregisterContext(extension: string): void { | ||
const ctx = this.contexts.get(extension); | ||
ctx?.dispose(); | ||
this.contexts.delete(extension); | ||
} | ||
|
||
getContext(extension: string): Context | undefined { | ||
return this.contexts.get(extension); | ||
} | ||
|
||
listContextInfos(): ContextInfo[] { | ||
const contexts: ContextInfo[] = []; | ||
Array.from(this.contexts.values()).forEach(value => { | ||
contexts.push({ | ||
extension: value.extension, | ||
id: value.id, | ||
parent: value.parent, | ||
}); | ||
}); | ||
return contexts; | ||
} | ||
|
||
getContextInfo(extension: string): ContextInfo { | ||
const context = this.contexts.get(extension); | ||
if (!context) { | ||
throw new Error(`no context found for extension ${extension}`); | ||
} | ||
return { | ||
extension: context.extension, | ||
id: context.id, | ||
parent: context.parent, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/********************************************************************** | ||
* Copyright (C) 2023 Red Hat, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
***********************************************************************/ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import type { ApiSenderType } from '../api.js'; | ||
import type { IContext } from '../api/context-into.js'; | ||
|
||
export class Context implements IContext { | ||
private _value: Record<string, any>; | ||
|
||
constructor( | ||
private _id: number, | ||
private _parent: Context | null, | ||
private _extension: string | null, | ||
private apiSender: ApiSenderType, | ||
) { | ||
this._value = Object.create(null); | ||
this._value['_contextId'] = _id; | ||
} | ||
|
||
get id(): number { | ||
return this._id; | ||
} | ||
|
||
get parent(): Context | null { | ||
return this._parent; | ||
} | ||
|
||
get extension(): string | null { | ||
return this._extension; | ||
} | ||
|
||
get value(): Record<string, any> { | ||
return { ...this._value }; | ||
} | ||
|
||
setValue(key: string, value: any): boolean { | ||
if (this._value[key] !== value) { | ||
this._value[key] = value; | ||
this.apiSender.send('context-value-updated', { | ||
extension: this._extension, | ||
key, | ||
value, | ||
}); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
removeValue(key: string): boolean { | ||
if (key in this._value) { | ||
delete this._value[key]; | ||
this.apiSender.send('context-key-removed', { | ||
extension: this._extension, | ||
key, | ||
}); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
getValue<T>(key: string): T | undefined { | ||
const ret = this._value[key]; | ||
if (typeof ret === 'undefined' && this._parent) { | ||
return this._parent.getValue<T>(key); | ||
} | ||
return ret; | ||
} | ||
|
||
updateParent(parent: Context): void { | ||
this._parent = parent; | ||
this.apiSender.send('context-parent-updated', { | ||
extension: this._extension, | ||
pid: parent._id, | ||
pextension: parent._extension, | ||
pp: parent.parent, | ||
}); | ||
} | ||
|
||
collectAllValues(): Record<string, any> { | ||
let result = this._parent ? this._parent.collectAllValues() : Object.create(null); | ||
result = { ...result, ...this._value }; | ||
delete result['_contextId']; | ||
return result; | ||
} | ||
|
||
dispose(): void { | ||
this._parent = null; | ||
} | ||
} |
Oops, something went wrong.