Skip to content
This repository has been archived by the owner on Aug 31, 2021. It is now read-only.

Commit

Permalink
global events for scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
mkloubert committed Dec 28, 2017
1 parent 9127caa commit 4cd36b0
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 13 deletions.
2 changes: 2 additions & 0 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/

import * as deploy_contracts from './contracts';
import * as deploy_events from './events';
import * as deploy_helpers from './helpers';
import * as deploy_log from './log';
import * as deploy_values from './values';
Expand Down Expand Up @@ -186,6 +187,7 @@ export async function reloadCommands(newCfg: deploy_contracts.Configuration) {
const CTX: ScriptCommandExecutionContext = {
button: btn,
command: id,
globalEvents: deploy_events.EVENTS,
globals: ME.globals,
globalState: GLOBAL_STATE,
logger: deploy_log.CONSOLE,
Expand Down
4 changes: 4 additions & 0 deletions src/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,10 @@ export interface PlatformItem {
* Arguments for a script.
*/
export interface ScriptArguments {
/**
* Gets the emitter for global extension events.
*/
readonly globalEvents: NodeJS.EventEmitter;
/**
* Global data.
*/
Expand Down
24 changes: 24 additions & 0 deletions src/events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* This file is part of the vscode-deploy-reloaded distribution.
* Copyright (c) Marcel Joachim Kloubert.
*
* vscode-deploy-reloaded is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, version 3.
*
* vscode-deploy-reloaded 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import * as Events from 'events';


/**
* Stores the global event emitter.
*/
export const EVENTS: NodeJS.EventEmitter = new Events.EventEmitter();
11 changes: 11 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const CompareVersion = require('compare-versions');
import * as deploy_commands from './commands';
import * as deploy_compare from './compare';
import * as deploy_contracts from './contracts';
import * as deploy_events from './events';
import * as deploy_gui from './gui';
import * as deploy_helpers from './helpers';
import * as deploy_html from './html';
Expand Down Expand Up @@ -652,6 +653,16 @@ async function activateExtension(context: vscode.ExtensionContext) {
currentContext = context;
});

// events
WF.next(() => {
context.subscriptions.push({
dispose: () => {
deploy_events.EVENTS
.removeAllListeners();
}
});
});

// output channel
WF.next(() => {
context.subscriptions.push(
Expand Down
2 changes: 2 additions & 0 deletions src/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import * as deploy_clients from './clients';
import * as deploy_contracts from './contracts';
import * as deploy_events from './events';
import * as deploy_files from './files';
import * as deploy_helpers from './helpers';
import * as deploy_log from './log';
Expand Down Expand Up @@ -533,6 +534,7 @@ export abstract class FileToUploadBase implements FileToUpload {
);

const CONTEXT: deploy_transformers.DataTransformerContext = {
globalEvents: deploy_events.EVENTS,
globals: ME.workspace.globals,
globalState: ME.workspace.sessionState['upload']['states']['global'],
logger: deploy_log.CONSOLE,
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/

import * as deploy_contracts from '../contracts';
import * as deploy_events from '../events';
import * as deploy_files from '../files';
import * as deploy_helpers from '../helpers';
import * as deploy_log from '../log';
Expand Down Expand Up @@ -119,6 +120,7 @@ class ScriptPlugin extends deploy_plugins.PluginBase<ScriptTarget> {
cancellationToken: undefined,
dir: context['dir'],
files: context['files'],
globalEvents: deploy_events.EVENTS,
globals: context.target.__workspace.globals,
globalState: this._GLOBAL_STATE,
isCancelling: undefined,
Expand Down
2 changes: 2 additions & 0 deletions src/pull.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/

import * as deploy_contracts from './contracts';
import * as deploy_events from './events';
import * as deploy_helpers from './helpers';
import * as deploy_log from './log';
import * as deploy_packages from './packages';
Expand Down Expand Up @@ -267,6 +268,7 @@ export async function pullFilesFrom(files: string[],
);

const CONTEXT: deploy_transformers.DataTransformerContext = {
globalEvents: deploy_events.EVENTS,
globals: ME.globals,
globalState: ME.sessionState['pull']['states']['global'],
logger: deploy_log.CONSOLE,
Expand Down
2 changes: 2 additions & 0 deletions src/targets/operations/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/

import * as deploy_contracts from '../../contracts';
import * as deploy_events from '../../events';
import * as deploy_helpers from '../../helpers';
import * as deploy_http from '../../http';
import * as deploy_log from '../../log';
Expand Down Expand Up @@ -202,6 +203,7 @@ export async function execute(context: deploy_targets.TargetOperationExecutionCo
getBodyToSend = async () => {
const ARGS: HttpBodyModuleExecutionArguments = {
context: context,
globalEvents: deploy_events.EVENTS,
globals: WORKSPACE.globals,
globalState: WORKSPACE.sessionState['target_operations']['http']['global'],
logger: deploy_log.CONSOLE,
Expand Down
2 changes: 2 additions & 0 deletions src/targets/operations/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/

import * as deploy_contracts from '../../contracts';
import * as deploy_events from '../../events';
import * as deploy_helpers from '../../helpers';
import * as deploy_log from '../../log';
import * as deploy_targets from '../../targets';
Expand Down Expand Up @@ -100,6 +101,7 @@ export async function execute(context: deploy_targets.TargetOperationExecutionCo
if (EXECUTE) {
const ARGS: ScriptTargetOperationExecutionArguments = {
context: context,
globalEvents: deploy_events.EVENTS,
globals: WORKSPACE.globals,
globalState: WORKSPACE.sessionState['target_operations']['script']['global'],
logger: deploy_log.CONSOLE,
Expand Down
18 changes: 5 additions & 13 deletions src/workspaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,6 @@ export class Workspace extends deploy_objects.DisposableBase implements deploy_c
*/
protected _startTime: Moment.Moment;
private readonly _SWITCH_BUTTONS: TargetWithButton<SwitchTarget>[] = [];
/**
* Stores the states for 'sync when open'.
*/
protected _syncWhenOpenStates: SyncWhenOpenStates;
private _targets: deploy_targets.Target[];
/**
* The current translation function.
Expand Down Expand Up @@ -437,6 +433,10 @@ export class Workspace extends deploy_objects.DisposableBase implements deploy_c
NEW_SESSION_STATE['pull']['states']['global'] = {};
NEW_SESSION_STATE['pull']['states']['data_transformers'] = {};

NEW_SESSION_STATE['sync'] = {};
NEW_SESSION_STATE['sync']['whenOpen'] = {};
NEW_SESSION_STATE['sync']['whenOpen']['states'] = {};

NEW_SESSION_STATE['upload'] = {};
NEW_SESSION_STATE['upload']['states'] = {};
NEW_SESSION_STATE['upload']['states']['global'] = {};
Expand Down Expand Up @@ -1625,12 +1625,6 @@ export class Workspace extends deploy_objects.DisposableBase implements deploy_c
await this.deployOnSave(e.fileName);
}


/** @inheritdoc */
protected onDispose() {
this._syncWhenOpenStates = null;
}

/**
* Gets the name of that workspace.
*/
Expand Down Expand Up @@ -1883,8 +1877,6 @@ export class Workspace extends deploy_objects.DisposableBase implements deploy_c
}

finalizer = async () => {
ME._syncWhenOpenStates = {};

// runBuildTaskOnStartup
try {
await deploy_tasks.runBuildTaskOnStartup
Expand Down Expand Up @@ -2519,7 +2511,7 @@ export class Workspace extends deploy_objects.DisposableBase implements deploy_c
* Gets the states for 'sync when open'.
*/
public get syncWhenOpenStates(): SyncWhenOpenStates {
return this._syncWhenOpenStates;
return this.sessionState['sync']['whenOpen']['states'];
}

/** @inheritdoc */
Expand Down

0 comments on commit 4cd36b0

Please sign in to comment.