puter/src/definitions.js

115 lines
3.1 KiB
JavaScript
Raw Normal View History

2024-04-08 18:10:39 +08:00
/**
* Copyright (C) 2024 Puter Technologies Inc.
*
* This file is part of Puter.
*
* Puter is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2024-04-06 14:35:06 +08:00
export class Service {
//
};
2024-04-15 08:39:26 +08:00
2024-04-15 15:37:04 +08:00
export const PROCESS_INITIALIZING = { i18n_key: 'initializing' };
export const PROCESS_RUNNING = { i18n_key: 'running' };
// Something is cloning these objects, so '===' checks don't work.
// To work around this, the `i` property is used to compare them.
export const END_SOFT = { i: 0, end: true, i18n_key: 'end_soft' };
export const END_HARD = { i: 1, end: true, i18n_key: 'end_hard' };
2024-04-15 08:39:26 +08:00
export class Process {
constructor ({ uuid, parent, name, meta }) {
2024-04-15 08:39:26 +08:00
this.uuid = uuid;
this.parent = parent;
this.name = name;
2024-04-15 08:39:26 +08:00
this.meta = meta;
2024-04-15 15:37:04 +08:00
this.references = {};
this.status = PROCESS_INITIALIZING;
2024-04-15 08:39:26 +08:00
this._construct();
}
_construct () {}
2024-04-15 15:37:04 +08:00
chstatus (status) {
this.status = status;
}
is_init () {}
signal (sig) {
this._signal(sig);
}
2024-04-15 08:39:26 +08:00
get type () {
const _to_type_name = (name) => {
return name.replace(/Process$/, '').toLowerCase();
};
2024-04-15 11:58:14 +08:00
return this.type_ || _to_type_name(this.constructor.name) ||
2024-04-15 08:39:26 +08:00
'invalid'
}
};
2024-04-15 11:58:14 +08:00
export class InitProcess extends Process {
2024-04-15 08:39:26 +08:00
static created_ = false;
2024-04-15 15:37:04 +08:00
is_init () { return true; }
2024-04-15 08:39:26 +08:00
_construct () {
this.name = 'Puter';
2024-04-15 15:59:39 +08:00
this.type_ = 'init'; // thanks minify
2024-04-15 11:58:14 +08:00
if (InitProcess.created_) {
2024-04-15 08:39:26 +08:00
throw new Error('InitProccess already created');
}
2024-04-15 11:58:14 +08:00
InitProcess.created_ = true;
2024-04-15 08:39:26 +08:00
}
2024-04-15 15:37:04 +08:00
_signal (sig) {
const svc_process = globalThis.services.get('process');
for ( const process of svc_process.processes ) {
if ( process === this ) continue;
process.signal(sig);
}
if ( sig.i !== END_HARD.i ) return;
// Currently this is the only way to terminate `init`.
window.location.reload();
}
2024-04-15 08:39:26 +08:00
}
2024-04-15 14:12:39 +08:00
export class PortalProcess extends Process {
_construct () { this.type_ = 'app' }
2024-04-15 15:37:04 +08:00
_signal (sig) {
if ( sig.end ) {
$(this.references.el_win).close({
bypass_iframe_messaging: sig.i === END_HARD.i
});
}
}
2024-04-15 14:12:39 +08:00
};
2024-04-15 11:58:14 +08:00
export class PseudoProcess extends Process {
_construct () { this.type_ = 'ui' }
2024-04-15 15:37:04 +08:00
_signal (sig) {
if ( sig.end ) {
$(this.references.el_win).close({
bypass_iframe_messaging: sig.i === END_HARD.i
});
}
}
2024-04-15 11:58:14 +08:00
};